How to control a GPIO in kernel space

来自百问网嵌入式Linux wiki
Zhouyuebiao讨论 | 贡献2020年5月7日 (四) 23:52的版本 (创建页面,内容为“==Purpose== This article gives an example of a driver that '''controls GPIOs from kernel space'''.<br/> Sample source files are provided as examples: '''kernel modu…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

Purpose

This article gives an example of a driver that controls GPIOs from kernel space.
Sample source files are provided as examples: kernel module (driver), device tree and Makefile.
This example is available for STM32MP15_Evaluation_boards or STM32MP15_Discovery_kits

Code

Objective

Sample gpiolib usage code that makes an LED blink for 20 seconds.

Device tree

dummy_device {
   compatible = "st,dummy";
   status = "okay";
   greenled-gpios = <&gpioa 14 0>;
};

See GPIO_device_tree_configuration for more details of GPIO use in a device tree.

Kernel module code

#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>

struct gpio_desc *red, *green;

static int gpio_init_probe(struct platform_device *pdev)
{
   int i = 0;

   printk("GPIO example init\n");

   /* "greenled" label is matching the device tree declaration. OUT_LOW is the value at init */
   green = devm_gpiod_get(&pdev->dev, "greenled", GPIOD_OUT_LOW);

   /* blink of the green led */	
   while (i < 10)
   {
	ssleep(1);
	gpiod_set_value(green, 1);

	ssleep(1);
	gpiod_set_value(green, 0);
	
	i++;
   }

   return(0);
}

static int gpio_exit_remove(struct platform_device *pdev)
{
   printk("GPIO example exit\n");
   
   return(0);
}

/* this structure does the matching with the device tree */
/* if it does not match the compatible field of DT, nothing happens */
static struct of_device_id dummy_match[] = {
    {.compatible = "st,dummy"},
    {/* end node */}
};

static struct platform_driver dummy_driver = {
    .probe = gpio_init_probe,
    .remove = gpio_exit_remove,
    .driver = {
        .name = "dummy_driver",
                .owner = THIS_MODULE,
                .of_match_table = dummy_match,
    }
};
 
module_platform_driver(dummy_driver);

MODULE_AUTHOR("Bernard Puel");
MODULE_DESCRIPTION("Gpio example");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:dummy_driver");

Kernel module build

See Adding_external_out-of-tree_Linux_kernel_modules for further information on module compilation.

<securetransclude src="ProtectedTemplate:ReviewsComments" params="To be updated when the build-method example is added to the ST delivery"></securetransclude>{{#set:Has reviews comments=true}}

Kernel module use

PC $> scp dummy_driver.ko root@<board ip address>:/lib/modules/
  • Update dependency descriptions for loadable kernel modules, and synchronize the data on disk with memory
Board $> /sbin/depmod -a
Board $> sync
  • Insert the kernel module example into the Linux kernel
Board $> modprobe dummy_driver
[18167.821725] dummy_driver: GPIO example init

<securetransclude src="ProtectedTemplate:PublicationRequestId" params="8794 | 2018-09-25 | PhilipS"></securetransclude>