编写LED灯的驱动,创建三个设备文件,每个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制设备文件对应的这盏灯

.编写LED灯的驱动,创建三个设备文件,每个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制设备文件对应的这盏灯

实验现象

在这里插入图片描述

test.c

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"

void LED_control()
{
    int a, num, fd;
    while (1)
    {
        printf("请输入操作的设备文件:0(LED1) 1(LED2) 2(LED3) 其他字符(重新选择设备文件)>>>");
        scanf("%d", &num);
        switch (num)
        {
        case 0:
            fd = open("/dev/myled0", O_RDWR);
            break;
        case 1:
            fd = open("/dev/myled1", O_RDWR);
            break;
        case 2:
            fd = open("/dev/myled2", O_RDWR);
            break;
        default:
            printf("输入有误,请重新输入!\n");
            break;
        }
        if (!(num == 0 || num == 1 || num == 2))
        {
            continue;
        }
        if (fd < 0)
        {
            printf("打开led设备文件失败\n");
            exit(-1);
        }
        while (1)
        {
            // 从终端读取
            printf("请选择要实现的功能\n");
            printf("0(关灯) 1(开灯) 2(重新选择开关灯功能) 其他字符(重新选择设备文件)>>>");
            scanf("%d", &a);
            if (a == 1) // 开灯
            {
                ioctl(fd, LED_ON, &num);
            }
            else if (a == 0) // 关灯
            {
                ioctl(fd, LED_OFF, &num);
            }
            else if (a == 2)
            {
                continue;
            }
            else
                break;
        }
        close(fd);
    }
}

int main(int argc, char const *argv[])
{
    LED_control();
    return 0;
}

my_chardev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/slab.h>
#include "head.h"

struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0; // 次设备号的起始值
dev_t devno;
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)
{
    unsigned int aaa=MINOR(inode->i_rdev);//得到打开的文件对应的设备号
    file->private_data=(void *)aaa;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    unsigned int aaa=(int)file->private_data;
   switch(aaa)
   {
       case 0:
           //控制LED1的逻辑
            switch(cmd)
            {
                case LED_ON:
                    vir_led1->ODR |= (0x1 << 10);
                    break;
                case LED_OFF:
                    vir_led1->ODR &= (~(0X1 << 10));
                    break;
            }
           break;   
        case 1:
           //控制LED2的逻辑
           switch(cmd)
            {
                case LED_ON:
                    vir_led2->ODR |= (0x1 << 10);
                    break;
                case LED_OFF:
                    vir_led2->ODR &= (~(0X1 << 10));
                    break;
            }
           break; 
        case 2:
           //控制LED3的逻辑
           switch(cmd)
            {
                case LED_ON:
                    vir_led3->ODR |= (0x1 << 8);
                    break;
                case LED_OFF:
                    vir_led3->ODR &= (~(0X1 << 8));
                    break;
            }
           break; 
   }
    return 0;
}

int all_led_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);
    // led1
    vir_led1->MODER &= (~(3 << 20));
    vir_led1->MODER |= (1 << 20);
    vir_led1->ODR &= (~(1 << 10));
    // led2
    vir_led2->MODER &= (~(3 << 20));
    vir_led2->MODER |= (1 << 20);
    vir_led2->ODR &= (~(1 << 10));
    // led3
    vir_led3->MODER &= (~(3 << 16));
    vir_led1->MODER |= (1 << 16);
    vir_led1->ODR &= (~(1 << 8));
    printk("寄存器初始化成功\n");
 
    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)
{
    int ret, i;
    // 1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("分配字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("字符设备驱动对象申请成功\n");
    // 2.字符设备驱动对象部分初始化   cdev_init
    cdev_init(cdev, &fops);
    // 3.申请设备号      register_chrdev_region/alloc_chrdev_region
    if (major > 0) // 静态申请设备号
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "myled");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major = MAJOR(devno); // 根据设备号得到主设备号
        minor = MINOR(devno); // 根据设备号得到次设备号
    }
    printk("申请设备号成功\n");
    // 4.注册字符设备驱动对象  cdev_add()
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");
    // 5.向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");
    // 6.向上提交设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%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)
{
    // 1.销毁设备信息  device_destroy
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 2.销毁目录  class_destroy
    class_destroy(cls);
    // 3.注销对象  cdev_del()
    cdev_del(cdev);
    // 4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 5.释放对象空间  kfree()
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 
typedef struct{
    volatile unsigned int MODER;   // 0x00
	volatile unsigned int OTYPER;  // 0x04
	volatile unsigned int OSPEEDR; // 0x08
	volatile unsigned int PUPDR;   // 0x0C
	volatile unsigned int IDR;     // 0x10
	volatile unsigned int ODR;     // 0x14
	volatile unsigned int BSRR;    // 0x18
	volatile unsigned int LCKR;    // 0x1C 
	volatile unsigned int AFRL;    // 0x20 
	volatile unsigned int AFRH;    // 0x24
	volatile unsigned int BRR;     // 0x28
	volatile unsigned int res;
	volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR    0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR    0X50000A28
#define GPIOB 0X50003000
#define GPIOE 0X50006000
#define GPIOF 0X50007000
 
 
#define LED_ON _IOW('l',1,int)  //开灯
#define LED_OFF _IOW('l',0,int)//关灯


#endif 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值