head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
}gpio_t;
//查看开发扩展板原理图可知
//蜂鸣器 PB6
//风扇 PE9
//马达 PF6
//LED1 PE10
//LED2 PF10
//LED3 PE8
#define PHY_BEE_ADDR 0X50003000
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
#define LED_ON _IOW('l',1,int) //开灯
#define LED_OFF _IOW('l',0,int)//关灯
#define BEE_ON _IOW('l',3,int) //打开蜂鸣器
#define BEE_OFF _IOW('l',2,int)//关闭蜂鸣器
#define FAN_ON _IOW('l',5,int) //开风扇
#define FAN_OFF _IOW('l',4,int)//关风扇
#define MOTOR_ON _IOW('l',7,int)//开马达
#define MOTOR_OFF _IOW('l',6,int)//关马达
#endif
test.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int main(int argc, char const *argv[])
{
int a,b;
char buf[128] = {0};
int fd = open("/dev/mychrdev0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
//从终端读取
printf("请选择要实现的功能\n");
printf("1(打开) 0(关闭)\n");
scanf("%d",&a);
printf("请输入要控制的部件\n");
printf("1(LED1)2(LED2)3(LED3)4(BEE)5(FAN)6(MOTOR)\n");
getchar();
scanf("%d",&b);
getchar();
if(a==1)//打开
{
switch(b)
{
case 1://LED1
ioctl(fd,LED_ON,&b);break;
case 2://LED2
ioctl(fd,LED_ON,&b);break;
case 3://LED3
ioctl(fd,LED_ON,&b);break;
case 4://BEE
ioctl(fd,BEE_ON,&b);break;
case 5://FAN
ioctl(fd,FAN_ON,&b);break;
case 6://MOTOR
ioctl(fd,MOTOR_ON,&b);break;
}
}
else if(a==0)//关闭
{
switch(b)
{
case 1://LED1
ioctl(fd,LED_OFF,&b);break;
case 2://LED2
ioctl(fd,LED_OFF,&b);break;
case 3://LED3
ioctl(fd,LED_OFF,&b);break;
case 4://BEE
ioctl(fd,BEE_OFF,&b);break;
case 5://FAN
ioctl(fd,FAN_OFF,&b);break;
case 6://MOTOR
ioctl(fd,MOTOR_OFF,&b);break;
}
}
}
close(fd);
return 0;
}
mychrdev.c
#include
#include
#include
#include
#include
#include
#include "head.h"
int all_led_init(void);
struct class *cls;//用于保存目录信息
struct device *dev;//用于保存设备节点信息
int major; // 用于保存主设备号
char kbuf[128]={0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_bee;
gpio_t *vir_fan;
gpio_t *vir_motor;
unsigned int *vir_rcc;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which;
//通过拷贝函数获取ioctl第三个参数对应空间的数值
int ret=copy_from_user(&which,(void *)arg,4);
if(ret)
{
printk("拷贝用户空间数据失败\n");
return -EIO;
}
//进行功能控制
switch (cmd)
{
case LED_ON: // 开灯
switch (which)
{
case 1: // LED1
vir_led1->ODR |= (0x1 << 10);
break;
case 2: // LED2
vir_led2->ODR |= (0x1 << 10);
break;
case 3: // LED3
vir_led3->ODR |= (0x1 << 8);
break;
}
break;
case LED_OFF: // 关灯
switch (which)
{
case 1:
vir_led1->ODR &= (~(0X1 << 10));
break;
case 2:
vir_led2->ODR &= (~(0X1 << 10));
break;
case 3:
vir_led3->ODR &= (~(0X1 << 8));
break;
}
break;
case BEE_ON:
vir_bee->ODR |= (1<<6);break;
case FAN_ON:
vir_fan->ODR |= (1<<9);break;
case MOTOR_ON:
vir_motor->ODR |= (1<<6);break;
case BEE_OFF:
vir_bee->ODR &=(~(1<<6));break;
case FAN_OFF:
vir_fan->ODR &=(~(1<<9));break;
case MOTOR_OFF:
vir_motor->ODR &=(~(1<<6));break;
}
return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops =
{
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int i;
//字符设备驱动注册
major = register_chrdev(0, "mychrdev", &fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
//向上提交目录
cls=class_create(THIS_MODULE,"mychrdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
//为三盏灯创建三个设备文件
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mychrdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
}
//为蜂鸣器创建设备文件
dev=device_create(cls,NULL,MKDEV(major,3),NULL,"mychrdev_BEE");
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
//为风扇创建设备文件
dev=device_create(cls,NULL,MKDEV(major,4),NULL,"mychrdev_FAN");
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
//为马达创建设备信息
dev=device_create(cls,NULL,MKDEV(major,5),NULL,"mychrdev_MOTOR");
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点信息成功\n");
//映射物理寄存器
//寄存器映射以及初始化
all_led_init();
return 0;
}
int all_led_init(void)
{
//寄存器地址的映射
vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
if(vir_led1==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
if(vir_led2==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
vir_led3=vir_led1;
vir_rcc=ioremap(PHY_RCC_ADDR,4);
if(vir_rcc==NULL){
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
vir_bee=ioremap(PHY_BEE_ADDR,sizeof(gpio_t));
if(vir_bee==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
vir_fan=vir_led1;
vir_motor=vir_led2;
printk("物理地址映射成功\n");
//寄存器的初始化
//rcc
(*vir_rcc) |= (1<<1);
(*vir_rcc) |= (3<<4);
//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_led1->MODER |= (1<<16);
vir_led1->ODR &= (~(1<<8));
//BEE
vir_bee->MODER &=(~(3<<12));
vir_bee->MODER |= (1<<12);
vir_bee->ODR &= (~(1<<6));
//FAN
vir_fan->MODER &=(~(3<<18));
vir_fan->MODER |=(1<<18);
vir_fan->ODR &=(~(1<<9));
//MOTOR
vir_motor->MODER &= (~(3<<12));
vir_motor->MODER |= (1<<12);
vir_motor->ODR &=(~(1<<6));
printk("寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
//取消地址映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_rcc);
iounmap(vir_bee);
//销毁设备节点信息
for(i=0;i<6;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
//字符设备驱动的注销
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
Makefile
arch?=arm
modname?=demo
#指定内核顶层目录的路径
ifeq ($(arch),arm)
KERNELDIR:=/home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61 #编译为ARM架构的内核路径
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build #编译生成x86架构文件的内核路径
endif
#指定当前源码所在的路径
PWD:=$(shell pwd) #将shell命令pwd的执行结果赋值给变量PWD
#指定使用的符号表文件的路径
KBUILD_EXTRA_SYMBOLS +=/home/ubuntu/23031_driver/day2/1/Module.symvers
all:
#make modules表示进行模块化编译
#make -C $(KERNELDIR)先切换路径到KERNELDIR下,按照这个路径下Makefile的规则进行make
#M=$(PWD)指定模块化编译的路径
make -C $(KERNELDIR) M=$(PWD) modules
clean:
#编译清除
make -C $(KERNELDIR) M=$(PWD) clean
#将指定的.o文件独立链接为模块文件
obj-m:=$(modname).o
在这后面分别输入01 02 03 04 05 06分别关闭了6个设备
输入11 12 13后的现象
输入11后的现象
分别输入01 02 03 04 05 06后的现象
输入11 12后的现象
输入14 15 16后的现象