【嵌入式 Linux 驱动开发基础知识】按键驱动

目录

1.写代码

2.将代码传到开发板

3.装载驱动程序

4.装载成功

5. 结果 


1.写代码

botton_drv.c

​
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>

#include "button_drv.h"

//主设备号是用来寻找驱动文件的,次设备号是用来自己随便用的
//1,定义主设备号,让系统分配
static int major = 0;

static struct button_operations *p_button_opr;//2.2,写button_dev,由底层代码向上一层提供指针
static struct class *button_class;

/* 2.1 实现对应的open/read/write等函数,填入file_operations结构体   */
static int button_open (struct inode *inode, struct file *file)
{
    int minor = iminor(inode);//获得次设备号minor
    p_button_opr->init(minor);//初始化引脚,调用底层提供的一个结构体,调用init函数来操作硬件
    return 0;
}

static ssize_t button_read (struct file *file, char __user *buf, size_t size, loff_t *off)
{
    unsigned int minor = iminor(file_inode(file));
    char level;
    int err;
    
    level = p_button_opr->read(minor);//读回引脚电平
    err = copy_to_user(buf, &level, 1);
    return 1;
}

//2,定义file_operations结构体
static struct file_operations button_fops = {
    .open = button_open,
    .read = button_read,
};

//
void register_button_operations(struct button_operations *opr)
{
    int i;

    p_button_opr = opr;
    for (i = 0; i < opr->count; i++)
    {
        device_create(button_class, NULL, MKDEV(major, i), NULL, "100ask_button%d", i);//这个函数用来在虚拟文件系统构造信息,可以用来构造设备结点
    }
}

void unregister_button_operations(void)
{
    int i;

    for (i = 0; i < p_button_opr->count; i++)
    {
        device_destroy(button_class, MKDEV(major, i));
    }
}

//定义了两个函数要给别的驱动使用要把它导出来
EXPORT_SYMBOL(register_button_operations);
EXPORT_SYMBOL(unregister_button_operations);

//3,将结构体告诉内核
//把file_operations结构体告诉内核:注册驱动程序                               
//谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
int button_init(void)
{
    major = register_chrdev(0, "100ask_button", &button_fops);

    button_class = class_create(THIS_MODULE, "100ask_button");
    if (IS_ERR(button_class))
        return -1;
    
    return 0;
}

//4,有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
void button_exit(void)
{
    class_destroy(button_class);
    unregister_chrdev(major, "100ask_button");
}

//5,修饰一下.其他完善:提供设备信息,自动创建设备节点
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");

/*
查询方式
drv_open  配置GPIO为输入引脚
drv_read  直接返回引脚状态

休眠唤醒方式
drv_open  配置GPIO为输入引脚,注册按键中断服务程序
drv_read  有按键数据,直接返回,无按键数据,休眠
按键触发中断,根据中断服务程序保存数据,唤醒应用程序,APP返回数据

poll
drv_open  配置GPIO为输入引脚,注册按键中断服务程序
drv_poll  有按键数据,直接返回,无按键数据,休眠一段时间,在这段时间里没唤醒就会返回无数据的一个状态
按键触发中断,根据中断服务程序保存按键数据,唤醒应用程序,APP返回数据
drv_read  有按键数据,直接返回,无按键数据,休眠

异步通信
drv_open     配置GPIO为输入引脚,注册按键中断服务程序
drv_fasync   记录进程PID
			 注册信号处理函数signal(SIGIO,my_signal_fun);
			 设置FASYNC标志fcntl(fd,F_SETFL,Oflags |FASYNC);
按键触发中断,根据中断服务程序保存按键数据,给进程PID发信号(函数kill_fasync发SIGIO)
drv_read  有按键数据,直接返回,无按键数据,休眠

*/

​

board_xxx.c

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>

#include "button_drv.h"


static void board_xxx_button_init_gpio (int which)
{
    printk("%s %s %d, init gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
}

static int board_xxx_button_read_gpio (int which)
{
    printk("%s %s %d, read gpio for button %d\n", __FILE__, __FUNCTION__, __LINE__, which);
    return 1;
}

static struct button_operations my_buttons_ops ={
    .count = 1,//有1个按键
    .init  = board_xxx_button_init_gpio,//初始化引脚,初始化为输入引脚
    .read  = board_xxx_button_read_gpio,//读取引脚的电平状态
};

int board_xxx_button_init(void)
{
    register_button_operations(&my_buttons_ops);
    return 0;
}

void board_xxx_button_exit(void)
{
    unregister_button_operations();
}

module_init(board_xxx_button_init);
module_exit(board_xxx_button_exit);

MODULE_LICENSE("GPL");


button_drv.h

#ifndef _BUTTON_DRV_H
#define _BUTTON_DRV_H

//2.2,分配设置注册一个button_operations结构体
struct button_operations {
    int count;//有多少个按键
    void (*init) (int which);//要初始化哪个按键
    int (*read) (int which);//要读哪个按键
};

void register_button_operations(struct button_operations *opr);
void unregister_button_operations(void);

#endif

button_test.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./button_test /dev/100ask_button0
 *
 */
int main(int argc, char **argv)
{
	int fd;
	char val;
	
	/* 1. 判断参数 */
	if (argc != 2) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	/* 3. 写文件 */
	read(fd, &val, 1);
	printf("get button : %d\n", val);
	
	close(fd);
	
	return 0;
}


Makefire


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/book/eLinuxCore_100ask-t113-pro/linux

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f hello_drv_test

obj-m	+= hello_drv.o

2.将代码传到开发板

3.装载驱动程序

顺序不能乱,因为有依赖关系

4.装载成功

5. 结果 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值