通過GPIO子系統(tǒng)函數(shù)點(diǎn)亮LED
1、GPIO子系統(tǒng)函數(shù)
1.1 確定 led 的GPIO標(biāo)號(hào),查看內(nèi)核中的gpiochip
查看 gpiochip ,以正點(diǎn)原子的IMX6ULL阿爾法開發(fā)板為例
[root@100ask:/sys/class/gpio]# cat /sys/kernel/debug/gpio
查看原理圖,發(fā)現(xiàn)led接的引腳是 GPIO1_IO3,對(duì)應(yīng) /sys/kernel/debug/gpio 中的 gpiochip0 組,gpiochip0 組從0開始算起,所以 GPIO1_IO3 對(duì)應(yīng)的標(biāo)號(hào)就是 0+3 = 3了
(可是實(shí)際操作中設(shè)置為4才能點(diǎn)亮LED,這里不知道是什么道理,懂得朋友麻煩評(píng)論區(qū)指點(diǎn)一下)
1.2 請(qǐng)求GPIO引腳(代碼添加到驅(qū)動(dòng)模塊入口函數(shù)里)
int gpio_request(unsigned gpio, const char *label)
參數(shù):
gpio : GPIO引腳在 gpiochip 中對(duì)應(yīng)的標(biāo)號(hào)
label : 為GPIO起的標(biāo)簽名
返回值:小于0失敗
代碼示例:
int err;
unsigned int led_gpio = 4; //對(duì)照原理圖 led控制引腳
/*申請(qǐng)led_gpio引腳*/
err = gpio_request(led_gpio, "user_led");
if(err < 0){
printk("led gpio request failed!\n");
return -ENODEV;
}
記住這里傳入的第二個(gè)參數(shù) “user_led”,后面驗(yàn)證的時(shí)候會(huì)看到
1.3 讀GPIO
int gpio_get_value(unsigned int gpio)
參數(shù):
gpio : GPIO引腳在 gpiochip 中對(duì)應(yīng)的標(biāo)號(hào)
返回值:返回引腳狀態(tài),0或1
代碼示例:
static ssize_t led_read (struct file *filp, char *buf, size_t size, loff_t *offset)
{
printk("led_drv_read\n");
/*read led_gpio value*/
buff[0] = gpio_get_value(led_gpio);
//將讀取到的值傳給用戶程序(也可以直接通過return的方式傳到用戶程序)
copy_to_user(buf, buff, 1);
/*返回什么不重要,也可以直接返回gpio的值
但是最好還是按照驅(qū)動(dòng)程序模式來寫,使用copy_to_user*/
return buff[0];
}
1.4 寫GPIO
void gpio_set_value(unsigned int gpio, int value)
參數(shù):
gpio : GPIO引腳在 gpiochip 中對(duì)應(yīng)的標(biāo)號(hào)
value : 寫值,0或1
返回值:無
代碼示例:
static ssize_t led_write (struct file *filp, const char *buf, size_t size, loff_t *offset)
{
printk("led_drv_write\n");
// 從應(yīng)用程序獲取要寫入的值,buf[0]
copy_from_user(buff, buf, 1);
/*write led_gpio*/
gpio_set_value(led_gpio, buff[0]);
return led_gpio;
}
1.5 設(shè)置GPIO方向
int gpio_direction_output(unsigned gpio, int value)
參數(shù):
gpio : GPIO引腳在 gpiochip 中對(duì)應(yīng)的標(biāo)號(hào)
value : 初始輸出電平,0或1,
返回值:小于0失敗
代碼示例:
/*設(shè)置led_gpio輸出模式*/
gpio_direction_output(led_gpio, 1);
2、在驅(qū)動(dòng)卸載函數(shù)中釋放GPIO引腳
/*釋放led_gpio引腳*/
gpio_free(led_gpio);
3、完成LED驅(qū)動(dòng)程序
在之前的hello驅(qū)動(dòng)程序基礎(chǔ)上修改,使用上面的GPIO子系統(tǒng)函數(shù),添加一些邏輯性的代碼即可
這里要注意的是需要增加一個(gè)頭文件
#include <linux/gpio.h>
先展示一下驅(qū)動(dòng)的流程,然后貼代碼
IMX6ULL—LED驅(qū)動(dòng)程序
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/capability.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <asm/mach-types.h>
#include <asm/uaccess.h>
#include <asm/therm.h>
#include <linux/string.h>
#include <linux/gpio.h>
static int major;
static unsigned char buff[100];
static struct class *led_class;
/*led args*/
//unsigned int led_gpio = 129; //對(duì)照原理圖 蜂鳴器
//unsigned int led_gpio = 19; //對(duì)照原理圖 按鍵
unsigned int led_gpio = 4; //對(duì)照原理圖 led
static int led_open (struct inode *node, struct file *filp)
{
printk("led_open\n");
printk("%s %s %d\n",__FILE__, __FUNCTION__, __LINE__);
return 0;
}
static ssize_t led_read (struct file *filp, char *buf, size_t size, loff_t *offset)
{
printk("led_drv_read\n");
/*read led_gpio value*/
buff[0] = gpio_get_value(led_gpio);
copy_to_user(buf, buff, 1);
//返回什么不重要,也可以直接返回gpio的值,但是最好還是按照驅(qū)動(dòng)程序模式來寫,使用copy_to_user
return buff[0];
}
static ssize_t led_write (struct file *filp, const char *buf, size_t size, loff_t *offset)
{
printk("led_drv_write\n");
copy_from_user(buff, buf, 1);
/*write led_gpio*/
gpio_set_value(led_gpio, buff[0]);
return led_gpio;
}
static int led_release (struct inode *node, struct file *filp)
{
printk("led_release\n");
return 0;
}
/*1.定義 file_operations 結(jié)構(gòu)體*/
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.read = led_read,
.write = led_write,
.open = led_open,
.release = led_release,
};
/*3.入口函數(shù)*/
static int led_init(void)
{
struct device *dev;
int err;
/************* 1.注冊(cè)設(shè)備,返回設(shè)備號(hào) ************/
major = register_chrdev(0,"led_drv",&led_fops);
/*2.在內(nèi)核中創(chuàng)建設(shè)備*/
led_class = class_create(THIS_MODULE, "led_class");
if (IS_ERR(led_class)) {
printk("led class create failed!\n");
}
/*3.在/dev下面創(chuàng)建設(shè)備節(jié)點(diǎn)*/
device_create(led_class, NULL, MKDEV(major, 0), NULL, "led_drv");
if(IS_ERR(dev)) {
printk("led device_create failed!\n");
}
/************* 4.初始化led引腳 ************/
/*申請(qǐng)led_gpio引腳*/
err = gpio_request(led_gpio, "user_led");
if(err < 0){
printk("led gpio request failed!\n");
return -ENODEV;
}
/*設(shè)置led_gpio輸出模式*/
gpio_direction_output(led_gpio, 1);
return 0;
}
/*4.退出函數(shù)*/
static int led_exit(void)
{
//卸載設(shè)備
unregister_chrdev(major,"led_fops");
//銷毀設(shè)備
device_destroy(led_class, MKDEV(major, 0));
//刪除設(shè)備類
class_destroy(led_class);
/*釋放led_gpio引腳*/
gpio_free(led_gpio);
printk("led_exit\n");
return 0;
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
4、編寫LED應(yīng)用程序
第一步確定LED應(yīng)用程序的使用方式文章來源:http://www.zghlxwxcb.cn/news/detail-498203.html
./led_test /dev/led_drv on 點(diǎn)亮LED
./led_test /dev/led_drv off 熄滅LED
./led_test /dev/led_drv 讀取LED狀態(tài)
與之前的hello應(yīng)用程序區(qū)別不大,直接貼代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-498203.html
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/*
./led_test /dev/led_drv on
./led_test /dev/led_drv off
./led_test /dev/led_drv
*/
int main(int argc, char *argv[])
{
int len;
char buf[10];
if(argc < 2){
printf("please input at least 2 args\n");
printf("%s /dev/led_drv [on/off]\n", argv[0]);
return -1;
}
/*open*/
int fd;
fd = open(argv[1], O_RDWR);
if(fd < 0){
printf("open failed\n");
return -2;
}
/*read led*/
if(argc == 2){
int res = read(fd, buf, 1);
printf("led state : %s \n", buf[0] == 1 ? "off" : "on");
}
/*write led*/
if(argc == 3){
if(strcmp(argv[2], "on") == 0)
buf[0] = 0;
else if(strcmp(argv[2], "off") == 0)
buf[0] = 1;
write(fd, buf, 1);
}
close(fd);
return 0;
}
到了這里,關(guān)于【IMX6ULL驅(qū)動(dòng)開發(fā)學(xué)習(xí)】08.IMX6ULL通過GPIO子系統(tǒng)函數(shù)點(diǎn)亮LED的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!