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

- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- #include <linux/gpio.h>
- #include <linux/io.h>
- #include <linux/device.h>
- #include <linux/uaccess.h>
-
- unsigned int major;
- struct device_node *dnode;
- struct gpio_desc *gpiono1;
- struct gpio_desc *gpiono2;
- struct gpio_desc *gpiono3;
- struct class *cls;
- struct device *dev;
- char kbuf[128] = {0};
-
- // 封装操作方法
- int mycdev_open(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- return 0;
- }
-
- ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
- {
- int ret;
-
- ret = copy_to_user(ubuf, kbuf, size);
- if (ret)
- {
- printk("copy_to_ user err\n");
- return -EIO;
- }
-
- return 0;
- }
- ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
- {
- int ret;
- if (size > sizeof(kbuf))
- size = sizeof(kbuf);
- // 从用户拷贝数据
- ret = copy_from_user(kbuf, ubuf, size);
- if (ret)
- {
- printk("copy_from_user err\n");
- return -EIO;
- }
-
- switch (kbuf[0])
- {
- case '1':
- gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
- break;
- case '2':
- gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
- break;
- case '3':
- gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
- 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,
- .read = mycdev_read,
- .write = mycdev_write,
- .release = mycdev_close,
- };
-
- static int __init mycdev_init(void)
- {
- // 1字符设备驱动注册
- major = register_chrdev(0, "myled", &fops);
- if (major < 0)
- {
- printk("字符设备驱动注册失败\n");
- return major;
- }
- printk("字符设备驱动注册成功:major=%d\n", major);
-
- // 2向上提交目录
- cls = class_create(THIS_MODULE, "MYLED");
- if (IS_ERR(cls))
- {
- printk("向上提交目录失败\n");
- return -PTR_ERR(cls);
- }
- printk("向上提交目录成功\n");
-
- // 3向上提交设备节点信息
- 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");
- return -PTR_ERR(dev);
- }
- }
- printk("向上提交设备节点信息成功\n");
-
- // 4解析LED的设备树节点
- dnode = of_find_node_by_path("/myled");
- if (dnode == NULL)
- {
- printk("解析设备树节点失败\n");
- return -1;
- }
- printk("解析GPIO信息成功\n");
- // 5申请gpio对象
- gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
- if (IS_ERR(gpiono1))
- {
- printk("申请gpio1对象失败\n");
- return -ENXIO;
- }
- gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
- if (IS_ERR(gpiono2))
- {
- printk("申请gpio2对象失败\n");
- return -ENXIO;
- }
- gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
- if (IS_ERR(gpiono3))
- {
- printk("申请gpio3对象失败\n");
- return -ENXIO;
- }
- printk("申请gpio信息对象成功\n");
-
- return 0;
- }
- 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);
-
- // 字符设备驱动注销
- unregister_chrdev(major, "myled");
- }
- 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>
-
- int main(int argc, char const *argv[])
- {
- int a;
- char buf[128] = {0};
- int fd = open("/dev/myled0", O_RDWR);
-
- if (fd < 0)
- {
- printf("设备文件打开失败\n");
- exit(-1);
- }
-
- while (1)
- {
- printf("请输入控制的灯:1(LED1) 2(LED2) 3(LED3)\n");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1] = '\0';
- //像设备文件中写
- write(fd,buf,sizeof(buf));
- }
-
- close(fd);
-
- return 0;
- }
输入灯编号,对状态取反;

