LED的字符设备驱动
RCC_MP_AHB4ENSETR 0x50000a28 [4] 1 GPIOE时钟使能
GPIOx_MODER 0x50006000 [21:20] 01 输出
GPIOx_ODR 0x50006014 [10] 1 LED1亮 0 LED1灭
思考:地址映射和灯的初始化应该放在哪来做?
地址映射可以在open 也可以在init 区别是:
init :安装的时候地址已经映射完了,灯也初始化好了,卸载的时候地址才会被释放。
open :当调用open接口的时候才会被映射和初始化,调用close的时候被取消。
驱动调用的频率很低 的情况下在open映射和初始化,节省资源 。
驱动调用的频率很高 的情况下在init映射和初始化。
LED1亮灭相关驱动
myled.h
#ifndef __MYLED_H__
#define __MYLED_H__
#define RCC_ENB 0x50000a28
#define GPIOE_MODER 0x50006000
#define GPIOE_ODR 0x50006014
#endif
myled.c
#include "myled.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define CNAME "myled"
int major;
char kbuf[128] = { 0 };
unsigned int *rcc, *moder, *odr;
int myled_open(struct inode* inode, struct file* file)
{
// 1.地址映射
rcc = ioremap(RCC_ENB, 4);
if (rcc == NULL) {
printk("ioremap rcc error\n");
return -ENOMEM;
}
moder = ioremap(GPIOE_MODER, 4);
if (moder == NULL) {
printk("ioremap moder error\n");
return -ENOMEM;
}
odr = ioremap(GPIOE_ODR, 4);
if (moder == NULL) {
printk("ioremap odr error\n");
return -ENOMEM;
}
// 2.LED1的初始化
*rcc |= (1 << 4); // gpioe 时钟使能
*moder &= ~(3 << 20);
*moder |= (1 << 20); // gpioe10 输出
*odr &= ~(1 << 10); // gpioe10输出低电平
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file* file,
char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy to user error\n");
return -EIO;
}
return size;
}
ssize_t myled_write(struct file* file,
const char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy from user error\n");
return -EIO;
}
// 通过用户传递的数据控制LED1亮灭
// kbuf[0]=1 LED1亮 kbuf[0]=0 LED1熄灭
kbuf[0] == 1 ? (*odr |= (1 << 10)) : (*odr &= ~(1 << 10));
return size;
}
int myled_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
iounmap(odr);
iounmap(moder);
iounmap(rcc);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
static int __init myled_init(void)
{
// 1.注册字符设备驱动
major = register_chrdev(0, CNAME, &fops);
if (major < 0) {
printk("register_chrdev error\n");
return -EAGAIN;
}
printk("register chrdev success,major = %d\n", major);
return 0;
}
static void __exit myled_exit(void)
{
// 2.注销字符设备驱动
unregister_chrdev(major, CNAME);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
test.c
#include <head.h>
int main(int argc, const char* argv[])
{
int fd;
char buf[1] = { 0 };
if ((fd = open("/dev/myled", O_RDWR)) == -1)
PRINT_ERR("open error");
while (1) {
buf[0] = !buf[0];
write(fd, buf, sizeof(buf));
sleep(1);
}
close(fd);
return 0;
}
测试
make arch=arm modname=myled
arm-linux-gnueabihf-gcc test.c
arm-linux-gnueabihf- 编译器的路径中需要包含test.c中的头文件<head.h>
扩展:
我们在应用层的时候,把<head.h>放在了<stdio.h>头文件的路径下,因为预处理阶段可以自动找到<stdio.h>并展开头文件。我们把<head.h>放在同路径下一定也可以被识别并展开,不会报错。
此时编译器报错是因为在arm-linux-gnueabihf- 中找不到test.c中的头文件<head.h>,
同理我们需要找到<stdio.h>的头文件路径,并且把<head.h>放到同一路径在即可。
我们把arm-linux-gnueabihf- 编译器安装在了 家目录下的toolchain文件
所以需要在 cd ~/toolchain 这个路径下找<stdio.h>的头文件路径
linux@ubuntu:~/toolchain$ find -name stdio.h
./gcc-7.5.0/lib/gcc/arm-linux-gnueabihf/7.5.0/include/ssp/stdio.h
./gcc-7.5.0/arm-linux-gnueabihf/include/c++/7.5.0/tr1/stdio.h
./gcc-7.5.0/arm-linux-gnueabihf/libc/usr/include/bits/stdio.h
./gcc-7.5.0/arm-linux-gnueabihf/libc/usr/include/stdio.h
明显看到 /usr/include/这个路径是我们要找的路径
所以把 head.h 放到 ./gcc-7.5.0/arm-linux-gnueabihf/libc/usr/include/ 路径下即可
linux@ubuntu:~$ cp /usr/include/head.h .
linux@ubuntu:~$ cd toolchain/
linux@ubuntu:~/toolchain$ cd ./gcc-7.5.0/arm-linux-gnueabihf/libc/usr/include/
linux@ubuntu:~/toolchain/gcc-7.5.0/arm-linux-gnueabihf/libc/usr/include$ mv ~/head.h .
以上就把<head.h>头文件放到了可展开的交叉编译器头文件路径下 编译就不会报错了。
测试
cp *.ko a.out ~/rootfs
连接开发板 看现象
在串口工具中
insmod myled.ko //安装模块
mknod /dev/myled c 240 0 // 安装结点
./a.out
练习:编写字符设备驱动控制三盏亮灭
myled.h
#ifndef __MYLED_H__
#define __MYLED_H__
//c语言中寄存器的封装方式
#define RCC_MP_AHB4ENSETR 0x50000a28
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;
} gpio_t;
// #define GPIOE ((gpio_t *)0x50006000) 这是一个结构体指针ioremap第一个参数需要的是地址才行
// #define GPIOF ((gpio_t *)0x50007000)
#define GPIOE (0x50006000)
#define GPIOF (0x50007000)
enum leds{ //枚举 循环
LED1,
LED2,
LED3
};
enum led_status{ //开关
OFF,
ON
};
#endif
myled.c
#include "myled.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define CNAME "myled"
int major;
char kbuf[128]={0};
unsigned int *rcc;
gpio_t * gpioe; //虚拟地址
gpio_t * gpiof; //虚拟地址
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
//1.地址映射
rcc=ioremap(RCC_MP_AHB4ENSETR,4);
if(rcc == NULL){
printk("ioremap rcc error\n");
return -ENOMEM;
}
gpioe=ioremap(GPIOE,sizeof(gpio_t));
if(gpioe==NULL){
printk("ioremap gpioe error\n");
return -ENOMEM;
}
gpiof=ioremap(GPIOF,sizeof(gpio_t));
if(gpiof==NULL){
printk("ioremap gpiof error\n");
return -ENOMEM;
}
//2.led灯初始化
*rcc |= (1<<4); //gpioe时钟使能
*rcc |= (1<<5); //gpiof时钟使能
gpioe->MODER &= ~(3<<20); //清零moder 21-20bit--led1 PE10
gpioe->MODER |= (1<<20); //将gpioe设置为输出---led1
gpiof->MODER &= ~(3<<20); //清零moder 21-20bit--led2 PF10
gpiof->MODER |= (1<<20); //将gpioe设置为输出---led2
gpioe->MODER &= ~(3<<16); //清零moder 21-20bit--led3 PE8
gpioe->MODER |= (1<<16); //将gpioe设置为输出---led3
gpioe->ODR &= ~(1<<10); //将led1熄灭
gpiof->ODR &= ~(1<<10); //将led2熄灭
gpioe->ODR &= ~(1<<8); //将led3熄灭
return 0;
}
ssize_t mycdev_read(struct file*file,
char __user*ubuf, size_t size, loff_t*offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret){
printk("copy_to_user error\n");
return -EIO;
}
return size; //如果成功的话返回size
}
ssize_t mycdev_write(struct file*file,
const char __user*ubuf, size_t size, loff_t*offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret){
printk("copy_from_user error\n");
return -EIO;
}
//写1 LED1亮 写0 LED1 灭
//(*(int *)kbuf == 1)?(*odr |= (1<<10)):(*odr &= ~(1<<10));
(kbuf[0]==LED1 && kbuf[1]==ON)?(gpioe->ODR |= (1<<10) ):(gpioe->ODR &= ~(1<<10));
(kbuf[0]==LED2 && kbuf[1]==ON)?(gpiof->ODR |= (1<<10) ):(gpiof->ODR &= ~(1<<10));
(kbuf[0]==LED3 && kbuf[1]==ON)?(gpioe->ODR |= (1<<8) ):(gpioe->ODR &= ~(1<<8));
return size;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 取消地址映射
iounmap(rcc);
iounmap(gpioe);
iounmap(gpiof);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
// 1.注册字符设备驱动 register_chrdev
major = register_chrdev(0, CNAME, &fops);
if (major < 0) {
printk("register_chrdev error\n");
return major;
}
printk("register char device driver success,major = %d\n", major);
return 0;
}
static void __exit mycdev_exit(void)
{
// 2.注销字符设备驱动 unregister_chrdev
unregister_chrdev(major, CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include <head.h>
#include "myled.h"
void led_blink(int fd, int which)
{
char ubuf[2] = { which, ON };
write(fd, ubuf, sizeof(ubuf));
sleep(1);
ubuf[1] = OFF;
write(fd, ubuf, sizeof(ubuf));
sleep(1);
}
int main(int argc,const char * argv[])
{
int fd;
if((fd = open("/dev/myled",O_RDWR))==-1)
PRINT_ERR("open error");
while(1){
led_blink(fd,LED1);
led_blink(fd,LED2);
led_blink(fd,LED3);
}
close(fd);
return 0;
}