Hi,
I am trying to control values of GPIO pins from C by writing into memory through mmap, but I reached a dead end and I don’t know what I could be doing wrong.
I read 8KB starting at 0x10005000 and I try to set mode to GPIO (0), direction to OUT (1) and value to 1, but when I try to read the values right after I set it, I get all zeroes, except for direction. When I started, I was successful at writing to values, but now I can’t even do that.
This is (part of) my code
#define BLOCK_SIZE 4096
#define MTK_GPIO_BASE_ADDR 0x10005000
#define MTK_GPIO_DIR 0x00
#define MTK_GPIO_PULLE 0x150
#define MTK_GPIO_DOUT 0x500
#define MTK_GPIO_DIN 0x630
#define MTK_GPIO_MODE 0x760
int pin = 25;
int fd;
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) {
printf("Unable to open /dev/mem\n");
return -1;
}
gpio = (uint32_t *)mmap(NULL, 2 * BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, MTK_GPIO_BASE_ADDR);
if (((int32_t)gpio == -1) || ((int32_t)gpio == -1 )) {
printf("Failed to initialize mmap\n");
return -1;
}
volatile uint32_t *dirAddr = gpio + MTK_GPIO_DIR + 16 * (pin / 16);
*dirAddr = 0x20;
volatile uint32_t *valAddr = gpio + MTK_GPIO_DOUT + 16 * (pin / 16);
*valAddr= 0x20;
The result for this part of code is
direction address: 10005010
direction: 0000010000000000
value address: 10005510
value: 0000000000000000
I calculate address like this
memAddr = addr - gpio + MTK_GPIO_BASE_ADDR
Thanks for any help or hint!