Thursday, December 03, 2009

Registers in Peripherals

Hardware devices that reside outside the processor chip are called external peripherals. The embedded processor interacts with peripheral device through a set of control and status registers. These registers are part of the peripheral. Usually the peripheral devices are located either in the processor’s memory space or within the I/O space. The most common is memory-mapped peripherals. Memory-mapped control and status registers can be made to look like regular variables: declare a pointer to the register and set the value of the pointer explicitly.

uint32_t *p = (uint32_t *)(0x20E00132);

The contents of register can be changed without the knowledge of your program. That is because the register contents can also be modified by the peripheral hardware. You have to use ‘volatile’ for a device register.  in following code:

uint32_t volatile *p = (uint32_t volatile *)(0x20E00132);

void foo(void)
*p = 1;     // 1st write
*p = 2;     // 2nd write

without ‘volatile’, the compiler may remove the line ‘*p =1’ for optimization since it looks no use. Then you can never reach what you want.

No comments:

Post a Comment