头文件:
#ifndef __HEAD_H__
#define __HEAD_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 0x50006000
#define GPIOF 0x50007000
#define PHY_LED_RCC 0x50000A28
#define LED_ON _IO('l',1) //开灯
#define LED_OFF _IO('l',0) //关灯
#endif
驱动文件:
#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"
// extern int add(int i,int j);
char kbuf[128] = {0};
struct class *cls;
struct device *dev;
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
// 定义指针接收映射成功的虚拟内存首地址
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
unsigned int minor = MINOR(inode->i_rdev); // 获取操作的文件的次设备号
file->private_data = (void *)minor; // 将次设备号当作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中得到的文件的次设备号
printk("%d\n",which);
switch (which)
{
case 0: // LED1控制
switch (cmd)
{
case LED_ON:
vir_gpioe->ODR |= (0x1 << 10);
break;
case LED_OFF:
vir_gpioe->ODR &= (~(0x1 << 10));
break;
}
break;
case 1: // LED2控制
switch (cmd)
{
case LED_ON:
vir_gpiof->ODR |= (0x1 << 10);
break;
case LED_OFF:
vir_gpiof->ODR &= (~(0x1 << 10));
break;
}
break;
case 2: // LED3控制
switch (cmd)
{
case LED_ON:
vir_gpioe->ODR |= (0x1 << 8);
break;
case LED_OFF:
vir_gpioe->ODR &= (~(0x1 << 8));
break;
}
break;
}
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user failed\n");
return ret;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user failed\n");
return ret;
}
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,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
static int __init mycdev_init(void)
{
int ret;
// 1.分配字符设备驱动对象
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, "demo3");
if (ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 3, "demo3");
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("字符设备驱动注册成功major=%d\n", major);
// 物理内存映射首地址
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if (vir_gpioe == NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("物理内存映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(PHY_LED_RCC, 4);
if (vir_rcc == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
printk("物理内存映射成功\n");
// 硬件寄存器初始化
// gpioe、gpiof寄存器组使能
(*vir_rcc) |= (0x3 << 4);
// LED1设置---PE10
vir_gpioe->MODER &= (~(0x3 << 20));
vir_gpioe->MODER |= (0x1 << 20);
vir_gpioe->ODR &= (~(0x1 << 10));
// LED2设置---PF10
vir_gpiof->MODER &= (~(0x3 << 20));
vir_gpiof->MODER |= (0x1 << 20);
vir_gpiof->ODR &= (~(0x1 << 10));
// LED3设置---PE8
vir_gpioe->MODER &= (~(0x3 << 16));
vir_gpioe->MODER |= (0x1 << 16);
vir_gpioe->ODR &= (~(0x1 << 8));
// 创建设备节点文件
// 向上提交目录信息
cls = class_create(THIS_MODULE, "demo3");
if (IS_ERR(cls))
{
printk("向上提交目录信息失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功");
// 向上提交设备信息
int i;
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "demo%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备信息失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备信息成功\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)
{
// 取消物理内存的映射
iounmap(vir_gpioe);
iounmap(vir_gpiof);
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(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用文件:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
/* code */
char buf[128]={0};
int a;
int fd=open("/dev/demo0", O_RDWR);
if(fd<0){
printf("打开设备文件失败\n");
exit(-1);
}
printf("成功打开设备文件\n");
while(1){
printf("请输入LED控制方式:1(开灯)0(关灯)>");
scanf("%d",&a);
switch(a)
{
case 1:
ioctl(fd,LED_ON);
printf("1111\n");
break;
case 0:
ioctl(fd,LED_OFF);
break;
}
}
close(fd);
return 0;
}