2023-3-22作业

任务:

1.通过字符设备驱动分步操作进行6个灯的操作(ioctl)(CSDN)

2.拓展6灯,通过指定的设备文件控制指定的灯,不能控制其他灯


1.通过字符设备驱动分步操作进行6个灯的操作(ioctl)(CSDN) 

mycdev.h:

#ifndef __MYCDEV_H__
#define __MYCDEV_H__

typedef struct {
	volatile unsigned int MODER;   // 0x00
	volatile unsigned int OTYPER;  // 0x04
	volatile unsigned int OSPEEDR; // 0x08
	volatile unsigned int PUPDR;   // 0x0C
	volatile unsigned int IDR;     // 0x10
	volatile unsigned int ODR;     // 0x14
	volatile unsigned int BSRR;    // 0x18
	volatile unsigned int LCKR;    // 0x1C 
	volatile unsigned int AFRL;    // 0x20 
	volatile unsigned int AFRH;    // 0x24
	volatile unsigned int BRR;     // 0x28
	volatile unsigned int res;
	volatile unsigned int SECCFGR; // 0x30

}gpio_t;

#define PHY_RCC 0x50000A28
#define PHY_RCC2 0x54000210
#define PHY_GPIOE 0x50006000
#define PHY_GPIOF 0x50007000
#define PHY_GPIOZ 0x54004000

#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)

enum{
	LED1_1,
	LED1_2,
	LED1_3,
	LED2_1,
	LED2_2,
	LED2_3,
};

#endif

mycdev.c:

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

#define CNAME "myled"  //驱动的名字


#if 1
unsigned int major = 0; // 动态申请主设备号
#else
unsigned int major = 500; // 静态申请主设备号
#endif

struct cdev *my_cdev;
dev_t dev_number;           // 设备号(主设备号/次设备号)
unsigned int baseminor = 0; // 次设备号起始值
unsigned int minor = 0;     // 次设备号
struct class *cls;
struct device *dev;

unsigned int* virt_rcc;
unsigned int* virt_rcc2;
gpio_t* virt_gpioe;
gpio_t* virt_gpiof;
gpio_t* virt_gpioz;

#define LED1_1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED1_2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED1_2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED1_3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED1_3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))

#define LED2_1_ON (virt_gpioz->ODR |= (0x1 << 5))
#define LED2_1_OFF (virt_gpioz->ODR &= (~(0x1 << 5)))
#define LED2_2_ON (virt_gpioz->ODR |= (0x1 << 6))
#define LED2_2_OFF (virt_gpioz->ODR &= (~(0x1 << 6)))
#define LED2_3_ON (virt_gpioz->ODR |= (0x1 << 7))
#define LED2_3_OFF (virt_gpioz->ODR &= (~(0x1 << 7)))

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    //判断应用层传递过来的命令码 cmd
    //判断操作哪盏灯进行点亮 copy_from_user
    int whitch;
    int res;
    switch(cmd)
    {
    case LED_ON:
        res = copy_from_user(&whitch,(void*)args,sizeof(args));
        if(res)
        {
            printk("copy from user is error\n");
            return  -EIO;
        }
        switch(whitch)
        {
        case LED1_1:
            LED1_1_ON;
            break;
        case LED1_2:
            LED1_2_ON;
            break;
        case LED1_3:
            LED1_3_ON;
            break; 
        case LED2_1:
            LED2_1_ON;
            break;
        case LED2_2:
            LED2_2_ON;
            break;
        case LED2_3:
            LED2_3_ON;
            break;
        }
        break;

    case LED_OFF:
        res = copy_from_user(&whitch,(void*)args,sizeof(args));
        if(res)
        {
            printk("copy from user is error\n");
            return  -EIO;
        }
        switch(whitch)
        {
        case LED1_1:
            LED1_1_OFF;
            break;
        case LED1_2:
            LED1_2_OFF;
            break;
        case LED1_3:
            LED1_3_OFF;
            break; 
        case LED2_1:
            LED2_1_OFF;
            break;
        case LED2_2:
            LED2_2_OFF;
            break; 
        case LED2_3:
            LED2_3_OFF;
            break; 
        }
        break;
    }
    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,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};

static int __init mycdev_init(void)
{
    int ret, i;

    // 1.分配字符设备驱动对象空间
    my_cdev = cdev_alloc();
    if (NULL == my_cdev)
    {
        printk("1.分配字符设备驱动对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("1.分配字符设备驱动对象空间成功\n");
    // 2.初始化驱动对象结构体
    cdev_init(my_cdev, &fops);

    // 3.动态申请设备号
    if (major == 0)  //动态
    {
        ret = alloc_chrdev_region(&dev_number, baseminor, 6, CNAME);
        if (ret)
        {
            printk("2.动态申请设备号失败\n");
            goto ERR2;
        }
        major = MAJOR(dev_number);
        minor = MINOR(dev_number);
        printk("2.动态申请设备号成功\n");
    }
    else if (major > 0)  //静态
    {
        ret = register_chrdev_region(MKDEV(major, minor), 6, CNAME);
        if (ret)
        {
            printk("2.静态申请设备号失败\n");
            goto ERR2;
        }
        printk("2.静态申请设备号成功\n");
    }

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

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

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

    //将物理地址映射为虚拟地址
    //映射RCC地址
    virt_rcc = ioremap(PHY_RCC,4);
    if(NULL == virt_rcc)
    {
        printk("rcc ioremap error\n");
        return -ENOMEM;
    }
    virt_rcc2 = ioremap(PHY_RCC2,4);
    if(NULL == virt_rcc2)
    {
        printk("rcc2 ioremap error\n");
        return -ENOMEM;
    }
    //映射GPIOE地址
    virt_gpioe = ioremap(PHY_GPIOE,4);
    if(NULL == virt_gpioe)
    {
        printk("gpioe ioremap error\n");
        return -ENOMEM;
    }
    //映射GPIOF地址
    virt_gpiof = ioremap(PHY_GPIOF,4);
    if(virt_gpiof == NULL)
    {
        printk("gpiof ioremap is error\n");
        return -ENOMEM;
    }
    //映射GPIOZ地址
    virt_gpioz = ioremap(PHY_GPIOZ,4);
    if(virt_gpioz == NULL)
    {
        printk("gpioz ioremap is error\n");
        return -ENOMEM;
    }

    //RCC初始化
    *virt_rcc |= (0x1 << 4);
    *virt_rcc |= (0x1 << 5);
    *virt_rcc2 |= (0x1 << 0);

    //对LED1_1初始化,设置引脚功能
    virt_gpioe->MODER &= (~(0x3 << 20));
    virt_gpioe->MODER |= (0x1 << 20);
    virt_gpioe->ODR &= (~(0x1 << 10));
    //对LED1_2初始化,设置引脚功能
    virt_gpiof->MODER &= (~(0x3<<20));
    virt_gpiof->MODER |= (0x1<<20);
    virt_gpiof->ODR &= (~(0x1<<10));
    //对LED1_3初始化,设置引脚功能
    virt_gpioe->MODER &= (~(0x3<<16));
    virt_gpioe->MODER |= (0x1<<16);
    virt_gpioe->ODR &= (~(0x1<<8));

    //对LED2_1初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 10));
    virt_gpioz->MODER |= (0x1 << 10);
    virt_gpioz->ODR &= (~(0x1 << 5));
    //对LED2_2初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 12));
    virt_gpioz->MODER |= (0x1 << 12);
    virt_gpioz->ODR &= (~(0x1 << 6));
    //对LED2_3初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 14));
    virt_gpioz->MODER |= (0x1 << 14);
    virt_gpioz->ODR &= (~(0x1 << 7));

    return 0;
ERR5:
    // 将前面提交成功的设备信息销毁
    for (--i; i >= 0; i--)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);

ERR4:
    cdev_del(my_cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), 6);
ERR2:
    kfree(my_cdev);
ERR1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    int i;

    //取消映射
    iounmap(virt_rcc);
    iounmap(virt_rcc2);
    iounmap(virt_gpioe);
    iounmap(virt_gpiof);
    iounmap(virt_gpioz);

    // 1.注销向上提交的字符设备信息
    for (i = 0; i < 6; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    printk("1.注销向上提交的字符设备信息成功\n");

    // 2.注销向上提交的目录信息
    class_destroy(cls);
    printk("2.注销向上提交的目录信息成功\n");

    // 3.注销字符设备驱动对象
    cdev_del(my_cdev);
    printk("3.注销字符设备驱动对象成功\n");

    // 4.释放申请到的设备号
    unregister_chrdev_region(MKDEV(major, minor), 3);
    printk("4.释放申请到的设备号成功\n");

    // 5.释放申请到的字符设备驱动对象空间
    kfree(my_cdev);
    printk("5.释放申请到的字符设备驱动对象空间成功\n");
}

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

text.c:

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

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

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

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

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

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

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

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

    return 0;
}

2.拓展6灯,通过指定的设备文件控制指定的灯,不能控制其他灯

mycdev.h:

#ifndef __MYCDEV_H__
#define __MYCDEV_H__

typedef struct {
	volatile unsigned int MODER;   // 0x00
	volatile unsigned int OTYPER;  // 0x04
	volatile unsigned int OSPEEDR; // 0x08
	volatile unsigned int PUPDR;   // 0x0C
	volatile unsigned int IDR;     // 0x10
	volatile unsigned int ODR;     // 0x14
	volatile unsigned int BSRR;    // 0x18
	volatile unsigned int LCKR;    // 0x1C 
	volatile unsigned int AFRL;    // 0x20 
	volatile unsigned int AFRH;    // 0x24
	volatile unsigned int BRR;     // 0x28
	volatile unsigned int res;
	volatile unsigned int SECCFGR; // 0x30

}gpio_t;

#define PHY_RCC 0x50000A28
#define PHY_RCC2 0x54000210
#define PHY_GPIOE 0x50006000
#define PHY_GPIOF 0x50007000
#define PHY_GPIOZ 0x54004000

#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)

#endif

mycdev.c:

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

#define CNAME "myled"  //驱动的名字
#define COUNT 6        //设备的数量

#if 1
unsigned int major = 0; // 动态申请主设备号
#else
unsigned int major = 500; // 静态申请主设备号
#endif

struct cdev *my_cdev;
dev_t dev_number;           // 设备号(主设备号/次设备号)
unsigned int baseminor = 0; // 次设备号起始值
unsigned int minor = 0;     // 次设备号
struct class *cls;
struct device *dev;
int flag = -1;

unsigned int* virt_rcc;
unsigned int* virt_rcc2;
gpio_t* virt_gpioe;
gpio_t* virt_gpiof;
gpio_t* virt_gpioz;

#define LED1_1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED1_2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED1_2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED1_3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED1_3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))

#define LED2_1_ON (virt_gpioz->ODR |= (0x1 << 5))
#define LED2_1_OFF (virt_gpioz->ODR &= (~(0x1 << 5)))
#define LED2_2_ON (virt_gpioz->ODR |= (0x1 << 6))
#define LED2_2_OFF (virt_gpioz->ODR &= (~(0x1 << 6)))
#define LED2_3_ON (virt_gpioz->ODR |= (0x1 << 7))
#define LED2_3_OFF (virt_gpioz->ODR &= (~(0x1 << 7)))

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    //判断应用层传递过来的命令码 cmd
    //判断操作哪盏灯进行点亮 copy_from_user
    
    switch (MINOR(file->f_inode->i_rdev))   //判断次设备号
    {
    case 0:                 //规定次设备号为0的设备文件只能控制LED1_1
        if(cmd == LED_ON)
            LED1_1_ON;
        else if(cmd == LED_OFF)
            LED1_1_OFF;
        break;
    case 1:                 //规定次设备号为1的设备文件只能控制LED1_2
        if(cmd == LED_ON)
            LED1_2_ON;
        else if(cmd == LED_OFF)
            LED1_2_OFF;
        break;
    case 2:                 //规定次设备号为2的设备文件只能控制LED1_3  
        if(cmd == LED_ON)
            LED1_3_ON;
        else if(cmd == LED_OFF)
            LED1_3_OFF;
        break;
    case 3:                 //规定次设备号为3的设备文件只能控制LED2_1
        if(cmd == LED_ON)
            LED2_1_ON;
        else if(cmd == LED_OFF)
            LED2_1_OFF;
        break;
    case 4:                 //规定次设备号为4的设备文件只能控制LED2_2
        if(cmd == LED_ON)
            LED2_2_ON;
        else if(cmd == LED_OFF)
            LED2_2_OFF;
        break;
    case 5:                 //规定次设备号为5的设备文件只能控制LED2_3
        if(cmd == LED_ON)
            LED2_3_ON;
        else if(cmd == LED_OFF)
            LED2_3_OFF;
        break;
    }
    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,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};

static int __init mycdev_init(void)
{
    int ret, i;

    // 1.分配字符设备驱动对象空间
    my_cdev = cdev_alloc();
    if (NULL == my_cdev)
    {
        printk("1.分配字符设备驱动对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("1.分配字符设备驱动对象空间成功\n");
    // 2.初始化驱动对象结构体
    cdev_init(my_cdev, &fops);

    // 3.动态申请设备号
    if (major == 0)  //动态
    {
        ret = alloc_chrdev_region(&dev_number, baseminor, COUNT, CNAME);
        if (ret)
        {
            printk("2.动态申请设备号失败\n");
            goto ERR2;
        }
        major = MAJOR(dev_number);
        minor = MINOR(dev_number);
        printk("2.动态申请设备号成功\n");
    }
    else if (major > 0)  //静态
    {
        ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
        if (ret)
        {
            printk("2.静态申请设备号失败\n");
            goto ERR2;
        }
        printk("2.静态申请设备号成功\n");
    }

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

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

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

    //将物理地址映射为虚拟地址
    //映射RCC地址
    virt_rcc = ioremap(PHY_RCC,4);
    if(NULL == virt_rcc)
    {
        printk("rcc ioremap error\n");
        return -ENOMEM;
    }
    virt_rcc2 = ioremap(PHY_RCC2,4);
    if(NULL == virt_rcc2)
    {
        printk("rcc2 ioremap error\n");
        return -ENOMEM;
    }
    //映射GPIOE地址
    virt_gpioe = ioremap(PHY_GPIOE,4);
    if(NULL == virt_gpioe)
    {
        printk("gpioe ioremap error\n");
        return -ENOMEM;
    }
    //映射GPIOF地址
    virt_gpiof = ioremap(PHY_GPIOF,4);
    if(virt_gpiof == NULL)
    {
        printk("gpiof ioremap is error\n");
        return -ENOMEM;
    }
    //映射GPIOZ地址
    virt_gpioz = ioremap(PHY_GPIOZ,4);
    if(virt_gpioz == NULL)
    {
        printk("gpioz ioremap is error\n");
        return -ENOMEM;
    }

    //RCC初始化
    *virt_rcc |= (0x1 << 4);
    *virt_rcc |= (0x1 << 5);
    *virt_rcc2 |= (0x1 << 0);

    //对LED1_1初始化,设置引脚功能
    virt_gpioe->MODER &= (~(0x3 << 20));
    virt_gpioe->MODER |= (0x1 << 20);
    virt_gpioe->ODR &= (~(0x1 << 10));
    //对LED1_2初始化,设置引脚功能
    virt_gpiof->MODER &= (~(0x3<<20));
    virt_gpiof->MODER |= (0x1<<20);
    virt_gpiof->ODR &= (~(0x1<<10));
    //对LED1_3初始化,设置引脚功能
    virt_gpioe->MODER &= (~(0x3<<16));
    virt_gpioe->MODER |= (0x1<<16);
    virt_gpioe->ODR &= (~(0x1<<8));

    //对LED2_1初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 10));
    virt_gpioz->MODER |= (0x1 << 10);
    virt_gpioz->ODR &= (~(0x1 << 5));
    //对LED2_2初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 12));
    virt_gpioz->MODER |= (0x1 << 12);
    virt_gpioz->ODR &= (~(0x1 << 6));
    //对LED2_3初始化,设置引脚功能
    virt_gpioz->MODER &= (~(0x3 << 14));
    virt_gpioz->MODER |= (0x1 << 14);
    virt_gpioz->ODR &= (~(0x1 << 7));

    return 0;
ERR5:
    // 将前面提交成功的设备信息销毁
    for (--i; i >= 0; i--)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);

ERR4:
    cdev_del(my_cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
    kfree(my_cdev);
ERR1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    int i;

    //取消映射
    iounmap(virt_rcc);
    iounmap(virt_rcc2);
    iounmap(virt_gpioe);
    iounmap(virt_gpiof);
    iounmap(virt_gpioz);

    // 1.注销向上提交的字符设备信息
    for (i = 0; i < COUNT; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    printk("1.注销向上提交的字符设备信息成功\n");

    // 2.注销向上提交的目录信息
    class_destroy(cls);
    printk("2.注销向上提交的目录信息成功\n");

    // 3.注销字符设备驱动对象
    cdev_del(my_cdev);
    printk("3.注销字符设备驱动对象成功\n");

    // 4.释放申请到的设备号
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
    printk("4.释放申请到的设备号成功\n");

    // 5.释放申请到的字符设备驱动对象空间
    kfree(my_cdev);
    printk("5.释放申请到的字符设备驱动对象空间成功\n");
}

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

text.c:

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

int main(int argc, const char *argv[])
{
    char buf[128] = {0};
    int fd[6] = {0};
    char *dir[6] = {"/dev/myled0","/dev/myled1","/dev/myled2","/dev/myled3","/dev/myled4","/dev/myled5"};
    for (int i = 0; i < 6; i++)
    {
        fd[i] = open(dir[i], O_RDWR);
        if (-1 == fd[i])
        {
            perror("open is error\n");
            return -1;
        }
    }

    while (1)
    {
        for (int i = 0; i < 6; i++)
        {
            ioctl(fd[i], LED_ON);
            sleep(1);
            ioctl(fd[i], LED_OFF);
            sleep(1);
        }
    }

    for (int i = 0; i < 6; i++)
        close(fd[i]);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值