题目: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