Volatile and Registers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Halloran

    Volatile and Registers

    Normally, when I write software to access a register, I will do
    something like this:
    *((volatile u32*) 0x00000022) = 5; (with some wrappers to make it
    cleaner)

    however, I noticed that in a recent codebase that my company aquired,
    volatile
    is not used:
    *((u32*) 0x00000022) = 5;

    Is this a proper way of coding? I was under the understanding that if
    volatile is not used, the code could be optimized out. Is this
    correct? What does
    everyone else do?
  • Richard Bos

    #2
    Re: Volatile and Registers

    Eric Sosman <esosman@ieee-dot-org.invalidwrot e:
    Mike Halloran wrote:
    Normally, when I write software to access a register, I will do
    something like this:
    *((volatile u32*) 0x00000022) = 5; (with some wrappers to make it
    cleaner)
    >
    First, I hope you realize that converting integers to
    and from pointers is inherently "unclean," and relies on
    characteristics of the particular system you happen to be
    using. It's a common technique for getting at things like
    hardware registers, but it's not a portable technique.
    Actually, the technique itself is portable. The numbers are not. I would
    not expect any given system to let you write to any given address, but
    if some system does allow you to write directly to some addresses, this
    is precisely the technique I would expect to be able to use.

    Richard

    Comment

    Working...