实现过程:
Makefile:
#定义一个存放架构变量
ARCH ?= X86
#定义一个存放文件名变量
modname ?= demo
ifeq ($(ARCH),arm)
#定义一个变量,存放linux内核源码目录,生成ARM架构
KERNEDIR:=/home/ubuntu/linux-5.10.61
else
#定义一个变量,存放ubuntu的linux内核源码目录,生成X86架构
KERNEDIR:=/lib/modules/$(shell uname -r)/build
endif
#定义一个变量,开启一个终端,执行pwd命令
PWD:=$(shell pwd)
all:
@#-C:跳转到内核顶层目录下,读取内核顶层目录下的Makefile文件
@#在内核源码顶层目录下执行:make M=$(shell pwd) modules
@#M=$(shell pwd):回到当前目录下,只编译当前目录下的文件
@#make modules:采用模块化方式进行编译
make -C $(KERNEDIR) M=$(shell pwd) modules
clean:
make -C $(KERNEDIR) M=$(shell pwd) clean
#指定模块化方式编译的文件
obj-m:=$(modname).o
myled.h:
#ifndef __MYLED_H__
#define __MYLED_H__
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
//GPIOE基地址
#define PHY_GPIOE_ADDR 0x50006000
#define PHY_GPIOF_ADDR 0x50007000
//RCC基地址:0x50000A28
#define PHY_RCC_LED1 0x50000A28
//开关灯命令码
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
};
//字符串命令码
#define UACCESS_BUF _IOW('a',1,char[128])
//结构体命令码
typedef struct
{
int high;
int width;
}image_t;
#define IMAGE_T _IOW('a',1,image_t)
#endif
mycdev.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#include <linux/device.h>
#define CNAME "myled"
int major; //主设备号
char kbuf[128] = {0}; //接收用户信息数组
image_t image; //接收结构体信息
gpio_t* virt_gpioe = NULL; //GPIOE组的虚拟地址
gpio_t* virt_gpiof = NULL; //GPIOF组的虚拟地址
unsigned int* virt_rcc = NULL; //RCC的虚拟地址
struct class* cls = NULL; //自动创建设备节点提交目录信息
struct device* dev = NULL; //自动创建设备节点提交设备信息
#define LED1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))
int myled_open(struct inode *inode, struct file *file)
{
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 *loff)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
if(size > sizeof(kbuf)) size = sizeof(kbuf);
//2.将数据从内核空间拷贝到用户空间
ret = copy_to_user(ubuf,&image,size);
if(ret) //3.判断是否错误
{
printk("copy to user is error\n");
return -EIO;
}
return size; //5.返回拷贝数据大小
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
if(size > sizeof(kbuf)) size = sizeof(kbuf);
//2.将数据从用户空间拷贝到内核空间
ret = copy_from_user(kbuf,ubuf,size);
if(ret) //3.判断是否错误
{
printk("copy from user is error\n");
return -EIO;
}
//4.打印传递数据内容
printk("copy from user kbuf:%s\n",kbuf);
//kbuf[0]:代表操作的是那一盏灯,kbuf[0] = 0 kbuf[0] = 1 kbuf[0] = 2
//kbuf[1]:代表led灯的状态 kbuf[1] = 0 kbuf[1] = 1
switch(kbuf[0])
{
case 0:
kbuf[1]?LED1_ON:LED1_OFF;
break;
case 1:
kbuf[1]?LED2_ON:LED2_OFF;
break;
case 2:
kbuf[1]?LED3_ON:LED3_OFF;
break;
}
return size; //5.返回拷贝数据大小
}
long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{
//1.判断cmd switch(cmd),需要控制灯还是打印字符串、结构体
//2.判断操作哪盏灯进行点亮,打印的内容是什么等 copy_from_user
int whitch; //需要操作的灯是哪一盏
int ret; //接收用户信息的函数返回值
switch(cmd)
{
case LED_ON: //开灯操作
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
LED1_ON;
break;
case LED2:
LED2_ON;
break;
case LED3:
LED3_ON;
break;
}
break;
case LED_OFF: //关灯操作
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
LED1_OFF;
break;
case LED2:
LED2_OFF;
break;
case LED3:
LED3_OFF;
break;
}
break;
case UACCESS_BUF: //打印字符串操作
ret = copy_from_user(kbuf,(void*)args,sizeof(kbuf));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
printk("copy from user is :%s\n",kbuf);
break;
case IMAGE_T: //接收结构体操作
ret = copy_from_user(&image,(void*)args,sizeof(image_t));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
printk("copy from user is :high=%d\n",image.high);
printk("copy from user is :width=%d\n",image.width);
image.high += 10;
image.width += 10;
}
return 0;
}
int myled_close (struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
static int __init mycdev_init(void)
{
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0) //2.判断返回值
{
printk("register chrdev is error\n");
}
//2.打印主设备号
printk("register chrdev major=%d\n",major);
//提交目录信息
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls))
{
return PTR_ERR(cls);
}
//提交设备信息
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"myled");
if(IS_ERR(dev))
{
return PTR_ERR(dev);
}
//3.将物理地址映射为虚拟地址
// 将rcc地址映射
virt_rcc = ioremap(PHY_RCC_LED1,4);
if(virt_rcc == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
//映射GPIOE地址
virt_gpioe = ioremap(PHY_GPIOE_ADDR,sizeof(gpio_t));
if(virt_gpioe== NULL)
{
printk("virt_gpioe ioremap is error\n");
return -ENOMEM;
}
//映射GPIOF地址
virt_gpiof = ioremap(PHY_GPIOF_ADDR,sizeof(gpio_t));
if(virt_gpiof== NULL)
{
printk("virt_gpiof ioremap is error\n");
return -ENOMEM;
}
//5.对led1---->PE10引脚初始化 对led3---->PE8引脚初始化
*virt_rcc |= (0x1 << 4);//5.1 使能GPIOE组时钟[4]=1
virt_gpioe->MODER &= (~(0x3 << 20));//5.2 设置PE10引脚为输出模式 [21:20] = 01
virt_gpioe->MODER |= (0x1 << 20);
virt_gpioe->ODR &= (~(0x1 << 10)); //5.3 设置PE10引脚输出低电平
//6. 对led2---->PF10引脚初始化
*virt_rcc |= (0x1 << 5);//5.1 使能GPIOF组时钟[5]=1
virt_gpiof->MODER &= (~(0x3 << 20));//5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpiof->MODER |= (0x1 << 20);
virt_gpiof->ODR &= (~(0x1 << 10)); //5.3 设置PF10引脚输出低电平
//7.对led3---->PE8引脚初始化
virt_gpioe->MODER &= (~(0x3 << 16));//5.2 设置PE8引脚为输出模式 [17:16] = 01
virt_gpioe->MODER |= (0x1 << 16);
virt_gpioe->ODR &= (~(0x1 << 8)); //5.3 设置PE8引脚输出低电平
return 0;
}
static void __exit mycdev_exit(void)
{
//1.取消地址映射
iounmap(virt_rcc);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
//2.取消设备信息
device_destroy(cls,MKDEV(major,0));
//3.取消目录信息
class_destroy(cls);
//4.注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc,const char * argv[])
{
char buf[128] = "hello world!";
int whitch;
int fd = -1;
image_t image = {10,20}; //结构体赋值
fd = open("/dev/myled",O_RDWR);
if(fd == -1)
{
perror("open is error\n");
return -1;
}
while(1)
{
whitch = LED1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
ioctl(fd,UACCESS_BUF,buf);
sleep(1);
ioctl(fd,IMAGE_T,&image);
sleep(1);
read(fd,&image,sizeof(image_t));
printf("copy to user is %d\n",image.high);
printf("copy to user is %d\n",image.width);
}
close(fd);
return 0;
}
实现效果: