3月29号作业

该代码实现了一个Linux平台驱动,通过ioctl接口控制LED灯的流水效果,并在按键KEY1被按下时启动风扇转动。驱动程序使用GPIO接口与硬件交互,包含中断处理函数以响应按键事件。同时,它注册了一个字符设备并提供了打开、关闭及ioctl操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:platform架构完成,在应用层通过ioctl控制LED灯流水,当按键KEY1按下让风扇转动

代码:

platform_driver.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/poll.h>
#include<linux/of.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/timer.h>
#include"myled.h"
#include<linux/of_gpio.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
struct resource *res;
int irqno;
int major;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct gpio_desc* fan;
struct class* class;
struct device* device;
//分配对象并且初始化
//probe函数
irqreturn_t irq_handler(int irqno,void *arg)
{
    gpiod_set_value(fan,!gpiod_get_value(fan));
    return IRQ_HANDLED;
}

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

long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{

    //1.判断cmd switch(cmd)
    //2.判断操作哪盏灯进行点亮 copy_from_user
    int whitch;
    int ret;
    switch(cmd)
    {
    case LED_ON:
        ret = copy_from_user(&whitch,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (whitch)
        {
            case LED1:
                gpiod_set_value(gpiono1,1);
                break;
            case LED2:
                gpiod_set_value(gpiono2,1);
                break;
            case LED3:
                gpiod_set_value(gpiono3,1);
                break;
        }
    break;
    case LED_OFF:
        ret = copy_from_user(&whitch,(void*)args,sizeof(int));
        if(ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (whitch)
        {
            case LED1:
                gpiod_set_value(gpiono1,0);
                break;
            case LED2:
                gpiod_set_value(gpiono2,0);
                break;
            case LED3:
                gpiod_set_value(gpiono3,0);
                break;
        }
        break;    
    }
    return 0;
}
const struct file_operations fops = {
    .open = myled_open,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};
int pdrv_probe(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    major=register_chrdev(0,"mycdev",&fops);
    if(major<0)
    {
        printk("注册字符设备驱动失败\n");
        return -EIO;
    }
    class=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(class))
    {
        printk("向上提交目录失败\n");
        return -EIO;
    }
    device=device_create(class,NULL,MKDEV(major,0),NULL,"mycdev0");
    if(IS_ERR(device))
    {
        printk("向上提交设备节点失败\n");
        return PTR_ERR(device);
    }
    //获取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);

    gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
    gpiono2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
    gpiono3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
    fan=gpiod_get_from_of_node(pdev->dev.of_node,"fan",0,GPIOD_OUT_LOW,NULL);

    if(IS_ERR(gpiono1)||IS_ERR(gpiono2)||IS_ERR(gpiono3)||IS_ERR(fan))
    {
        printk("解析gpio编号失败\n");
        return -EIO;
    }
    printk("解析gpio编号成功\n");

    request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"myirq",NULL);

    return 0;
}
//remove函数
int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    free_irq(irqno,NULL);
    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    gpiod_set_value(gpiono3,0);
    gpiod_set_value(fan,0);
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
    gpiod_put(fan);
    device_destroy(class,MKDEV(major,0));
    class_destroy(class);
    unregister_chrdev(major,"mycdev");
    return 0;
}

struct of_device_id oftable[]=
{
    {.compatible="hqyj,irq"},
    {},
};

//分配对象并且初始化
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="my",    
        .of_match_table=oftable,
    },
};

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

text.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 <sys/ioctl.h>
#include "myled.h"


int main(int argc, const char *argv[])
{
    int whitch;
    int fd = -1;

    fd = open("/dev/mycdev0", O_RDWR);
    if (fd == -1)
    {
        perror("open is error\n");
        return -1;
    }
    
    while (1)
    {
        whitch = LED1;
        ioctl(fd, LED_ON, &whitch);
        sleep(1);
        ioctl(fd, LED_OFF, &whitch);
        sleep(1);

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

        whitch = LED3;
        ioctl(fd, LED_ON, &whitch);
        sleep(1);
        ioctl(fd, LED_OFF, &whitch);
        sleep(1);
    }
    close(fd);
    return 0;
}

myled.h

#ifndef __MYLED_H__
#define __MYLED_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;

enum{
	LED1,
	LED2,
	LED3,
	LED4,
	LED5,
	LED6
};
typedef struct 
{
	int width;
	int high;
}image_t;
//GPIOE基地址
#define PHY_GPIOE_ADDR 0x50006000
#define PHY_GPIOF_ADDR 0x50007000
#define PHY_GPIOZ_ADDR 0x54004000
//RCC基地址:0x50000A28
#define PHY_RCC_LED_123 0x50000A28
#define PHY_RCC_LED_456 0x54000210

#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#define UACCESS_BUF _IOW('a',1,char[128])
#define UACCESS_STRUCT _IOW('a',1,image_t)



#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值