10.12 创建三个设备节点分别控制三个LED灯

创建三个设备节点分别控制三个LED灯

在这里插入图片描述

myled.h

#ifndef __MYLED_H__
#define __MYLED_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_BEE_ADDR 0X50003000
#define PHY_FAN_ADDR 0X50006000
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR   0X50006000
#define PHY_RCC_ADDR  0X50000A28 
    enum LED
    {
        LED1=1,
        LED2,
        LED3
    };

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

#endif

myled.c

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

struct cdev *cdev;
dev_t dev_n;
int minor = 0; //次设备号从0开始
int major;
const int count = 3; //设备节点个数为1
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_bee;
gpio_t *vir_fan;
unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    return 0;
}
// write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    return 0;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret, which;
    switch (cmd)
    {
    case LedON:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("copy failed\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            vir_led1->ODR |= 1 << 10;
            break;
        case LED2:
            vir_led2->ODR |= (1 << 10);
            break;
        case LED3:
            vir_led3->ODR |= (1 << 8);
            break;
        }
        break;
    case LedOFF:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("copy failed\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            vir_led1->ODR &= ~(1 << 10);
            break;
        case LED2:
            vir_led2->ODR &= ~(1 << 10);
            break;
        case LED3:
            vir_led3->ODR &= ~(1 << 8);
            break;
        }
        break;
    default:
        printk("cmd error\n");
        break;
    }
    return 0;
}
// close()
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,
        .unlocked_ioctl = ioctl,
        .release = mycdev_close,
};
int all_led_init(void)
{
    //进行物理地址的映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("vir_led1 MAP FAILED\n");
        return -ENOMEM;
    }
    printk("vir_led1 MAP SUCCESS\n");
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("vir_led2 MAP FAILED\n");
        return -ENOMEM;
    }
    printk("vir_led2 MAP SUCCESS\n");
    vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
    if (vir_led3 == NULL)
    {
        printk("vir_led3 MAP FAILED\n");
        return -ENOMEM;
    }
    printk("vir_led3 MAP SUCCESS\n");
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("vir_rcc MAP FAILED\n");
        return -ENOMEM;
    }
    printk("vir_rcc MAP SUCCESS\n");

    //寄存器的初始化
    // led1
    vir_led1->MODER &= ~(3 << 20);
    vir_led1->MODER |= (1 << 20);
    vir_led1->ODR &= ~(1 << 10);

    // led2
    vir_led2->MODER &= ~(3 << 20);
    vir_led2->MODER |= (1 << 20);
    vir_led2->ODR &= ~(1 << 10);

    // led3
    vir_led3->MODER &= ~(3 << 16);
    vir_led3->MODER |= (1 << 16);
    vir_led3->ODR &= ~(1 << 8);

    (*vir_rcc) |= (3 << 4);
    return 0;
}
static int __init mycdev_init(void)
{
    // 1.分配设备驱动对象
    int ret, i;
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("cdev alloc memory err\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("alloc success\n");
    // 2.初始化对象
    cdev_init(cdev, &fops);
    // 3.设备号的申请
    ret = alloc_chrdev_region(&dev_n, minor, count, "myled");
    if (ret)
    {
        printk("alloc dev_no failed\n");
        goto ERR2;
    }
    major = MAJOR(dev_n);
    minor = MINOR(dev_n);
    printk("alloc dev_no success\n");
    // 4.注册字符设备驱动
    ret = cdev_add(cdev, MKDEV(major, minor), count);
    if (ret)
    {
        printk("register failed\n");
        goto ERR3;
    }
    printk("register failed success\n");
    // 5.自动创建设备节点
    //向上提交节点目录
    cls = class_create(THIS_MODULE, "LED");
    if (IS_ERR(cls))
    {
        printk("up submit dir failed\n");
        return PTR_ERR(cls);
        goto ERR4;
    }
    printk("up submit dir success\n");
    //创建设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "my_led%d", i);
        if (IS_ERR(dev))
        {
            printk("create jiedian failed\n");
            ret = PTR_ERR(dev);
            goto ERR5;
        }
    }
    printk("create jiedian success\n");
    //寄存器的初始化
    all_led_init();
    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), count);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    // 1.销毁设备节点
    int i;
    for(i=0;i<count;i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
    //取消映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(vir_rcc);
    // 2.注销字符设备驱动
    cdev_del(cdev);
    // 3.释放设备号
    unregister_chrdev_region(MKDEV(major, minor), count);
    // 4.释放动态申请的空间
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

test.c


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

int main(int argc, char const *argv[])
{
    char buf[128] = {};
    int which;
    int fd1, fd2, fd3;
    fd1 = open("/dev/my_led0", O_RDWR);
    if (fd1 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led0 success\n");
    fd2 = open("/dev/my_led1", O_RDWR);
    if (fd2 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led1 success\n");
    fd3 = open("/dev/my_led2", O_RDWR);
    if (fd3 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led2 success\n");
    while (1)
    {
        printf("please input cmd:0:LED OFF 1:LED ON\n");
        fgets(buf, sizeof(buf), stdin);

        buf[strlen(buf) - 1] = '\0';
        if (buf[0] == '1')
        {
            printf("please choose led:1->LED1  2->LED2 3->LED3\n");
            scanf("%d", &which);
            getchar();
            if (which == 1)
            {
                ioctl(fd1, LedON, &which);
            }
            if (which == 2)
            {
                ioctl(fd2, LedON, &which);
            }
            if (which == 3)
            {
                ioctl(fd3, LedON, &which);
            }
            // ioctl(fd,LedON,&which);
        }
        else
        {
            printf("please choose led:1->LED1  2->LED2 3->LED3\n");
            scanf("%d", &which);
            getchar();
            if (which == 1)
            {
                ioctl(fd1, LedOFF, &which);
            }
            if (which == 2)
            {
                ioctl(fd2, LedOFF, &which);
            }
            if (which == 3)
            {
                ioctl(fd3, LedOFF, &which);
            }
        }
    }
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名社畜L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值