
On Sun, May 15, 2011 at 03:15:46PM -0700, Simon Glass wrote:
I believe that this problem is getting worse - e.g. USB on Tegra2 writes various fields of about 20 registers to get things up and running. I find translating SOC datasheet register definitions into C code with shifts and masks to be slow and error-prone work. Also we do need to maintain this code, and it gets reused for new SOC variants, etc. So it is not as if it is written once and then buried and forgotten. There is also a tendency to use 'magic' constants rather than #define values or something with a sensible name, then hopefully add a half-hearted comment. This requires constant return looks at the datasheet to see what bits were chosen.
I have nothing against helper functions like this (although I think they should be inline functions, not macros, to guard against bugs due to side-effects in arguments).
But wouldn't it be even better to define the registers as C structures containing bitfields, so the C compiler can do the error-prone shifting and masking? Well-commented structure definitions for these registers also eliminate the need to refer back to the datasheet when working on the code.