通过GPIO子系统编写LED驱动,应用程序控制LED灯亮灭

头文件

#ifndef __HEAD_H__
#define __HEAD_H__ 

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

#endif 

驱动程序

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

/*myleds{
    led1 = <&gpioe 10 0>;
    led2 = <&gpiof 10 0>;
    led3 = <&gpioe 8 0>;
};
};*/

int gpiono1;
int gpiono2;
int gpiono3;
char kbuf[128] = {0};
struct gpio_desc *gpiono;
struct device_node *dnode;
unsigned int minor = 0;
unsigned int major = 500;
dev_t devno;
struct cdev *cdev;
struct class *cls;
struct device *dev;
int ret = 0;

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret2;
    int which;
    ret2 = copy_from_user(&which, (void *)arg, sizeof(int));
    switch (which)
    {
    case 1:
        switch (cmd)
        {
        case LED_ON:
            gpio_set_value(gpiono1, 1);
            break;
        case LED_OFF:
            gpio_set_value(gpiono1, 0);
            break;
        }
        break;
    case 2:
        switch (cmd)
        {
        case LED_ON:
            gpio_set_value(gpiono2, 1);
            break;
        case LED_OFF:
            gpio_set_value(gpiono2, 0);
            break;
        }
        break;
    case 3:
        switch (cmd)
        {
        case LED_ON:
            gpio_set_value(gpiono3, 1);
            break;
        case LED_OFF:
            gpio_set_value(gpiono3, 0);
            break;
        }
        break;
    }
    return 0;
}
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 *iof)
{
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy to user filed\n");
        return -EIO;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy from user filed\n");
        return -EIO;
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
    .unlocked_ioctl=mycdev_ioctl,
};

static int __init mycdev_init(void)
{
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("分配字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto LOOP1;
    }
    printk("分配对象空间成功\n");
    cdev_init(cdev, &fops);
    if (major > 0)
    {
        ret = register_chrdev_region(MKDEV(major, minor), 1, "myleds");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
            goto LOOP2;
        }
    }
    else if (major == 0)
    {
        ret = alloc_chrdev_region(&devno, minor, 1, "myleds");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto LOOP2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    printk("申请设备号成功\n");
    ret = cdev_add(cdev, MKDEV(major, minor), 1);
    if (ret)
    {
        printk("字符设备驱动对象注册失败\n");
        goto LOOP3;
    }
    printk("添加字符设备驱动对象注册进内核成功\n");
    cls = class_create(THIS_MODULE, "myleds");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
        goto LOOP4;
    }
    printk("向上提交目录成功\n");
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myleds");
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点失败\n");
        ret = -PTR_ERR(dev);
        goto LOOP5;
    }
    printk("向上提交设备节点成功\n");
    dnode = of_find_node_by_name(NULL, "myleds");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENOMEM;
    }
    printk("解析设备树节点成功\n");
    gpiono1 = of_get_named_gpio(dnode, "led1", 0);
    if (gpiono1 < 0)
    {
        printk("解析设备号失败\n");
        return -EIO;
    }
    ret = gpio_request(gpiono1, NULL);
    if (ret)
    {
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("申请gpio编号成功%d\n", gpiono1);
    gpio_direction_output(gpiono1, 0);
    gpiono2 = of_get_named_gpio(dnode, "led2", 0);
    if (gpiono2 < 0)
    {
        printk("解析设备号失败\n");
        return -EIO;
    }
    ret = gpio_request(gpiono2, NULL);
    if (ret)
    {
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("申请gpio编号成功%d\n", gpiono2);
    gpio_direction_output(gpiono2, 0);
    gpiono3 = of_get_named_gpio(dnode, "led3", 0);
    if (gpiono3 < 0)
    {
        printk("解析设备号失败\n");
        return -EIO;
    }
    ret = gpio_request(gpiono3, NULL);
    if (ret)
    {
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("申请gpio编号成功%d\n", gpiono3);
    gpio_direction_output(gpiono3, 0);
    return 0;
LOOP5:
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
LOOP4:
    cdev_del(cdev);
LOOP3:
    unregister_chrdev_region(MKDEV(major, minor), 1);
LOOP2:
    kfree(cdev);
LOOP1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    gpio_free(gpiono1);
    gpio_free(gpiono2);
    gpio_free(gpiono3);
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major, minor), 1);
    kfree(cdev);
}

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

应用程序

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

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/myleds", O_RDWR);
    if (fd < 0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }
    int a, b, c;
    while (1)
    {
        printf("请输入要进行的操作>>>\n 1(开灯)\t 0(关灯)\t 其他(退出)\n");
        scanf("%d", &b);
        switch (b)
        {
        case 1:
            printf("请输入要操作的灯>>>\n 1(LED1)\t 2(LED2)\t \n 3(LED3)\n");
            scanf("%d", &c);
            ioctl(fd, LED_ON, &c);
            break;
        case 0:
            printf("请输入要操作的灯>>>\n 1(LED1)\t 2(LED2)\t \n 3(LED3)\n");
            scanf("%d", &c);
            ioctl(fd, LED_OFF, &c);
            break;
        default:
            close(fd);
            return 0;
        }
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(驱动开发,单片机,stm32)