点亮LED1灯

pdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

//资源信息结构体
struct resource res[] = {
    [0] = {
        .start = 0x50006000,  //GPIOE的起始地址
        .end = 0x50006000 + 31,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 0x50000A28,  //RCC的起始地址
        .end = 0x50000A28 + 3,
        .flags = IORESOURCE_MEM,
    },
};
//信息端卸载时执行release函数
void pdev_release(struct device *dev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
}
//设备信息端结构体初始化
struct platform_device pdev = {
    .name = "Hello world",
    .id = PLATFORM_DEVID_AUTO,
    .dev = {
        .release = pdev_release,
    },
    .num_resources = ARRAY_SIZE(res),
    .resource = res,
};


//入口函数
static int __init pdev_init(void)
{
    //注册设备信息端
    return platform_device_register(&pdev);
}

//出口函数
static void __exit pdev_exit(void)
{
    //注销设备信息端
    platform_device_unregister(&pdev);
}

module_init(pdev_init);  //指定入口地址
module_exit(pdev_exit);  //指定出口地址
MODULE_LICENSE("GPL");   //遵循GPL协议

pdrv.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of_gpio.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include "myled.h"

struct resource* res[2];
unsigned int* rcc_virt = NULL; //RCC虚拟地址
gpio_t* gpioe_virt = NULL;  //gpioe组虚拟地址
struct device* device = NULL;
struct class* cls = NULL;
#define CNAME "myled"
unsigned int major = 0;  //返回主设备号的值
#define LED1_ON (gpioe_virt->ODR |= (0x1 << 10))  //LED1点亮
#define LED1_OFF (gpioe_virt->ODR &= (~(0x1 << 10)))  //LED1熄灭

int mycdev_open(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//LED1灯的初始化功能函数
int led1_init(struct resource* res1,struct resource* res2)
{
    //GPIOE组物理地址映射到虚拟地址
    gpioe_virt = ioremap(res1->start,(res1->end - res1->start+1));
    if(gpioe_virt == NULL)
    {
        printk("ioremap is error\n");
        return -EIO;
    }
    //RCC地址映射到虚拟地址
    rcc_virt = ioremap(res2->start,(res2->end - res2->start+1));
    if(rcc_virt == NULL)
    {
        printk("ioremap is error\n");
        return -EIO;
    }
    //LED1初始化  PE10
    //设置时钟使能
    *rcc_virt |= (0x1 << 4);
    //将引脚设置为输出模式
    gpioe_virt->MODER &= (~(0x3<<20));
    gpioe_virt->MODER |= (0x1<<20);
    //默认低电平输出
    gpioe_virt->ODR &= (~(0x1<<10));
    return 0;
}

void led_ioctl(int whitch,int status)
{
    switch(whitch)
    {
        case led1:
            status == 1 ? LED1_ON:LED1_OFF;
            printk("kkk\n");
        break;
        case led2:
        break;
        case led3:
        break;
    }
}
long mycdev_ioctl(struct file *file,unsigned int cmd, unsigned long args)
{
    switch (cmd)
    {
    case LED_ON: //灯点亮
        led_ioctl(led1,1);
        printk("fsafsf\n");
        break;
    case LED_OFF: //灯熄灭 
        led_ioctl(led1,0); 
        printk("hhhhf\n");
        break;
    default:
        break;
    }
    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,
     .unlocked_ioctl = mycdev_ioctl,
     .release = mycdev_close, 
};

//当设备驱动端和设备信息端匹配成功后,进入probe函数
int pdrv_probe(struct platform_device *pdev)
{  
    int i = 0;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //获取设备信息端内容
    for(i=0;i<2;i++)
    {
      res[i] = platform_get_resource(pdev,IORESOURCE_MEM,0);
      if(res == NULL){
        printk("platform get resource is error\n");
        return -EIO;
      }
    }
    //打印从设备信息端获取到的信息
    printk("gpioe = %#x rcc=%#x\n",res[0]->start,res[1]->start);
    //LED地址映射,寄存器初始化
    led1_init(res[0],res[1]);
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0)
    {
        printk("register chrdev is error\n");
        return -EIO;
    }
    printk("major = %d\n",major);
    //向上层提交目录信息
    cls = class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
        printk("cls create is error\n");
        return PTR_ERR(cls);
    }
    //向上层提交设备节点信息
    device = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
    if(IS_ERR(device))
    {
        printk("device create is error\n");
        return PTR_ERR(device);
    }
    return 0;
}
//当任意一方卸载,执行remove函数
int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //取消地址映射
    iounmap(rcc_virt);
    iounmap(gpioe_virt);
    device_destroy(cls,MKDEV(major,0));//取消向上层提交设备节点信息
    class_destroy(cls); //取消向上层提交目录信息   
    unregister_chrdev(major,CNAME);//注销字符设备驱动
    return 0;
}

//初始化设备驱动端结构体
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = { //设备驱动端父类
        .name = "Hello world", //用于匹配名字
    },
};

//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

test.c

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

int main(int argc, char const *argv[])
{
    int fd = -1;
    int whitch = 0;
    fd = open("/dev/myled",O_RDWR);
    if(fd == -1)
    {
        perror("open is error");
        return -1;
    }
    while(1)
    {
        ioctl(fd,LED_ON);
        sleep(1);
        ioctl(fd,LED_OFF);
        sleep(1);
    }
    close(fd);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值