为什么嵌入式系统上的寄存器需要 Read-Modify-Write? [英] Why is Read-Modify-Write necessary for registers on embedded systems?

查看:12
本文介绍了为什么嵌入式系统上的寄存器需要 Read-Modify-Write?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 http://Embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/,上面写着:

I was reading http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/, which said:

使用读/写位,固件在需要时设置和清除位.它通常首先读取寄存器,修改所需的位,然后将修改后的值写回

With read/write bits, firmware sets and clears bits when needed. It typically first reads the register, modifies the desired bit, then writes the modified value back out

我在维护一些由老盐嵌入人员在这里编写的生产代码时遇到了这个结构.我不明白为什么这是必要的.

and I have run into that consrtuct while maintaining some production code coded by old salt embedded guys here. I don't understand why this is necessary.

当我想设置/清除一点时,我总是使用位掩码或/nand.在我看来,这解决了任何线程安全问题,因为我假设设置(通过赋值或使用掩码进行 oring)寄存器只需要一个周期.另一方面,如果你先读取寄存器,然后修改,然后写入,读取和写入之间发生的中断可能会导致将旧值写入寄存器.

When I want to set/clear a bit, I always just or/nand with a bitmask. To my mind, this solves any threadsafe problems, since I assume setting (either by assignment or oring with a mask) a register only takes one cycle. On the other hand, if you first read the register, then modify, then write, an interrupt happening between the read and write may result in writing an old value to the register.

那么为什么要读-修改-写呢?还需要吗?

So why read-modify-write? Is it still necessary?

推荐答案

这在一定程度上取决于特定嵌入式设备的架构.我将给出三个涵盖常见情况的示例.然而,它的基本要点是,CPU 内核基本上不能直接对 I/O 设备的寄存器进行操作,只能以字节甚至字的方式读取和写入它们.

This depends somewhat on the architecture of your particular embedded device. I'll give three examples that cover the common cases. The basic gist of it, however, is that fundamentally the CPU core cannot operate directly on the I/O devices' registers, except to read and write them in a byte- or even word-wise fashion.

1) 68HC08 系列,8 位独立微控制器.

1) 68HC08 series, an 8-bit self-contained microcontroller.

这包括位设置"和位清除"指令.这些,如果您仔细阅读手册,实际上在内部会自行执行读取-修改-写入循环.它们确实具有原子操作的优势,因为作为单条指令它们不能被中断.

This includes a "bit set" and a "bit clear" instruction. These, if you read the manual carefully, actually internally perform a read-modify-write cycle by themselves. They do have the advantage of being atomic operations, since as single instructions they cannot be interrupted.

您还会注意到,它们比单独的读取或写入指令花费的时间更长,但比使用三个指令的时间要少(见下文).

You will also notice that they take longer than individual read or write instructions, but less time than using three instructions for the job (see below).

2) ARM 或 PowerPC,传统的 32 位 RISC CPU(也经常出现在高端微控制器中).

2) ARM or PowerPC, conventional 32-bit RISC CPUs (often found in high-end microcontrollers too).

这些不包括任何指令,这些指令既可以访问内存又可以一次执行计算(和/或).如果你用 C 写:

These do not include any instructions which can both access memory and perform a computation (the and/or) at once. If you write in C:

*register |= 0x40;

它变成以下程序集(对于这个 PowerPC 示例,r8 包含寄存器地址):

it turns into the folowing assembly (for this PowerPC example, r8 contains the register address):

LBZ r4,r8
ORI r4,r4,#0x40
STB r4,r8

因为这是多条指令,它不是原子的,它可以被中断.使其成为原子甚至 SMP 安全超出了此答案的范围 - 有特殊的说明和技术.

Because this is multiple instructions, it is NOT atomic, and it can be interrupted. Making it atomic or even SMP-safe is beyond the scope of this answer - there are special instructions and techniques for it.

3) IA32 (x86) 和 AMD64.为什么要将这些用于嵌入式",我无法理解,但它们是其他两个示例之间的中途之家.

3) IA32 (x86) and AMD64. Why you would use these for "embedded" is beyond me, but they are a half-way house between the other two examples.

我忘记了 x86 上是否有单指令内存位设置和位清除.如果没有,请参阅上面的 RISC 部分,它只需要两条指令而不是三条,因为 x86 可以在一条指令中加载和修改.

I forget whether there is a single-instruction in-memory bit-set and bit-clear on x86. If not, then see the RISC section above, it just takes only two instructions instead of three because x86 can load and modify in one instruction.

假设有这样的指令,它们需要在内部加载和存储寄存器以及修改它.现代版本会在内部明确地将指令分解为三个类似 RISC 的操作.

Assuming there are such instructions, they also need to internally load and store the register as well as modifying it. Modern versions will explcitly break the instruction into the three RISC-like operations internally.

奇怪的是,x86(与 HC08 不同)可以在内存总线上由总线主控中断,而不仅仅是传统的 CPU 中断.因此,您可以手动将 LOCK 前缀添加到需要执行多个内存周期才能完成的指令,如本例所示.不过,你不会从纯 C 中得到这个.

The oddity is that x86 (unlike the HC08) can be interrupted on the memory bus in mid-transaction by a bus master, not just by a conventional CPU interrupt. So you can manually add a LOCK prefix to an instruction that needs to do multiple memory cycles to complete, as in this case. You won't get this from plain C though.

这篇关于为什么嵌入式系统上的寄存器需要 Read-Modify-Write?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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