【问题标题】:Cannot read value from GPIO in a kernel module无法从内核模块中的 GPIO 读取值
【发布时间】:2017-11-20 02:18:18
【问题描述】:

我正在创建一个内核模块设备驱动程序来使用 GPIO。我花了很多时间,但仍然无法弄清楚 GPIO 读取有什么问题。

设置和清除 GPIO 效果很好。

这是包含所有常量和宏的头文件。

/*! This means pin HIGH, true, 3.3volts on a pin. */
#define HIGH 0x1
/*! This means pin LOW, false, 0volts on a pin. */
#define LOW  0x0

// ------ BCM2835 CONFIGURATION -----
#define BCM2835_GPFSEL0                      0x0000 /*!< GPIO Function Select 0 */
#define BCM2835_GPFSEL1                      0x0004 /*!< GPIO Function Select 1 */
#define BCM2835_GPFSEL2                      0x0008 /*!< GPIO Function Select 2 */
#define BCM2835_GPFSEL3                      0x000c /*!< GPIO Function Select 3 */
#define BCM2835_GPFSEL4                      0x0010 /*!< GPIO Function Select 4 */
#define BCM2835_GPFSEL5                      0x0014 /*!< GPIO Function Select 5 */
#define BCM2835_GPSET0                       0x001c /*!< GPIO Pin Output Set 0 */
#define BCM2835_GPSET1                       0x0020 /*!< GPIO Pin Output Set 1 */
#define BCM2835_GPCLR0                       0x0028 /*!< GPIO Pin Output Clear 0 */
#define BCM2835_GPCLR1                       0x002c /*!< GPIO Pin Output Clear 1 */
#define BCM2835_GPLEV0                       0x0034 /*!< GPIO Pin Level 0 */
#define BCM2835_GPLEV1                       0x0038 /*!< GPIO Pin Level 1 */
#define BCM2835_GPEDS0                       0x0040 /*!< GPIO Pin Event Detect Status 0 */
#define BCM2835_GPEDS1                       0x0044 /*!< GPIO Pin Event Detect Status 1 */
#define BCM2835_GPREN0                       0x004c /*!< GPIO Pin Rising Edge Detect Enable 0 */
#define BCM2835_GPREN1                       0x0050 /*!< GPIO Pin Rising Edge Detect Enable 1 */
#define BCM2835_GPFEN0                       0x0058 /*!< GPIO Pin Falling Edge Detect Enable 0 */
#define BCM2835_GPFEN1                       0x005c /*!< GPIO Pin Falling Edge Detect Enable 1 */
#define BCM2835_GPHEN0                       0x0064 /*!< GPIO Pin High Detect Enable 0 */
#define BCM2835_GPHEN1                       0x0068 /*!< GPIO Pin High Detect Enable 1 */
#define BCM2835_GPLEN0                       0x0070 /*!< GPIO Pin Low Detect Enable 0 */
#define BCM2835_GPLEN1                       0x0074 /*!< GPIO Pin Low Detect Enable 1 */
#define BCM2835_GPAREN0                      0x007c /*!< GPIO Pin Async. Rising Edge Detect 0 */
#define BCM2835_GPAREN1                      0x0080 /*!< GPIO Pin Async. Rising Edge Detect 1 */
#define BCM2835_GPAFEN0                      0x0088 /*!< GPIO Pin Async. Falling Edge Detect 0 */
#define BCM2835_GPAFEN1                      0x008c /*!< GPIO Pin Async. Falling Edge Detect 1 */
#define BCM2835_GPPUD                        0x0094 /*!< GPIO Pin Pull-up/down Enable */
#define BCM2835_GPPUDCLK0                    0x0098 /*!< GPIO Pin Pull-up/down Enable Clock 0 */
#define BCM2835_GPPUDCLK1                    0x009c /*!< GPIO Pin Pull-up/down Enable Clock 1 */

#define BCM2835_PERI_BASE       0x3F000000
#define GPIO_BASE               (BCM2835_PERI_BASE + 0x200000)    // GPIO controller
#define INP_GPIO(g) *(gpio.addr +((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio.addr+((g)/10)) |=  (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio.addr+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio.addr+7)  // sets   bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio.addr+10) // clears bits which are 1 ignores bits which are 0

#define GET_GPIO(g) (*(gpio.addr + BCM2835_GPLEV0/4)&(1<<g)) // 0 if LOW, (1<<g) if HIGH

#define GPIO_PULL *(gpio.addr+37) // Pull up/pull down
#define GPIO_PULLCLK0 *(gpio.addr+38) // Pull up/pull down clock

struct bcm2835_peripheral {
    unsigned long addr_p;
    int mem_fd;
    void *map;
    volatile unsigned int *addr;
};

struct bcm2835_peripheral gpio = {GPIO_BASE};


// ********* GPIO Support ************
#define MOTION_SENSOR_PIN 12
#define MOTION_DRIVER_DEVICE_NAME   "motionDetector"

这里是GPIO的初始化

gpio.map     = ioremap(GPIO_BASE, 4096);
gpio.addr    = (volatile unsigned int *)gpio.map;
printk("module gpio : MOTION GPIO Init\n");
INP_GPIO(MOTION_SENSOR_PIN);

我正在尝试读取这样的值

motion_value = GET_GPIO(MOTION_SENSOR_PIN);

它总是返回 0,但是如果我的 PIN 码设置为 12,如上面的代码所示。它读取 4096 的值。我猜这是 2^12,没关系,我可以 & or |它来找出级别是高还是低。

但问题是,如果我使用 PIN 21 或其他任何密码,它总是返回 0。当然,我将传感器重新连接到此引脚。

我的代码有什么问题?为什么它不起作用?

使用 wiringpibcm2835 库等效果很好。

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 为什么要写已经写好的驱动?!

标签: linux-kernel raspberry-pi kernel-module raspberry-pi3 gpio


【解决方案1】:

我假设您需要首先使用 BCM2835_GPFSEL 寄存器将引脚配置为输入,因为内部引脚连接到多路复用器,如果您说输出/清除工作,它可能会被一个寄存器上拉到地面,所以你总是会读到零。

查看一些现有的 GPIO 库(甚至是 Linux 内核代码)作为示例。 https://github.com/machinekit/machinekit/blob/master/src/hal/drivers/hal_gpio.c https://gist.github.com/kinsamanka/94ba7940e4e222c844c8

【讨论】:

    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2015-01-04
    • 2014-10-19
    • 2016-01-16
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多