参考正点原子I.MX6U嵌入式Linux驱动开发指南
一、简介
int request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char *name,void *dev)
void free_irq(unsigned int irq, void *dev)
irqreturn_t (*irq_handler_t) (int, void *)
enum irqreturn {
IRQ_NONE = (0 << 0),
IRQ_HANDLED = (1 << 0),
IRQ_WAKE_THREAD = (1 << 1),
};
typedef enum irqreturn irqreturn_t;
return IRQ_RETVAL(IRQ_HANDLED)
void enable_irq(unsigned int irq)void disable_irq(unsigned int irq)
void disable_irq_nosync(unsigned int irq)
local_irq_enable()local_irq_disable()
local_irq_save(flags)local_irq_restore(flags)
二、上半部与下半部
struct softirq_action
{
void (*action)(struct softirq_action *);
};
static struct softirq_action softirq_vec[NR_SOFTIRQS];
enum
{
HI_SOFTIRQ=0, /* 高优先级软中断 */
TIMER_SOFTIRQ, /* 定时器软中断 */
NET_TX_SOFTIRQ, /* 网络数据发送软中断 */
NET_RX_SOFTIRQ, /* 网络数据接收软中断 */
BLOCK_SOFTIRQ,
BLOCK_IOPOLL_SOFTIRQ,
TASKLET_SOFTIRQ, /* tasklet 软中断 */
SCHED_SOFTIRQ, /* 调度软中断 */
HRTIMER_SOFTIRQ, /* 高精度定时器软中断 */
RCU_SOFTIRQ, /* RCU 软中断 */
NR_SOFTIRQS
};
void open_softirq(int nr, void (*action)(struct softirq_action *))
void raise_softirq(unsigned int nr)
void __init softirq_init(void)
{
int cpu;
for_each_possible_cpu(cpu)
{
per_cpu(tasklet_vec, cpu).tail =
&per_cpu(tasklet_vec, cpu).head;
per_cpu(tasklet_hi_vec, cpu).tail =
&per_cpu(tasklet_hi_vec, cpu).head;
}
open_softirq(TASKLET_SOFTIRQ, tasklet_action);
open_softirq(HI_SOFTIRQ, tasklet_hi_action);
}
struct tasklet_struct
{
struct tasklet_struct *next; /* 下一个 tasklet */
unsigned long state; /* tasklet 状态 */
atomic_t count; /* 计数器,记录对 tasklet 的引用数 */
void (*func)(unsigned long); /* tasklet 执行的函数 */
unsigned long data; /* 函数 func 的参数 */
};
void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long),unsigned long data);
DECLARE_TASKLET(name, func, data)
void tasklet_schedule(struct tasklet_struct *t)
三、设备树中断节点
intc: interrupt-controller@00a01000 {
compatible = "arm,cortex-a7-gic";
#interrupt-cells = <3>;
interrupt-controller;
reg = <0x00a01000 0x1000>,
<0x00a02000 0x100>;
};
gpio5 : gpio @020ac000{
compatible = "fsl,imx6ul-gpio", "fsl,imx35-gpio";
reg = <0x020ac000 0x4000>;
interrupts = <GIC_SPI 74 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 75 IRQ_TYPE_LEVEL_HIGH>;
gpio - controller;
#gpio - cells = < 2>;
interrupt - controller;
#interrupt - cells = < 2>;
};
1 fxls8471@1e {
2 compatible = "fsl,fxls8471";
3 reg = <0x1e>;
4 position = <0>;
5 interrupt-parent = <&gpio5>;
6 interrupts = <0 8>;
7 };
unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
int gpio_to_irq(unsigned int gpio)
四、示例实验
4.1 设备树
key {
#address-cells = <1>;
#size-cells = <1>;
compatible = "lyh-key";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_key>;
led-gpio = <&gpio1 18 GPIO_ACTIVE_LOW>;
interrupt-parent = <&gpio1>;
interrupts = <18 IRQ_TYPE_EDGE_BOTH>; /* FALLING RISING */
status = "okay";
};…………
pinctrl_key: keygrp {
fsl,pins = <
MX6UL_PAD_UART1_CTS_B__GPIO1_IO18 0xF080 /* KEY */
>;
};
4.2 中断驱动
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名 : keyirq.c
作者 : lyh
版本 : V1.0
描述 : LED驱动文件。
其他 : 无
日志 : 初版V1.0 2024/4/14 lyh创建
***************************************************************/
#define KEY_CNT 1 /* 设备号个数 */
#define KEY_NAME "key" /* 名字 */
#define KEY0VALUE 0X01 /* KEY0 按键值 */
#define INVAKEY 0XFF /* 无效的按键值 */
#define KEY_NUM 1 /* 按键数量 */
/* 中断 IO 描述结构体 */
struct irq_keydesc {
int gpio; /* gpio */
int irqnum; /* 中断号 */
unsigned char value; /* 按键对应的键值 */
char name[10]; /* 名字 */
irqreturn_t (*handler)(int, void *); /* 中断服务函数 */
};
/* key设备结构体 */
struct key_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct device_node *nd; /* 设备节点 */
struct timer_list timer;
atomic_t key_value;
atomic_t releasekey;
struct irq_keydesc irqkeydesc[KEY_NUM]; /* 按键描述数组 */
unsigned char curkeynum; /* 当前的按键号 */
};
struct key_dev key; /* key设备 */
/* @description : 中断服务函数,开启定时器,延时 10ms,
* 定时器用于按键消抖。
* @param - irq : 中断号
* @param - dev_id : 设备结构。
* @return : 中断执行结果
*/
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct key_dev *dev = (struct key_dev *)dev_id;
dev->curkeynum = 0;
dev->timer.data = (unsigned long)dev;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10));
return IRQ_RETVAL(IRQ_HANDLED);
}
void timer_func(unsigned long arg)
{
struct key_dev *dev = (struct key_dev *)arg;
unsigned char value;
int num = dev->curkeynum;
struct irq_keydesc *keydesc = &dev->irqkeydesc[num];
value = gpio_get_value(keydesc->gpio);
if(value == 0){ //按下按键
atomic_set(&dev->key_value, keydesc->value);
} else { //松开按键
atomic_set(&dev->key_value, 0x80 | keydesc->value);
atomic_set(&dev->releasekey, 1);
}
return;
}
/*
* @description : 打开设备
* @param - inode : 传递给驱动的inode
* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量
* 一般在open的时候将private_data指向设备结构体。
* @return : 0 成功;其他 失败
*/
static int key_open(struct inode *inode, struct file *filp)
{
filp->private_data = &key; /* 设置私有数据 */
return 0;
}
/*
* @description : 从设备读取数据
* @param - filp : 要打开的设备文件(文件描述符)
* @param - buf : 返回给用户空间的数据缓冲区
* @param - cnt : 要读取的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t key_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue = 0;
unsigned char releasekey = 0;
struct key_dev *dev = (struct key_dev *)filp->private_data;
keyvalue = atomic_read(&dev->key_value);
releasekey = atomic_read(&dev->releasekey);
if (releasekey){ /* 有按键按下 */
if (keyvalue & 0x80){
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
}
else{
goto data_error;
}
atomic_set(&dev->releasekey, 0); /* 按下标志清零 */
}
else{
goto data_error;
}
return 0;
data_error:
return -EINVAL;
}
/*
* @description : 向设备写数据
* @param - filp : 设备文件,表示打开的文件描述符
* @param - buf : 要写给设备写入的数据
* @param - cnt : 要写入的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 写入的字节数,如果为负值,表示写入失败
*/
static ssize_t key_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
/*
* @description : 关闭/释放设备
* @param - filp : 要关闭的设备文件(文件描述符)
* @return : 0 成功;其他 失败
*/
static int key_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* 设备操作函数 */
static struct file_operations key_fops = {
.owner = THIS_MODULE,
.open = key_open,
.read = key_read,
.write = key_write,
.release = key_release,
};
static int keyio_init(void)
{
int ret;
int i;
/* 找到设备节点 */
key.nd = of_find_node_by_path("/key");
if(key.nd == NULL) {
printk("key node cant not found!\r\n");
return -EINVAL;
} else {
printk("key node has been found!\r\n");
}
/* 获取设备树中的 gpio 编号 */
for(i = 0; i < KEY_NUM; i++){
key.irqkeydesc[i].gpio = of_get_named_gpio(key.nd, "key-gpio", i);
if(key.irqkeydesc[i].gpio < 0){
printk("can't get key-gpio %d", i);
return -EINVAL;
}
}
/* gpio 初始化,申请中断号 */
for(i = 0; i < KEY_NUM; i++){
memset(key.irqkeydesc[i].name, 0, sizeof(key.irqkeydesc[i].name));
sprintf(key.irqkeydesc[i].name, "key%d", i);
gpio_request(key.irqkeydesc[i].gpio, key.irqkeydesc[i].name);
ret = gpio_direction_input(key.irqkeydesc[i].gpio);
if(ret < 0) {
printk("can't set gpio %d direction!\r\n", i);
return -EINVAL;
}
key.irqkeydesc[i].irqnum = gpio_to_irq(key.irqkeydesc[i].gpio);
#if 0
key.irqkeydesc[i].irqnum = irq_of_parse_and_map(key.nd, i);
#endif
printk("key%d:gpio=%d, irqnum=%d\r\n", i, key.irqkeydesc[i].gpio,
key.irqkeydesc[i].irqnum);
}
/* 申请中断 */
key.irqkeydesc[0].handler = key0_handler;
key.irqkeydesc[0].value = KEY0VALUE;
for (i = 0; i < KEY_NUM; i++){
ret = request_irq(key.irqkeydesc[i].irqnum,
key.irqkeydesc[i].handler,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
key.irqkeydesc[i].name,
&key);
if (ret < 0){
printk("irq %d request failed!\r\n", key.irqkeydesc[i].irqnum);
return -EFAULT;
}
}
/* 创建定时器 */
init_timer(&key.timer);
key.timer.function = timer_func;
return 0;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static int __init keyirq_init(void)
{
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (key.major) { /* 定义了设备号 */
key.devid = MKDEV(key.major, 0);
register_chrdev_region(key.devid, KEY_CNT, KEY_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&key.devid, 0, KEY_CNT, KEY_NAME); /* 申请设备号 */
key.major = MAJOR(key.devid); /* 获取分配号的主设备号 */
key.minor = MINOR(key.devid); /* 获取分配号的次设备号 */
}
printk("key major=%d,minor=%d\r\n",key.major, key.minor);
/* 2、初始化cdev */
key.cdev.owner = THIS_MODULE;
cdev_init(&key.cdev, &key_fops);
/* 3、添加一个cdev */
cdev_add(&key.cdev, key.devid, KEY_CNT);
/* 4、创建类 */
key.class = class_create(THIS_MODULE, KEY_NAME);
if (IS_ERR(key.class)) {
return PTR_ERR(key.class);
}
/* 5、创建设备 */
key.device = device_create(key.class, NULL, key.devid, NULL, KEY_NAME);
if (IS_ERR(key.device)) {
return PTR_ERR(key.device);
}
/* 5、初始化按键 */
atomic_set(&key.key_value, INVAKEY);
atomic_set(&key.releasekey, 0);
keyio_init();
return 0;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static void __exit keyirq_exit(void)
{
unsigned int i = 0;
/* 删除定时器 */
del_timer_sync(&key.timer);
/* 释放中断 */
for (i = 0; i < KEY_NUM; i++) {
free_irq(key.irqkeydesc[i].irqnum, &key);
gpio_free(key.irqkeydesc[i].gpio);
}
/* 注销字符设备驱动 */
cdev_del(&key.cdev);/* 删除cdev */
unregister_chrdev_region(key.devid, KEY_CNT); /* 注销设备号 */
device_destroy(key.class, key.devid);
class_destroy(key.class);
}
module_init(keyirq_init);
module_exit(keyirq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lyh");
4.3 测试应用
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "linux/ioctl.h"
int main(int argc, char *argv[])
{
int fd;
int ret = 0;
char *filename;
unsigned char data;
if (argc != 2){
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0){
printf("Can't open file %s\r\n", filename);
return -1;
}
while (1){
ret = read(fd, &data, sizeof(data));
if (ret < 0){
/* 数据读取错误或者无效 */
}
else{ /* 数据读取正确 */
if (data) /* 读取到数据 */
printf("key value = %#X\r\n", data);
}
}
close(fd);
return ret;
}