使用结构体形式对寄存器地址进行映射,实现3盏LED灯的循环点亮
1、创建LED灯点亮所需要的GPIO寄存器的结构体,并对寄存器地址进行宏定义
2、①通过ioremap函数将物理地址映射为虚拟地址
void* ioremap(phys_addr_t offset, size_t size)
函数功能:将物理地址映射为虚拟地址
参数:
@offset:物理地址
@size:映射大小,单位字节
返回值: 成功返回虚拟地址,失败返回NULL
②通过映射的虚拟地址对寄存器进行初始化
3、通过copy_from_user函数从用户空间读取信息,对要进行操作的灯进行判断,并对相应寄存器进行操作
int copy_from_user(void *to, const void __user volatile *from, unsigned long n)
函数功能:将数据从用户空间拷贝到内核空间
参数:
@to:内核空间首地址
@from:用户空间首地址
@ n:拷贝数据大小(以字节为单位)
返回值: 成功返回0 失败返回未拷贝的字节数
4、通过iounmap取消对寄存器的映射
void iounmap(void __iomem *addr)
函数功能:取消映射
参数:
@offset:映射之后的虚拟地址
返回值: 无
附实现代码
测试代码:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- char buf[128] = {0};
-
- int main(int argc, const char *argv[])
- {
- int fd = -1;
- int i=0;
- fd = open("/dev/led",O_RDWR);
- if(-1 == fd)
- {
- perror("open is error");
- exit(1);
- }
- while(1)
- {
- buf[0]='1';
- buf[1]='1';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[1]='0';
- write(fd,buf,sizeof(buf));
- sleep(1);
-
- buf[0]='2';
- buf[1]='1';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[1]='0';
- write(fd,buf,sizeof(buf));
- sleep(1);
-
- buf[0]='3';
- buf[1]='1';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[1]='0';
- write(fd,buf,sizeof(buf));
- }
- close(fd);
-
- return 0;
- }
- #ifndef __LED_H__
- #define __LED_H__
-
- typedef struct{
- volatile unsigned int MODER;
- volatile unsigned int OTYPER;
- volatile unsigned int OSPEEDR;
- volatile unsigned int PUPDR;
- volatile unsigned int IDR;
- volatile unsigned int ODR;
- }gpio_t;
-
- #define GPIOE_ADDR 0x50006000
- #define GPIOF_ADDR 0x50007000
- #define RCC_ADDR 0x50000A28
-
-
-
- #endif
功能代码
- #include
- #include
- #include
- #include
- #include
- #include"./led.h"
-
- #define GNAME "mydev"
- volatile gpio_t* VIRT_GPIOE;
- volatile gpio_t* VIRT_GPIOF;
- volatile unsigned int* VIRT_RCC;
- int major;
- char kbuf[128]={0};
- int mydev_open(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- ssize_t mydev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
- {
- int set;
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- if(size > sizeof(kbuf)) size = sizeof(kbuf);
- set = copy_to_user(ubuf,kbuf,size);
- if(set)
- {
- printk("copy to user is error\n");
- return -EIO;
- }
- return size;
- }
- ssize_t mydev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
- {
- int ret;
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- if(size > sizeof(kbuf)) size = sizeof(kbuf);
- ret = copy_from_user(kbuf,ubuf,size);
- if(ret)
- {
- printk("copy from user is error\n");
- return -EIO;
- }
- printk("copy from user kbuf = %s\n",kbuf);
- switch(kbuf[0])
- {
- case '1':
- if(kbuf[1]=='1')
- {
- VIRT_GPIOE->ODR |= (0x1<<10);
- }
- else if(kbuf[1]=='0')
- {
- VIRT_GPIOE->ODR &= (~(0x1<<10));
- }
- break;
- case '3':
- if(kbuf[1]=='1')
- {
- VIRT_GPIOE->ODR |= (0x1<<8);
- }
- else if(kbuf[1]=='0')
- {
- VIRT_GPIOE->ODR &= (~(0x1<<8));
- }
- break;
- case '2':
- if(kbuf[1]=='1')
- {
- VIRT_GPIOF->ODR |= (0x1<<10);
- }
- else if(kbuf[1]=='0')
- {
- VIRT_GPIOF->ODR &= (~(0x1<<10));
- }
- break;
- }
-
- return size;
- }
- int mydev_close(struct inode *inode, struct file *file)
- {
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- struct file_operations fops={
- .open=mydev_open,
- .read=mydev_read,
- .write=mydev_write,
- .release=mydev_close,
- };
-
- static int __init mydev_init(void)
- {
- major=register_chrdev(0,GNAME,&fops);
- if(major<0)
- {
- printk("register file\n");
- return major;
- }
- printk("major=%d\n",major);
- VIRT_RCC = ioremap(RCC_ADDR,4);
- if(NULL == VIRT_RCC)
- {
- printk("VIRT_RCC error\n");
- return -ENXIO;
- }
- VIRT_GPIOE = ioremap(GPIOE_ADDR,4);
- if(NULL == VIRT_GPIOE)
- {
- printk("VIRT_GPIOE error\n");
- return -ENXIO;
- }
- VIRT_GPIOF = ioremap(GPIOF_ADDR,4);
- if(NULL == VIRT_GPIOF)
- {
- printk("VIRT_GPIOF error\n");
- return -ENXIO;
- }
- *VIRT_RCC |= (0x3<<4);
-
- VIRT_GPIOE->MODER &= (~(0x3<<20));
- VIRT_GPIOE->MODER |= (0x1<<20);
- VIRT_GPIOE->ODR &= (~(0x1<<10));
-
- VIRT_GPIOE->MODER &= (~(0x3<<16));
- VIRT_GPIOE->MODER |= (0x1<<16);
- VIRT_GPIOE->ODR &= (~(0x1<<8));
-
- VIRT_GPIOF->MODER &= (~(0x3<<20));
- VIRT_GPIOF->MODER |= (0x1<<20);
- VIRT_GPIOF->ODR &= (~(0x1<<10));
- return 0;
- }
-
- static void __exit mydev_exit(void)
- {
- unregister_chrdev(major,GNAME);
- iounmap(VIRT_GPIOE);
- iounmap(VIRT_GPIOF);
- iounmap(VIRT_RCC);
- }
-
- module_init(mydev_init);
- module_exit(mydev_exit);
-
- MODULE_LICENSE("GPL");
led