1.头文件
#ifndef __HEAD_H__
#define __HEAD_H__
//LED1 PE10 和 LED3 PE8
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0X50000A28
//LED2 PF10
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#endif
2.驱动代码
#include
#include
#include
#include
#include
#include s
#include "head.h"
void delay_ms(int ms)
{
int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 1800; j++)
;
}
unsigned int major; // 定义一个变量保存主设备号
char kbuf[128] = {0};
// 定义指针接收映射成功的虚拟内存首地址
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
unsigned int *vir2_moder;
unsigned int *vir2_odr;
struct class *cls;
struct device *dev;
// 封装操作方法
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 *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
int ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user filad\n");
return ret;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user filed\n");
return ret;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
/*
if (kbuf[0] == '1') // 开灯
{
// 开灯逻辑
(*vir_odr) |= (0X1 << 10);
}
else if (kbuf[0] == '0') // 关灯
{
// 关灯逻辑
(*vir_odr) &= (~(0X1 << 10));
}
*/
switch (kbuf[0])
{
case '1': // LED1灯
switch (kbuf[1])
{
case '0': // LED1灯灭
(*vir_odr) &= (~(0X1 << 10));
break;
case '1': // LED1灯亮
(*vir_odr) |= (0X1 << 10);
break;
}
break;
case '2': // LED2灯
switch (kbuf[1])
{
case '0': // LED2灯灭
(*vir2_odr) &= (~(0X1 << 10));
break;
case '1': // LED2灯亮
(*vir2_odr) |= (0X1 << 10);
break;
}
break;
case '3': // LED3灯
switch (kbuf[1])
{
case '0': // LED3灯灭
(*vir_odr) &= (~(0X1 << 8));
break;
case '1': // LED3灯亮
(*vir_odr) |= (0X1 << 8);
break;
}
break;
case '4': // 流水灯
switch (kbuf[1])
{
case '0': // 顺序流水灯 1 2 3 循环10次
{
printk("顺序流水灯\n");
int k = 0;
for (k = 0; k < 10; k++)
{
(*vir_odr) |= (0X1 << 10);
delay_ms(500);
(*vir_odr) &= (~(0X1 << 10));
delay_ms(500);
(*vir2_odr) |= (0X1 << 10);
delay_ms(500);
(*vir2_odr) &= (~(0X1 << 10));
delay_ms(500);
(*vir_odr) |= (0X1 << 8);
delay_ms(500);
(*vir_odr) &= (~(0X1 << 8));
delay_ms(500);
}
}
break;
case '1': // 逆向流水灯 3 2 1 循环10次
{
printk("顺序流水灯\n");
int m = 0;
for (m = 0; m < 10; m++)
{
(*vir_odr) |= (0X1 << 8);
delay_ms(500);
(*vir_odr) &= (~(0X1 << 8));
delay_ms(500);
(*vir2_odr) |= (0X1 << 10);
delay_ms(500);
(*vir2_odr) &= (~(0X1 << 10));
delay_ms(500);
(*vir_odr) |= (0X1 << 10);
delay_ms(500);
(*vir_odr) &= (~(0X1 << 10));
delay_ms(500);
}
break;
}
}
break;
default:
printk("输入错误,请重新输入\n");
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,
.release = mycdev_close,
.read = mycdev_read,
.write = mycdev_write,
};
static int __init mycdev_init(void)
{
int i = 0;
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功 主设备号major=%d\n", major);
// 向上提交目录信息
cls = class_create(THIS_MODULE, "mycdev");
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, "mycdev%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(cls);
}
}
printk("向上提交设备节点信息成功\n");
// 完成硬件寄存器物理内存的映射
// LED1 和 LED3 物理内存映射
vir_moder = ioremap(PHY_LED1_MODER, 4);
if (vir_moder == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_odr = ioremap(PHY_LED1_ODR, 4);
if (vir_odr == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_rcc = ioremap(PHY_LED1_RCC, 4);
if (vir_rcc == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
printk("LED1 LED3 物理内存映射成功\n");
// LED2 物理内存映射
vir2_moder = ioremap(PHY_LED2_MODER, 4);
if (vir2_moder == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir2_odr = ioremap(PHY_LED2_ODR, 4);
if (vir2_odr == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
printk("LED2 物理内存映射成功\n");
// rcc使能 GPIOE组 GPIOF组
(*vir_rcc) |= (0X1 << 4);
(*vir_rcc) |= (0X1 << 5);
// LED1灯初始化 PE10
// 硬件寄存器的初始化
(*vir_moder) &= (~(0X3 << 20)); // 设置为输出
(*vir_moder) |= (0X1 << 20);
// 默认关灯
(*vir_odr) &= (~(0X1 << 10));
// LED2灯初始化 PF10
// 硬件寄存器的初始化
(*vir2_moder) &= (~(0X3 << 20)); // 设置为输出
(*vir2_moder) |= (0X1 << 20);
// 默认关灯
(*vir2_odr) &= (~(0X1 << 10));
// LED3灯初始化 PE8
// 硬件寄存器的初始化
(*vir_moder) &= (~(0X3 << 16)); // 设置为输出
(*vir_moder) |= (0X1 << 16);
// 默认关灯
(*vir_odr) &= (~(0X1 << 8));
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消物理内存的映射
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
iounmap(vir2_moder);
iounmap(vir2_odr);
// 销毁设备信息
int i;
for (i = 0; i < 3; 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");
3.测试代码
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char *argv[])
{
char buf[128] = {};
int fd = open("/dev/mycdev0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("成功打开设备文件\n");
while (1)
{
printf("------------------------------------\n");
printf("请输入要实现的逻辑》\n");
printf("11:LED1亮\n");
printf("10:LED1灭\n");
printf("21:LED2亮\n");
printf("20:LED2灭\n");
printf("31:LED3亮\n");
printf("30:LED3灭\n");
printf("00:退出\n");
//printf("41:顺序流水灯\n");
//printf("40:逆向流水灯\n");
printf("------------------------------------\n");
fgets(buf, sizeof(buf), stdin); // 从终端读一个字符串存放在buf
buf[strlen(buf) - 1] = '\0';
if(strcmp(buf,"00")==0)
{
break;
}
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
实验现象