1
\$\begingroup\$

I'm working on learning to TDD embedded C by creating an LED driver for an AVR. In James Grenning's book, he recommends injecting the PORT by passing a pointer to the register into the function.

I came across the following code in the AvrLibc documentation, How do I pass an IO port as a parameter to a function?, and am confused as to why the *port parameter is declared as volatile.

void
set_bits_func_correct (volatile uint8_t *port, uint8_t mask)
{
    *port |= mask;
}

Can anyone explain why they're passing it as volatile? As someone who's a beginner with C, I'm confused and would appreciate an explanation (if there is one).

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Just to check - you are aware that volatile uint8_t *port means that port is a pointer to a volatile uint8_t and not a volatile pointer to a uint8_t and that you know the difference ? \$\endgroup\$
    – brhans
    Sep 8, 2017 at 1:16
  • \$\begingroup\$ I hadn't realized that @brhans, but I do understand the difference. \$\endgroup\$
    – RubberDuck
    Sep 8, 2017 at 2:29

1 Answer 1

2
\$\begingroup\$

It's passed as volatile so the compiler doesn't optimize away any accesses to said parameter. This is required because a compiler may see something like this:

port = 1;
port = 2;
port = 3;

Or perhaps

while (port & 0x01) ...

Assuming that the ending state is all that matters, that can be optimized to

port = 3;

And

some_cpu_register = port;
while (some_cpu_register & 0x01) ...

Saving on instructions, memory accesses, and code space. However, since port actually accesses physical resources, these reads and writes actually are significant and the compiler should be instructed that the programmer knows best here and the compiler should not remove any reads or writes so that the program will perform the intended operations.

\$\endgroup\$
1
  • \$\begingroup\$ If you passed a port's address as a non-volatile parameter, the compiler would complain anyway for discarding a volatile qualifier. The header files already have the SFRs defined volatile for good reason! \$\endgroup\$
    – Jubatian
    Dec 15, 2017 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.