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

前端 未结 1 934
终归单人心
终归单人心 2021-02-10 19:31

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 t

相关标签:
1条回答
  • 2021-02-10 20:25

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

    0 讨论(0)
提交回复
热议问题