如何为TMS320F2812 DSP编写内存屏障? [英] How do I write a memory barrier for a TMS320F2812 DSP?

查看:201
本文介绍了如何为TMS320F2812 DSP编写内存屏障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了TI C/C ++编译器v6.1用户指南(

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

在这方面,asm语句似乎没有提供任何内容,该手册甚至警告不要更改变量的值(p132).用于声明变量影响的GNU扩展未实现(p115).

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

搜索网络或TI论坛也没有任何结果.

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

还有其他提示如何进行吗?

Any other hints how to proceed?

推荐答案

内存障碍与内存访问的顺序有关,但是您还必须确保值不保留在寄存器中,而是完全写入内存.

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.

使用TI的编译器强制执行此操作的唯一方法是使用volatile.

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

请注意,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.

要执行此操作,请正常声明变量,并仅在要强制读取或写入变量时才添加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;
}

或使用从Linux窃取的这个漂亮宏,可用于读/写和所有类型:

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;

(名称有历史原因;最好将其命名为FORCE_ACCESS.)

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

这篇关于如何为TMS320F2812 DSP编写内存屏障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆