Usually a microcontroller/DSP manufacturer would provide a register definition header along with a development tool. However, some header files do not come in a good way, which offers difficulties in C/C++ programming. So before coding in C, do some foundation work in the register header file if it was not good shaped.
A good shape of a register header file shall have structure definition in both integer and bits format, as well as assigned to the respective register address.
Example: Renesas SH7047, port A data register L (PADRL) is a 16-bit readable/writable register that stores port A data. It shall be defined in header file (sh7047reg.h) as the following. struct st_pa // struct PA { union // PADRL { unsigned short WORD; // Word Access struct // Byte Access { unsigned char H; // High byte unsigned char L; // Low byte } BYTE; struct // Bit Access { unsigned char B15:1; // Bit 15 unsigned char B14:1; // Bit 14 unsigned char B13:1; // Bit 13 unsigned char B12:1; // Bit 12 unsigned char B11:1; // Bit 11 unsigned char B10:1; // Bit 10 unsigned char B9 :1; // Bit 9 unsigned char B8 :1; // Bit 8 unsigned char B7 :1; // Bit 7 unsigned char B6 :1; // Bit 6 unsigned char B5 :1; // Bit 5 unsigned char B4 :1; // Bit 4 unsigned char B3 :1; // Bit 3 unsigned char B2 :1; // Bit 2 unsigned char B1 :1; // Bit 1 unsigned char B0 :1; // Bit 0 } BIT; } DRL; };
#define PA (*(volatile struct st_pa *)0xFFFF8382) // PA Address
then in C code, you can access the register either by word, by byte, or by bits as shown below.
#include “sh7047reg.h”
PA.DRL.WORD = 0x1234; //access word PA.DRL.BYTE.H = 0x12; //access high byte PA.DRL.BYTE.L = 0x34; //access low byte PA.DRL.BIT.B0 = 1; //access LSB
|