Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: [lpc2000] To bit pack or not

2005-04-01 by Charles Manning

On Friday 01 April 2005 13:00, peterburdine wrote:
> While not lpc2000 specific, I wanted to see if anyone else here was
> using bit packing to access registers or if they were writing macros
> or just doing to shifts and masking (w/o macros).
>
> I spoke with the software guys here where I work (I'm and EE) and 2
> out of 3 of them didn't think that using bit packing was a good idea
> for any application, but the 1 thought that using it to access fields
> with a register was a great idea.  I know that portability is a
> concern of theirs, but since this is a device driver for 1 chip and
> the code already has extensions which force it to be compiled with a
> specific compiler (Keil's in the case) that I don't see that as a
> valid argument.  What do you think?

I think you should always target portability. Code has a habit of outlasting 
its original programming. I'm using some code that has been through 15 years 
and 6 projects.

Also, compilers can vary with versions and compile options. To depend on 
specific non-define behaviour is asking for trouble.


>
> For those who don't know bit packing it allows you to do this (for
> example the ADDR Register):
>
> struct	ADDR_REG {
> 	unsigned	pad0:6;  // not used
> 	unsigned	V_V3A:10;
> 	unsigned	pad1:8; // not used
> 	unsigned	CHN:3;
> 	unsigned	pad2:3; // not used
> 	unsigned	OVERRUN:1;
> 	unsigned	DONE:1;
> };
>
> #define ADDR           (*((volatile struct ADDR_REG *) 0xE0034004))
>
> while(!ADDR.DONE); // spin
> adcVal = ADDR.V_V3A; // read value

One of the problems with bit packing is that is encourages you to abstract 
away what is happening on the registers.

In some devices/peripherals, you should read-modify-write and in some you 
just set a bit (eg IO setting clearing).

It is not apparent what bitfields will do.

Sure you can test it on a particular compiler but they might change it in the 
next version (or with different compile options).

with

  REGX |= REGX_FLAG5;

or 
  REGX = FLAG5;

You **definitely** know what the compiler will do.

I regularly use bitfields for software variables, but I'd never use them for 
peripherals.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.