Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Volatile... or is it??

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....

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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