驱动开发day(7.26)

该代码示例展示了如何编写一个Linux字符设备驱动程序来控制LED灯。通过打开设备文件,使用ioctl调用来开关LED。驱动程序中包含了设备注册、内存映射以及对GPIO寄存器的操作。用户空间程序通过交互式输入选择LED并发出控制命令。
摘要由CSDN通过智能技术生成

头文件

#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 <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, b, c;
    char buf[128] = {0};
    printf("invoking open\n");
    int fd1 = open("/dev/myled0", O_RDWR);
    if (fd1 < 0)
    {
        printf("open file failed");
        exit(-1);
    }
    int fd2 = open("/dev/myled1", O_RDWR);
    if (fd2 < 0)
    {
        printf("open file failed");
        exit(-1);
    }
    int fd3 = open("/dev/myled2", O_RDWR);
    if (fd3 < 0)
    {
        printf("open file failed");
        exit(-1);
    }

    while (1)
    {

        printf("请选择LED灯1(LED1)2(LED2) 3(LED3)>");
        scanf("%d", &b);
        printf("请输入控制指令:0(关灯) 1(开灯)>");
        scanf("%d", &a);
        switch (b)
        {
        case 1:
            switch (a)
            {
            case 1:
                ioctl(fd1, LED_ON);
                break;
            case 0:
                ioctl(fd1, LED_OFF);
                break;
            }
            break;
        case 2:
            switch (a)
            {
            case 1:
                ioctl(fd2, LED_ON);
                break;
            case 0:
                ioctl(fd2, LED_OFF);
                break;
            }
            break;
        case 3:
            switch (a)
            {
            case 1:
                ioctl(fd3, LED_ON);
                break;
            case 0:
                ioctl(fd3, LED_OFF);
                break;
            }
            break;
        }
    }
    printf("invoking close\n");
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

驱动函数

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#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)
{
    int a = inode->i_rdev;
    file->private_data = (void *)MINOR(a);
    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)
{
    unsigned int a = (unsigned int)file->private_data;
    switch (a)
    {
    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;
}
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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值