2.4.8. Use of volatile¶
2.4.8.2. Peripheral access¶
The volatile keyword must be used when accessing memory locations that represent memory mapped peripherals.
Such memory locations might change value in ways that the compiler cannot predict.
This ensures the compiler preserves reads and writes to memory exactly as written in the C code.
A missing volatile qualifier can result in the compiler incorrectly optimizing away or reordering reads/writes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | static inline void
GPIO_writePin(uint32_t pin, uint32_t outVal)
{
volatile uint32_t *gpioDataReg;
uint32_t pinMask;
//
// Check the arguments.
//
ASSERT(GPIO_isPinValid(pin));
gpioDataReg = (uint32_t *)GPIODATA_BASE +
((pin / 32U) * GPIO_DATA_REGS_STEP);
pinMask = (uint32_t)1U << (pin % 32U);
if(outVal == 0U)
{
gpioDataReg[GPIO_GPxCLEAR_INDEX] = pinMask;
}
else
{
gpioDataReg[GPIO_GPxSET_INDEX] = pinMask;
}
}
|