思维导图
练习
实现设备文件和设备的绑定,编写LED驱动
head.h
#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 ORD;
}gpio_t;#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define RCC 0x50000A28#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)
#endif
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include "head.h"unsigned int major; // 定义一个变量保存主设备号
char kbuf[128] = {0};struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
unsigned int *rcc;
gpio_t *led1;
gpio_t *led2;
gpio_t *led3;// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{// printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);minor = MINOR(inode->i_rdev); // 获取 操作的文件的次设备号file->private_data = (void *)minor; // 将次设备当作file对象的私有数据存放return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);int ret;if (size > sizeof(kbuf))size = sizeof(kbuf);ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user failed\n");return ret;}return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{int ret;if (size > sizeof(kbuf))size = sizeof(kbuf);ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user failed\n");return ret;}switch (kbuf[0]) // 选灯{case '1':if (kbuf[1] == '1') // 开灯led1->ORD |= (0x1 << 10);else if (kbuf[1] == '0') // 关灯led1->ORD &= (~(0x1 << 10));break;case '2':if (kbuf[1] == '1')led2->ORD |= (0x1 << 10);else if (kbuf[1] == '0')led2->ORD &= (~(0x1 << 10));break;case '3':if (kbuf[1] == '1')led3->ORD |= (0x1 << 8);else if (kbuf[1] == '0')led3->ORD &= (~(0x1 << 8));break;}return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{// printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{unsigned int which = (unsigned int)file->private_data; // 获取到open中得到的文件的次设备号;switch (which) // 根据次设备号的不同控制不同的LED灯{case 0: // LED1控制switch (cmd){case LED_ON: // LED1开灯led1->ORD |= (0x1 << 10);break;case LED_OFF: // LED1关灯led1->ORD &= (~(0x1 << 10));break;}break;case 1: // LED2控制switch (cmd){case LED_ON: // LED2开灯led2->ORD |= (0x1 << 10);break;case LED_OFF: // LED2关灯led2->ORD &= (~(0x1 << 10));break;}break;case 2: // LED3switch (cmd){case LED_ON: // LED3开灯led3->ORD |= (0x1 << 8);break;case LED_OFF: // LED3关灯led3->ORD &= (~(0x1 << 8));break;}break;}return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {.open = mycdev_open,.release = mycdev_close,.read = mycdev_read,.write = mycdev_write,.unlocked_ioctl = mycdev_ioctl,
};// LED初始化
int all_led_init(void)
{// 完成硬件寄存器物理内存的映射led1 = ioremap(GPIOE, sizeof(gpio_t));if (led1 == NULL){printk("led1物理内存映射失败\n");return -EFAULT;}led2 = ioremap(GPIOF, sizeof(gpio_t));if (led2 == NULL){printk("led2物理内存映射失败\n");return -EFAULT;}led3 = led1;rcc = ioremap(RCC, 4);if (rcc == NULL){printk("rcc物理内存映射失败\n");return -EFAULT;}printk("物理内存映射成功\n");// rcc使能(*rcc) |= (0x3 << 4);// 硬件寄存器初始化led1->MODER &= (~(0x3 << 20));led1->MODER |= (0x1 << 20);led2->MODER &= (~(0x3 << 20));led2->MODER |= (0x1 << 20);led3->MODER &= (~(0x3 << 16));led3->MODER |= (0x1 << 16);// 默认关灯led1->ORD &= (~(0x1 << 10));led2->ORD &= (~(0x1 << 10));led3->ORD &= (~(0x1 << 8));return 0;
}static int __init mycdev_init(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 = register_chrdev_region(MKDEV(major, minor), 3, "mycdev");if (ret){printk("静态指定设备号失败\n");goto OUT2;}}else{ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");if (ret){printk("动态指定设备号失败\n");goto OUT2;}minor = MINOR(devno); // 根据设备号获取次设备号major = MAJOR(devno); // 根据设备号获取主设备号}printk("申请设备号成功\n");// 4.注册字符设备驱动对象ret = cdev_add(cdev, MKDEV(major, minor), 3);if (ret){printk("注册字符设备驱动对象失败\n");goto OUT3;}printk("注册字符设备驱动对象成功\n");// 5.向上提交设备目录cls = class_create(THIS_MODULE, "mycdev");if (IS_ERR(cls)){printk("向上提交目录失败\n");ret = -PTR_ERR(cls);goto OUT4;}printk("向上提交目录成功\n");// 6.向上提交设备节点信息int i;for (i = 0; i < 3; i++){dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);if (IS_ERR(dev)){printk("向上提交设备节点信息失败\n");ret = -PTR_ERR(dev);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 mycdev_exit(void)
{// 取消物理内存的映射iounmap(rcc);iounmap(led1);iounmap(led2);// 销毁设备节点信息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");
test.c
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "head.h"int main(int argc, char const *agrv[])
{int a, b, fd;while (1){LAB1:printf("请输入要控制的灯: 1(LED1) 2(LED2) 3(LED3) 4(退出) >");scanf("%d", &a);switch (a){case 1:fd = open("/dev/mycdev0", O_RDWR);break;case 2:fd = open("/dev/mycdev1", O_RDWR);break;case 3:fd = open("/dev/mycdev2", O_RDWR);break;case 4:return 0;default:printf("输入错误,请重新输入!\n");goto LAB1;}if (fd < 0){printf("打开设备文件失败\n");exit(-1);}while (1){LAB2:printf("请输入要实现的功能: 0(关灯) 1(开灯) 2(重选灯) >");scanf("%d", &b);switch (b){case 0:ioctl(fd, LED_OFF);break;case 1:ioctl(fd, LED_ON);break;case 2:close(fd);goto LAB1;default:printf("输入错误,请重新输入!\n");goto LAB2;}}}return 0;
}
实验现象