Structured C access to Coldfire peripheral registers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Structured C access to Coldfire peripheral registers

1,981 Views
vpmeister
Contributor I
I am just moving from the 8-bit world to a Coldfire project. Am I missing something, or do the .h header files that get copied into a Coldfire Stationary project not have bit-wise and grouped bits access via structures like in the HCS08 headers?

I had to cook something up like the following to get simple bit access to various structures. I am sure with a little more thought I could have made the names just like the names in the HCS08 headers. That might make porting easier. Hopefully, somebody else has already done this. I sure would hate to have to reproduce this for all the peripheral registers on the MCF5xxx family!

typedef union {
unsigned char Byte;
struct {
unsigned char x7 :1;
unsigned char x6 :1;
unsigned char x5 :1;
unsigned char x4 :1;
unsigned char x3 :1;
unsigned char x2 :1;
unsigned char x1 :1;
unsigned char x0 :1;
} Bits;
} ByteSTR;
#define _PORTAS ((ByteSTR*)(&__IPSBAR[0x10000C]))
#define PORTAS _PORTAS->Byte
#define PORTAS_x0 _PORTAS->Bits.x0
#define PORTAS_x1 _PORTAS->Bits.x1
#define PORTAS_x2 _PORTAS->Bits.x2
#define PORTAS_x3 _PORTAS->Bits.x3
#define PORTAS_x4 _PORTAS->Bits.x4
#define PORTAS_x5 _PORTAS->Bits.x5
#define PORTAS_x6 _PORTAS->Bits.x6
#define PORTAS_x7 _PORTAS->Bits.x7
#define _PORTASDD ((ByteSTR*)(&__IPSBAR[0x100020]))
#define PORTASDD _PORTASDD->Byte
#define PORTASDD_x0 _PORTASDD->Bits.x0
#define PORTASDD_x1 _PORTASDD->Bits.x1
#define PORTASDD_x2 _PORTASDD->Bits.x2
#define PORTASDD_x3 _PORTASDD->Bits.x3
#define PORTASDD_x4 _PORTASDD->Bits.x4
#define PORTASDD_x5 _PORTASDD->Bits.x5
#define PORTASDD_x6 _PORTASDD->Bits.x6
#define PORTASDD_x7 _PORTASDD->Bits.x7
Labels (1)
0 Kudos
Reply
1 Reply

729 Views
mvincent
Contributor I
I guess this is a matter of preference.

Bit fields are implementation-dependant, so your code might work with one compiler but not with another. Also, assigning bits using bit fields might result in a read-modify-write for each bit assignments, while using bit masks result in a single write:
> reg.bitA=1; reg.bitB=1; vs reg=MASK_A | MASK_B;

Reading a single bit using either mask or bit field generates very similar copmpiled code:
> if(reg.bitA)... vs. if(reg & MASK_A)...
0 Kudos
Reply