首先在头函数led.h中声明gpio结构体和gpioe,gpiof,rcc的物理地址。
- #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 WL_GPIOE 0x50006000
- #define WL_GPIOF 0x50007000
- #define WL_RCC 0x50000a28
- #endif
在驱动程序中,定义gpio结构体类型的两个指针接受物理地址映射的虚拟地址,然后通过结构体来寻找对应的寄存器,而不用手动添加偏移量。
在init函数中申请驱动设备号,映射地址,使能并初始化灯。在write函数中接受来自用户程序的值,存入kbuf中。kbuf[0]中的值用以确定指定操作的小灯编号,kbud[1]中的值确定亮灭操作。
最后在exit函数中接触映射并注销字符设备驱动。
- #include
- #include
- #include
- #include
- #include
- #include"led.h"
- #define CNAME "myled"
- int major;
- volatile int* RCC=NULL;
- volatile gpio_t* GPIOE=NULL;
- volatile gpio_t* GPIOF=NULL;
- char kbuf[128]={0};
- int mydev_open (struct inode *inode, struct file *file)
- {
- return 0;
- }
- ssize_t mydev_read (struct file *file, char __user *ubuf, size_t size, loff_t *loffs)
- {
- int res;
- if(size>sizeof(kbuf))
- size=sizeof(kbuf);
- res=copy_to_user(ubuf,kbuf,size);
- if(res)
- {
- printk("copy to user error");
- return EIO;
- }
- return size;
- }
- ssize_t mydev_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
- {
- int res;
- if(size > sizeof(kbuf))
- size=sizeof(kbuf);
- res=copy_from_user(kbuf,ubuf,size);
- if(res)
- {
- printk("copy from user error\n");
- return EIO;
- }
- printk("copy over , kbuf=%s\n",kbuf);
- //接收后亮灯逻辑,kbuf[0]表示灯,kbuf[1]表示亮灭
- if(kbuf[0]=='1')
- {
- if(kbuf[1]=='1')
- {
- GPIOE->ODR|=(0x1<<10);
- }
- else if(kbuf[1]=='0')
- {
- GPIOE->ODR&=(~(0x1<<10));
- }
- }
- if(kbuf[0]=='2')
- {
- if(kbuf[1]=='1')
- {
- GPIOF->ODR|=(0x1<<10);
- }
- else if(kbuf[1]=='0')
- {
- GPIOF->ODR&=(~(0x1<<10));
- }
- }
- if(kbuf[0]=='3')
- {
- if(kbuf[1]=='1')
- {
- GPIOE->ODR|=(0x1<<8);
- }
- else if(kbuf[1]=='0')
- {
- GPIOE->ODR&=(~(0x1<<8));
- }
- }
- return size;
- }
- int mydev_close (struct inode *inode, struct file *file)
- {
- return 0;
- }
- const struct file_operations fops= {
- .open=mydev_open,
- .read=mydev_read,
- .write=mydev_write,
- .release=mydev_close,
- };
- static int __init demo_init(void)
- {
- major=register_chrdev(major,CNAME,&fops);
- if(major<0)
- {
- printk("register_chrdev error\n");
- return major;
- }
- printk("major=%d\n",major);
- //RCC映射
- RCC=ioremap(WL_RCC,4);
- if(NULL==RCC)
- {
- printk("rcc ioremap error\n");
- return ENOMEM;
- }
- //GPIOE映射
- GPIOE=ioremap(WL_GPIOE,sizeof(gpio_t));
- if(NULL==GPIOE)
- {
- printk("gpioE ioremap error\n");
- return ENOMEM;
- }
- //GPIOF映射
- GPIOF=ioremap(WL_GPIOF,sizeof(gpio_t));
- if(NULL==GPIOF)
- {
- printk("gpioF ioremap error\n");
- return ENOMEM;
- }
- //初始化灯
- *RCC|=(0x1<<4);
- *RCC|=(0x1<<5);
- GPIOE->MODER&=(~(0x3<<20));
- GPIOE->MODER|=(0x1<<20);
- GPIOF->MODER&=(~(0x3<<20));
- GPIOF->MODER|=(0x1<<20);
- GPIOE->MODER&=(~(0x3<<16));
- GPIOE->MODER|=(0x1<<16);
- return 0;
- }
- static void __exit demo_exit(void)
- {
- //解除映射
- iounmap(GPIOE);
- iounmap(GPIOF);
- iounmap(RCC);
- unregister_chrdev(major,CNAME);
- }
- module_init(demo_init);
- module_exit(demo_exit);
- MODULE_LICENSE("GPL");
在用户程序启动前先下载(insmod)驱动程序(.ko文件)并在/dev/下添加申请的驱动。
在调用open,write,read,等函数时会调用驱动代码中对应的函数。
通过在死循环中写入1灯亮,1灯灭,2灯亮,2灯灭,3灯亮,3灯灭的循环,并在每步操作后sleep休眠来观察现象。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- char buf[128]={""};
- int main(int argc, char const *argv[])
- {
- int fd=-1;
- fd=open("/dev/myled",O_RDWR);
- if(-1==fd)
- {
- perror("open error");
- return -1;
- }
- while (1)
- {
- buf[0]='1';
- buf[1]='1';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[0]='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[0]='2';
- buf[1]='0';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[0]='3';
- buf[1]='1';
- write(fd,buf,sizeof(buf));
- sleep(1);
- buf[0]='3';
- buf[1]='0';
- write(fd,buf,sizeof(buf));
- sleep(1);
- }
-
- close(fd);
- return 0;
- }
流水灯