How do I write a memory barrier for a TMS320F2812 DSP?

扶醉桌前 提交于 2019-12-03 09:03:27

Memory barriers are about the ordering of memory accesses, but you also have to ensure that values do not stay in registers but are written to memory at all.

The only way to enforce this with TI's compiler is to use volatile.

Please note that volatile, while being a modifier of a variable, is in its implementation not about the variable itself (i.e., its memory), but about all the accesses to this variable. So if you want to avoid the effects of too little optmization, write your program so that only some variable accesses are volatile.

To do this, declare your variables normally, and add volatile only when you want to force a read or write of a variable. You can use helper functions like this:

inline void force_write(int *ptr, int value)
{
    *(volatile int *)ptr = value;
}

or use this nifty macro stolen from Linux, usable for both reading/writing and for all types:

#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
...
if (ACCESS_ONCE(ready) != 0)
    ACCESS_ONCE(new_data) = 42;

(The name has historical reasons; better call it FORCE_ACCESS.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!