mirror of
https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
synced 2025-12-03 05:11:14 +08:00
add 09_UART/07
This commit is contained in:
BIN
IMX6ULL/doc_pic/09_UART/06_TTY驱动程序框架深入分析.tif
Normal file
BIN
IMX6ULL/doc_pic/09_UART/06_TTY驱动程序框架深入分析.tif
Normal file
Binary file not shown.
115
IMX6ULL/doc_pic/09_UART/07_字符设备驱动程序的另一种注册方法.md
Normal file
115
IMX6ULL/doc_pic/09_UART/07_字符设备驱动程序的另一种注册方法.md
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
## 字符设备驱动程序的另一种注册方法
|
||||||
|
|
||||||
|
参考资料
|
||||||
|
|
||||||
|
* 参考代码:`drivers\char\pc8736x_gpio.c`
|
||||||
|
|
||||||
|
* 本节课程源码在GIT仓库里
|
||||||
|
|
||||||
|
```shell
|
||||||
|
驱动大全:
|
||||||
|
doc_and_source_for_drivers\IMX6ULL\source\09_UART\
|
||||||
|
03_char_dev_driver\
|
||||||
|
01b_hello_drv
|
||||||
|
doc_and_source_for_drivers\STM32MP157\source\A7\09_UART\
|
||||||
|
03_char_dev_driver\
|
||||||
|
01b_hello_drv
|
||||||
|
|
||||||
|
快速入门:
|
||||||
|
01_all_series_quickstart\05_嵌入式Linux驱动开发基础知识\source\
|
||||||
|
01b_hello_drv
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1. 字符设备驱动程序框架
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编写驱动程序的套路:
|
||||||
|
* 确定主设备号,也可以让内核分配
|
||||||
|
* 定义自己的file_operations结构体
|
||||||
|
* 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体
|
||||||
|
* 把file_operations结构体告诉内核:register_chrdev
|
||||||
|
* 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
|
||||||
|
* 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
|
||||||
|
* 其他完善:提供设备信息,自动创建设备节点:class_create, device_create
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2. 新的注册方法
|
||||||
|
|
||||||
|
参考代码:`drivers\char\pc8736x_gpio.c`
|
||||||
|
|
||||||
|
* 注册字符设备区域
|
||||||
|
* 有主设备号:register_chrdev_region
|
||||||
|
* 无主设备号:alloc_chrdev_region
|
||||||
|
* 分配/设置/注册cdev
|
||||||
|
* cdev_alloc
|
||||||
|
* cdev_init
|
||||||
|
* cdev_add
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3. 上机实验
|
||||||
|
|
||||||
|
#### 3.1 IMX6ULL
|
||||||
|
|
||||||
|
先设置工具链:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
export ARCH=arm
|
||||||
|
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||||
|
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编译、执行程序:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
Ubuntu:
|
||||||
|
cd 01_hello_drv\
|
||||||
|
make
|
||||||
|
|
||||||
|
开发板:
|
||||||
|
insmod hello_drv.ko
|
||||||
|
./hello_drv_test -w abcd
|
||||||
|
./hello_drv_test -r
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 3.2 STM32MP157
|
||||||
|
|
||||||
|
先设置工具链:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
export ARCH=arm
|
||||||
|
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||||
|
export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编译、执行程序:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
Ubuntu:
|
||||||
|
cd 01_hello_drv\
|
||||||
|
make
|
||||||
|
|
||||||
|
开发板:
|
||||||
|
insmod hello_drv.ko
|
||||||
|
./hello_drv_test -w abcd
|
||||||
|
./hello_drv_test -r
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BIN
IMX6ULL/doc_pic/09_UART/pic/09_UART/22_chr_dev_driver.png
Normal file
BIN
IMX6ULL/doc_pic/09_UART/pic/09_UART/22_chr_dev_driver.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 80 KiB |
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
# 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/100ask_imx6ull-sdk/Linux-4.9.88
|
||||||
|
|
||||||
|
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
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/errno.h>
|
||||||
|
#include <linux/miscdevice.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/major.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/stat.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/tty.h>
|
||||||
|
#include <linux/kmod.h>
|
||||||
|
#include <linux/gfp.h>
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static char kernel_buf[1024];
|
||||||
|
static struct class *hello_class;
|
||||||
|
|
||||||
|
|
||||||
|
#define MIN(a, b) (a < b ? a : b)
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_to_user(buf, kernel_buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(kernel_buf, buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations hello_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = hello_drv_open,
|
||||||
|
.read = hello_drv_read,
|
||||||
|
.write = hello_drv_write,
|
||||||
|
.release = hello_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
|
||||||
|
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
|
||||||
|
static int __init hello_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
|
||||||
|
|
||||||
|
|
||||||
|
hello_class = class_create(THIS_MODULE, "hello_class");
|
||||||
|
err = PTR_ERR(hello_class);
|
||||||
|
if (IS_ERR(hello_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
|
||||||
|
static void __exit hello_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
device_destroy(hello_class, MKDEV(major, 0));
|
||||||
|
class_destroy(hello_class);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(hello_init);
|
||||||
|
module_exit(hello_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./hello_drv_test -w abc
|
||||||
|
* ./hello_drv_test -r
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char buf[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s -w <string>\n", argv[0]);
|
||||||
|
printf(" %s -r\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open("/dev/hello", O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file /dev/hello\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件或读文件 */
|
||||||
|
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
|
||||||
|
{
|
||||||
|
len = strlen(argv[2]) + 1;
|
||||||
|
len = len < 1024 ? len : 1024;
|
||||||
|
write(fd, argv[2], len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = read(fd, buf, 1024);
|
||||||
|
buf[1023] = '\0';
|
||||||
|
printf("APP read : %s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
# 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/100ask_imx6ull-sdk/Linux-4.9.88
|
||||||
|
|
||||||
|
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
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/errno.h>
|
||||||
|
#include <linux/miscdevice.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/major.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/stat.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/tty.h>
|
||||||
|
#include <linux/kmod.h>
|
||||||
|
#include <linux/gfp.h>
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct cdev hello_cdev;
|
||||||
|
|
||||||
|
static char kernel_buf[1024];
|
||||||
|
static struct class *hello_class;
|
||||||
|
|
||||||
|
|
||||||
|
#define MIN(a, b) (a < b ? a : b)
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_to_user(buf, kernel_buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(kernel_buf, buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations hello_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = hello_drv_open,
|
||||||
|
.read = hello_drv_read,
|
||||||
|
.write = hello_drv_write,
|
||||||
|
.release = hello_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
|
||||||
|
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
|
||||||
|
static int __init hello_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
int rc;
|
||||||
|
dev_t devid;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
//major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
|
||||||
|
rc = alloc_chrdev_region(&devid, 0, 1, "hello");
|
||||||
|
major = MAJOR(devid);
|
||||||
|
cdev_init(&hello_cdev, &hello_drv);
|
||||||
|
cdev_add(&hello_cdev, devid, 1);
|
||||||
|
|
||||||
|
hello_class = class_create(THIS_MODULE, "hello_class");
|
||||||
|
err = PTR_ERR(hello_class);
|
||||||
|
if (IS_ERR(hello_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
|
||||||
|
static void __exit hello_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
device_destroy(hello_class, MKDEV(major, 0));
|
||||||
|
class_destroy(hello_class);
|
||||||
|
|
||||||
|
//unregister_chrdev(major, "hello");
|
||||||
|
cdev_del(&hello_cdev);
|
||||||
|
unregister_chrdev_region(MKDEV(major,0), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(hello_init);
|
||||||
|
module_exit(hello_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./hello_drv_test -w abc
|
||||||
|
* ./hello_drv_test -r
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char buf[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s -w <string>\n", argv[0]);
|
||||||
|
printf(" %s -r\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open("/dev/hello", O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file /dev/hello\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件或读文件 */
|
||||||
|
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
|
||||||
|
{
|
||||||
|
len = strlen(argv[2]) + 1;
|
||||||
|
len = len < 1024 ? len : 1024;
|
||||||
|
write(fd, argv[2], len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = read(fd, buf, 1024);
|
||||||
|
buf[1023] = '\0';
|
||||||
|
printf("APP read : %s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -334,6 +334,12 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
|
|||||||
06_Linux串口应用编程_GPS
|
06_Linux串口应用编程_GPS
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 2021.07.21 发布"UART子系统"
|
||||||
|
|
||||||
|
```shell
|
||||||
|
07_字符设备驱动程序的另一种注册方法
|
||||||
|
```
|
||||||
|
|
||||||
## 6. 联系方式
|
## 6. 联系方式
|
||||||
|
|
||||||
* 官网:http://www.100ask.net
|
* 官网:http://www.100ask.net
|
||||||
|
|||||||
115
STM32MP157/doc_pic/09_UART/07_字符设备驱动程序的另一种注册方法.md
Normal file
115
STM32MP157/doc_pic/09_UART/07_字符设备驱动程序的另一种注册方法.md
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
## 字符设备驱动程序的另一种注册方法
|
||||||
|
|
||||||
|
参考资料
|
||||||
|
|
||||||
|
* 参考代码:`drivers\char\pc8736x_gpio.c`
|
||||||
|
|
||||||
|
* 本节课程源码在GIT仓库里
|
||||||
|
|
||||||
|
```shell
|
||||||
|
驱动大全:
|
||||||
|
doc_and_source_for_drivers\IMX6ULL\source\09_UART\
|
||||||
|
03_char_dev_driver\
|
||||||
|
01b_hello_drv
|
||||||
|
doc_and_source_for_drivers\STM32MP157\source\A7\09_UART\
|
||||||
|
03_char_dev_driver\
|
||||||
|
01b_hello_drv
|
||||||
|
|
||||||
|
快速入门:
|
||||||
|
01_all_series_quickstart\05_嵌入式Linux驱动开发基础知识\source\
|
||||||
|
01b_hello_drv
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1. 字符设备驱动程序框架
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编写驱动程序的套路:
|
||||||
|
* 确定主设备号,也可以让内核分配
|
||||||
|
* 定义自己的file_operations结构体
|
||||||
|
* 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体
|
||||||
|
* 把file_operations结构体告诉内核:register_chrdev
|
||||||
|
* 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
|
||||||
|
* 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
|
||||||
|
* 其他完善:提供设备信息,自动创建设备节点:class_create, device_create
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2. 新的注册方法
|
||||||
|
|
||||||
|
参考代码:`drivers\char\pc8736x_gpio.c`
|
||||||
|
|
||||||
|
* 注册字符设备区域
|
||||||
|
* 有主设备号:register_chrdev_region
|
||||||
|
* 无主设备号:alloc_chrdev_region
|
||||||
|
* 分配/设置/注册cdev
|
||||||
|
* cdev_alloc
|
||||||
|
* cdev_init
|
||||||
|
* cdev_add
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3. 上机实验
|
||||||
|
|
||||||
|
#### 3.1 IMX6ULL
|
||||||
|
|
||||||
|
先设置工具链:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
export ARCH=arm
|
||||||
|
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||||
|
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编译、执行程序:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
Ubuntu:
|
||||||
|
cd 01_hello_drv\
|
||||||
|
make
|
||||||
|
|
||||||
|
开发板:
|
||||||
|
insmod hello_drv.ko
|
||||||
|
./hello_drv_test -w abcd
|
||||||
|
./hello_drv_test -r
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 3.2 STM32MP157
|
||||||
|
|
||||||
|
先设置工具链:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
export ARCH=arm
|
||||||
|
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||||
|
export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
编译、执行程序:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
Ubuntu:
|
||||||
|
cd 01_hello_drv\
|
||||||
|
make
|
||||||
|
|
||||||
|
开发板:
|
||||||
|
insmod hello_drv.ko
|
||||||
|
./hello_drv_test -w abcd
|
||||||
|
./hello_drv_test -r
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BIN
STM32MP157/doc_pic/09_UART/pic/09_UART/22_chr_dev_driver.png
Normal file
BIN
STM32MP157/doc_pic/09_UART/pic/09_UART/22_chr_dev_driver.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 80 KiB |
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
# 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/100ask_stm32mp157_pro-sdk/Linux-5.4
|
||||||
|
|
||||||
|
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
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/errno.h>
|
||||||
|
#include <linux/miscdevice.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/major.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/stat.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/tty.h>
|
||||||
|
#include <linux/kmod.h>
|
||||||
|
#include <linux/gfp.h>
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static char kernel_buf[1024];
|
||||||
|
static struct class *hello_class;
|
||||||
|
|
||||||
|
|
||||||
|
#define MIN(a, b) (a < b ? a : b)
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_to_user(buf, kernel_buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(kernel_buf, buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations hello_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = hello_drv_open,
|
||||||
|
.read = hello_drv_read,
|
||||||
|
.write = hello_drv_write,
|
||||||
|
.release = hello_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
|
||||||
|
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
|
||||||
|
static int __init hello_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
|
||||||
|
|
||||||
|
|
||||||
|
hello_class = class_create(THIS_MODULE, "hello_class");
|
||||||
|
err = PTR_ERR(hello_class);
|
||||||
|
if (IS_ERR(hello_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
|
||||||
|
static void __exit hello_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
device_destroy(hello_class, MKDEV(major, 0));
|
||||||
|
class_destroy(hello_class);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(hello_init);
|
||||||
|
module_exit(hello_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./hello_drv_test -w abc
|
||||||
|
* ./hello_drv_test -r
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char buf[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s -w <string>\n", argv[0]);
|
||||||
|
printf(" %s -r\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open("/dev/hello", O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file /dev/hello\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件或读文件 */
|
||||||
|
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
|
||||||
|
{
|
||||||
|
len = strlen(argv[2]) + 1;
|
||||||
|
len = len < 1024 ? len : 1024;
|
||||||
|
write(fd, argv[2], len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = read(fd, buf, 1024);
|
||||||
|
buf[1023] = '\0';
|
||||||
|
printf("APP read : %s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
# 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/100ask_stm32mp157_pro-sdk/Linux-5.4
|
||||||
|
|
||||||
|
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
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/errno.h>
|
||||||
|
#include <linux/miscdevice.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/major.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/proc_fs.h>
|
||||||
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/stat.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/tty.h>
|
||||||
|
#include <linux/kmod.h>
|
||||||
|
#include <linux/gfp.h>
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct cdev hello_cdev;
|
||||||
|
|
||||||
|
static char kernel_buf[1024];
|
||||||
|
static struct class *hello_class;
|
||||||
|
|
||||||
|
|
||||||
|
#define MIN(a, b) (a < b ? a : b)
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_to_user(buf, kernel_buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(kernel_buf, buf, MIN(1024, size));
|
||||||
|
return MIN(1024, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hello_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations hello_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = hello_drv_open,
|
||||||
|
.read = hello_drv_read,
|
||||||
|
.write = hello_drv_write,
|
||||||
|
.release = hello_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
|
||||||
|
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
|
||||||
|
static int __init hello_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
int rc;
|
||||||
|
dev_t devid;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
//major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
|
||||||
|
rc = alloc_chrdev_region(&devid, 0, 1, "hello");
|
||||||
|
major = MAJOR(devid);
|
||||||
|
cdev_init(&hello_cdev, &hello_drv);
|
||||||
|
cdev_add(&hello_cdev, devid, 1);
|
||||||
|
|
||||||
|
hello_class = class_create(THIS_MODULE, "hello_class");
|
||||||
|
err = PTR_ERR(hello_class);
|
||||||
|
if (IS_ERR(hello_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "hello");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
|
||||||
|
static void __exit hello_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
device_destroy(hello_class, MKDEV(major, 0));
|
||||||
|
class_destroy(hello_class);
|
||||||
|
|
||||||
|
//unregister_chrdev(major, "hello");
|
||||||
|
cdev_del(&hello_cdev);
|
||||||
|
unregister_chrdev_region(MKDEV(major,0), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(hello_init);
|
||||||
|
module_exit(hello_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./hello_drv_test -w abc
|
||||||
|
* ./hello_drv_test -r
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char buf[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s -w <string>\n", argv[0]);
|
||||||
|
printf(" %s -r\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open("/dev/hello", O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file /dev/hello\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件或读文件 */
|
||||||
|
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
|
||||||
|
{
|
||||||
|
len = strlen(argv[2]) + 1;
|
||||||
|
len = len < 1024 ? len : 1024;
|
||||||
|
write(fd, argv[2], len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = read(fd, buf, 1024);
|
||||||
|
buf[1023] = '\0';
|
||||||
|
printf("APP read : %s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user