一. 简介
前面几篇文章基本完成了 Led驱动代码,前面通过加载驱动模块也测试了 驱动程序。
这里通过运行应用程序,通过应用程序调用 Led驱动程序,从而驱动 打开或者关闭 Led灯。
二. 通过应用程序测试Led驱动程序
1. 驱动代码实现
gpioled.c文件代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#define LED_OFF 0
#define LED_ON 1
#define GPIOLED_NAME "gpioled"
#define GPIOLED_CNT 1
//Led设备结构体
struct gpio_led {
dev_t devid;
int major;
int minor;
struct cdev cdev;
struct class* class;
struct device* dev;
struct device_node * dev_node;
int gpio_number;
};
struct gpio_led gpioled;
static int gpioled_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpioled;
return 0;
}
static ssize_t gpioled_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
int ret = 0;
char rx_buf[2] = {0};
ret = copy_from_user(rx_buf, buf, count);
if(ret != 0)
{
printk("copy_from_user failed!\n");
}
if(rx_buf[0] == LED_ON)
{
//设置低电平,打开Led
gpio_set_value(gpioled.gpio_number, 0);
}
else if(rx_buf[0] == LED_OFF)
{
//设置高电平,关闭Led
gpio_set_value(gpioled.gpio_number, 1);
}
return 0;
}
static int gpioled_close(struct inode *inode, struct file *filp)
{
return 0;
}
const struct file_operations gpioled_fops = {
.owner = THIS_MODULE,
.open = gpioled_open,
.release = gpioled_close,
.write = gpioled_write,
};
/*驱动模块入口函数*/
static int __init gpioled_init(void)
{
int ret = 0;
/*1.注册设备号*/
gpioled.major = 0;
if(gpioled.major) //给出主设备号
{
gpioled.devid = MKDEV(gpioled.major, 0);
ret = register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
}
else //未给设备号时,向Linux申请设备号
{
ret = alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled.major: %d\n", gpioled.major);
printk("gpioled.minor: %d\n", gpioled.minor);
if(ret < 0)
{
printk("apply dev_number failed!\n");
goto apply_devid_failed;
}
/*2.设备初始化 */
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
if(ret < 0)
{
printk("cdev_add failed\n");
goto cdev_add_failed;
}
/*3.自动创建设备节点*/
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class)) {
ret = PTR_ERR(gpioled.class);
goto class_create_failed;
}
gpioled.dev = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.dev)) {
ret = PTR_ERR(gpioled.dev);
goto dev_create_failed;
}
/*读取设备节点*/
gpioled.dev_node = of_find_node_by_path("/gpioled");
if(NULL == gpioled.dev_node)
{
printk("find dev_node failed!\n");
goto read_devnode_failed;
}
/*获取Led所对应的GPIO的编号*/
gpioled.gpio_number = of_get_named_gpio(gpioled.dev_node, "led-gpio", 0);
if(gpioled.gpio_number < 0)
{
printk("get_named_gpio failed!\n");
goto read_devnode_failed;
}
/*申请GPIO管脚 */
ret = gpio_request(gpioled.gpio_number, GPIOLED_NAME);
if(ret != 0)
{
printk("gpio_request failed!\n");
goto read_devnode_failed;
}
/*设置GPIO为输出,设置为高电平,关闭Led */
ret = gpio_direction_output(gpioled.gpio_number, 1);
if(ret != 0)
{
printk("gpio_direction_input failed!\n");
goto set_gpio_input_failed;
}
return 0;
set_gpio_input_failed:
gpio_free(gpioled.gpio_number);
read_devnode_failed:
device_destroy(gpioled.class, gpioled.devid);
dev_create_failed:
class_destroy(gpioled.class);
class_create_failed:
cdev_del(&gpioled.cdev);
cdev_add_failed:
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
apply_devid_failed:
return ret;
}
/*驱动模块出口函数*/
static void __exit gpioled_exit(void)
{
//关闭Led灯
gpio_set_value(gpioled.gpio_number, 1);
/*1. 删除设备*/
cdev_del(&gpioled.cdev);
/*2. 注销设备号*/
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
//3. 销毁设备
device_destroy(gpioled.class, gpioled.devid);
/*4. 销毁类*/
class_destroy(gpioled.class);
/*释放IO */
gpio_free(gpioled.gpio_number);
}
/*驱动入口与出口*/
module_init(gpioled_init);
module_exit(gpioled_exit);
MODULE_LICENSE("GPL"); //License
MODULE_AUTHOR("WeiWuXian"); //author
2. 驱动测试
这里应用程序还使用 前面实现所使用的,开发板之前有拷贝过 应用程序 led_app。
重新编译驱动程序,驱动程序跟之前只做了简单的更改。驱动入口函数 gpioled_init()中,默认关闭了 Led灯,驱动出口函数 gpioled_exit()函数中也关闭了 Led灯。
开发板上电后进入系统,进入 /lib/modules/4.1.15/目录下,运行测试程序,打开Led灯:
/lib/modules/4.1.15 # ./led_app /dev/gpioled 1
这时可以查看开发板上的红色 Led灯是否开启:
这里可以看出,Led红色灯已亮。说明Led灯关闭功能可以正常运行。
运行应用程序,即运行关闭 Led的命令:
/lib/modules/4.1.15 # ./led_app /dev/gpioled 0
这时可以查看开发板上的红色 Led灯是否已灭:
可以看出,Led红色灯已经灭。说明 关闭Led灯功能可以正常运行。
综上所示,驱动模块,设备树文件都可以正常运行。至此,pinctrl子系统与 gpio子系统实验已经顺利完成。
从驱动代码实现中可以看出,与之前直接配置寄存器来驱动Led灯的方法相比,在 pinctrl子系统与gpio子系统基础上实现 的 GPIO驱动代码更简洁。