#ifndef __H__
#define __H__
#define LED_MODERE 0X50006000
#define LED_MODERF 0X50007000
#define LED_ODRE 0X50006014
#define LED_ODRF 0X50007014
#define LED_RCC 0X50000A28
#endif
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include "head.h"
int main(int argc, const char *argv[])
{
char buf[128]="";
int fd=open("/dev/mychrdev",O_RDWR);
if(fd<0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while(1)
{
printf("请开灯关灯(0/1)\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
memset(buf,0,sizeof(buf));
read(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/compiler.h>
#include <linux/io.h>
#include "head.h"
unsigned int major;
char kbuf[128]="";
unsigned int *vir_modere;
unsigned int *vir_moderf;
unsigned int *vir_rcc;
unsigned int *vir_odre;
unsigned int *vir_odrf;
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;
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_from_user err\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 *lof)
{
int ret;
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user err\n");
return -EIO;
}
if(kbuf[0]=='1')
{
//开灯
//led1
(*vir_odre) |= (0x1<<10);
//led2
(*vir_odrf) |= (0x1<<10);
//led3
(*vir_odre) |= (0x1<<8);
printk("开灯-->%d\n",*kbuf);
}
else if(kbuf[0]=='0')
{
//关灯
//led1
(*vir_odre) &= (~(0x1<<10));
//led2
(*vir_odrf) &= (~(0x1<<10));
//led3
(*vir_odre) &= (~(0x1<<8));
printk("关灯-->%d\n",*kbuf);
}
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,"mychrdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功%d\n",major);
//进行映射
vir_rcc=ioremap(LED_RCC,4);
if(vir_rcc==NULL)
{
printk("物理映射失败%d\n",__LINE__);
}
vir_modere=ioremap(LED_MODERE,4);
if(vir_rcc==NULL)
{
printk("物理映射失败%d\n",__LINE__);
}
vir_moderf=ioremap(LED_MODERF,4);
if(vir_rcc==NULL)
{
printk("物理映射失败%d\n",__LINE__);
}
vir_odre=ioremap(LED_ODRE,4);
if(vir_rcc==NULL)
{
printk("物理映射失败%d\n",__LINE__);
}
vir_odrf=ioremap(LED_ODRF,4);
if(vir_rcc==NULL)
{
printk("物理映射失败%d\n",__LINE__);
}
//rcc使能
(*vir_rcc) |= (0x1<<4);
(*vir_rcc) |= (0x1<<5);
// moder设置输出模式
//led1,PE10
(*vir_modere) &= (~(0x3<<20));
(*vir_modere) |= (0x1<<20);
//led2,PF10
(*vir_moderf) &= (~(0x3<<20));
(*vir_moderf) |= (0x1<<20);
//led3,PE8
(*vir_modere) &= (~(0x3<<16));
(*vir_modere) |= (0x1<<16);
//设置默认为关灯
(*vir_odre) &= (~(0x1<<10));
(*vir_odrf) &= (~(0x1<<10));
(*vir_odre) &= (~(0x1<<8));
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,"mychrdev");
iounmap(vir_rcc);
iounmap(vir_modere);
iounmap(vir_moderf);
iounmap(vir_odre);
iounmap(vir_odrf);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
![](https://i-blog.csdnimg.cn/blog_migrate/1e830165ba6e538b7331ca4e7c939a03.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/097c23cbb4112b26ba8706502a6ba6f3.jpeg)