Article ID: 000083953 Content Type: Troubleshooting Last Reviewed: 02/13/2006

Why does my Nios® microprocessor fail to read from my memory-mapped peripheral, when I try to read from it immediately following a write to the same peripheral?

Environment

BUILT IN - ARTICLE INTRO SECOND COMPONENT
Description Pointers to memory-mapped peripherals should be declared as volatile types. A pointer that is not declared as a volatile may not behave as expected under certain levels of compiler optimization. For example, consider the following code segment.

int *pointer = (int *) na_peripheral;
int read_back;
...
*pointer = 49;
read_back = *pointer;


With a compiler optimization level of -o2 (the default), this program will write a value of 49 to the peripheral. However, when read_back is set to the value of the pointer, it will be loaded with the value that was just written to the peripheral, which is still stored in a Nios core internal register. This may be acceptable in some circumstances, but it can cause problems if the peripheral is updated externally at the same time as the read. To ensure that the Nios core reads the correct value from the peripheral, the pointer should be declared as follows:


volatile int *pointer = (int *) na_peripheral;


This will ensure that Nios asserted the chip-select and read-enable signals for the desired peripheral.

Related Products

This article applies to 1 products

Intel® Programmable Devices

1