基于platform驱动模型和子系统实现LED的驱动编写,并且编写应用程序测试

//led.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)//关灯
#define LED1_ON _IO('l',1)
#endif

//test.c 测试

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


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

       
    close(fd);

    return 0;
}

//驱动
mydev.c

#include <linux/init.h>
#include <linux/module.h>
#include<linuxatform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include "led.h"
/*myplatform{
        compatible ="hqyj,platform";
        reg = <0x12345678 0x14>;
        interrupt-parent = <&gpiof>;
        interrupts = <9 0>;
        led1=<&gpioe 10 0>;

    };
*/
struct resource *res;
int irqno;
struct gpio_desc *gpiono;

int major;
char kbuf[128]={0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
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__);
     unsigned long ret;
    //向用户空间读取拷贝
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
    unsigned long ret;
    //从用户空间读取数据
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
//ioctl
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int which,ret;
    //将ioctl第三个参数的数值拷贝给which;
    ret=copy_from_user(&which,(void *)arg,sizeof(int));
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    //根据功能码进行硬件功能控制
    switch(cmd)
    {
        case LED_ON:
            switch(which)
            {
                case 1:
                    vir_led1->ODR |= (1<<10);
                    break;
                case 2:
                    vir_led2->ODR |= (1<<10);
                    break;
                case 3:
                    vir_led3->ODR |= (1<<8);
                    break;
            }
            break;
        case LED_OFF:
            switch(which)
            {
                case 1:
                    vir_led1->ODR &= (~(1<<10));
                    break;
                case 2:
                    vir_led2->ODR &= (~(1<<10));
                    break;
                case 3:
                    vir_led3->ODR &= (~(1<<8));
                    break;
            }
            break;

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

//分配对象并且初始化
//probe函数
int pdrv_probe(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //获取MEM类型设备资源
    res=platform_get_resource(pdev,IORESOURCE_MEM,0);
    if(res==NULL)
    {
        printk("获取设备信息失败\n");
        return -ENODATA;
    }
    //获取中断类型资源
    irqno=platform_get_irq(pdev,0);
    if(irqno<0)
    {
        printk("获取中断类型资源失败\n");
        return -ENODATA;
    }
    printk("mem类型资源数值为:%x\n",res->start);
    printk("中断类型的资源数值为%d\n",irqno);
    gpiono=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0, GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("解析gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiod_set_value(gpiono,1);
    return 0;
}
//remove函数
int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //灭灯
       gpiod_set_value(gpiono,0);
       gpiod_put(gpiono);
    return 0;
}
//构建设备树匹配标配
struct of_device_id oftable[]=
{
    {.compatible="hqyj,platform"},
     {.compatible="hqyj,platform1"},
    {},
};
//分配对象并且初始化
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="aaaaa",
        .of_match_table=oftable,//设置设备树匹配    
    },
};

//定义操作方法结构体变量并赋值
struct file_operations fops={

    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
    .unlocked_ioctl=mycdev_ioctl,
    .release=mycdev_close,
};

//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值