实现驱动三盏灯
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
unsigned int major;
char kbuf[128]={0};
unsigned int *vir_moder1_3;
unsigned int *vir_odr1_3;
unsigned int *vir_moder2;
unsigned int *vir_odr2;
unsigned int *vir_rcc;
void delay(int i)
{
int time;
while(i--)
{
time=12000;
while(time--);
}
}
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 *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user failed %d\n",__LINE__);
return ret;
}
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 *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user failed %d\n",__LINE__);
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(kbuf[0]=='1')
{
//全亮
(*vir_odr1_3)|=(1<<10);
(*vir_odr1_3)|=(1<<8);
(*vir_odr2)|=(1<<10);
}
else if(kbuf[0]=='2')
{
//全灭
(*vir_odr1_3)&=(~(1<<10));
(*vir_odr1_3)&=(~(1<<8));
(*vir_odr2)&=(~(1<<10));
}
else if(kbuf[0]=='3')
{
//流水灯
(*vir_odr1_3)&=(~(1<<10));
(*vir_odr1_3)&=(~(1<<8));
(*vir_odr2)&=(~(1<<10));
delay(500);
(*vir_odr1_3)|=(1<<10);
delay(500);
(*vir_odr2)|=(1<<10);
delay(500);
(*vir_odr1_3)|=(1<<8);
}
else if(kbuf[0]=='4')
{
//流水灯
(*vir_odr1_3)|=(1<<10);
(*vir_odr2)|=(1<<10);
(*vir_odr1_3)|=(1<<8);
delay(500);
(*vir_odr1_3)&=(~(1<<10));
delay(500);
(*vir_odr2)&=(~(1<<10));
delay(500);
(*vir_odr1_3)&=(~(1<<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);
//LED1和LED3相关寄存器
vir_moder1_3=ioremap(PHY_LED1_3_MODER,4);
if(vir_moder1_3==NULL)
{
printk("MODER寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
vir_odr1_3=ioremap(PHY_LED1_3_ODR,4);
if(vir_odr1_3==NULL)
{
printk("ODR寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
//LED2
vir_moder2=ioremap(PHY_LED2_MODER,4);
if(vir_moder2==NULL)
{
printk("MODER寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
vir_odr2=ioremap(PHY_LED2_ODR,4);
if(vir_odr2==NULL)
{
printk("ODR寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
//RCC
vir_rcc=ioremap(PHY_LED_RCC,4);
if(vir_rcc==NULL)
{
printk("RCC寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
printk("寄存器映射成功\n");
//寄存器初始化
(*vir_rcc)|=(1<<4); //E
(*vir_rcc)|=(1<<5); //F
(*vir_moder1_3)&=(~(3<<20)); //PE10脚
(*vir_moder1_3)|=(1<<20);
(*vir_moder1_3)&=(~(3<<16)); //PE8脚
(*vir_moder1_3)|=(1<<16);
(*vir_moder2)&=(~(3<<20)); //PF10脚
(*vir_moder2)|=(1<<20);
(*vir_odr1_3)&=(~(1<<10));
(*vir_odr1_3)&=(~(1<<8));
(*vir_odr2)&=(~(1<<10));
printk("寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_moder1_3);
iounmap(vir_odr1_3);
iounmap(vir_moder2);
iounmap(vir_odr2);
iounmap(vir_rcc);
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
的亮灭