字符设备驱动的架构
编写驱动首先进行字符设备驱动的注册,字符设备驱动注册以后会得到一个字符设备的主设备号,基于得到的主设备号和指定的次设备号我们可以在文件系统中创建一个设备文件。
应用层通过对设备文件进行操作,从而转到驱动相关代码执行。
用户空间与内核空间的数据传递
相关API
#include<linux/uaccess.h>
1.int copy_to_user(void __user volatile *to, const void *from,unsigned long n)
功能:实现内核空间数据拷贝到用户空间
参数:
to:用户空间存放数据的空间首地址
from:内核空间存放数据的空间首地址
n:大小(字节)
返回值:成功返回0,失败但会未拷贝的数据大小
2. unsigned long
copy_from_user(void *to, const void __user *from, unsigned long n)
功能:实现用户空间数据拷贝到内核空间
参数:
to:内核空间存放数据的首地址
from:用户空间存放数据的首地址
n:大小(字节)
返回值:成功返回0,失败但会未拷贝的数据大小
物理地址映射
#include<linux/io.h>
1.void *ioremap(phys_addr_t offset, size_t size)
功能:将指定的物理地址映射成虚拟内存地址
参数:
offset:物理地址
size:映射的大小
返回值:成功返回虚拟内存地址,失败返回空
2.void iounmap(void *addr)
功能:取消物理内存的映射
参数:addr:虚拟内存地址
返回值:无
驱动点灯实验
驱动代码编写
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "test.h"
int major;
char kbuf[128] = {
0};
unsigned int *vir_rcc;
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
int mycdev_open(struct inode *inode, struct file *file)
{
// printk("%s %s %d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
int res;
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
res = copy_to_user(ubuf, kbuf, size);
if (res)
{
printk("copy to user faild\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
int res;
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
res = copy_from_user(kbuf, ubuf, size);
if (res)
{
printk("copy from user faild\n");
return -EIO;
}
switch (kbuf[0])
{
case '0':
vir_led1->ODR |= (1 << 10);
break;
case '1':
vir_led1->ODR &= (~(1 << 10));
break;
case '2':
vir_led2->ODR |= (1 << 10);
break;
case '3':
vir_led2->ODR &= (~(1 << 10));
break;
case '4':
vir_led3->ODR |= (1 << 8);
break;
case '5':
vir_led3->ODR &= (~(1 << 8));
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
// printk("%s %s %d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops =
{
.open = mycdev_open,
.write = mycdev_write,
.read = mycdev_read,
.release = mycdev_close,
};
int all_leds_init(void)
{
// 进行物理地址映射
vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
if (vir_led1 == NULL)
{
printk("LED1寄存器映射失败\n");
return -ENOMEM;
}
vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
if (vir_led2 == NULL)
{
printk("LED2寄存器映射失败\n");
return -ENOMEM;
}
vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
if (vir_led3 == NULL)
{
printk("LED3寄存器映射失败\n");
return -ENOMEM;
}
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -ENOMEM;
}
printk("寄存器物理地址映射成功\n");
// rcc初始化
(*vir_rcc) |= (3 << 4); // rcc使能
// LED1初始化
vir_led1->MODER &= (~(3 << 20)); // 设置为输出模式
vir_led1->MODER |= (1 << 20);
vir_led1->ODR &= (~(1 << 10)); // 输出低电平
// LED2初始化
vir_led2->MODER &= (~(3 << 20)); // 设置为输出模式
vir_led2->MODER |= (1 << 20);
vir_led2->ODR &= (~(1 << 10)); // 输出低电平
// LED3初始化
vir_led3->MODER &= (~(3 << 16)); // 设置为输出模式
vir_led3->MODER |= (1 << 16);
vir_led3->ODR &= (~(1 << 8)); // 输出低电平
printk("寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
major = register_chrdev(0, "mycddev", &fops);
if (major < 0)
{
printk("字符驱动注册失败\n");
return major;
}
else
{
printk("字符设备注册成功 major = %d\n", major);
}
all_leds_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消物理内存的映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试代码
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char buf[128] = {
0};
int fd = open("/dev/chardev", O_RDWR);
if (fd < 0)
{
printf("文件打开失败\n");
exit(-1);
}
while (1)
{
printf("-----------------------------功能菜单------------------------------\n");
printf("\t\t\t0.开LED1灯\n");
printf("\t\t\t1.关LED1灯\n");
printf("\t\t\t2.开LED2灯\n");
printf("\t\t\t3.关LED2灯\n");
printf("\t\t\t4.开LED3灯\n");
printf("\t\t\t5.关LED3灯\n");
printf("------------------------------------------------------------------\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
头文件
#ifndef __TEST_H__
#define __TEST_H__
typedef struct
{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
} gpio_t;
#define PHY_RCC_ADDR 0X50000A28
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#endif
测试
通过串口查看
一、安装驱动:insmod chardev.ko
二、添加设备文件:mknod /dev/chardev c 242 0
三、运行测试文件