通过字符设备驱动分步操作进行六个灯的操作

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 GPIO_RCC 0x50000a28
#define GPIO_RCC2 0x54000210
#define GPIOZ_ADDR 0x54004000
#define GPIOE_ADDR 0x50006000
#define GPIOF_ADDR 0x50007000

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

enum{
	LED_1,
	LED_2,
	LED_3,
	LED2_1,
	LED2_2,
	LED2_3,
};
#endif

mycdev.c

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

#define CNAME "mycdev"
unsigned int kbuf[128] = {0};
struct cdev* cdev;
dev_t devno;
unsigned int minor;
unsigned int* virt_rcc;
unsigned int* virt_rcc2;
gpio_t *virt_gpioe;
gpio_t *virt_gpiof;
gpio_t *virt_gpioz;

#if 1
    unsigned int major = 0;
#else
    unsigned int major = 500;
#endif 
struct class *cls;
struct device* dev;

#define LED_1_ON  (virt_gpioe->ODR |= (0x1 << 10))
#define LED_1_OFF  (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED_2_ON  (virt_gpiof->ODR |= (0x1 << 10))
#define LED_2_OFF  (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED_3_ON  (virt_gpioe->ODR |= (0x1 << 8))
#define LED_3_OFF  (virt_gpioe->ODR &= (~(0x1 << 8)))

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

int mycdev_open(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 args)
{
    int whitch;
    int res;
    switch(cmd)
    {
        case LED_ON:
        res = copy_from_user(&whitch,(void*)args,sizeof(args));
        if(res)
        {
            printk("copy_from_user failed\n");
            return -EIO;
        }
        switch (whitch)
        
        {
        case LED_1:
            LED_1_ON;
            break;
        case LED_2:
            LED_2_ON;
            break;
        case LED_3:
            LED_3_ON;
            break;
        case LED2_1:
            LED2_1_ON;
            break;
        case LED2_2:
            LED2_2_ON;
            break;
        case LED2_3:
            LED2_3_ON;
            break;
        }

        case LED_OFF:
        res = copy_from_user(&whitch,(void*)args,sizeof(args));
        if(res)
        {
            printk("copy_from_user failed\n");
            return -EIO;
        }
        switch (whitch) 
        {
        case LED_1:
            LED_1_OFF;
            break;
        case LED_2:
            LED_2_OFF;
            break;
        case LED_3:
            LED_3_OFF;
            break;
        case LED2_1:
            LED2_1_OFF;
            break;
        case LED2_2:
            LED2_2_OFF;
            break;
        case LED2_3:
            LED2_3_OFF;
            break;  
        }
    }
    return 0;
}

ssize_t mycdev_read(struct file* file,char __user* ubuf,size_t size,loff_t* offs)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__); 
    return 0;
}
ssize_t mycdev_write(struct file* file,const char __user* ubuf,size_t size,loff_t* offs)
{
    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;
}

const 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)
{
    int ret,i;

    // 1.分配对象
   cdev = cdev_alloc();
   if(cdev == NULL)
   {
    printk("cdev_alloc error\n");
    ret = -ENOMEM;
    goto ERR1;
   }
   printk("cdev_alooc success\n");

    // 2.对象初始化
   cdev_init(cdev,&fops);
   
    //3.设备资源的申请
   if(major == 0)
   {
        ret = alloc_chrdev_region(&devno,minor,6,"mycdev");
        if(ret)
        {
            printk("register_chrdev_region failed\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
   }
   else if(major > 0)
   {
        ret = register_chrdev_region(MKDEV(major,minor),6,"mycdev");
        if(ret)
        {
            printk("register_chrdev_region failed\n");
            goto ERR2;
        }
   }
   
   //4.注册
   ret = cdev_add(cdev,MKDEV(major,minor),6);
   if(ret)
   {
        printk("cdev_add failed\n");
        goto ERR3;
   }
   //5,向上提交目录
   cls = class_create(THIS_MODULE,"mycdev");
   if(IS_ERR(cls))
   {
        printk("class_create failed\n");
        goto ERR4;
   }
   printk("class_create success\n");

    // 6.向上提交设备节点信息
    for(i=0; i<6;i++)
    {
        dev = device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("device_create failed\n");
            goto ERR5;
        }
    }
    //将rcc物理地址映射为虚拟地址
    virt_rcc = ioremap(GPIO_RCC,4);
    if(virt_rcc == NULL)
    {
        printk("ioremap error\n");
        return -ENOMEM;
    }

     virt_rcc2 = ioremap(GPIO_RCC2,4);
    if(virt_rcc == NULL)
    {
        printk("ioremap error\n");
        return -ENOMEM;
    }
    //分别将GPIOE,GPIOF,GPIOZ的物理地址映射为虚拟地址
    virt_gpioe = ioremap(GPIOE_ADDR,4);
    if(virt_gpioe == NULL)
    {
        printk("ioremap error\n");
        return -ENOMEM;
    }

    virt_gpiof = ioremap(GPIOF_ADDR,4);
    if(virt_gpiof == NULL)
    {
        printk("ioremap error\n");
        return -ENOMEM;
    }

    virt_gpioz = ioremap(GPIOZ_ADDR,4);
    if(virt_gpioz == NULL)
    {
        printk("ioremap error\n");
        return -ENOMEM;
    }

    //RCC初始化
    *virt_rcc |= (0x1 << 4);
    *virt_rcc |= (0x1 << 5);
    *virt_rcc2 |= (0x1 << 0);
    //对LED_1初始化,设置引脚功能
    virt_gpioe->MODER &= (~(0x3 << 20));
    virt_gpioe->MODER |= (0x1 << 20);
    virt_gpioe->ODR &= (~(0x1 << 10));
    //对LED_2初始化,设置引脚功能
    virt_gpiof->MODER &= (~(0x3<<20));
    virt_gpiof->MODER |= (0x1<<20);
    virt_gpiof->ODR &= (~(0x1<<10));
    //对LED_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(cdev);    
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),6);
ERR2:
    kfree(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);

    for(i=0;i<6;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);

    cdev_del(cdev);

    unregister_chrdev(MKDEV(major,minor),CNAME);

    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 <errno.h>
#include <string.h>
#include "mycdev.h"
#include <sys/ioctl.h>


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

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

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

        whitch = LED_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);

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值