I'm confused with register wrting method by compound assignment and/or pointer because I don't know where are some of the values come from.

Example 1

REG_PMC_PCER1 |= PMC_PCER1_PID36;  // Enable PWM - Peripheral Identifiers 36
  • This works the same as REG_PMC_PCER1 = (1<<4)
  • REG_PMC_PCER1 is not readable, so |= operator shouldn't work.
  • What is PMC_PCER1_PID36 ? Is the some sort of bit mask defined in the IDE ?

Example 2

PMC->PMC_PCER0 |= PMC_PCER0_PID13;   // PWM controller power ON
  • This works the same as REG_PMC_PCER0 = (1<<13)
  • This is similar to Example 1 but there is pointer PMC. Why it's needed ? If PMC_PCER0_PID13 is a bit mask then REG_PMC_PCER0 need not indexing.

So my questions are:

  1. Why compound assignment works on write-only registers ?
  2. Where are those constants (PMC_PCER1_PID36,PMC,etc.) defined ?

Thanks in advance !

1 Answer 1


Why compound assignment works on write-only registers ?

Probably out of force of habit more than anything. People are used to having to set bits in registers where the other bits can be read and they want them to stay the same, in which case |= makes sense.

I'd considered that |= might be generating a more efficient instruction, but at least with your REG_PMC_PCER1 |= PMC_PCER1_PID36 example objdump indicates that this is not the case.

Where are those constants (PMC_PCER1_PID36,PMC,etc.) defined ?

Code-wise, they're defined in the ATMEL's (now Microchip's) CMSIS files. These are header files whose existence is somewhat standardized by ARM for these ARM Cortex processors. Much of their content isn't however. E.g., you may see a few things related Cortex M3 that are the same between this ATMEL device in an STM32 counterpart, but GPIO registers may be completely different.

My broadly, they're defined by ATMEL in their datasheets.

  • I haven't found a statement that explains flatly that a write-only register will appear to be all zero bits when attempting to be read, but that is likely the case. If I find a simple statement to that effect I'll call it out.
    – timemage
    Nov 22, 2020 at 13:57

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.