Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a 32bit registers specific bits without changing other bits?

I want to manipulate some bits of a register directly using its physical address. However I couldn't find a way to make this. I saw some posts about setting bit masks but I find them too confusing.

My registers physical address is: 0x4A10005C

I want to manipulate its bit which was between 18-16 bits. I want to set 0x3 inside those bits.

I will be really glad if you guys can provide an answer or a way to do it. Thanks.

like image 756
denizt Avatar asked Nov 02 '12 13:11

denizt


1 Answers

You can just define a pointer to the register and then use normal C bitwise operations to manipulate the individual bits:

volatile uint32_t * const my_register = (uint32_t *) 0x4A10005C;
                              // set up a pointer to the register

uint32_t val = *my_register;  // read register

val &= ~(0x7 << 16);          // clear bits 16..18

val |= (0x3 << 16);           // set bits 16..18 to 0x03 (i.e. set bits 16 and 17)

*my_register = val;           // write register

(The above assumes that you are talking about three bits within the register, bits 16, 17 and 18, and that you want to set bit 18 to zero and bits 16 and 17 to 1.)

like image 116
Paul R Avatar answered Nov 15 '22 05:11

Paul R