linux驱动 - 20230817

练习: 通过字符设备驱动分步注册方式编写LED灯的驱动,应用程序使用ioctl函数编写硬件控制逻辑

头文件 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 ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR    0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR    0X50000A28

#define LED_ON _IOW('l',1,int)//开灯
#define LED_OFF _IOW('l',0,int)//关灯

#endif 

驱动代码

#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

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

char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;

int mycdev_open(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)
{
    int which;
    int ret=copy_from_user(&which,(unsigned int *)arg,4);
    if(ret)
    {
        printk("copy_from_user err\n");
        return ret;
    }
    // 根据用户空间功能码的不同实现硬件不同的控制
    switch (cmd)
    {
    case LED_ON: // 开灯
        switch (which)
        {
        case 1: // LED1
            vir_led1->ODR |= (0X1 << 10);
            break;
        case 2: // LED2
            vir_led2->ODR |= (0X1 << 10);
            break;
        case 3: // LED3
            vir_led3->ODR |= (0X1 << 8);
            break;
        }
        break;
    case LED_OFF: // 关灯
        switch (which)
        {
        case 1:
            vir_led1->ODR &= (~(0X1 << 10));
            break;
        case 2:
            vir_led2->ODR &= (~(0X1 << 10));
            break;
        case 3:
            vir_led3->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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

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;
}

static int __init mycdev_init(void)
{
    // 字符设备驱动注册
    // major = register_chrdev(0, "mychrdev", &fops);
    // if (major < 0)
    // {
    //     printk("字符设备驱动注册失败\n");
    //     return major;
    // }
    /**
     * 分步实现字符设备驱动注册
    */
    int ret;
    //1. 分配字符设备驱动对象
    cdev = cdev_alloc();
    if (cdev == NULL) {
        printfk("申请字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    //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);
    }
    //4. 注册字符设备驱动对象
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret) {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
    // 向上提交目录信息
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录信息失败\n");
        return -PTR_ERR(cls);
    }
    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");
            return -PTR_ERR(cls);
        }
    }
    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(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(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用层测试代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int a, b;
    int fd = open("/dev/mycdev0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入要实现的功能:1(开灯)0(关灯>)");
        scanf("%d", &a);
        printf("请输入要控制的灯:1(LED1) 2(LED2) 3(LED3)>");
        scanf("%d", &b);
        switch (a)
        {
        case 1:
            ioctl(fd, LED_ON, &b);
            break;
        case 0:
            ioctl(fd, LED_OFF, &b);
            break;
        }
    }

    close(fd);

    return 0;
}

你可能感兴趣的:(linux)