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

允我心安 提交于 2019-12-21 02:55:21

问题


I've looked through the TI C/C++ compiler v6.1 user's guide (spru514e) but didn't find anything.

The asm statement doesn't seem to provide anything in this regard, the manual even warns against changing values of variables (p132). The GNU extension for declaring effects on variables is not implemented (p115).

I also didn't find any intrinsic for memory barriers (like __memory_changed() in Keil's armcc).

Searching the web or the TI forums also turned up nothing.

Any other hints how to proceed?


回答1:


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.)



来源:https://stackoverflow.com/questions/12797181/how-do-i-write-a-memory-barrier-for-a-tms320f2812-dsp

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