Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 311
starblue Avatar asked Feb 18 '23 22:02

starblue


1 Answers

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

like image 184
CL. Avatar answered Mar 02 '23 16:03

CL.