I have been reading about accessing Memory Mapped Registers of peripheral devices and it seems you can do multiple ways. For example:
Method 1:
#define MyReg 0x30610000
volatile int *ptrMyReg;
ptrMyReg = (volatile int *) MyReg;
*ptrMyReg = 0x7FFFFFFF; /* Turn ON all bits */
Method 2:
#define MyReg 0x30610000
volatile unsigned char *ptrMyReg;
ptrMyReg = (volatile unsigned char *) MyReg;
*ptrMyReg = 0x7FFFFFFF; /* Turn ON all bits */
Question: Is there any specific reason as to why one would choose one over another?
Assume: Size of int on architecture is 4 bytes.
*ptrMyReg = 0x7FFFFFFF;
In the second case, *ptrMyReg
is of type unsigned char
so 0x7FFFFFFF
will be converted to unsigned char
(i.e., value after conversion will be 0xFF
) before assignment and only one byte will be written. I don't think this what you want if you originally intended to write 4 bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With