k1.c
#include
#include
#include
#include
#include
#include "head.h"
char kbuf[128] = {};
unsigned int major;
//定义三个指针指向映射后的虚拟内存
unsigned int *vir_rcc;
unsigned int *vir_moder_led1;
unsigned int *vir_odr_led1;
unsigned int *vir_moder_led2;
unsigned int *vir_odr_led2;
unsigned int *vir_moder_led3;
unsigned int *vir_odr_led3;
// 封装操作方法
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)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret=0;
ret = copy_to_user(ubuf,kbuf,size);
if(ret = 0)
{
printk("copy_to_user 内核->用户 filed!\n");
return -EIO;
}
//printk("copy_to_user 内核->用户 success!,ubuf=%s\n",ubuf);
printk("copy_to_user 内核->用户 success!\n");
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
int ret=0;
ret = copy_from_user(kbuf,ubuf,size);
if(ret = 0)
{
printk("copy_from_user 用户->内核 filed!\n");
return -EIO;
}
//printk("copy_from_user 用户->内核 success!,ubuf=%s\n",ubuf);
printk("copy_from_user 用户->内核 success!\n");
if(kbuf[0] == '0') //关灯
{
(*vir_odr_led1) &= (~(0x1 << 10));
(*vir_odr_led2) &= (~(0x1 << 10));
(*vir_odr_led3) &= (~(0x1 << 8));
}
else if(kbuf[0] == '1') //开灯
{
(*vir_odr_led1) |= (0x1 << 10);
(*vir_odr_led2) |= (0x1 << 10);
(*vir_odr_led3) |= (0x1 << 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,"mychrdev",&fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
//进行寄存器的地址映射
vir_rcc = ioremap(PHY_RCC,4);
if(vir_rcc == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_moder_led1 = ioremap(PHY_LED1_MODER,4);
if(vir_moder_led1 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_odr_led1 = ioremap(PHY_LED1_ODR,4);
if(vir_odr_led1 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_moder_led2 = ioremap(PHY_LED2_MODER,4);
if(vir_moder_led2 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_odr_led2 = ioremap(PHY_LED2_ODR,4);
if(vir_odr_led2 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_moder_led3 = ioremap(PHY_LED3_MODER,4);
if(vir_moder_led3 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
vir_odr_led3 = ioremap(PHY_LED3_ODR,4);
if(vir_odr_led3 == NULL)
{
printk("物理内存映射filed! %d\n",__LINE__);
return -EFAULT;
}
printk("物理内存映射成功!\n");
//LED1寄存器初始化
(*vir_rcc) |= (0x3 << 4); //GPIOE控制器时钟使能
(*vir_moder_led1) &= (~(0x3 << 20)); //MODER[21:20] => 00
(*vir_moder_led1) |= (0x1 << 20); //MODER[21:20] => 01
(*vir_odr_led1) &= (~(0x1 << 10)); //默认为关灯
(*vir_moder_led2) &= (~(0x3 << 20)); //MODER[21:20] => 00
(*vir_moder_led2) |= (0x1 << 20); //MODER[21:20] => 01
(*vir_odr_led2) &= (~(0x1 << 10)); //默认为关灯
(*vir_moder_led3) &= (~(0x3 << 16)); //MODER[17:16] => 00
(*vir_moder_led3) |= (0x1 << 16); //MODER[17:16] => 01
(*vir_odr_led3) &= (~(0x1 << 8)); //默认为关灯
return 0;
}
static void __exit mycdev_exit(void)
{
//取消内存映射
iounmap(vir_rcc);
iounmap(vir_moder_led1);
iounmap(vir_odr_led1);
iounmap(vir_moder_led2);
iounmap(vir_odr_led2);
iounmap(vir_moder_led3);
iounmap(vir_odr_led3);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include
#include
#include
#include
#include
#include
int main(int argc,const char * argv[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev",O_RDWR);
if(fd < 0)
{
printf("文件/dev/mychrdev打开失败\n");
return -1;
}
printf("文件/dev/mychrdev打开成功\n");
while(1)
{
printf("输入要进行的操作=> 0(关灯) <-> 1(开灯) : ");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//User -> 内核
write(fd,buf,sizeof(buf));
}
/*
//User -> 内核
write(fd,buf,sizeof(buf));
//内核 -> User
read(fd,buf,sizeof(buf));
*/
close(fd);
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_RCC 0x50000A28
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
#endif