ARM:使用来自 C 或 C++ 的位带内存
ARM Cortex 支持位带内存,其中各个位映射到某些区域中的“字节”。我相信只有 RAM 的某些部分是位带的。我想使用 C 和 C++ 中的位带。
我该怎么办?看来我需要:
- 告诉编译器将某些变量放置在位带区域中。如何?如果变量是结构体的元素怎么办?
- 告诉编译器,当我想访问位时,将
if (flags & 0x4)
转换为if (flags_bb_04)
。理想情况下,我希望这是自动的,并且如果位带不可用,则返回到前者。
ARM Cortex supports bit-banded memory, where individual bits are mapped to "bytes" in certain regions. I believe that only certain parts of RAM are bit-banded. I'd like to use bit-banding from C and C++.
How do I this? It seems I'd need to:
- Tell the compiler to place certain variables in bit-banded regions. How? What if the variables are elements of a
struct
? - Tell the compiler, when I want to access a bit, to turn
if (flags & 0x4)
intoif (flags_bb_04)
. Ideally, I'd like this to be automatic, and to fall back to the former if bit banding isn't available.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是使用常规变量并通过其位带地址访问它们。为此,您不需要“告诉编译器”任何内容。例如,假设:
您可以创建一个 32 位“数组”,因此:
现在,如果您有一个具有外部或 CCM 存储器的部件,例如,该部件不可位带化,则您确实需要确保链接器(不是编译器)将普通内存对象放置在可位带内存中。如何完成此操作是特定于工具链的,但例如在 gnu 中,您可能会这样:
位带化对于以原子方式访问外围寄存器中的各个位可能最有用。用于快速且线程安全的访问。
一些编译器支持位带,并且可以使用位带自动优化单位位域访问。例如;
编译器(至少是armcc v5)可以对此进行优化,以利用对bits.bit1和bits.bit2的位带访问。 YMMV。
The simplest solution is to use regular variables and access them through thier bit-band address. For that you do not need to "tell the compiler" anything. For example, given:
you could create a 32bit "array" thus:
Now if you have a part with external or CCM memory for example that is not bitbandable, you do need to ensure that the linker ( not the compiler) places the normal memory object in bitbandable memory. How that is done is toolchain specific but in gnu for example you might have:
Bitbanding is perhaps most useful for atomically accessing individual bits in peripheral registers. For fast and thread-safe access.
Some compilers are bitband aware and may automatically optimise single bit bitfield access using bitbanding. So for example;
The compiler (at least armcc v5) may optimise this to utilise the bitband access to
bits.bit1
andbits.bit2
. YMMV.