1、作业要求
使用platform架构完成,在应用层通过ioctl控制LED灯流水,当按下按键KEY1让风扇转动。
2、实现过程
1)myled.h头文件
#ifndef __MYLED_H__
#define __MYLED_H__
#define LED_ON _IOW('a', 1, int)
#define LED_OFF _IOW('a', 0, int)
enum{
LED1,
LED2,
LED3,
};
#endif
2)pdrv.c驱动源文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include "myled.h"
#define CNAME "myled"
struct device_node *dnode;
int irqno1, major, i;
char *led[]={"led1", "led2", "led3"};
struct gpio_desc *gpiono[4];
irqreturn_t irq1_handle(int irqno1, void *arg)
{
gpiod_set_value(gpiono[3], !gpiod_get_value(gpiono[3]));
return IRQ_HANDLED;
}
int myled_open(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(gpiono[0], 1);
break;
case LED2:
gpiod_set_value(gpiono[1], 1);
break;
case LED3:
gpiod_set_value(gpiono[2], 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(gpiono[0], 0);
break;
case LED2:
gpiod_set_value(gpiono[1], 0);
break;
case LED3:
gpiod_set_value(gpiono[2], 0);
break;
}
break;
}
return 0;
}
int myled_close (struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
//分配对象并且初始化,probe函数
int pdrv_probe(struct platform_device *pdev)
{
printk("%s : %s : %d\n", __FILE__, __func__, __LINE__);
//注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0)
{
printk("register chrdev is error\n");
}
//打印主设备号
printk("register chrdev major=%d\n",major);
//解析设备树节点
dnode=of_find_node_by_name(NULL, "myleds");
if(dnode==NULL) {
printk("设备树节点解析失败\n");
return -EIO;
}
printk("设备树节点解析成功\n");
//获取中断类型资源
irqno1=platform_get_irq(pdev, 0);
if(irqno1<0) {
printk("获取中断类型资源失败\n");
return -ENODATA;
}
//注册软中断号
request_irq(irqno1, irq1_handle, IRQF_TRIGGER_FALLING, "myirq1", NULL);
for(i=0; i<3; i++) {
gpiono[i]=gpiod_get_from_of_node(pdev->dev.of_node, led[i], 0, GPIOD_OUT_LOW, NULL);
if(IS_ERR(gpiono[i])) {
printk("解析gpio%d编号失败\n", i);
return PTR_ERR(gpiono[i]);
}
}
gpiono[3]=gpiod_get_from_of_node(pdev->dev.of_node, "fan", 0, GPIOD_OUT_LOW, NULL);
if(IS_ERR(gpiono[3])) {
printk("解析gpio3编号失败\n");
return PTR_ERR(gpiono[3]);
}
return 0;
}
//remove函数
int pdrv_remove(struct platform_device *pdev)
{
printk("%s : %s : %d\n", __FILE__, __func__, __LINE__);
//注销软中断
free_irq(irqno1, NULL);
for(i=0; i<4; i++) {
//释放gpiod
gpiod_set_value(gpiono[i], 0);
gpiod_put(gpiono[i]);
}
return 0;
}
//构建设备树匹配标配
struct of_device_id oftable[] =
{
{.compatible = "hqyj,platform"},
{},
};
//分配对象并且初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "aaaaa",
.of_match_table = oftable,
},
};
//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
3)test.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, char const *argv[])
{
int whitch;
int fd = -1;
fd = open("/dev/myled", O_RDWR);
if(fd == -1) {
perror("open id error\n");
return -1;
}
while(1) {
whitch = LED1;
ioctl(fd, LED_ON, &whitch);
sleep(1);
whitch = LED2;
ioctl(fd, LED_ON, &whitch);
sleep(1);
whitch = LED3;
ioctl(fd, LED_ON, &whitch);
sleep(1);
whitch = LED1;
ioctl(fd, LED_OFF, &whitch);
sleep(1);
whitch = LED2;
ioctl(fd, LED_OFF, &whitch);
sleep(1);
whitch = LED3;
ioctl(fd, LED_OFF, &whitch);
sleep(1);
}
close(fd);
return 0;
}