目的:通过字符设备驱动的分步实现编写LED驱动,另外实现特备文件和设备的绑定
驱动文件 :
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/io.h>
#include "head.h"struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
struct class *cls;
struct device *dev;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)
{printk("%s : %s : %d\n",__FILE__,__func__,__LINE__);int min = MINOR(inode->i_rdev);file->private_data = (void *)min;return 0;
}long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{printk("%s : %s : %d\n",__FILE__,__func__,__LINE__);int which = (int)file->private_data;printk("which = %d\n",which);switch(cmd){case LED_ON:switch(which){case 0://LED1vir_led1->ODR |= (0x1 << 10);break;case 1://LED2vir_led2->ODR |= (0x1 << 10);break;case 2://LED3vir_led3->ODR |= (0x1 << 8);break;}break;{case LED_OFF:switch(which){case 0://LED1vir_led1->ODR &= (~(0x1 << 10));break;case 1://LED2vir_led2->ODR &= (~(0x1 << 10));break;case 2://LED3vir_led3->ODR &= (~(0x1 << 8));break;}}break;}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,.unlocked_ioctl = mycdev_ioctl,.release = mycdev_close,
};static int __init mycdev_init(void)
{//寄存器地址的映射vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));if(vir_led1==NULL){printk("ioremap filed:%d\n",__LINE__);return -ENOMEM;}vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));if(vir_led2==NULL){printk("ioremap filed:%d\n",__LINE__);return -ENOMEM;}vir_led3=vir_led1;vir_rcc=ioremap(PHY_RCC_ADDR,4);if(vir_rcc==NULL){printk("ioremap filed:%d\n",__LINE__);return -ENOMEM;}printk("物理地址映射成功\n");//寄存器的初始化//rcc(*vir_rcc) |= (3<<4);//led1vir_led1->MODER &= (~(3<<20));vir_led1->MODER |= (1<<20);vir_led1->ODR &= (~(1<<10));//led2vir_led2->MODER &= (~(3<<20));vir_led2->MODER |= (1<<20);vir_led2->ODR &= (~(1<<10));//led3vir_led3->MODER &= (~(3<<16));vir_led3->MODER |= (1<<16);vir_led3->ODR &= (~(1<<8));printk("寄存器初始化成功\n");int ret;//申请一个对象空间cdev = cdev_alloc();if(cdev == NULL){printk("cdev_alloc failed\n");ret = -EFAULT;goto out1;}printk("cdev_alloc success\n");//初始化对象cdev_init(cdev,&fops);//申请设备号if(major == 0)//动态申请{dev_t devnu;ret = alloc_chrdev_region(&devnu,minor,3,"mydev");if(ret){printk("alloc_chrdev_region failed\n");goto out2;}major = MAJOR(devnu);minor = MINOR(devnu);printk("alloc_chrdev_region success\n");}else//静态申请{ret = register_chrdev_region(major,3,"mydev");if(ret){printk("register_chrdev_region failed\n");goto out2;}printk("register_chrdev_region success\n");}//注册驱动对象ret = cdev_add(cdev,MKDEV(major,minor),3);if(ret){printk("cdev_add failed\n");goto out3;}printk("cdev_add success\n");//向上提交目录cls = class_create(THIS_MODULE,"mychrdev");if(IS_ERR(cls)){printk("class_create failed\n");goto out4;}printk("class_create success\n");//向上提交节点信息int i;for(i=0;i<3;i++){dev = device_create(cls,NULL,MKDEV(major,i),NULL,"mychrdev%d",i);if(IS_ERR(dev)){printk("device_create failed\n");goto out5;}}printk("device_create success\n");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 mycdev_exit(void)
{//销毁设备节点信息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(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序 :
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "head.h"int main(int argc, char const *argv[])
{// 打开第一个设备文件int fd1 = open("/dev/mychrdev0", O_RDWR);if (fd1 < 0){printf("打开设备文件失败\n");exit(-1);}// 打开第二个设备文件int fd2 = open("/dev/mychrdev1", O_RDWR);if (fd2 < 0){printf("打开设备文件失败\n");exit(-1);}// 打开第三个设备文件int fd3 = open("/dev/mychrdev2", O_RDWR);if (fd3 < 0){printf("打开设备文件失败\n");exit(-1);}while (1){int a, b;// 从终端读取printf("请输入字符\n");printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");scanf("%d", &a);printf("第二个字符:0(关灯) 1(开灯)\n");printf("请输入>");scanf("%d", &b);// 向设备文件中写switch (b){case 1:switch (a){case 1: // LED1ioctl(fd1, LED_ON);break;case 2: // LED2ioctl(fd2, LED_ON);break;case 3: // LED3ioctl(fd3, LED_ON);break;}break;case 0:switch (a){case 1: // LED1ioctl(fd1, LED_OFF);break;case 2: // LED2ioctl(fd2, LED_OFF);break;case 3: // LED3ioctl(fd3, LED_OFF);break;}break;}}close(fd1);close(fd2);close(fd3);return 0;
}
头文件 :
#ifndef __HEAD_H__
#define __HEAD_H__ typedef struct{unsigned int MODER;unsigned int OTYPER;unsigned int OSPEEDR;unsigned int PUPDR;unsigned int IDR;unsigned int ODR;
}gpio_t;#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)#endif