Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Volatile... or is it??

Altera_Forum
Honored Contributor II
1,030 Views

Hi there, 

 

I just wanted to save you guys some gray hairs when working with pointers to your hardware in a kernel module. 

I had a module with a volatile struct pointing to the registers of my custom peripheral on the avalon-bus. All worked fine untill I got strange errors when changing values "fast" (each millisecond).  

Values would not be updated correctly... 

 

Even though I declared everything volatile, it wouldn't work when I wrote and read data every milisecond. When I changed my delay 'till 50ms or higher everything worked like expected. 

 

Changing the volatile struct with the corresponding inl and outl functions solved my problems. 

 

I Hope I can save someone some time....
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
271 Views

If I remember correctly, the volatile keyword does not cause the Nios II compiler to generate code to bypass the cache. That could be the source of your problem. I think there was a thread on this a while ago in the general software forum, you could try searching for it. 

 

Dennis Scott 

Microtronix Datacom
0 Kudos
Altera_Forum
Honored Contributor II
271 Views

Hi All, 

 

> If I remember correctly, the volatile keyword does not cause the Nios II compiler to 

> generate code to bypass the cache. 

 

Correct. And it shouldn't ... the presence of a cache (or lack thereof) has nothing to do 

with compiler optimizations that may affect the stability of the object. 

 

With gcc the volatilie keyword traditionally boils down to accessing the object 

each time it is referenced ... nothing more. If you're accessing hardware 

registers, do yourself a favor and use the HAL macros -- that's what they're for. 

 

Regards, 

--Scott
0 Kudos
Reply