字符设备分布注册实现LED灯

目标:通过字符设备的分布注册实现LED灯的控制

字符分布注册:

 

test.c文件

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

int main(int argc,const char * argv[])
{
    char buf[1024] = {0};
    int a,b;
    int fd = open("/dev/mycdev0",O_RDWR); 
    if(fd < 0)
    {
        printf("打开设备失败\n");
        exit(-1);
    }

    while (1)
    {
        printf("实现功能:1(开灯) 0(关灯) > \n");
        scanf("%d",&a);
        printf("需要控制的灯1(LED1) 2(LED2) 3(LED3) > \n");
        scanf("%d",&b);
        switch (a)
        {
        case 1:
            ioctl(fd,LED_ON,&b);
            break;
        case 0:
            ioctl(fd,LED_OFF,&b);
            break;
        default:
            break;
        }
    }
    close(fd);
    return 0;
}

head.h文件

#ifndef __HEAD_H__
#define __HEAD_H__

typedef struct
{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPD;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;

#define PHY_LED1_ADDR 0x50006000
#define PHY_LED2_ADDR 0x50007000
#define PHY_LED3_ADDR 0x50006000
#define PHY_RCC_ADDR  0x50000A28

// #define LED_ON  _IO('l',1)//开灯
// #define LED_OFF  _IO('l',0)//关灯

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


#endif 

Character_devive.c文件

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


unsigned int major = 0;
unsigned int minor = 0;

gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;

struct cdev *cdev;
dev_t devnum;

//向上申请的结构体目录信息
struct class *cls;
//向上申请的结构体节点信息
struct device *dev;

//封装操作方法
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 *lof)
{
    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 *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

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

long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int which;
    int ret = copy_from_user(&which,(unsigned int *)arg,4);
    if(ret)
    {
        printk("copy_from_user is ERR\n");
        return ret;
    }
    switch (cmd)
    {
    case LED_ON:
        switch (which)
        {
        case 1:
            vir_led1->ODR |= 0x1 << 10;
            break;
        case 2:
            vir_led2->ODR |= 0x1 << 10;
            break;
        case 3:
            vir_led3->ODR |= 0x1 << 8;
            break;
        default:
            break;
        }
        break;
    case LED_OFF:
        switch (which)
        {
        case 1:
            vir_led1->ODR &= (~(0x1 << 10));
            break;
        case 2:
            vir_led2->ODR &= (~(0x1 << 10));
            break;
        case 3:
            vir_led3->ODR &= (~(0x1 << 8));
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
    return 0;
}

// 寄存器地址映射
int all_led_init(void)
{
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }

    vir_led2 = ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led3 = vir_led1;
    
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("物理内存映射成功\n");
    //rcc
    (*vir_rcc) |= 0x3 << 4;
    //LED1
    vir_led1->MODER &= (~(0x3 << 20));
    vir_led1->MODER |= 0x1 << 20;
    vir_led1->ODR &= (~(0x1 << 10));
    //LED2
    vir_led2->MODER &= (~(0x3 << 20));
    vir_led2->MODER |= 0x1 << 20;
    vir_led2->ODR &= (~(0x1 << 10));
    //LED3
    vir_led3->MODER &= (~(0x3 << 16));
    vir_led3->MODER |= 0x1 << 16;
    vir_led3->ODR &= (~(0x1 << 8));

    return 0;
}

struct file_operations fops = {
    .open = mycdev_open,
    .release = mycdev_close,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl=mycdev_ioctl,
};

static int __init mycdev_init(void)
{
    int ret;
    //1.分配字符设备驱动对象
    cdev = cdev_alloc();
    if(cdev == NULL)
    {
        printk("申请字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("申请字符设备驱动成功\n");
    //2.初始化字符设备驱动对象
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major > 0)//静态指定
    {
        ret = register_chrdev_region(MKDEV(major,minor),3,"mycdev");
        if(ret){
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    }else{
        ret = alloc_chrdev_region(&devnum,minor,3,"mycdev");
        if(ret){
        printk("静态指定设备号失败\n");
        goto OUT2;
        }
        minor = MINOR(devnum);
        major = MAJOR(devnum);
    }
    printk("申请设备号成功\n");
    //4.注册字符设备驱动对象
    ret = cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");

    all_led_init();
    
    // 向上提交目录信息
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录信息失败\n");
        ret = -PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录信息成功\n");

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

    OUT5:
        for(--i;i>=0;i++)
            device_destroy(cls,MKDEV(major,i));
        class_destroy(cls);
    OUT4:
        cdev_del(cdev);
    OUT3:
        unregister_chrdev_region(MKDEV(major,minor),3);
    OUT2:
        kfree(cdev);
    OUT1:
        return ret;
}

static void __exit mycdev_exit(void)
{
    // 取消物理映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);

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

串口运行结果:

开发板运行实例:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值