驱动开发day9

目录

应用层写程序控制开发板6盏灯的亮灭,要求使用GPIO子系统

头文件:

驱动代码:

应用层代码:

驱动安装:

应用层使用gpio子系统控制灯的亮灭效果:

1.控制LED2熄灭:

2.控制LED6熄灭:

3.控制LED2重新点亮:


应用层写程序控制开发板6盏灯的亮灭,要求使用GPIO子系统

头文件:

#ifndef __MYLED_H_
#define __MYLED_H_

typedef enum {
    LED1=1,
    LED2,
    LED3,
    LED4,
    LED5,
    LED6
}LED;
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#endif

驱动代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "myled.h"
#define CNAME "myled"
struct device_node *node;
struct property *pr;
struct gpio_desc *gpiono[5];
#if 1
unsigned int major = 0;//动态申请设备号
#else
unsigned int major = 500; //静态指定设备号
#endif
char kbuf[128]={};
int minor=0;
const int count=6;
struct class *cls;
struct device *dev;
struct cdev *cdev;
int mycdev_open(struct inode *inode,struct file *file)
{
	return 0;
};
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy to user is error\n");
		return -EIO;
    }
    return size;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loffs)
{
    int ret;
	if(size>sizeof(kbuf)) size=sizeof(kbuf);
	ret=copy_from_user(kbuf,ubuf,size);
	if(ret)
	{
		printk("copy from user is error\n");
		return -EIO;
	}
	printk("copy from user kbuf=%s\n",kbuf);
    return size;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int whitch;
    int ret;
    switch (cmd)
    {
        case LED_ON:
            ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
            if(ret)
            {
                printk("copy from user led on!!!!\n");
                return -EIO;
            }
            switch(whitch) 
            {
                case LED1:
                gpiod_set_value(gpiono[0],1);
                break;
                case LED2:
                gpiod_set_value(gpiono[1],1);
                break;
                case LED3:
                gpiod_set_value(gpiono[2],1);
                break;
                case LED4:
                gpiod_set_value(gpiono[3],1);
                break;
                case LED5:
                gpiod_set_value(gpiono[4],1);
                break;
                case LED6:
                gpiod_set_value(gpiono[5],1);
                break;
            }
            break;
        case LED_OFF:
            ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
            if(ret)
            {
                printk("copy from user led on!!!!\n");
                return -EIO;
            }
            switch(whitch) 
            {
                case LED1:
                gpiod_set_value(gpiono[0],0);
                break;
                case LED2:
                gpiod_set_value(gpiono[1],0);
                break;
                case LED3:
                gpiod_set_value(gpiono[2],0);
                break;
                case LED4:
                gpiod_set_value(gpiono[3],0);
                break;
                case LED5:
                gpiod_set_value(gpiono[4],0);
                break;
                case LED6:
                gpiod_set_value(gpiono[5],0);
                break;
            }
            break;
    }
    return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
	return 0;
}
const struct file_operations fops={
	.open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
	.unlocked_ioctl=mycdev_ioctl,
	.release=mycdev_close,
};
static int __init mycdev_init(void)
{
    int ret;
    dev_t devno;
    int i;
    cdev=cdev_alloc();
    if(NULL==cdev)
    {
        printk("cdev alloc error\n");
        return -EIO;
        goto ERR1;
    }
    cdev_init(cdev,&fops);
    if(major>0)
    {
        ret=register_chrdev_region(MKDEV(major,minor),count,CNAME);
        if(ret)
        {
            printk("register chrdev region is error\n");
            ret= -ENOMEM;
            goto ERR2;
        }
    }
    else 
    {
        ret=alloc_chrdev_region(&devno,0,count,CNAME);
        if(ret)
        {
            printk("alloc chrdev region is error\n");
            ret=-ENOMEM;
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("cdev add is error\n");
        ret = -EIO;
        goto ERR3;
    }
    cls=class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
    	printk("class create is error\n");
        ret= PTR_ERR(cls);
        goto ERR4;
    }
    for(i=0;i0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
    kfree(cdev);
ERR1:
    return -EIO;
}
static void __exit mycdev_exit(void)
{
    int i;
	for(i=0;i

应用层代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "myled.h"
int main(int argc, char const *argv[])
{
    char buf[128]={};
    int whitch;
    int fd1,fd2,fd3,fd4,fd5,fd6;
    fd1=open("/dev/myled0",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd2=open("/dev/myled1",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd3=open("/dev/myled2",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd4=open("/dev/myled3",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd5=open("/dev/myled4",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd6=open("/dev/myled5",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    while (1)
    {
        printf("请输入:0->LED OFF|1->LED ON\n>>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        if(buf[0]=='1')
        {
            printf("请选择要控制的LED灯:1->LED1 2->LED2 3->LED3 4->LED4 5->LED5 6->LED6\n>>");
            scanf("%d",&whitch);
            getchar();
            if(whitch==1)
            {
                ioctl(fd1,LED_ON,&whitch);
            }
            if(whitch==2)
            {
                ioctl(fd2,LED_ON,&whitch);
            }
            if(whitch==3)
            {
                ioctl(fd3,LED_ON,&whitch);
            }
            if(whitch==4)
            {
                ioctl(fd4,LED_ON,&whitch);
            }
            if(whitch==5)
            {
                ioctl(fd5,LED_ON,&whitch);
            }
            if(whitch==6)
            {
                ioctl(fd6,LED_ON,&whitch);
            }
        }
        else if(buf[0]=='0')
        {
            printf("请选择要控制的led灯:1->LED1 2->LED2 3->LED3 4->LED4 5->LED5 6->LED6\n>>");
            scanf("%d",&whitch);
            getchar();
            if(whitch==1)
            {
                ioctl(fd1,LED_OFF,&whitch);
            }
            if(whitch==2)
            {
                ioctl(fd2,LED_OFF,&whitch);
            }
            if(whitch==3)
            {
                ioctl(fd3,LED_OFF,&whitch);
            }
            if(whitch==4)
            {
                ioctl(fd4,LED_OFF,&whitch);
            }
            if(whitch==5)
            {
                ioctl(fd5,LED_OFF,&whitch);
            }
            if(whitch==6)
            {
                ioctl(fd6,LED_OFF,&whitch);
            }
        }
        else
        {
            printf("请重新输入!!!\n");
        }
    }
    close(fd1);
    close(fd2);
    close(fd3);
    close(fd4);
    close(fd5);
    close(fd6);
    return 0;
}

驱动安装:

6盏灯全部点亮

应用层使用gpio子系统控制灯的亮灭效果:

驱动开发day9_第1张图片

1.控制LED2熄灭:

2.控制LED6熄灭:

3.控制LED2重新点亮:

你可能感兴趣的:(驱动开发,c语言,arm)