实验目的:使用函数让设备文件和设备绑定,完成对LED的简单控制
在test.c中完成硬件逻辑控制
test.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
void LED_CONTROL()
{
int a, b, fd;
while (1)
{
printf("需要控制的灯1(LED1) 2(LED2) 3(LED3) > \n");
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;
default:
break;
}
if (fd < 0)
{
printf("打开设备失败\n");
exit(-1);
}
printf("实现功能:1(开灯) 0(关灯) > \n");
scanf("%d", &b);
switch (b)
{
case 1:
ioctl(fd, LED_ON, &a);
break;
case 0:
ioctl(fd, LED_OFF, &a);
break;
default:
break;
}
close(fd);
}
}
int main(int argc, const char *argv[])
{
LED_CONTROL();
return 0;
}
Character_device.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include "head.h"
unsigned int major = 0;
unsigned int minor = 0;
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct cdev *cdev;
dev_t devnum;
// 向上申请的结构体目录信息
struct class *cls;
// 向上申请的结构体节点信息
struct device *dev;
spinlock_t lock;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned int 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__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
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;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned int which = (unsigned int)file->private_data; // 获取open中得到的文件次设备号
int ret = copy_from_user(&which, (unsigned int *)arg, 4);
if (ret)
{
printk("copy_from_user is ERR\n");
return ret;
}
spin_lock(&lock);
switch (which)
{
case 1:
switch (cmd)
{
case LED_ON:
vir_led1->ODR |= 0x1 << 10;
break;
case LED_OFF:
vir_led1->ODR &= (~(0x1 << 10));
break;
default:
break;
}
break;
case 2:
switch (cmd)
{
case LED_ON:
vir_led2->ODR |= 0x1 << 10;
break;
case LED_OFF:
vir_led2->ODR &= (~(0x1 << 10));
break;
default:
break;
}
break;
case 3:
switch (cmd)
{
case LED_ON:
vir_led3->ODR |= 0x1 << 8;
break;
case LED_OFF:
vir_led3->ODR &= (~(0x1 << 8));
break;
default:
break;
}
break;
default:
break;
}
spin_unlock(&lock);
return 0;
}
// 寄存器地址映射
int all_led_init(void)
{
vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
if (vir_led1 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
if (vir_led2 == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_led3 = vir_led1;
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("物理内存映射失败%d\n", __LINE__);
return -EFAULT;
}
printk("物理内存映射成功\n");
// rcc
(*vir_rcc) |= 0x3 << 4;
// 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));
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.release = mycdev_close,
.read = mycdev_read,
.write = mycdev_write,
.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, "mycdev");
if (ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
else
{
ret = alloc_chrdev_region(&devnum, minor, 3, "mycdev");
if (ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
minor = MINOR(devnum);
major = MAJOR(devnum);
}
printk("申请设备号成功\n");
// 4.注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto OUT3;
}
printk("注册字符设备驱动对象成功\n");
// 寄存器映射函数
all_led_init();
// 向上提交目录信息
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录信息失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录信息成功\n");
// 向上提交设备信息
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(cls);
goto OUT5;
}
}
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_led1);
iounmap(vir_led2);
iounmap(vir_rcc);
// 销毁设备信息
int i;
for (i = 0; i < 3; i++)
device_destroy(cls, MKDEV(major, i));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动对象
unregister_chrdev_region(MKDEV(major, minor), 3);
// 释放空间对象
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
实现效果
串口查看:
实际现象: