头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED2_MODER 0x50007000
#define PHY_LED3_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED2_ODR 0x50007014
#define PHY_LED3_ODR 0x50006014
#define PHY_RCC 0x50000A28
#endif
demo.c文件
#include
#include
#include
#include
#include
#include "head.h"
unsigned int major;
char kbuf[128]={};
//定义三个指针指向映射后的虚拟内存
unsigned int *vir_led1_moder;
unsigned int *vir_led2_moder;
unsigned int *vir_led3_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led2_odr;
unsigned int *vir_led3_odr;
unsigned int *vir_rcc;
// 封装操作方法
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 fild\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret;
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
if(kbuf[0]=='0')
{
(*vir_led1_odr)&=(~(0x1<<10));
(*vir_led2_odr)&=(~(0x1<<10));
(*vir_led3_odr)&=(~(0x1<<8));
}
else if(kbuf[0]=='1')
{
(*vir_led1_odr)|=(0x1<<10);
(*vir_led2_odr)|=(0x1<<10);
(*vir_led3_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, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
//进行寄存器的地址映射
vir_led1_moder = ioremap(PHY_LED1_MODER,4);
if(vir_led1_moder==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led1_odr=ioremap(PHY_LED1_ODR,4);
if(vir_led1_odr==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led2_moder = ioremap(PHY_LED2_MODER,4);
if(vir_led2_moder==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led2_odr=ioremap(PHY_LED2_ODR,4);
if(vir_led2_odr==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led3_moder = ioremap(PHY_LED3_MODER,4);
if(vir_led3_moder==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_led3_odr=ioremap(PHY_LED3_ODR,4);
if(vir_led3_odr==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
vir_rcc=ioremap(PHY_RCC,4);
if(vir_rcc==NULL)
{
printk("物理内存地址映射失败%d\n",__LINE__);
return -EFAULT;
}
printk("寄存器内存映射成功\n");
//LED1寄存器初始化
(*vir_rcc)|=(0x1<<4);
(*vir_led1_moder)&=(~(0x3<<20));
(*vir_led1_moder)|=(0x1<<20);
(*vir_led1_odr)&=(~(0x1<<10));
(*vir_led2_moder)&=(~(0x3<<20));
(*vir_led2_moder)|=(0x1<<20);
(*vir_led2_odr)&=(~(0x1<<10));
(*vir_led3_moder)&=(~(0x3<<16));
(*vir_led3_moder)|=(0x1<<16);
(*vir_led3_odr)&=(~(0x1<<8));
return 0;
}
static void __exit mycdev_exit(void)
{
//取消内存映射
iounmap(vir_led1_moder);
iounmap(vir_led1_odr);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
iounmap(vir_led3_moder);
iounmap(vir_led3_odr);
iounmap(vir_rcc);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");