实现三盏灯的控制,编写应用程序测试
- #ifndef __HEAD_H__
- #define __HEAD_H__
-
- //PE10
- #define LED1_RCC 0X50000A28
- #define LED1_MODER 0X50006000
- #define LED1_ODR 0X50006014
-
- //PF10
- #define LED2_RCC 0X50000A28
- #define LED2_MODER 0X50007000
- #define LED2_ODR 0X50007014
-
- #endif
- #include
- #include
- #include
- #include
- #include "head.h"
- #include
-
-
- char kbuf[128] = {0};
-
- unsigned int *vir_rcc;
- unsigned int *vir_moder;
- unsigned int *vir_odr;
-
- unsigned int *vir_rcc_f;
- unsigned int *vir_moder_f;
- unsigned int *vir_odr_f;
-
- //定义操作方法
- int mychrdev_open(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
-
- ssize_t mychrdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
- {
- int ret;
- ret = copy_to_user(ubuf,kbuf,sizeof(kbuf));
- if(ret)
- {
- printk("copy_to_user err\n");
- return -EIO;
- }
-
- return 0;
- }
-
- ssize_t mychrdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
- {
- int ret;
- ret = copy_from_user(kbuf,ubuf,sizeof(ubuf));
- if(ret)
- {
- printk("copy_from_user err\n");
- return -EIO;
- }
-
- //PE10
- if(kbuf[0] == '1') //开灯
- {
- //开灯逻辑
- (*vir_odr) |= (0x1 << 10);
- }
- else if(kbuf[0] == '0') //关灯
- {
- //关灯逻辑
- (*vir_odr) &= (~(0x1 << 10));
- }
-
- //PF10
- if(kbuf[1] == '1') //开灯
- {
- //开灯逻辑
- (*vir_odr_f) |= (0x1 << 10);
- printk("%d\n",__LINE__);
- }
- else if(kbuf[1] == '0') //关灯
- {
- //关灯逻辑
- (*vir_odr_f) &= (~(0x1 << 10));
- }
-
- //PE8
- if(kbuf[2] == '1') //开灯
- {
- //开灯逻辑
- (*vir_odr) |= (0x1 << 8);
- }
- else if(kbuf[2] == '0') //关灯
- {
- //关灯逻辑
- (*vir_odr) &= (~(0x1 << 8));
- }
-
- return 0;
- }
-
- int mychrdev_close(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
-
- //定义存放主设备号变量
- unsigned int major;
-
- //定义操作方法的结构体变量
- struct file_operations fops={
- .open = mychrdev_open,
- .read = mychrdev_read,
- .write = mychrdev_write,
- .release = mychrdev_close
- };
-
- static int __init mycdev_init(void)
- {
- //字符设备驱动注册
- major = register_chrdev(0,"mychrdev",&fops);
- if(major < 0)
- {
- printk("字符设备注册失败\n");
- }
- printk("字符设备注册成功:%d\n",major);
-
- //相关寄存器的内存映射
- //GPIOE
- vir_rcc = ioremap(LED1_RCC,4);
- if(vir_rcc == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- vir_moder = ioremap(LED1_MODER,4);
- if(vir_moder == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- vir_odr = ioremap(LED1_ODR,4);
- if(vir_odr == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- //GOIOF
- vir_rcc = ioremap(LED2_RCC,4);
- if(vir_rcc == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- vir_moder_f = ioremap(LED2_MODER,4);
- if(vir_moder_f == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- vir_odr_f = ioremap(LED2_ODR,4);
- if(vir_odr_f == NULL)
- {
- printk("物理地址映射失败%d\n",__LINE__);
- }
-
- printk("物理地址映射成功\n");
-
- //相关寄存器的初始化
- //RCC寄存器
- (*vir_rcc) |= (0x1 << 4);
-
- //GPIOE_MODER寄存器(PE10)
- (*vir_moder) &= (~(0x3 << 20));
- (*vir_moder) |= (0x1 << 20);
- //GPIOE_ODR寄存器
- (*vir_odr) &= (~(0x1 << 10));
-
- //RCC寄存器(PF10)
- (*vir_rcc) |= (0x1 << 5);
- //GPIOF_MODER寄存器
- (*vir_moder_f) &= (~(0x3 << 20));
- (*vir_moder_f) |= (0x1 << 20);
- //GPIOF_ODR寄存器
- (*vir_odr_f) &= (~(0x1 << 10));
-
- //GPIOE_MODER寄存器(PE8)
- (*vir_moder) &= (~(0x3 << 16));
- (*vir_moder) |= (0x1 << 16);
- //GPIOE_ODR寄存器
- (*vir_odr) &= (~(0x1 << 8));
-
- return 0;
- }
-
- static void __exit mycdev_exit(void)
- {
- //取消物理地址映射
- iounmap(vir_rcc);
- iounmap(vir_moder);
- iounmap(vir_odr);
-
- iounmap(vir_rcc_f);
- iounmap(vir_moder_f);
- iounmap(vir_odr_f);
-
- //字符设备驱动注销
- unregister_chrdev(major,"mychrdev");
- }
-
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- int main(int args,char const *argv[])
- {
- char buf[128] = {0};
- int fd = open("/dev/mychrdev",O_RDWR);
-
- if(fd < 0)
- {
- printf("打开文件失败\n");
- exit(-1);
- }
-
- while(1)
- {
- printf("输入指令控制开/关灯(LED1|LED2|LED3)----0(OFF)、1(ON)>");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1] = '\0';
- write(fd,buf,sizeof(buf));
- }
-
- close(fd);
-
- return 0;
- }
111->LED1,LED2,LED3全部点亮
010->LED1,LED3熄灭 LED2点亮