分布注册方式点灯

.h

#ifndef __HEAD_H__
#define __HEAD_H__ 
typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    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 _IOW('l',1,int)//开灯
#define LED_OFF _IOW('l',0,int)//关灯
#endif 

驱动

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


struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;

gpio_t *vir_led1;     //LED1
gpio_t *vir_led2;     //LED2  
gpio_t *vir_led3;     //LED3
unsigned int *vir_rcc;

//led初始化
int all_led_init(void)
{
    vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
    if(vir_led1==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
     vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
    if(vir_led2==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
     vir_led3=vir_led1;
    vir_rcc=ioremap(PHY_RCC_ADDR,4);
    if(vir_rcc==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器的初始化
    //rcc
    (*vir_rcc) |= (3<<4);
    //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_led1->MODER |= (1<<16);
    vir_led1->ODR &= (~(1<<8));
    printk("寄存器初始化成功\n");
    return 0;
}
//led注销
void Des_Led(void)
{
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
}

int mycdev_open(struct inode* inode,struct file *file)
{
    printk("%s:%s :%d",__FILE__,__func__,__LINE__);
    return 0;
}
int mycdev_close(struct inode* inode,struct file *file)
{
    printk("%s:%s :%d",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s :%d",__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",__FILE__,__func__,__LINE__);
    return 0;
}
long mycdev_ioctl(struct file*file,unsigned int cmd,unsigned long arg)
{
    int witch;
    int ret=copy_from_user(&witch,(unsigned int *)arg,4);
    if(ret)
    {
        printk("copy_from_user err\n");
        return ret;
    }
    switch(cmd)
    {
        case LED_ON:
            switch (witch)
            {
                case 1:
                    vir_led1->ODR |= (0x1 << 10);
                    break;
                case 2:
                    vir_led2->ODR |= (0x1 << 10);
                    break;
                case 3:
                    vir_led3->ODR |= (0x1 << 8);
                    break;
            }
            break;
        case LED_OFF:
            switch (witch)
            {
                case 1:
                    vir_led1->ODR &= (~(0x1 << 10));
                    break;
                case 2:
                    vir_led2->ODR &= (~(0x1 << 10));
                    break;
                case 3:
                    vir_led3->ODR &= (~(0x1 << 8));
                    break;
            }
            break;
    }
    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;
    int i;
    //1.分配字符设备驱动对象
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("字符设备驱动对象申请失败\n");
        ret=-EFAULT;
        goto OUT1;
    }
    cdev_init(cdev,&fops);
    if(major>0)
    {
        if(register_chrdev_region(MKDEV(major,minor),3,"mycdev"))               //MKDEV(major,minor)     MINOR(dev) MAJOR(dev)
        {
            printk("设备号(静态)申请失败\n");
            goto OUT2;
        }
    }else{
            ret = alloc_chrdev_region(&devno,minor,3,"mycdev");
            if(ret)
            {
                printk("设备号(动态)申请失败\n");
                goto OUT2;
            }
            major=MAJOR(devno);
            minor=MINOR(devno);
    }
    printk("设备号申请成功\n");
    ret = cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
    cls=class_create(THIS_MODULE,"mycdev2");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录成功\n");
    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");
    all_led_init();
    printk("LED初始化成功\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)
{
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major,minor),3);
    kfree(cdev);
    Des_Led();
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用

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

int main(int argc,const char * argv[])
{
    int fd;
    int a,b;
    char buf[128] = {};
    if((fd=open("/dev/mycdev0",O_RDWR))<0)
    {
        printf("打开设备文件失败\n");
        return 0;
    }
    printf("成功打开设备文件\n");
    while(1)
    {
        printf("请输入要实现的功能:1(开灯)0(关灯)");
        scanf("%d",&a);
        printf("请输入要控制的灯:1(LED1)2(LED2)3(LED3)>");
        scanf("%d",&b);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON,&b);
                break;
            case 0:
                ioctl(fd,LED_OFF,&b);
                break;
        }
    }
    close(fd);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值