Controlling read and write access width to memory mapped registers in C

Posted by srking on Stack Overflow See other posts from Stack Overflow or by srking
Published on 2010-06-14T18:51:21Z Indexed on 2010/06/14 19:42 UTC
Read the original article Hit count: 200

I'm using and x86 based core to manipulate a 32-bit memory mapped register. My hardware behaves correctly only if the CPU generates 32-bit wide reads and writes to this register. The register is aligned on a 32-bit address and is not addressable at byte granularity.

What can I do to guarantee that my C (or C99) compiler will only generate full 32-bit wide reads and writes in all cases?

For example, if I do a read-modify-write operation like this:

volatile uint32_t* p_reg = 0xCAFE0000;
*p_reg |= 0x01;

I don't want the compiler to get smart about the fact that only the bottom byte changes and generate 8-bit wide read/writes. Since the machine code is often more dense for 8-bit operations on x86, I'm afraid of unwanted optimizations. Disabling optimizations in general is not an option.

© Stack Overflow or respective owner

Related posts about c

    Related posts about embedded