.h
#ifndef __1_h_
#define __1_h_
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
int Init_Led1(void);
int Init_Led2(void);
int Init_Led3(void);
void Des_Led(void);
#endif
驱动
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "1.h"
int major;
char kbuf[128]={0};
unsigned int *vir1_moder;
unsigned int *vir1_odr;
unsigned int *vir_rcc;
unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir3_moder;
unsigned int *vir3_odr;
int Init_Led1(void)
{
vir1_moder=ioremap(PHY_LED1_MODER,4);
if(vir1_moder==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir1_odr=ioremap(PHY_LED1_ODR,4);
if(vir1_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;
}
(*vir1_moder) &= (~(0x3 << 20 ));
(*vir1_moder) |= (0x1 << 20 );
(*vir_rcc) |= (0x1 << 4 );
(*vir1_odr) &= (~(0x1 << 10 ));
return 0;
}
int Init_Led2(void)
{
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;
}
(*vir_rcc) |= (0x1 << 5 );
(*vir2_moder) &= (~(0x3 << 20 ));
(*vir2_moder) |= (0x1 << 20 );
(*vir2_odr) &= (~(0x1 << 10 ));
return 0;
}
int Init_Led3(void)
{
vir3_moder=ioremap(PHY_LED3_MODER,4);
if(vir3_moder==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
vir3_odr=ioremap(PHY_LED3_ODR,4);
if(vir3_odr==NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
return -EFAULT;
}
(*vir3_moder) &= (~(0x3 << 16 ));
(*vir3_moder) |= (0x1 << 16 );
(*vir3_odr) &= (~(0x1 << 8 ));
return 0;
}
void Des_Led(void)
{
iounmap(vir1_moder);
iounmap(vir1_odr);
iounmap(vir_rcc);
iounmap(vir2_moder);
iounmap(vir2_odr);
iounmap(vir3_moder);
iounmap(vir3_odr);
}
struct class *cls;
struct device *dev;
ssize_t demo_read (struct file * file, char *buf, size_t size, loff_t * loff)
{
printk("%s:%s %d\n",__FILE__,__func__,__LINE__);
int ret;
ret=copy_to_user(buf,kbuf,size);
if(ret)
{
printk("copy_to_user filad\n");
}
return 0;
}
ssize_t demo_write(struct file * file, const char *buf, size_t size, loff_t * loff)
{
int ret;
ret = copy_from_user(kbuf,buf,size);
if(ret)
{
printk("copy_from_user filad\n");
}
if(kbuf[0]=='1')
{
if(kbuf[1]=='1')
{
(*vir1_odr) |= (0x1 <<10);
}else if(kbuf[1]=='0')
{
(*vir1_odr) &= (~(0x1 << 10 ));
}
}else if(kbuf[0]=='2')
{
if(kbuf[1]=='1')
{
(*vir2_odr) |= (0x1 <<10);
}else if(kbuf[1]=='0')
{
(*vir2_odr) &= (~(0x1 << 10 ));
}
}else if(kbuf[0]=='3')
{
if(kbuf[1]=='1')
{
(*vir3_odr) |= (0x1 <<8);
}else if(kbuf[1]=='0')
{
(*vir3_odr) &= (~(0x1 << 8 ));
}
}
return 0;
}
int demo_open(struct inode *inode, struct file *file)
{
printk("%s:%s %d\n",__FILE__,__func__,__LINE__);
return 0;
}
int demo_close(struct inode *inode, struct file *file)
{
printk("%s:%s %d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops=
{
.release=demo_close,
.read=demo_read,
.write=demo_write,
.open=demo_open,
};
static int __init mycdev_init(void)
{
int i;
major = register_chrdev(0,"demo1",&fops);
if(major < 0)
{
printk("字符驱动注册失败\n");
return major;
}
printk("字符驱动注册成功,设备号:%d",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");
int j;
if((j=Init_Led1())!=0)
{
printk("LED1初始化失败\n");
return 0;
}
if((j =Init_Led2())!=0)
{
printk("LED2初始化失败\n");
return 0;
}
if((j =Init_Led3())!=0)
{
printk("LED3初始化失败\n");
return 0;
}
return 0;
}
static void __exit mycdev_exit(void)
{
int i=0;
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
Des_Led();
unregister_chrdev(major,"demo1");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试
#include <stdio.h>
#include<sys/types.h>
#include <fcntl.h>
#include<sys/stat.h>
#include <stdlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc,const char * argv[])
{
int fd;
char buf[128] = {};
if((fd=open("/dev/mycdev0",O_RDWR))<0)
{
printf("打开设备文件失败\n");
return 0;
}
printf("成功打开设备文件\n");
while(1)
{
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");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}