驱动开发day3 7.25

ioctl控制LED、蜂鸣器、风扇、马达 (为每一个硬件注册一个驱动)

头文件

#ifndef __HEAD_H__
#define __HEAD_H__

typedef struct{
    volatile unsigned int MODER;
    volatile unsigned int OTYPER;
    volatile unsigned int OSPEEDR;
    volatile unsigned int PUPDR;
    volatile unsigned int IDR;
    volatile 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 _IO('l',1)
#define LED_OFF _IO('l',0)
#endif

功能函数

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

int main(int argc, char const *argv[])
{
    int a;
    char buf[128] = {0};
    printf("invoking open\n");
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("open file failed");
        exit(-1);
    }
    while (1)
    {
        printf("请输入控制指令:0(关灯) 1(开灯)>");
        scanf("%d",&a);
        switch (a)
        {
        case 1:
            ioctl(fd,LED_ON);
            break;
        case 0:
            ioctl(fd,LED_OFF);
        default:
            break;
        }
    }
    printf("invoking close\n");
    close(fd);
    return 0;
}

LED驱动函数

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

// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
unsigned int *vir_moder;
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;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned long ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    long ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }

    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)
{
    switch (cmd)
    {
    case LED_ON:
        switch (arg)
        {
        case 1:
            vir_led1->ODR |= (0x1 << 10);
            break;
        case 2:
            vir_led2->ODR |= (0x1 << 10);
            break;
        case 3:
            vir_led3->ODR |= (0x1 << 8);
            break;
        default:
            break;
        }
        break;
    case LED_OFF:
        switch (arg)
        {
        case 1:
            vir_led1->ODR &= (~(0x1 << 10));
            break;
        case 2:
            vir_led2->ODR &= (~(0x1 << 10));
            break;
        case 3:
            vir_led1->ODR &= (~(0x1 << 8));
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
    return 0;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};

int all_led_init(void)
{
    // 寄存器地址的映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
    if (vir_led3 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");

    // 寄存器初始化
    // rcc
    (*vir_rcc) |= (0x3 << 4);

    // led1
    vir_led1->MODER &= (~(0x3 << 20));
    vir_led1->MODER |= (0x1 << 20);

    // led2
    vir_led2->MODER &= (~(0x3 << 20));
    vir_led2->MODER |= (0x1 << 20);

    // led3
    vir_led3->MODER &= (~(0x3 << 16));
    vir_led3->MODER |= (0x1 << 16);
    printk("寄存器初始化成功\n");

    return 0;
}
static int __init mycdev_init(void)
{
    int i;
    // 注册字符设备驱动
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("register error\n");
        return major;
    }
    printk("register success\n");

    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功]\n");

    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交结点设备信息失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");

    // 寄存器映射以及初始化
    all_led_init();

    return 0;
}

static void __exit mycdev_exit(void)
{
    // 取消地址映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);

    // 销毁节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mycdev");
}

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

蜂鸣器驱动函数

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

// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_buzzer;
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;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned long ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    long ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }

    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)
{
    switch (cmd)
    {
    case BUZZ_ON:
        vir_buzzer->ODR |= (0x1 << 6);
        break;
    case BUZZ_OFF:
        vir_buzzer->ODR &= (~(0x1 << 6));
        break;
    default:
        break;
    }
    return 0;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};

int all_buzzer_init(void)
{
    // 寄存器地址的映射
    vir_buzzer = ioremap(PHY_BUZZ_ADDR, 4);
    if (vir_buzzer == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");

    // 寄存器初始化
    // rcc
    (*vir_rcc) |= (0x1 << 1);

    // fan
    vir_buzzer->MODER &= (~(0x3 << 12));
    vir_buzzer->MODER |= (0x1 << 12);
   // vir_fan->AFRH &= (~(0xf << 4));
    //vir_fan->AFRH |= (0x1 << 4);

    printk("寄存器初始化成功\n");

    return 0;
}
static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("register error\n");
        return major;
    }
    printk("register success\n");

    cls = class_create(THIS_MODULE, "mybuzzer");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功]\n");

    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mybuzzer");
    if (IS_ERR(dev))
    {
        printk("向上提交结点设备信息失败\n");
        return -PTR_ERR(dev);
    }
printk("向上提交设备节点成功\n");

// 寄存器映射以及初始化
all_buzzer_init();

return 0;
}

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

    // 销毁节点信息
    device_destroy(cls, MKDEV(major, 0));

    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mycdev");
}

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

风扇驱动程序

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

// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_fan;
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;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned long ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    long ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }

    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)
{
    switch (cmd)
    {
    case fan_ON:
        vir_fan->ODR |= (0x1 << 9);
        break;
    case fan_OFF:
        vir_fan->ODR &= (~(0x1 << 9));
        break;
    default:
        break;
    }
    return 0;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};

int all_fan_init(void)
{
    // 寄存器地址的映射
    vir_fan = ioremap(PHY_FAN_ADDR, 4);
    if (vir_fan == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");

    // 寄存器初始化
    // rcc
    (*vir_rcc) |= (0x1 << 4);

    // fan
    vir_fan->MODER &= (~(0x3 << 18));
    vir_fan->MODER |= (0x1 << 18);
   // vir_fan->AFRH &= (~(0xf << 4));
    //vir_fan->AFRH |= (0x1 << 4);

    printk("寄存器初始化成功\n");

    return 0;
}
static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("register error\n");
        return major;
    }
    printk("register success\n");

    cls = class_create(THIS_MODULE, "myfan");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功]\n");

    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myfan");
    if (IS_ERR(dev))
    {
        printk("向上提交结点设备信息失败\n");
        return -PTR_ERR(dev);
    }
printk("向上提交设备节点成功\n");

// 寄存器映射以及初始化
all_fan_init();

return 0;
}

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

    // 销毁节点信息
    device_destroy(cls, MKDEV(major, 0));

    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mycdev");
}

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

马达驱动函数

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

// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_motor;
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;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned long ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    long ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }

    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)
{
    switch (cmd)
    {
    case MOTOR_ON:
        vir_motor->ODR |= (0x1 << 6);
        break;
    case MOTOR_OFF:
        vir_motor->ODR &= (~(0x1 << 6));
        break;
    default:
        break;
    }
    return 0;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};

int all_motor_init(void)
{
    // 寄存器地址的映射
    vir_motor = ioremap(PHY_MOTOR_ADDR, 4);
    if (vir_motor == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");

    // 寄存器初始化
    // rcc
    (*vir_rcc) |= (0x1 << 5);

    // fan
    vir_motor->MODER &= (~(0x3 << 12));
    vir_motor->MODER |= (0x1 << 12);
   // vir_fan->AFRH &= (~(0xf << 4));
    //vir_fan->AFRH |= (0x1 << 4);

    printk("寄存器初始化成功\n");

    return 0;
}
static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("register error\n");
        return major;
    }
    printk("register success\n");

    cls = class_create(THIS_MODULE, "mymotor");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功]\n");

    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mymotor");
    if (IS_ERR(dev))
    {
        printk("向上提交结点设备信息失败\n");
        return -PTR_ERR(dev);
    }
printk("向上提交设备节点成功\n");

// 寄存器映射以及初始化
all_motor_init();

return 0;
}

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

    // 销毁节点信息
    device_destroy(cls, MKDEV(major, 0));

    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mycdev");
}

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

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