#include
#include
#include
#include
#include
#include "head.h"
//定义变量存放映射的虚拟地址
//LED1
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
//LED2
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led2_rcc;
//LED3
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_led3_rcc;
int major;
char kbuf[128]={0};
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 *off)
{
int ret;
if(size>sizeof(kbuf)) //size传过来的是sizeof(ubuf),如果ubuf>kbuf,那么实际长度为kbuf
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy to user failed...\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 *off)
{
int ret;
if(size>sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy from user failed...\n");
return -EIO;
}
switch(kbuf[0])
{
case '0':
(*vir_odr) &= (~(1 << 10));
break;
case '1':
(*vir_odr) |= (1 << 10);
break;
case '2':
(*vir_led2_odr) &= (~(1 << 10));
break;
case '3':
(*vir_led2_odr) |= (1 << 10);
break;
case '4':
(*vir_led3_odr) &= (~(1 << 8));
break;
case '5':
(*vir_led3_odr) |= (1 << 8);
break;
}
/*
if(kbuf[0] == '1')
(*vir_odr) |= (1 << 10); //亮
else if(kbuf[0] == '0')
(*vir_odr) &= (~(1 << 10)); //灭
*/
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
vir_moder = ioremap(PHY_LED1_MODER, 4);
if(vir_moder == NULL)
{
printk("LED1_MODER寄存器映射失败...\n");
return -ENOMEM;
}
vir_odr = ioremap(PHY_LED1_ODR, 4);
if(vir_odr == NULL)
{
printk("LED1_ODR寄存器映射失败...\n");
return -ENOMEM;
}
vir_rcc = ioremap(PHY_LED1_RCC, 4);
if(vir_rcc == NULL)
{
printk("LED1_RCC寄存器映射失败...\n");
return -ENOMEM;
}
//LED2
vir_led2_moder = ioremap(PHY_LED2_MODER, 4);
if(vir_led2_moder == NULL)
{
printk("LED2_MODER寄存器映射失败...\n");
return -ENOMEM;
}
vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
if(vir_led2_odr == NULL)
{
printk("LED2_ODR寄存器映射失败...\n");
return -ENOMEM;
}
vir_led2_rcc = ioremap(PHY_LED1_RCC, 4);
if(vir_led2_rcc == NULL)
{
printk("LED2_RCC寄存器映射失败...\n");
return -ENOMEM;
}
printk("LED2寄存器初始化成功...\n");
//LED3
vir_led3_moder = ioremap(PHY_LED1_MODER, 4);
if(vir_led3_moder == NULL)
{
printk("LED3_MODER寄存器映射失败...\n");
return -ENOMEM;
}
vir_led3_odr = ioremap(PHY_LED1_ODR, 4);
if(vir_led3_odr == NULL)
{
printk("LED3_ODR寄存器映射失败...\n");
return -ENOMEM;
}
vir_led3_rcc = ioremap(PHY_LED1_RCC, 4);
if(vir_led3_rcc == NULL)
{
printk("LED3_RCC寄存器映射失败...\n");
return -ENOMEM;
}
printk("物理地址映射成功...\n");
//初始化LED1寄存器
(*vir_moder) &= (~(3 << 20));
(*vir_moder) |= (1 << 20); //设置为输出模式
(*vir_rcc) |= (1 << 4); //rcc使能
(*vir_odr) &= (~(1 << 10)); //低电平,灭
printk("LED1寄存器初始化成功...\n");
//初始化LED2寄存器
(*vir_led2_moder) &= (~(3 << 20));
(*vir_led2_moder) |= (1 << 20); //设置为输出模式
(*vir_led2_rcc) |= (1 << 5); //rcc使能
(*vir_led2_odr) &= (~(1 << 10)); //低电平,灭
//初始化LED3寄存器
(*vir_led3_moder) &= (~(3 << 16));
(*vir_led3_moder) |= (1 << 16); //设置为输出模式
(*vir_led3_rcc) |= (1 << 4); //rcc使能
(*vir_led3_odr) &= (~(1 << 8)); //低电平,灭
printk("LED3寄存器初始化成功...\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
iounmap(vir_led2_rcc);
iounmap(vir_led3_moder);
iounmap(vir_led3_odr);
iounmap(vir_led3_rcc);
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
#define PHY_LED2_ODR 0x50007014
#define PHY_LED2_MODER 0x50007000
#endif
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char *argv[])
{
char buf[128]={0};
int fd = open("/dev/mycdev", O_RDWR);
if(fd<0)
{
printf("error...\n");
exit(-1);
}
while(1)
{
printf("请输入:关灯(0), 开灯(1)...");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}