一个芯片厂商生产出芯片后会给linux提供一个当前芯片中gpio外设的驱动,我们当前只需要调用对应的厂商驱动即可完成硬件的控制。而linux内核源码中的gpio厂商驱动有很多,这里linux内核对厂商驱动做了一些封装,提供了一系列的API,我们在自己编写的设备驱动中只需要调用这些API即可访问对应的厂商驱动,进而完成GPIO的控制

添加LED的设备树节点
myled
{
led1-gpio=<&gpioe 10 0>;//10表示使用的gpioe第几个管脚 0,表示gpio默认属性
led2-gpio=<&gpiof 10 0>;
led3-gpio=<&gpioe 8 0>;
};
或者
myled{ led-gpios=<&gpioe 10 0>,<&gpiof 10 0>,<&gpioe 8 0>; };
执行make dtbs编译设备树,将编译生成的设备树镜像拷贝到~/tftpboot目录下,重启开发板
struct device_node *of_find_node_by_path(const char *path)
#include
int of_get_named_gpio(struct device_node *np(设备树节点指针),const char *propname(gpio编号信息对应的键名), int index(属性键值对中的索引号))
#include
int gpio_request(unsigned gpio, const char *label)
int gpio_direction_output(unsigned gpio, int value)
void gpio_set_value(unsigned gpio, int value)
int gpio_get_value(unsigned gpio)
void gpio_free(unsigned gpio)
核心不再是GPIO编号,而是GPIO对象
1)在设备树节点中解析出GPIO对象,并向内核申请
struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
const char *propname, int index, enum gpiod_flags dflags, const char *label)

2)int gpiod_direction_output(struct gpio_desc *desc, int value)
int gpiod_direction_input(struct gpio_desc *desc)
void gpiod_set_value(struct gpio_desc *desc, int value)
int gpiod_get_value(const struct gpio_desc *desc)
void gpiod_put(struct gpio_desc *desc)//释放gpi对象指针
定时时间到达之后可以执行当前的定时器处理函数
内核中用于保存内核节拍数的一个变量。它的值从内核启动开始就不断从0开始增加。
内核节拍数一秒钟增加的数量被称为内核的频率,内核的频率在内核顶层目录下的.config文件中被设置
struct timer_list mytimer;

void timer_setup(struct timer_list *timer, void (*func)(struct timer_list *), unsigned int flags);
void add_timer(struct timer_list *timer)
int mod_timer(struct timer_list *timer, unsigned long expires)
int del_timer(struct timer_list *timer)
- #ifndef __HEAD_H__
- #define __HEAD_H__
- //构建LED开关功能码,添加ioctl第三个参数
- #define LED_ON _IO('l',1)
- #define LED_OFF _IO('l',0)
- #endif
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- #include <linux/gpio.h>
- #include <linux/timer.h>
- #include<linux/cdev.h>
- #include<linux/fs.h>
- #include<linux/device.h>
- #include<linux/uaccess.h>
- #include<linux/slab.h>
- #include<linux/io.h>
- #include"head.h"
- struct cdev* cdev;
- unsigned int major=0;
- unsigned int minor=0;
- dev_t devno;
- module_param(major,uint,0664);
- struct class* cls;
- struct device* dev;
- struct device_node *dnode;
- struct gpio_desc* gpiono1;
- struct gpio_desc* gpiono2;
- struct gpio_desc* gpiono3;
- //封装操作的方法
- int mycdev_open(struct inode *inode, struct file *file)
- {
- int min=MINOR(inode->i_rdev);
- file->private_data=(void *)min;
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- long mydev_ioctl(struct file* file,unsigned int cmd,unsigned long arg)
- {
- int min=(int)file->private_data;
- switch(min)
- {
- case 0:
- switch(cmd)
- {
- case LED_ON: //开灯
- gpiod_set_value(gpiono1,1);
- break;
- case LED_OFF: //关灯
- gpiod_set_value(gpiono1,0);
- break;
- }
- break;
- case 1:
- switch(cmd)
- {
- case LED_ON: //开灯
- gpiod_set_value(gpiono2,1);
- break;
- case LED_OFF: //关灯
- gpiod_set_value(gpiono2,0);
- break;
- }
- break;
- case 2:
- switch(cmd)
- {
- case LED_ON: //开灯
- gpiod_set_value(gpiono3,1);
- break;
- case LED_OFF: //关灯
- gpiod_set_value(gpiono3,0);
- break;
- }
- break;
-
- }
- return 0;
- }
- int mycdev_close(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
-
- //定义操作方法结构体变量并赋值
- struct file_operations fops={
-
- .open=mycdev_open,
- .unlocked_ioctl=mydev_ioctl,
- .release=mycdev_close,
- };
- static int __init mycdev_init(void)
- {
- int ret;
- //为字符设备驱动对象申请空间
- cdev=cdev_alloc();
- if(cdev==NULL)
- {
- printk("字符设备驱动对象申请空间失败\n");
- ret=-EFAULT;
- goto out1;
- }
- printk("申请对象空间成功\n");
- //初始化字符设备驱动对象
- cdev_init(cdev,&fops);
- //申请设备号
- if(major>0)//静态指定设备号
- {
- ret=register_chrdev_region(MKDEV(major,minor),3,"myled");
- if(ret)
- {
- printk("静态申请设备号失败\n");
- goto out2;
- }
- }
- else if(major==0)//动态申请设备号
- {
- ret=alloc_chrdev_region(&devno,minor,3,"myled");
- if(ret)
- {
- printk("动态申请设备号失败\n");
- goto out2;
- }
- major=MAJOR(devno);//获取主设备号
- minor=MINOR(devno);//获取次设备号
-
- }
- printk("申请设备号成功\n");
- //注册字符设备驱动对象
- ret=cdev_add(cdev,MKDEV(major,minor),3);
- if(ret)
- {
- printk("注册字符设备驱动对象失败\n");
- goto out3;
- }
- printk("注册字符设备驱动对象成功\n");
-
- //向上提交目录信息
- cls=class_create(THIS_MODULE,"myled");
- if(IS_ERR(cls))
- {
- printk("向上提交目录失败\n");
- ret=-PTR_ERR(cls);
- goto out4;
- }
- printk("向上提交目录成功\n");
- //向上提交设备节点信息
- int i;
- for(i=0;i<3;i++)
- {
- dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
- if(IS_ERR(dev))
- {
- printk("向上提交设备节点信息失败\n");
- ret=-PTR_ERR(dev);
- goto out5;
- }
- }
- printk("向上提交设备信息成功\n");
-
- dnode=of_find_node_by_path("/myled");
- if(dnode==NULL)
- {
- printk("解析设备树节点失败\n");
- return -ENXIO;
- }
- printk("解析GPIO信息成功\n");
-
- //申请gpio对象
- gpiono1=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono1))
- {
- printk("申请gpio对象失败\n");
- return -ENXIO;
- }
- printk("申请led1-gpio信息对象成功\n");
-
- gpiono2=gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono2))
- {
- printk("申请gpio对象失败\n");
- return -ENXIO;
- }
- printk("申请led2-gpio信息对象成功\n");
-
- gpiono3=gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono3))
- {
- printk("申请gpio对象失败\n");
- return -ENXIO;
- }
- printk("申请led3-gpio信息对象成功\n");
- return 0;
- out5:
- //释放前一次提交成功的设备信息
- for(--i;i>=0;i--)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- class_destroy(cls);//释放目录
- out4:
- cdev_del(cdev);
- out3:
- unregister_chrdev_region(MKDEV(major,minor),3);
- out2:
- kfree(cdev);
- out1:
- return ret;
- }
- static void __exit mycdev_exit(void)
- {
- //灭灯
- gpiod_set_value(gpiono1,0);
- gpiod_set_value(gpiono2,0);
- gpiod_set_value(gpiono3,0);
- //释放gpio编号
- gpiod_put(gpiono1);
- gpiod_put(gpiono2);
- gpiod_put(gpiono3);
- //释放节点信息
- int i;
- for(i=0;i<3;i++)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- //销毁目录
- class_destroy(cls);
- //注销驱动对象
- cdev_del(cdev);
- //释放设备号
- unregister_chrdev_region(MKDEV(major,minor),3);
- //释放对象空间
- kfree(cdev);
-
-
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include "head.h"
- int main(int argc, const char *argv[])
- {
- char buf[128] = "";
- int a;
- int fd;
- while (1)
- {
- printf("请选择要打开的灯(1,2,3)\n");
- scanf(" %d", &a);
- switch (a)
- {
- case 1:
- fd = open("/dev/myled0", O_RDWR);
- if (fd < 0)
- {
- printf("设备文件打开失败\n");
- exit(-1);
- }
- printf("打开文件myled0成功\n");
- break;
- case 2:
- fd = open("/dev/myled1", O_RDWR);
- if (fd < 0)
- {
- printf("设备文件打开失败\n");
- exit(-1);
- }
- printf("打开文件myled1成功\n");
- break;
- case 3:
- fd = open("/dev/myled2", O_RDWR);
- if (fd < 0)
- {
- printf("设备文件打开失败\n");
- exit(-1);
- }
- printf("打开文件myled2成功\n");
- break;
- default:
- printf("请输入范围内的数\n");
- }
-
- int b;
- printf("请开灯关灯(0/1)\n");
- scanf(" %d",&b);
- switch(b)
- {
- case 1:
- ioctl(fd,LED_ON);
- break;
- case 0:
- ioctl(fd,LED_OFF);
- break;
- default:
- printf("请输入'0'或'1'\n");
- }
-
- }
- close(fd);
- printf("关闭文件\n");
- return 0;
- }
make arch=arm modname=myled
arm-linux-gnueabihf-gcc proc.c
cp a.out ~/nfs/rootfs
cp myled.ko ~/nfs/rootfs
insmod myled.ko
./a.out
rmmod myled3

- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- #include <linux/gpio.h>
- #include <linux/timer.h>
- struct device_node *dnode;
- struct gpio_desc* gpiono;
- //分配定时器对象
- struct timer_list mytimer;
- //设置一个定时器处理函数
- void mytimer_func(struct timer_list* timer)
- {
- //LED1一秒亮一秒灭
- gpiod_set_value(gpiono,!gpiod_get_value(gpiono));
- //再次启动定时器
- mod_timer(timer,jiffies+HZ);
-
- }
- static int __init mycdev_init(void)
- {
- dnode=of_find_node_by_path("/myled");
- if(dnode==NULL)
- {
- printk("解析设备树节点失败\n");
- return -ENXIO;
- }
- printk("解析GPIO信息成功\n");
-
- //申请gpio对象
- gpiono=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono))
- {
- printk("申请gpio对象失败\n");
- return -ENXIO;
- }
- printk("申请gpio信息对象成功\n");
-
- //初始化定时器对象
- timer_setup(&mytimer,mytimer_func,0);
- mytimer.expires=jiffies+HZ;
- //注册定时器
- add_timer(&mytimer);
-
- return 0;
- }
- static void __exit mycdev_exit(void)
- {
- //注销定时器
- del_timer(&mytimer);
- //灭灯
- gpiod_set_value(gpiono,0);
- //释放gpio编号
- gpiod_put(gpiono);
-
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");