Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between accessing Memory Mapped Registers using char and int

Tags:

c

embedded

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.

like image 472
modest Avatar asked Feb 16 '23 18:02

modest


1 Answers

*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.

like image 51
ouah Avatar answered Apr 09 '23 01:04

ouah