ARM:使用来自 C 或 C++ 的位带内存

发布于 2025-01-12 21:25:06 字数 286 浏览 4 评论 0原文

ARM Cortex 支持位带内存,其中各个位映射到某些区域中的“字节”。我相信只有 RAM 的某些部分是位带的。我想使用 C 和 C++ 中的位带。

我该怎么办?看来我需要:

  1. 告诉编译器将某些变量放置在位带区域中。如何?如果变量是结构体的元素怎么办?
  2. 告诉编译器,当我想访问位时,将 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:

  1. Tell the compiler to place certain variables in bit-banded regions. How? What if the variables are elements of a struct?
  2. Tell the compiler, when I want to access a bit, to turn if (flags & 0x4) into if (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2025-01-19 21:25:06

最简单的解决方案是使用常规变量并通过其位带地址访问它们。为此,您不需要“告诉编译器”任何内容。例如,假设:

extern "C" volatile uint32_t* getBitBandAddress( volatile const void* address, int bit )
{
    volatile uint32_t* bit_address = 0;
    uint32_t addr = reinterpret_cast<uint32_t>(address);

    // This bit maniplation makes the function valid for RAM
    // and Peripheral bitband regions
    uint32_t word_band_base = addr & 0xf0000000;
    uint32_t bit_band_base = word_band_base | 0x02000000;
    uint32_t offset = addr - word_band_base;

    // Calculate bit band address
    bit_address = reinterpret_cast<volatile uint32_t*>(bit_band_base + (offset * 32u) + (static_cast<uint32_t>(bit) * 4u));

    return bit_address ;
}

您可以创建一个 32 位“数组”,因此:

uint32_t word = 0 ;
uint32_t* bits = getBitbandAddress( word, 0 ) ;

bits[5] = 1 ; // word now == 32 (bit 5 set).

现在,如果您有一个具有外部或 CCM 存储器的部件,例如,该部件不可位带化,则您确实需要确保链接器(不是编译器)将普通内存对象放置在可位带内存中。如何完成此操作是特定于工具链的,但例如在 gnu 中,您可能会这样:

uint32_t word __attribute__ ((section ("ISRAM1"))) = 0 ;

位带化对于以原子方式访问外围寄存器中的各个位可能最有用。用于快速且线程安全的访问。

一些编译器支持位带,并且可以使用位带自动优化单位位域访问。例如;

struct
{
    bit1 : 1 ;
    bit2 : 1 ;
} bits __attribute__ ((section ("BITBANDABLE")));

编译器(至少是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:

extern "C" volatile uint32_t* getBitBandAddress( volatile const void* address, int bit )
{
    volatile uint32_t* bit_address = 0;
    uint32_t addr = reinterpret_cast<uint32_t>(address);

    // This bit maniplation makes the function valid for RAM
    // and Peripheral bitband regions
    uint32_t word_band_base = addr & 0xf0000000;
    uint32_t bit_band_base = word_band_base | 0x02000000;
    uint32_t offset = addr - word_band_base;

    // Calculate bit band address
    bit_address = reinterpret_cast<volatile uint32_t*>(bit_band_base + (offset * 32u) + (static_cast<uint32_t>(bit) * 4u));

    return bit_address ;
}

you could create a 32bit "array" thus:

uint32_t word = 0 ;
uint32_t* bits = getBitbandAddress( word, 0 ) ;

bits[5] = 1 ; // word now == 32 (bit 5 set).

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:

uint32_t word __attribute__ ((section ("ISRAM1"))) = 0 ;

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;

struct
{
    bit1 : 1 ;
    bit2 : 1 ;
} bits __attribute__ ((section ("BITBANDABLE")));

The compiler (at least armcc v5) may optimise this to utilise the bitband access to bits.bit1 and bits.bit2. YMMV.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文