volatile and non-cacheable

本文探讨了C代码中volatile关键字的作用及其在保护内存操作时的使用方法,包括防止变量被缓存和阻止代码优化工具的错误优化。通过设置MAR(Memory Attribute Register)来防止特定内存范围被缓存,确保外部硬件更新内存操作的有效执行。
摘要由CSDN通过智能技术生成

In some applications, some specific addresses may need to be read from their physical locations each time they are accessed (e.g., a status register within FPGA).

The L2 controller offers registers that control whether certain ranges of memory are cacheable, and whether one or more requestors are actually permitted to access these ranges. The registers are referred to as MARs (memory attribute registers). 
Note—Using the volatile keyword in the C language does not protect a variable from being cached. If an application uses a memory location periodically updated by external hardware, in order to protect this operation in C code follow these two steps:
• Use the volatile keyword to prevent the code generation tools from incorrectly optimizing the variable.
• You must program the MAR register of the range containing the variable to prevent caching.



c代码中volatile关键字的作用,除了阻止cpu直接使用寄存器和cache中变量,只有可能在其它模块中被修改的全局符号,只有在编写并发程序时,才可能出现使用volatile的必要性。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值