Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding low level initialization in C

Tags:

c

I have the following piece of code. It was generated by my IDE and I am trying to understand it.

#define TRGMR (*(volatile union un_trgmr *)0xF0250).trgmr

Here the timer manager is at the location 0xF0250 according to the data sheet. But what I cant understand is the syntax.

union un_trgmr {
    unsigned char trgmr;
    __BITS8 BIT;
};

I know about pointers. But I really cant understand what exactly is being done. Could someone please help me out?By the way BITS8 is another struct with bitfields as follows:

typedef struct {
    unsigned char no0 :1;
    unsigned char no1 :1;
    unsigned char no2 :1;
    unsigned char no3 :1;
    unsigned char no4 :1;
    unsigned char no5 :1;
    unsigned char no6 :1;
    unsigned char no7 :1;
} __BITS8;
like image 694
Developer Android Avatar asked Jul 21 '13 09:07

Developer Android


1 Answers

It's just a way of accessing a memory-mapped register at a fixed address 0xF0250. You can access individual bits of the registers via the BIT field of the union, or the whole 8 bit register via the trmgr field. The #define just gives you convenient access to the latter, so that you can write, e.g.

TRMGR = 0x3f; // write 0x3f to timer manager register

Note the use of volatile - this is a common technique with memory-mapped I/O registers to ensure that reads/writes always occur as intended and are not optimised away (as they might be with normal memory locations).

like image 64
Paul R Avatar answered Oct 18 '22 23:10

Paul R