led.h
#ifndef __LED_H__
#define _LED_H__
#define PHY_MODER 0X50006000
#define PHY_ODR 0X50006014
#define PHY_F_MODER 0X50007000
#define PHY_F_ODR 0X50007014
#define PHY_RCC 0X50000A28
#endif
mycdev.c
#include
#include
#include
#include
#include
#include "led.h"
unsigned int major; // 主设备号
char kbuf[128] = {0};
//定义三个指针指向映射后的虚拟地址
unsigned int *vir_moder;//设置输出01输入00复用10
unsigned int *vir_odr;//高低电平
unsigned int *vir_rcc;//给对应组使能(4:gpioe 5:gpiof)
unsigned int *vir_moder2;
unsigned int *vir_odr2;
// 封装各个操作方法
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)
{
int ret;
// 用户读取内核设备文件内容
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy to user filed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
// 用户给内核设备文件中写内容
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy from user filed\n");
return -EIO;
}
if('1'==kbuf[0])//led1灯亮
(*vir_odr) |=(0x1<<10);
else if('2'==kbuf[0])//led2灯亮
(*vir_odr2) |=(0x1<<10);
else if('3'==kbuf[0])//led3灯亮
(*vir_odr) |=(0x1<<8);
else if('4'==kbuf[0])//led1灯灭
(*vir_odr) &=(~(0x1<<10));
else if('5'==kbuf[0])//led2灯灭
(*vir_odr2) &=(~(0x1<<10));
else if('6'==kbuf[0])//led3灯灭
(*vir_odr) &=(~(0x1<<8));
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,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
major = register_chrdev(0, "mycdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
//led灯地址映射
vir_moder=ioremap(PHY_MODER,4);
if(vir_moder==NULL)
{
printk("MODER映射失败\n");
return -1;
}
vir_odr=ioremap(PHY_ODR,4);
if(vir_odr==NULL)
{
printk("ODR映射失败\n");
return -1;
}
vir_moder2=ioremap(PHY_F_MODER,4);
if(vir_moder2==NULL)
{
printk("MODER映射失败\n");
return -1;
}
vir_odr2=ioremap(PHY_F_ODR,4);
if(vir_odr==NULL)
{
printk("ODR映射失败\n");
return -1;
}
vir_rcc=ioremap(PHY_RCC,4);
if(vir_rcc==NULL)
{
printk("RCC射失败\n");
return -1;
}
printk("物理内存映射成功\n");
//相关寄存器初始化
(*vir_rcc) |=(0x1<<5);//gpiof组使能
(*vir_rcc) |=(0x1<<4);//gpioe组使能
(*vir_moder) &=(~(0x3<<20));
(*vir_moder) |=(0x1<<20);//gpio pe10为输出模式
(*vir_moder2) &=(~(0x3<<20));
(*vir_moder2) |=(0x1<<20);//gpio pf10为输出模式
(*vir_moder) &=(~(0x3<<16));
(*vir_moder) |=(0x1<<16);//gpio pe8为输出模式
(*vir_odr) &=(~(0x1<<10));//e10引脚灭灯
(*vir_odr) &=(~(0x1<<8));//10引脚灭灯
(*vir_odr2) &=(~(0x1<<10));//f10引脚灭灯
printk("引脚初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_rcc);
iounmap(vir_odr);
iounmap(vir_moder);
// 字符设备的注销
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include
#include
#include
#include
#include
#include
#include
#include "led.h"
int main(int argc,const char * argv[])
{
char buf[128]={0};
int fd=open("/dev/mycdev",O_RDWR);//调用驱动中的mycdev_open
if(fd<0)
{
printf("打开驱动设备失败\n");
exit(-1);
}
while(1)
{
memset(buf,0,sizeof(buf));//清空buf
printf("1(1亮灯)2(2亮灯)3(3亮灯)4(1亮灭)5(2亮灭)6(3亮灭)\n");
printf("请输入控制码:>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));//向设备文件中写数据
//read(fd,buf,sizeof(buf));//读取设备文件中的数据
// printf("buf: %s\n",buf);
}
close(fd);
return 0;
}
实验流程及现象: