Search code examples
microcontrolleravr

What happens when I write to a memory-mapped register?


I'm trying get familiar with AVR microcontrollers. I am a bit confused on how memory-mapped registers actually function.

I am using avr-libc and programming in C. Looking at avr-libc sources, there are many internal registers which are accessed by writing/reading SRAM locations. When I write to a register's memory location, does that byte in SRAM actually get overwritten, then asyncronously copied into the register?

Thanks!


Solution

  • The short answer: it depends.

    The long answer: when you write to a memory mapped register in C, the compiler emits the same instructions as you would any other memory access (load/store/move/etc). The processor would perform a memory access to the address as it would normally do, in the case of a write it would write the address of the register to the internal bus, enable the write strobe, and then output the desired data on the data lines of the bus.

    This is where it gets complicated. The output data may get routed to any other circuit in the MCU: it could indeed be stored in an SRAM structure or a FIFO, but it may simply be latched or “registered” by latches and flip flops, or it could even simply be routed somewhere without being stored. In most cases the write is likely synchronously registered (which may or may not be an SRAM), but that’s not guaranteed and it’s best to check the data sheet or reference manual of your particular chip on the particular registers you’re interested in.