Quantcast
Channel: Active questions tagged kernel-modules - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 1181

How do I Make a Kernel Module for GPIO and access it for use in User Space?

$
0
0

The manufacturing of the kernel modules in question are for peripherals like SPI, UART, GPIO, PWM, I2C, and/or other specific peripherals.

  1. I have gotten to write some very basic kernel modules.
  2. I write my module, install kernel headers for allocating the written module dynamically, install the module, and then use it.
  3. I then check logs from my printk statements via dmesg -w.

Also, why does insmod work and then not work? It only works at times but then works and sometimes it does not work at all. Currently, I have kernel headers installed, a module written, and it installed via make and Kbuild.

Here is the kernel module I found online and then referenced it for a Linux system SBC called the beaglebone black.

// Can be found here: // https://github.com/Johannes4Linux/Linux_Driver_Tutorial/blob/main/03_gpioctrl/gpioctrl.c#include <linux/module.h>#include <linux/init.h>#include <linux/gpio/consumer.h>static struct gpio_desc *led, *button;#define IO_LED 21#define IO_BUTTON 20#define IO_OFFSET 0static int __init my_init(void){    int status;    led = gpio_to_desc(IO_LED + IO_OFFSET);    if (!led) {        printk("gpioctrl - Error getting pin %d\n", IO_LED);        return -ENODEV;    }    button = gpio_to_desc(IO_BUTTON + IO_OFFSET);    if (!button) {        printk("gpioctrl - Error getting pin %d\n", IO_BUTTON);        return -ENODEV;    }    status = gpiod_direction_output(led, 0);    if (status) {        printk("gpioctrl - Error setting pin %d to output\n", IO_LED);        return status;    }    status = gpiod_direction_input(button);    if (status) {        printk("gpioctrl - Error setting pin %d to input\n", IO_BUTTON);        return status;    }    gpiod_set_value(led, 1);    printk("gpioctrl - Button is %spressed\n", gpiod_get_value(button) ? "" : "not ");    return 0;}static void __exit my_exit(void){    gpiod_set_value(led, 0);}module_init(my_init);module_exit(my_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Johannes 4Linux");MODULE_DESCRIPTION("An example for using GPIOs without the device tree");

I have since changed this kernel module to handle specific GPIO pins on my SBC but I am getting negative output from dmesg. What does the below dmesg output mean for me?

[  169.891446] invalid GPIO 600022[  169.891474] gpioctrl - Error getting pin 22[  182.258730] invalid GPIO 600022[  182.258757] gpioctrl - Error getting pin 22[  352.302909] gpioctrl - Error getting pin 24[  412.875153] gpioctrl - Error getting pin 24

I see the invalid GPIO address of 600022 but I am not referencing this specific address for the pins I am using that I found via gpiodetect.


Viewing all articles
Browse latest Browse all 1181

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>