Working with the reserved bits of the ARM-chip register

I am working with ARM Cortex M3 registers. Some of the bits may be "reserved" in the documentation. It is not clear to me how I should handle these reserved bits when writing to registers.

Are these reserved bits writable? Should I be careful not to touch them? Will there be anything bad if I touch them?


source to share

5 answers

This is a classic problem in the world about what to do with reserved bits! First, you must NOT write by accident so that your code is not portable. What happens when the architecture assigns a new value to reserved bits in the future? Your code will break. Therefore, the best mantra when dealing with registries with reserved bits is Read-Modify-Write

. those. read the contents of the register, change only the bits you want , and then write the value so that the reserved bits are intact (untouched, doesn't mean we don't write to them, but in the sense that we wrote what was there before )

For example, let's say there is a register in which only LSBit makes sense and everyone else is reserved. I would do it

ldr r0,=memoryAddress
ldr r1,[r0]
orr r1,r1,#1
str r1,[r0]




If there is no other key in the documentation, write zero. You cannot avoid writing multiple reserved bits in a 32 bit register.



Read-Modify-Write should work most of the time, however there are cases where reserved bits are undefined when read but must be written with a specific value. See this post from the LPC2000 group (the whole thread is interesting too). Therefore, always check the documents carefully, as well as any errors that may appear. In case of doubt or documents unclear, do not hesitate to write to the manufacturer.



Ideally you should read-modify-write, not guarantee success, when you upgrade to a new chip with different bits, you change your code anyway. I have seen vendors where writing zeros to reserved bits failed when they updated the chip and had to touch the code. Therefore, there are no guarantees. The biggest clue is that in the vendor code you see a register or set that explicitly read-modify-write or explicitly write. It can be different developers who write different sections of the example, or there is a register in the peripheral that is sensitive, has an undocumented bit and needs read-modify-write.

On the chips I'm working on, I make sure the undocumented (client-side) but not unused bits are flagged in some way to stand out from the other unused bits. Usually we mark the unused / reserved bits as zero, and these other bits are given a name and must write this value marking. Not all vendors do this.

There is no guarantee on the bottom line, suppose all the documentation and sample programs have errors and you need to hack your way to figure out what is right and what is wrong. Regardless of which path you take (read-modify-write, write zeros, etc.), you will make mistakes from time to time and have to re-execute the code according to the hardware change. I highly recommend that if the vendor has a chip ID then your software reads that ID, and if it's an ID that you haven't tested your code with, declare a failure rather than skip this part. When testing the product, long before the customer sees the product, a part change will be detected and the software will be involved in understanding the reason for the part change, a resolution being an alternative part,incompatible and rejected, or software changes, etc.



Reserved most of the time means they are not used in this chip, but they can be used on feature devices (in a different product line). (Most chip makers produce one peripheral driver and they use that for all chips. So it basically copies past work and has fewer bugs.) Most of the time, it doesn't matter if you write the reserved bits in the peripheral registers, this is because there is no logic attached to it.

It is possible that if you write something it will not be saved and the next time you try to read the register / bits it will not change.



All Articles