Writting register

On AM335x,a register has its base address and offset. Why do we need to divide offset by 4 for reference ?

An example from am335x sdk

We don’t need to, but I’ve seen others do that before . . . https://github.com/wphermans/BeagleBone-GPIO/blob/master/src/gpio.c#L98 Not my code, but I forked it. Because its a decent example how one might access some various registers. However,in the case of that specific example, I do not think it’s particularly safe to map the whole “register base” when working with hardware. Especially, when given code only needs todo one thing or another . . .

Me, I prefer to do something like this . . .

#define GPIO0 (0x44E07000)
#define GPIO2 (0x481AC000)
#define GPIO_SIZE (0x2000)

#define GPIO_DATAOUT (0x13C)
#define GPIO_DATAIN (0x138)

#define MA0 (1<<26) /gpio_0/
#define MA1 (1<<23) /gpio_2/
#define MA2 (1<<24) /gpio_2/

void line_select(uint8_t asp_value)
void *gpio_addr;
unsigned int *bank0_out, *bank2_out;

int fd = open("/dev/mem", O_RDWR);
if(fd == -1){

gpio_addr = mmap(0, GPIO_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO0);
if(gpio_addr == MAP_FAILED){
bank0_out = gpio_addr + GPIO_DATAOUT;

gpio_addr = mmap(0, GPIO_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO2);
if(gpio_addr == MAP_FAILED){
bank2_out = gpio_addr + GPIO_DATAOUT;


(asp_value & (1 << 0)) ? (*bank0_out |= MA0) : (*bank0_out &= ~(MA0));
(asp_value & (1 << 1)) ? (*bank2_out |= MA1) : (*bank2_out &= ~(MA1));
(asp_value & (1 << 2)) ? (*bank2_out |= MA2) : (*bank2_out &= ~(MA2));

But you also need to keep in mind this code is specific for one thing, and not some general purpose map of all the registers. Even though I’m using this as a general purpose way to map more than one register( GPIO banks, 2 in this case).