Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux kernel 无法从内核模块中的GPIO读取值_Linux Kernel_Raspberry Pi_Kernel Module_Raspberry Pi3_Gpio - Fatal编程技术网

Linux kernel 无法从内核模块中的GPIO读取值

Linux kernel 无法从内核模块中的GPIO读取值,linux-kernel,raspberry-pi,kernel-module,raspberry-pi3,gpio,Linux Kernel,Raspberry Pi,Kernel Module,Raspberry Pi3,Gpio,我正在创建一个内核模块设备驱动程序来使用GPIOs。我已经花了很多时间,但仍然无法找出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 CONFIGURAT

我正在创建一个内核模块设备驱动程序来使用GPIOs。我已经花了很多时间,但仍然无法找出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"
我试着这样读取值

motion_value = GET_GPIO(MOTION_SENSOR_PIN);
它总是返回0,但是如果我的PIN码设置为12,如上面的代码所示。它读取值4096。我猜这是2^12,这很好,我可以&或者|我可以看看水平是高还是低

但问题是,如果我使用PIN21或任何其他,它总是返回0。当然,我重新连接了我的传感器到这个引脚

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

使用wiringpibcm2835库和其他工具,它工作得很好


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

我假设您需要先使用BCM2835_GPFSEL寄存器将引脚配置为输入,因为内部引脚连接到多路复用器,如果您说输出/清除工作,它可能会通过寄存器拉到地上,因此您将始终读取零

以一些现有的GPIO库(甚至Linux内核代码)为例。

我假设您需要首先使用BCM2835_GPFSEL寄存器将管脚配置为输入,因为内部管脚连接到多路复用器,如果您说输出/清除工作,则可能会使用寄存器将其拉到地上,以便始终读取零

以一些现有的GPIO库(甚至Linux内核代码)为例。

为什么要编写已经编写的驱动程序?!为什么要写驱动程序已经写了?!
motion_value = GET_GPIO(MOTION_SENSOR_PIN);