ARM linux用户空间gpio操作使用mmap / dev / mem方法(能够写入GPIO寄存器,但无法读取它们)

时间:2016-10-05 14:44:41

标签: c linux arm mmap gpio

PHYTEC的内核版本3.12.30-AM335x-PD15.1.1。

如果我使用/ sys / class / gpio方式,我可以看到按钮输入引脚(AM3359的gpio103)值从0变为1.

完成本练习后http://elinux.org/EBC_Exercise_11b_gpio_via_mmap 并执行以下命令来读取gpio引脚usig / dev / mem方法:

`devmem2 0x481ae13c` 

(gpio bank 3的基础是0x481ae000 + 0x13c dataout offset)

无论按钮位置如何,我都得到以下输出。

/dev/mem opened
Memory mapped at address 0xb6fd1000.
Read at address  0x481AE13C (0xb6fd113c): 0x00000000

同样使用下面给出的c程序,我设法使用/ dev / mem切换gpios;但是我无法成功地阅读它们。

头文件中的

#define GPIO0_START_ADDRESS (0x44E07000)
#define GPIO1_START_ADDRESS (0x4804C000)
#define GPIO2_START_ADDRESS (0x481AC000)
#define GPIO3_START_ADDRESS (0x481AE000)

#define GPIO0_END_ADDRESS   (0x44e09000)
#define GPIO1_END_ADDRESS   (0x4804E000)
#define GPIO2_END_ADDRESS   (0x481AE000)
#define GPIO3_END_ADDRESS   (0x481B0000)

#define GPIO0_SIZE (GPIO0_END_ADDRESS - GPIO0_START_ADDRESS)
#define GPIO1_SIZE (GPIO1_END_ADDRESS - GPIO1_START_ADDRESS)
#define GPIO2_SIZE (GPIO2_END_ADDRESS - GPIO2_START_ADDRESS)
#define GPIO3_SIZE (GPIO3_END_ADDRESS - GPIO3_START_ADDRESS)

#define GPIO_SETDATAOUT (0x194)
#define GPIO_CLEARDATAOUT (0x190)
#define GPIO_DATAOUT (0x13c)

程序中的某个地方:

    int fd = open("/dev/mem", O_RDWR);
    //int fd = open("/dev/mem", O_RDWR|O_SYNC);

    gpio0_address = mmap(0, GPIO0_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO0_START_ADDRESS);
    gpio1_address = mmap(0, GPIO1_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO1_START_ADDRESS);
    gpio2_address = mmap(0, GPIO2_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO2_START_ADDRESS);
    gpio3_address = mmap(0, GPIO3_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO3_START_ADDRESS);

    if(gpio0_address == MAP_FAILED){ printf("unable to map GPIO0 bank.\n"); }
    if(gpio1_address == MAP_FAILED){ printf("unable to map GPIO1 bank.\n"); }
    if(gpio2_address == MAP_FAILED){ printf("unable to map GPIO2 bank.\n"); }
    if(gpio3_address == MAP_FAILED){ printf("unable to map GPIO3 bank.\n"); }


    gpio0_dataout_address = gpio0_address + GPIO_DATAOUT;

    gpio0_setdataout_address = gpio0_address + GPIO_SETDATAOUT;

    gpio0_cleardataout_address = gpio0_address + GPIO_CLEARDATAOUT;

    gpio1_dataout_address = gpio1_address + GPIO_DATAOUT;

    gpio1_setdataout_address = gpio1_address + GPIO_SETDATAOUT;

    gpio1_cleardataout_address = gpio1_address + GPIO_CLEARDATAOUT;

    gpio2_dataout_address = gpio2_address + GPIO_DATAOUT;

    gpio2_setdataout_address = gpio2_address + GPIO_SETDATAOUT;

    gpio2_cleardataout_address = gpio2_address + GPIO_CLEARDATAOUT;

    gpio3_dataout_address = gpio3_address + GPIO_DATAOUT;

    gpio3_setdataout_address = gpio3_address + GPIO_SETDATAOUT;

    gpio3_cleardataout_address = gpio3_address + GPIO_CLEARDATAOUT;
    read_GPIO_bank_memory(3, &GPIO3_bank_memory);

读取功能:

   void read_GPIO_bank_memory(int bank, four_bytes* gpio_bank_dataout)
    {
        switch(bank)
        {
            case 0:

                gpio_bank_dataout->ALL = *gpio0_dataout_address;
                break;
            case 1:

                gpio_bank_dataout->ALL = *gpio1_dataout_address;

                break;
            case 2:

                gpio_bank_dataout->ALL = *gpio2_dataout_address;

                break;
            case 3:

                gpio_bank_dataout->ALL = *gpio3_dataout_address;

                break;
        }
    }

1 个答案:

答案 0 :(得分:3)

您应该使用GPIO_DATAIN寄存器(偏移= 138h)来读取GPIO引脚,而不是GPIO_DATAOUT。