Go Down

Topic: How does write protection of individual register bits work? (Read 850 times) previous topic - next topic


I've been going through the SAMD21 datasheet and have noticed some places where it's mentioned that certain registers have write-protection, but only on certain bits. For example, on page 709, in reference to the TCC CTRLA register:

The following registers are enable-protected, meaning that they can only be written when the TCC is disabled(CTRLA.ENABLE=0):
        • Control A (CTRLA) register, except Run Standby (RUNSTDBY), Enable (ENABLE) and Software Reset (SWRST) bits
What's confusing me is how exactly individual bits can be write-protected when, at least as far as I know, it's not possible to write bits individually. I believe the smallest write one can do on this chip is 8-bit, and as you can see on page 745, writing any of the 8-bit sections of this register would definitely write to at least one write-protected bit. So even if you are allowed to write to some of the bits, you're guaranteed an access error for all the other bits.

Could someone explain how this is supposed to work?


Hi indeed,

The SAMD21 offers both enable protection and write protection on some of its peripehral registers.

Enable protection simply prevents some bits in the registers from being altered once the peripheral has been enabled. This is done as things would go awry if certain bits were to be changed during operation.

Write protection is used to protect peripheral registers from being accessed during critical sections of code. This can be useful when the microcontroller is running multi-tasking code such as a Real Time Operating System (RTOS). Write protection provides a form of mutual exclusion (mutex) that controls each task's access to the peripheral resource, thereby preventing one task from overwriting peripheral registers that are currently being used by another.


I know I said "write protection" in my post, but I meant to refer to the behaviour of the enable protection I mentioned (maybe "write protection" formally means something else).

In any case, are you sure the enable protection only ignores attempted writes and doesn't cause any access errors? Because I'm pretty sure I've observed errors (i.e. system crashes) when writing to enable-protected registers.


Jul 23, 2019, 12:37 pm Last Edit: Jul 23, 2019, 12:38 pm by david_prentice
If the datasheet advises you to disable before changing certain registers,   why would you deliberately ignore it?

In my experience the "write" is ignored.  i.e. no write.  no change.
But it could do the write but ignore the change.
Or it could bring down Hell and Damnation to spite you.

Yes,   datasheets often give dire warnings that don't actually take place.
You can investigate the behaviour for yourself.    And if you find that nothing bad happens publish firmware that works for your particular Silicon Revision but subsequent products with later chips fail.

It is not a good idea to ignore datasheets.
You seldom gain anything by skipping a disable-set-enable sequence.    (apart from a future bloody nose)



In this case there is a distinction between enable protection and write protection.

Register write protection is controlled by the SAMD21's Peripheral Access Controller (PAC) module. This allows or bars access to certain peripherals.

Enable protection should prevent certain register bits from being set once the peripheral has been enabled. On the SAMD21 after clearing the peripheral's ENABLE bit, you just have to make sure that synchronization has taken place before writing other bits in the register.

As David says, if the datasheet says that the register is enable protected, then it's best to only write to it when the peripheral is disabled.

Go Up