#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include<linux/uaccess.h>
#include <linux/io.h>
#include "led.h"
unsigned int major;
char kbuf[128]="";
unsigned int *vir_moder_e10;
unsigned int *vir_odr_e10;
unsigned int *vir_rcc_e10;
unsigned int *vir_moder_e8;
unsigned int *vir_odr_e8;
unsigned int *vir_rcc_e8;
unsigned int *vir_moder_f10;
unsigned int *vir_odr_f10;
unsigned int *vir_rcc_f10;
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 *iof)
{
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;
}
// printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{
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;
}
//pe10
if(kbuf[0]=='1')
(*vir_odr_e10) |=(0x1<<10);
else if(kbuf[0]=='0')
(*vir_odr_e10) &=(~(0x1<<10));
//pe8
else if(kbuf[0]=='3')
(*vir_odr_e8) |=(0x1<<8);
else if(kbuf[0]=='2')
(*vir_odr_e8) &=(~(0x1<<8));
//pf10
else if(kbuf[0]=='5')
(*vir_odr_f10) |=(0x1<<10);
else if(kbuf[0]=='4')
(*vir_odr_f10) &=(~(0x1<<10));
//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;
}
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);
vir_moder_e10=ioremap(PHY_MODERE,4);
if(vir_moder_e10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_odr_e10=ioremap(PHY_ODRE,4);
if(vir_odr_e10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_moder_e8=ioremap(PHY_MODERE,4);
if(vir_moder_e8==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_odr_e8=ioremap(PHY_ODRE,4);
if(vir_odr_e8==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_moder_f10=ioremap(PHY_MODERF,4);
if(vir_moder_f10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_odr_f10=ioremap(PHY_ODRF,4);
if(vir_odr_f10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc_e10=ioremap(PHY_RCC,4);
if(vir_rcc_e10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc_e8=ioremap(PHY_RCC,4);
if(vir_rcc_e8==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc_f10=ioremap(PHY_RCC,4);
if(vir_rcc_f10==NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
printk("物理内存映射为虚拟内存成功\n");
//相关寄存器初始化
//pe10
(*vir_rcc_e10) |= (0X1<<4);//GPIOE使能
(*vir_moder_e10) &= (~(0X3<<20));//设置为输出
(*vir_moder_e10) |=(0X1<<20);
(*vir_odr_e10) &= (~(0X1<<10));//灭灯
//pe8
(*vir_moder_e8) &= (~(0X3<<16));//设置为输出
(*vir_moder_e8) |=(0X1<<16);
(*vir_odr_e8) &= (~(0X1<<8));//灭灯
//pf10
(*vir_rcc_f10) |= (0X1<<5);//GPIOF使能
(*vir_moder_f10) &= (~(0X3<<20));//设置为输出
(*vir_moder_f10) |=(0X1<<20);
(*vir_odr_f10) &= (~(0X1<<10));//灭灯
printk("相关寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//字符设备驱动的注销
iounmap(vir_rcc_e10);
iounmap(vir_odr_e10);
iounmap(vir_moder_e10);
iounmap(vir_rcc_e8);
iounmap(vir_odr_e8);
iounmap(vir_moder_e8);
iounmap(vir_rcc_f10);
iounmap(vir_odr_f10);
iounmap(vir_moder_f10);
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128]="";
int fd = open("/dev/mycdev",O_RDWR );
if(fd<0)
{
printf("open failed\n");
exit(-1);
}
while(1)
{
printf("请输入控制码: led1:1(亮)0(灭)>>>\n");
printf("请输入控制码: led2:3(亮)2(灭)>>>\n");
printf("请输入控制码: led3:5(亮)4(灭)>>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}