通过字符设备驱动分布操作进行6个灯的操作(iocl)

该代码示例展示了如何在Linux内核中编写一个驱动程序来控制LED。驱动程序包括字符设备接口,使用ioctl命令开关LED,并通过ioremap映射硬件寄存器进行操作。测试程序通过ioctl调用来周期性地开关不同LED。
摘要由CSDN通过智能技术生成

源代码

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

#define CNAME "myled"
struct cdev *cdev;//字符设备驱动对象指针
struct class *cls;
struct device *dev;
#if 1
unsigned int major=0;//动态申请主设备号
#else
unsigned int major=500;//静态指定主设备号
#endif
int minor=0;//存放次设备号的变量
dev_t devno;//设备号

unsigned int* virt_rcc;
unsigned int* virt_rcc2;
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_led4;
gpio_t *vir_led5;
gpio_t *vir_led6;
#define LED1_ON  (vir_led1->ODR |= (0x1 << 10))
#define LED1_OFF  (vir_led1->ODR &= (~(0x1 << 10)))

#define LED2_ON  (vir_led2->ODR |= (0x1 << 10))
#define LED2_OFF  (vir_led2->ODR &= (~(0x1 << 10)))

#define LED3_ON  (vir_led3->ODR |= (0x1 << 8))
#define LED3_OFF  (vir_led3->ODR &= (~(0x1 << 8)))

#define LED4_ON  (vir_led4->ODR |= (0x1 << 5))
#define LED4_OFF  (vir_led4->ODR &= (~(0x1 << 5)))

#define LED5_ON  (vir_led5->ODR |= (0x1 << 6))
#define LED5_OFF  (vir_led5->ODR &= (~(0x1 << 6)))

#define LED6_ON  (vir_led6->ODR |= (0x1 << 7))
#define LED6_OFF  (vir_led6->ODR &= (~(0x1 << 7)))

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t myled_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    int ret,whitch;
    switch (cmd)
    {
    case LED_ON:
    ret=copy_from_user(&whitch,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (whitch)
        {
            case LED1:
                LED1_ON;
                break;
            case LED2:
                LED2_ON;
                break;
            case LED3:
                LED3_ON;
                break;
            case LED4:
                LED4_ON;
                break;
            case LED5:
                LED5_ON;
                break;
            case LED6:
                LED6_ON;
                break;
        }
    break;
    case LED_OFF:
        ret = copy_from_user(&whitch,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (whitch)
        {
            case LED1:
                LED1_OFF;
                break;
            case LED2:
                LED2_OFF;
                break;
            case LED3:
                LED3_OFF;
                break;
            case LED4:
                LED4_OFF;
                break;
            case LED5:
                LED5_OFF;
                break;
            case LED6:
                LED6_OFF;
                break;
        }
        break;    
    }
    return 0;
}
ssize_t myled_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
int myled_close (struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};
static int __init mycdev_init(void)
{
    
    //1.分配对象
    int ret,i;
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("给字符设备驱动对象申请空间失败\n");
        ret=-ENOMEM;
        goto ERR1;
    }
    printk("给字符设备驱动对象申请空间成功\n");

    //初始化驱动对象
    cdev_init(cdev,&fops);

    //申请设备号
    if(major==0)   //动态申请
    {
        ret=alloc_chrdev_region(&devno,minor,3,"mycdev");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto ERR2;
        }
        major=MAJOR(devno);
        minor=MINOR(devno);
    }
    else if(major>0) //静态申请
    {
        ret=register_chrdev_region(MKDEV(major,minor),3,"mycdev");
         if(ret)
        {
            printk("动态申请设备号失败\n");
            goto ERR2;
        }
    }

    //3.注册对象
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("字符设备驱动对象注册失败\n");
        goto ERR3;
    }
    printk("字符设备驱动对象注册成功\n");

    //向上提交目录
    cls=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto ERR4;
    }
    printk("向上提交目录成功\n");

    //向上提交设备节点信息
    for(i=0;i<6;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("向上提交设备信息失败\n");
            goto ERR5;
        }
    }
    printk("向上提交设备信息成功\n");
    

    //4.将物理地址映射为虚拟地址
    //4.1 将rcc地址映射
    virt_rcc = ioremap(PHY_RCC_ADDR,4);
    if(virt_rcc == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    virt_rcc2 = ioremap(PHY_RCC2_ADDR,4);
    if(virt_rcc2 == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //4.2 将MODER寄存器地址映射
    vir_led1 = ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
        if(vir_led1 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
        if(vir_led2 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    vir_led3 = ioremap(PHY_LED3_ADDR,sizeof(gpio_t));
    if(vir_led3 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    vir_led4 = ioremap(PHY_LED4_ADDR,sizeof(gpio_t));
        if(vir_led4 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    vir_led5 = ioremap(PHY_LED4_ADDR,sizeof(gpio_t));
        if(vir_led5 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    vir_led6 = ioremap(PHY_LED4_ADDR,sizeof(gpio_t));
        if(vir_led6 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    //5.对led1---->PE10引脚初始化
    *virt_rcc |= (0x1 << 4);//5.1 使能GPIOE组时钟[4]=1
    vir_led1->MODER &= (~(0x3 << 20));//5.2 设置PE10引脚为输出模式 [21:20] = 01
    vir_led1->MODER |= (0x1 << 20);
    vir_led1->ODR &= (~(0x1 << 10)); //5.3 设置PE10引脚输出低电平

    *virt_rcc |= (0x1 << 5);
    vir_led2->MODER &= (~(0x3 << 20));//5.2 设置PF10引脚为输出模式 [21:20] = 01
    vir_led2->MODER |= (0x1 << 20);
    vir_led2->ODR &= (~(0x1 << 10)); //5.3 设置PF10引脚输出低电平

    vir_led3->MODER &= (~(0x3 << 16));//5.2 设置PE8引脚为输出模式 [21:20] = 01
    vir_led3->MODER |= (0x1 << 16);
    vir_led3->ODR &= (~(0x1 << 8)); //5.3 设置PE8引脚输出低电平

    *virt_rcc2 |= (0x1 << 0);
    vir_led4->MODER &= (~(0x3 << 10));//5.2 设置PZ5引脚为输出模式 [21:20] = 01
    vir_led4->MODER |= (0x1 << 10);
    vir_led4->ODR &= (~(0x1 << 5)); //5.3 设置PZ5引脚输出低电平

    vir_led5->MODER &= (~(0x3 << 12));//5.2 设置PZ6引脚为输出模式 [21:20] = 01
    vir_led5->MODER |= (0x1 << 12);
    vir_led5->ODR &= (~(0x1 << 6)); //5.3 设置PZ6引脚输出低电平

    vir_led6->MODER &= (~(0x3 << 14));//5.2 设置PZ7引脚为输出模式 [21:20] = 01
    vir_led6->MODER |= (0x1 << 14);
    vir_led6->ODR &= (~(0x1 << 7)); //5.3 设置PZ7引脚输出低电平

    return 0;
ERR5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),3);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    int i;
    //2.取消地址映射
    iounmap(virt_rcc);
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(virt_rcc2);
    iounmap(vir_led4);
    iounmap(vir_led5);
    iounmap(vir_led6);

    for(i=0;i<6;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //销毁目录
    class_destroy(cls);
    //注销字符设备驱动
    cdev_del(cdev);
    //释放设备号
     unregister_chrdev_region(MKDEV(major,minor),3);
     //释放驱动对象空间
     kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

头文件

#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;
    volatile unsigned int   BSRR;
}gpio_t;

#define PHY_RCC_ADDR  0X50000A28
#define PHY_RCC2_ADDR 0x54000210
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_LED4_ADDR 0x54004000

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

enum
{
    LED1,
    LED2,
    LED3,
    LED4,
    LED5,
    LED6,
};

#endif

测试文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"

int main(int argc,const char * argv[])
{
    char buf[128] = {0};
    int fd = -1;
    int whitch;
    fd = open("/dev/mycdev0",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }

    while(1)
    {
        whitch = LED1;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);

        whitch = LED2;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);

        whitch = LED3;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);

        whitch = LED4;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);

        whitch = LED5;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);

        whitch = LED6;
        ioctl(fd,LED_ON,&whitch);
        sleep(1);
        ioctl(fd,LED_OFF,&whitch);
        sleep(1);
    }


    close(fd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值