驱动开发day4作业

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_RCC 0X50000A28
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
// 构建开灯关灯功能码
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)
#endif

test.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
int main(int argc, char const *argv[])
{
    int a, b, fd;
    while (1)
    {
        printf("请输入要操作的灯:1(LED1)2(LED2)3(LED3) >");
        scanf("%d", &a);
        switch (a)
        {
        case 1:
            fd = open("/dev/myled0", O_RDWR);
            if (fd < 0)
            {
                printf("打开设备文件失败\n");
                return -1;
            }
            break;
        case 2:
            fd = open("/dev/myled1", O_RDWR);
            if (fd < 0)
            {
                printf("打开设备文件失败\n");
                return -1;
            }
            break;
        case 3:
            fd = open("/dev/myled2", O_RDWR);
            if (fd < 0)
            {
                printf("打开设备文件失败\n");
                return -1;
            }
            break;
        }
        printf("请输入要进行的操作:0(关灯)1(开灯) >");
        scanf("%d", &b);
        switch (b)
        {
        case 1:
            ioctl(fd, LED_ON);
            break;
        case 0:
            ioctl(fd, LED_OFF);
            break;
        }
        close(fd);
    }

    return 0;
}

mycdev.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *ddev;
struct cdev *cdev;
dev_t dev;
unsigned int major = 0;
unsigned int minor = 0;
int num = 3;
char *name = "mycdev";
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
    //获取打开文件的次设备号
    int min = MINOR(inode->i_rdev);
    file->private_data = (void *)min;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    //获取到文件的次设备号
    int min = (int)file->private_data;
    switch (min)
    {
    case 0:
        switch (cmd)
        {
        case LED_ON:
            (vir_led1->ODR) |= (0x1 << 10);
            break;
        case LED_OFF:
            (vir_led1->ODR) &= (~(0x1 << 10));
            break;
        }
        break;
    case 1:
        switch (cmd)
        {
        case LED_ON:
            (vir_led2->ODR) |= (0x1 << 10);
            break;
        case LED_OFF:
            (vir_led2->ODR) &= (~(0x1 << 10));
            break;
        }
        break;
    case 2:
        switch (cmd)
        {
        case LED_ON:
            (vir_led3->ODR) |= (0x1 << 8);
            break;
        case LED_OFF:
            (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,
};
static int __init mydev_init(void)
{
    // 申请一个对象空间
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请对象空间失败\n");
        goto out5;
    }
    printk("申请对象空间成功\n");
    // 初始化对象
    cdev_init(cdev, &fops);
    // 申请块设备号
    if (major == 0) // 动态申请
    {
        if (alloc_chrdev_region(&dev, minor, num, name))
        {
            printk("动态申请块设备号失败\n");
            goto out4;
        }
        major = MAJOR(dev);
        minor = MINOR(dev);
    }
    else // 静态申请
    {
        if (register_chrdev_region(MKDEV(major, minor), num, name))
        {
            printk("静态指定设备号失败\n");
            goto out4;
        }
    }
    printk("申请块设备号成功\n");
    // 注册驱动对象
    if (cdev_add(cdev, MKDEV(major, minor), num))
    {
        printk("注册驱动对象失败\n");
        goto out3;
    }
    printk("注册驱动对象成功\n");
    // 向上提交目录
    cls = class_create(THIS_MODULE, name);
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto out2;
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i;
    for (i = minor; i < (minor + num); i++)
    {
        ddev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(ddev))
        {
            printk("向上提交设备节点信息失败\n");
            goto out1;
        }
    }
    printk("向上提交设备节点信息成功\n");
    // 进行寄存器地址映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("映射失败%d\n", __LINE__);
        goto out1;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("映射失败%d\n", __LINE__);
        goto led1;
    }
    vir_led3 = vir_led1;
    vir_rcc = ioremap(PHY_RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("映射失败%d\n", __LINE__);
        goto led2;
    }
    printk("映射成功\n");
    // LED1寄存器初始化
    (*vir_rcc) |= (0x1 << 4); // GPIOE控制器时钟使能
    (vir_led1->MODER) &= (~(0x3 << 20));
    (vir_led1->MODER) |= (0x1 << 20);
    (vir_led1->ODR) &= (~(0x1 << 10)); // 默认关灯
    // LED2寄存器初始化
    (*vir_rcc) |= (0x1 << 5); // GPIOF控制器时钟使能
    (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;
led1:
    iounmap(vir_led1);
led2:
    iounmap(vir_led2);
out1:
    // 将提交的设备节点释放
    for (--i; i >= minor; i--)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);
out2:
    // 注销字符设备驱动对象
    cdev_del(cdev);
out3:
    // 释放设备号
    unregister_chrdev_region(MKDEV(major, minor), num);
out4:
    // 释放申请到的字符设备驱动对象空间
    kfree(cdev);
out5:
    return -1;
}

static void __exit mydev_exit(void)
{
    // 取消内存映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
    // 销毁设备节点信息
    int i;
    for (i = minor; i < (minor + num); i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动对象
    cdev_del(cdev);
    // 释放设备号
    unregister_chrdev_region(MKDEV(major, minor), num);
    // 释放申请到的字符设备驱动对象空间
    kfree(cdev);
}

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

你可能感兴趣的:(驱动开发)