Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1694
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2023-03-22 17:22
文章分类
文章存档

2023年(5)

我的朋友
最近访客

分类: LINUX

2023-03-22 17:48:13

目录


1.写代码


2.将代码传到开发板


3.装载驱动程序


4.装载成功


5. 结果 


1.写代码
botton_drv.c



#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
#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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
#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
#include
#include
#include
#include
#include
 
/*
 * ./button_test /dev/100ask_button0
 *
 */
int main(int argc, char **argv)
{
int fd;
char val;

/* 1. 判断参数 */
if (argc != 2) 
{
printf("Usage: %s \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. 结果 


————————————————
版权声明:本文为CSDN博主「m0_61083792」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_61083792/article/details/129454153
阅读(239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~