基础知识
argc与argv
int main(int argc,char **argv)
- 这个
argc
是argument count
的缩写,表示传入main函数的参数个数,argv
是argument vector
的缩写,是存储传入参数用的。 char **argv
也可以写作char *argv[]
,该数组里存储了输入参数的地址,argv就是地址数组的头指针- 传入数据举例:Linux环境下运行时,输入
ledtest /dev/myled on
那么就输入了三个参数,程序名ledtest、/dev/myled、on
open、read、write
open函数输入参数依次为:
- 打开文件的绝对地址,还是以
ledtest /dev/myled on
为例,那么argv[1]对应的就是/dev/myled。 - 文件打开方式:读写O_RDWR、只读O_RDONLY、创造O_CREAT
- 模式:当访问方式为O_CREAT时,通过mode来设置文件权限(其余打开方式不填本参数)
open
函数返回值由int型接收,返回的是文件句柄,失败返回-1。
int fd = open(argv[1], O_RDWR);
read函数输入参数依次为:
- open函数返回的那个文件句柄
- 缓冲区的地址,就是数据接收buffer的指针,自己定义一个全局的拿来用就行了
- 读几个byte
read(fd, buf, 6);
返回值为读到的byte数,读取失败返回-1
write函数输入参数依次为:
- open函数返回的那个文件句柄
- 缓冲区的地址,就是放了数据的buffer,跟上面那个一样,从buffer里面写入到文件句柄位置
- 写几个byte进去
设备驱动框架
驱动程序编写流程
定义要用的参数
static int major = 0 //主设备号
static char kernel_buf[1024]; //数据接收buffer
static struct class *hello_class; //class定义
实现对应的 drv_open/drv_read/drv_write
等函数
copy_to_user
与copy_from_user
的输入参数为:去向指针、来源指针、长度
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;
}
定义自己的 file_operations
结构体,包含以上自定义功能函数
(语法就是这样,无需纠结,.owner
这个必须包含)
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,
};
把 file_operations 结构体告诉内核,创建驱动,即入口函数:register_chrdev()
register_chrdev()输入参数为:主设备号(此处0则由内核分配),“起个名字”,结构体指针。返回值用主设备号接收
static struct class *hello_class;
static int __init hello_init(void){
major = register_chrdev(0, "hello", &hello_drv);
hello_class = class_create(THIS_MODULE, "hello_class");//创建一个总领的设备大类
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello");//创建hello这个具体的device /dev/hello被创建
return 0;
}
有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev()
static void __exit hello_exit(void){
device_destroy(hello_class, MKDEV(major, 0));//销毁device
class_destroy(hello_class); //销毁class
unregister_chrdev(major, "hello");
}
其他完善:提供设备信息,自动创建设备节点:class_create()
、device_create()
module_init(hello_init);//将hello_init定为本module的入口函数
module_exit(hello_exit);//将hello_exit定为本module的出口函数
MODULE_LICENSE("GPL"); //声明本module使用的协议
led驱动举例
头文件包含
用到什么指令,利用man n 指令名 可以快速搜索头文件与用法
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <asm/io.h>
变量定义
这里使用static防止污染命名,用volatile防止编译器胡乱优化,因为有时需要0->1->0->1来回变化,编译器会直接赋最后一个值,忽略变化过程
static int major;
static struct class *led_class;
/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;
//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;
构建open函数
定义驱动led的open函数led_open
static int led_open(struct inode *inode, struct file *filp){
/* enable gpio5
* configure gpio5_io3 as gpio
* configure gpio5_io3 as output
*/
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &= ~0xf;
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |= 0x5;
*GPIO5_GDIR |= (1<<3);
return 0;
}
构建write函数
定义驱动led的write函数led_write
static ssize_t led_write(struct file *filp, const char __user *buf,size_t count, loff_t *ppos){
char val;
int ret;
/* copy_from_user : get data from app */
ret = copy_from_user(&val, buf, 1);
/* to set gpio register: out 1/0 */
if (val){
/* set gpio to let led on */
*GPIO5_DR &= ~(1<<3);
}
else{
/* set gpio to let led off */
*GPIO5_DR |= (1<<3);
}
return 1;
}
定义结构体
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.write = led_write,
.open = led_open,
};
入口函数
dvr_ioctl不能直接调用物理地址,需要使用ioremap(phy,size)
来映射到虚拟地址,之后才可以在驱动中正常调用,下面的各个寄存器就是这个道理
static int __init led_init(void){
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "100ask_led", &led_fops);
/* ioremap */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x02290000 + 0x14, 4);
// GPIO5_GDIR 地址:0x020AC004
GPIO5_GDIR = ioremap(0x020AC004, 4);
//GPIO5_DR 地址:0x020AC000
GPIO5_DR = ioremap(0x020AC000, 4);
led_class = class_create(THIS_MODULE, "myled_class"); myled_class这个大类被创建
device_create(led_class, NULL, MKDEV(major, 0), NULL, "myled"); /* /dev/myled */次设备号对应的,具体设备节点被创建
return 0;
}
出口函数
static void __exit led_exit(void){
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
unregister_chrdev(major, "100ask_led");
}
module声明
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
led应用函数举例
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
char status = 0;
//说明使用方法
if (argc != 3){
printf("Usage: %s <dev> <on|off>\n", argv[0]);
printf(" eg: %s /dev/myled on\n", argv[0]);
printf(" eg: %s /dev/myled off\n", argv[0]);
return -1;
}
// open
fd = open(argv[1], O_RDWR);
if (fd < 0){
printf("can not open %s\n", argv[0]);
return -1;
}
// write
if (strcmp(argv[2], "on") == 0){
status = 1;
}
write(fd, &status, 1);
return 0;
}