题目:ioctl控制LED流水灯,KEY1键按下风扇转动。
代码:
mycdev.c
#include<linux/io.h>
#include<linux/device.h>
#include<linux/poll.h>
#include<linux/of.h>
#include <linux/init.h>
#include <linux/module.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 timer_list timer;
struct device_node *node_gpio;
struct device_node *node_fan;
struct device_node *node_irq;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct gpio_desc* fan;
int major;
struct class* class;
struct device* device;
int irqno;
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,
};
//入口函数
static int __init mycdev_init(void)
{
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);
}
/
node_gpio=of_find_node_by_name(NULL,"myleds");
if(node_gpio==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
node_fan=of_find_node_by_name(NULL,"myfan");
if(node_fan==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
node_irq=of_find_node_by_name(NULL,"myirqs");
if(node_irq==NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
gpiono1=gpiod_get_from_of_node(node_gpio,"led1",0,GPIOD_OUT_LOW,NULL);
gpiono2=gpiod_get_from_of_node(node_gpio,"led2",0,GPIOD_OUT_LOW,NULL);
gpiono3=gpiod_get_from_of_node(node_gpio,"led3",0,GPIOD_OUT_LOW,NULL);
fan=gpiod_get_from_of_node(node_fan,"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");
irqno=irq_of_parse_and_map(node_irq,2);
if(!irqno)
{
printk("获取中断失败\n");
return -ENXIO;
}
request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"myirq",NULL);
return 0;
}
//出口函数
static void __exit mycdev_exit(void)
{
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");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
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;
//GPIOE基地址
#define PHY_GPIOE_ADDR 0x50006000
#define PHY_GPIOF_ADDR 0x50007000
//RCC基地址:0x50000A28
#define PHY_RCC_LED1 0x50000A28
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
};
#endif