head.h
- #ifndef __HEAD__H__
- #define __HEAD__H__
- typedef struct{
- unsigned int MODER;
- unsigned int OTYPER;
- unsigned int OSPEEDR;
- unsigned int PURDR;
- unsigned int IDR;
- unsigned int ODR;
- }gpio_t;
- #define PHY_LED1 0x50006000
- #define PHY_LED2 0x50007000
- #define PHY_LED3 0x50006000
- #define PHY_RCC 0x50000A28
- #define LED_ON _IOW('l',1,int)
- #define LED_OFF _IOW('l',0,int)
- #endif
demo.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include "head.h"
-
- struct cdev *cdev;
- unsigned int major=0;
- unsigned int minor=0;
- struct class *cls;
- struct device *dev;
- dev_t devno;
- gpio_t *vir_led1;
- gpio_t *vir_led2;
- gpio_t *vir_led3;
- unsigned int *vir_rcc;
-
- //封装操作方法
- int mycdev_open(struct inode *inode,struct file *file)
- {
- int min=MINOR(inode->i_rdev);
- file->private_data=(void *)min;
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
- }
- long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- {
- int min=(int)file->private_data;
- switch(min)
- {
- case 0:
- switch(cmd)
- {
- case LED_ON:
- (vir_led1->ODR) |= (0x1<<10);
- break;
- case LED_OFF:
- (vir_led1->ODR) &= (~(0x1<<10));
- break;
- }
- break;
- case 1:
- switch(cmd)
- {
- case LED_ON:
- (vir_led2->ODR) |= (0x1<<10);
- break;
- case LED_OFF:
- (vir_led2->ODR) &= (~(0x1<<10));
- break;
- }
- break;
- case 2:
- switch(cmd)
- {
- case LED_ON:
- (vir_led3->ODR) |= (0x1<<8);
- break;
- case LED_OFF:
- (vir_led3->ODR) &= (~(0x1<<8));
- break;
- }
- break;
- }
- printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
- 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,
- */
- .unlocked_ioctl = mycdev_ioctl,
- .release=mycdev_close,
- };
-
- int all_led_init(void)
- {
- vir_led1=ioremap(PHY_LED1,sizeof(gpio_t));
- vir_led2=ioremap(PHY_LED2,sizeof(gpio_t));
- vir_led3=vir_led1;
- if(vir_led1==NULL||vir_led2==NULL)
- {
- printk("物理内存地址映射失败%d\n",__LINE__);
- return -EFAULT;
- }
- vir_rcc=ioremap(PHY_RCC,4);
- if(vir_rcc==NULL)
- {
- printk("物理内存地址映射失败%d\n",__LINE__);
- return -EFAULT;
- }
- printk("物理内存地址映射成功\n");
-
- (*vir_rcc) |= (0x1<<4);//GPIOE控制器时钟使能
- (*vir_rcc) |= (0x1<<5);
- //LED1寄存器初始化
- (vir_led1->MODER) &= (~(0x3<<20));
- (vir_led1->MODER) |= (0x1<<20);
- (vir_led1->ODR) &= (~(0x1<<10));//默认关灯
- //LED2寄存器初始化
- (vir_led2->MODER) &= (~(0x3<<20));
- (vir_led2->MODER) |= (0x1<<20);
- (vir_led2->ODR) &= (~(0x1<<10));//默认关灯
- //LED3寄存器初始化
- (vir_led3->MODER) &= (~(0x3<<16));
- (vir_led3->MODER) |= (0x1<<16);
- (vir_led3->ODR) &= (~(0x1<<8));//默认关灯
- printk("寄存器初始化成功\n");
- return 0;
- }
-
- static int __init s1(void)
- {
- //1.申请字符设备驱动对象
- int ret;
- cdev=cdev_alloc();
- if(cdev==NULL)
- {
- printk("申请字符设备驱动对象失败\n");
- ret=-EFAULT;
- goto out1;
- }
- printk("字符设备驱动对象申请成功\n");
- //2.字符设备部分初始化
- cdev_init(cdev,&fops);
- //3.申请设备号
- if(major==0)//动态申请
- {
- ret=alloc_chrdev_region(&devno,minor,3,"mychrdev");
- if(ret)
- {
- printk("动态申请设备号失败");
- goto out2;
- }
- major=MAJOR(devno);
- minor=MINOR(devno);
- }
- else//静态申请设备号
- {
- ret=register_chrdev_region(MKDEV(major,minor),3,"mychrdev");
- if(ret)
- {
- printk("静态申请设备号失败\n");
- goto out2;
- }
- }
- printk("申请设备号成功\n");
- //注册字符设备驱动对象
- ret=cdev_add(cdev,MKDEV(major,minor),3);
- if(ret)
- {
- printk("注册字符设备驱动对象失败\n");
- goto out3;
- }
- printk("注册字符设备驱动对象成功\n");
- //向上提交目录
- cls=class_create(THIS_MODULE,"mychredv");
- if(IS_ERR(cls))
- {
- printk("向上提交目录失败\n");
- goto out4;
- }
- printk("向上提交目录成功\n");
- //向上提交设备节点信息
- int i=0;
- for(i=0;i<3;i++)
- {
- dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
- if(IS_ERR(dev))
- {
- printk("向上提交设备节点信息失败\n");
- goto out5;
- }
- }
- printk("向上提交设备节点信息成功\n");
- //寄存器映射以及初始化
- all_led_init();
- return 0;
- out5:
- for(--i;i>=0;i--)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- class_destroy(cls);
- out4:
- cdev_del(cdev);
- out3:
- unregister_chrdev_region(MKDEV(major,minor),3);
- out2:
- kfree(cdev);
- out1:
- return ret;
- }
- static void __exit s2(void)
- {
- //取消地址映射
- iounmap(vir_led1);
- iounmap(vir_led2);
- iounmap(vir_led3);
- iounmap(vir_rcc);
- //注销字符设备及驱动
- int i;
- for(i=0;i<3;i++)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- class_destroy(cls);
- cdev_del(cdev);
- unregister_chrdev_region(MKDEV(major,minor),3);
- kfree(cdev);
- }
- module_init(s1);
- module_exit(s2);
- MODULE_LICENSE("GPL");
test.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include "head.h"
- int main(int argc,char const *argv[])
- {
- char buf[128]={0};
- int a;
- int fd=open("/dev/myled0",O_RDWR);
- if(fd<0)
- {
- printf("打开设备文件失败\n");
- return -1;
- }
- printf("打开设备文件成功\n");
- while(1)
- {
- printf("请输入要进行的操作:0(关灯)1(开灯)");
- printf("请输入 >");
- scanf("%d",&a);
- switch(a)
- {
- case 1:
- ioctl(fd,LED_ON);
- break;
- case 0:
- ioctl(fd,LED_OFF);
- break;
- }
- /*
- fgets(buf,sizeof(buf),stdin);//从终端读取一个字符串
- buf[strlen(buf)-1]='\0';
- write(fd,buf,sizeof(buf));
- memset(buf,0,sizeof(buf));
- */
- }
- close(fd);
- return 0;
- }