user366312
user366312

Reputation: 16908

What is an atomic operation in the context of microcontrollers?

What is an "Atomic Operation" in the context of micro-controllers?

I am studying TI F28027 MCU.

The data sheet says that its operations are atomic. What does it mean?

Upvotes: 6

Views: 9498

Answers (3)

old_timer
old_timer

Reputation: 71546

It generally has to do with resources or features for a resource that require/desire two (more than one) accesses and require those two accesses to not be interrupted by some other unrelated access. So a read-modify-write, or a test and set are two common examples.

If you didnt have any atomic or the kind of atomic you needed then you would in software have to insure that the two accesses are not interrupted or interfered if possible, if not possible then you need other solutions. Often hardware will give you at least one (like a test and set) from which you can create other protected features in software. The software solutions for example might be protecting a foreground task with an interrupt service routine, if the interrupt were to happen in between the two accesses (test and set, read-modify-write read and write being the two accesses) then the isr might modify the data after the first read but before the following write making the first read now stale/incorrect. So when protecting yourself from an interrupt you typically disable interrupts temporarily and then re-enable. Having the hardware do this it insures that even if an interrupt occurs or even if there is an other peripheral that has access, it is held off, and/or you are held off depending on priority, so your atomic operation can happen uninterrupted.

Upvotes: 1

kkrambo
kkrambo

Reputation: 7057

Read the Wikipedia article on atomic operations for a description of what "atomic" means generally. Here's the nutshell excerpt:

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously. Atomicity is a guarantee of isolation from concurrent processes.

The TI F28x Piccolo microcontroller family has a special Atomic ALU (arithmetic logic unit) that allows read-modify-write instructions to be performed atomically. This C2000 Piccolo Workshop document has some information. I believe special assembly syntax is required to take advantage of the atomic ALU (page 1-6). I suspect TI's Code Composer compiler will produce atomic assembly syntax when it can. Although how you write your C code may affect how efficient the compiler is (page 3-5).

Consider consulting TI's E2E Community for more help.

Upvotes: 10

DoxyLover
DoxyLover

Reputation: 3484

I'm not familiar with that particular MCU, but typically, atomic operations are those that modify a memory location and no other context, hardware or software, can interrupt the read and susiquent write sub-operations. This guarantees that nothing else could change the memory location out from under the operation.

For example, an increment operator must read the target memory location, add one to the value, and then write back to the same location. If it were not atomic, something else might write to that same location between. Then when the write-back occurs, the intermediate write would be lost.

So what kind of other contexts are blocked by an atomic operation?

  • Interrupts where the ISR writes to memory.
  • DMA writes to RAM.
  • Other cores in a multi-core processor.

Upvotes: 1

Related Questions