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

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
//PE10-----led1
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
//pf10-----led2  
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED2_RCC 0x50000A28
//PE8-----led3
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
#define PHY_LED3_RCC 0x50000A28


//功能码
//LED灯
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)


#endif

main.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"

int main(int argc, char const *argv[])
{
    int a, c, fd;

    while (1)
    {
        printf("请输入要控制的/LED1/LED2/LED3/ >>>");
        scanf("%d", &c);
        switch (c)
        {
        case 0:
            fd = open("/dev/myled0", O_RDWR);
            if (fd < 0)
            {
                printf("打开led1文件描述符设备失败\n");

            }
            break;
        case 1:
            fd = open("/dev/myled1", O_RDWR);
            if (fd < 0)
            {
                printf("打开led2文件描述符设备失败\n");

            }
            break;
        case 2:
            fd = open("/dev/myled2", O_RDWR);
            if (fd < 0)
            {
                printf("打开led3文件描述符设备失败\n");

            }
            break;
        }
        printf("!提示!1(启动)0(关闭)\n");
        printf("please input 1 or 0 >>");
        scanf("%d", &a);
        switch (a)
        {
        case 1:
            ioctl(fd, LED_ON, &c); // 开灯
            break;
        case 0:
            ioctl(fd, LED_OFF, &c); // 关灯
            break;
        }
        break;

        close(fd);

        return 0;
    }
}

mychrled.c

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

char kbuf[128] = {0};
unsigned int major; // 定义一个变量保存主设备号

unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;

unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir2_rcc;

unsigned int *vir3_moder;
unsigned int *vir3_odr;
unsigned int *vir3_rcc;

struct class *cls;
struct device *dev;

struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0;
unsigned int ret;
dev_t devno;

int mycdev_open(struct inode *inode, struct file *file)
{
    int a = MINOR(inode->i_rdev);
    file->private_data = (void *)a;
    printk("%s : %s : %d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mydev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    unsigned int a = (unsigned int)file->private_data;
    switch (cmd)
    {
    case LED_ON:
        switch (a)
        {
        case 0:
            *vir_odr |= (0x1 << 10); // led1开灯
            break;
        case 1:
            *vir2_odr |= (0x1 << 10); // led2开灯
            break;
        case 2:
            *vir3_odr |= (0x1 << 8); // led3开灯
            break;
        }
        break;
    case LED_OFF:
        switch (a)
        {
        case 0:
            *vir_odr &= (~(0x1 << 10));
            break;
        case 1:
            *vir2_odr &= (~(0x1 << 10));
            break;
        case 2:
            *vir3_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,
        .release = mycdev_close,
        .unlocked_ioctl = mydev_ioctl,
};
static int __init mydev_init(void)
{
    // 进行LED1寄存器地址的映射
    vir_moder = ioremap(PHY_LED1_MODER, 4);
    if (vir_moder == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr = ioremap(PHY_LED1_ODR, 4);
    if (vir_odr == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_rcc = ioremap(PHY_LED1_RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("映射物理内存成功\n");
    // 硬件LED1寄存器初始化
    (*vir_rcc) |= (0x1 << 4);
    (*vir_moder) &= (~(0x3 << 20));
    (*vir_moder) |= (0x1 << 20);
    (*vir_odr) &= (~(0x1 << 10));
    printk("硬件初始化成功\n");

    // 进行LED2寄存器地址的映射
    vir2_moder = ioremap(PHY_LED2_MODER, 4);
    if (vir2_moder == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir2_odr = ioremap(PHY_LED2_ODR, 4);
    if (vir2_odr == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir2_rcc = ioremap(PHY_LED2_RCC, 4);
    if (vir2_rcc == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("映射物理内存成功\n");
    // 硬件LED2寄存器初始化
    (*vir2_rcc) |= (0x1 << 5);
    (*vir2_moder) &= (~(0x3 << 20));
    (*vir2_moder) |= (0x1 << 20);
    (*vir2_odr) &= (~(0x1 << 10));
    printk("硬件初始化成功\n");

    // 进行LED3寄存器地址的映射
    vir3_moder = ioremap(PHY_LED3_MODER, 4);
    if (vir3_moder == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir3_odr = ioremap(PHY_LED3_ODR, 4);
    if (vir3_odr == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir3_rcc = ioremap(PHY_LED3_RCC, 4);
    if (vir3_rcc == NULL)
    {
        printk("映射物理内存失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("映射物理内存成功\n");
    // 硬件LED3寄存器初始化
    (*vir3_rcc) |= (0x1 << 4);
    (*vir3_moder) &= (~(0x3 << 16));
    (*vir3_moder) |= (0x1 << 16);
    (*vir3_odr) &= (~(0x1 << 8));
    printk("硬件初始化成功\n");

    // 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), 4, "myled");
        if (ret)
        {
            printk("静态申请设备号失败\n");
            goto out2;
        }
        printk("静态申请设备号成功\n");
    }
    else // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 4, "myled");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        printk("动态申请设备号成功\n");
        // 统一后面操作
        major = MAJOR(devno);
        minor = MINOR(devno);
    }

    // 4.注册驱动对象 cdev_add()
    ret = cdev_add(cdev, MKDEV(major, minor), 4);
    if (ret)
    {
        printk("注册驱动对象失败\n");
        goto out3;
    }
    printk("注册驱动对象成功\n");

    // 5.向上提交目录 class_add()
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");
    // 6.向上提交设备信息 device_create()
    int i;
    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");

    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, major), 4);
out2:
    kfree(cdev);
out1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    // 取消地址映射
    iounmap(vir_moder);
    iounmap(vir_odr);
    iounmap(vir_rcc);

    iounmap(vir2_moder);
    iounmap(vir2_odr);
    iounmap(vir2_rcc);

    iounmap(vir3_moder);
    iounmap(vir3_odr);
    iounmap(vir3_rcc);

     // 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), 4);
    // 5.释放对象空间 kfree()
    kfree(cdev);

}

module_init(mydev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值