掌機 - Miyoo Mini Plus - C/C++ - Read GPIO



main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <time.h>

int main(int argc, char **argv)
{
    int fd = open("/dev/mem", O_RDWR);

    if (fd < 0){
        printf("failed to open /dev/mem\n");
        return -1;
    }

    uint8_t *gpio = mmap(0, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x1f000000 + 0x200000);
    printf("gpio table ptr: 0x%x\n", gpio);

    // gpio-11 = 0x20782c
    while (1) {
        printf("0x%x\n", gpio[0x782c] & 1);
        usleep(500000);
    }
    close(fd);
    return 0;
}