头文件:
#ifndef __LED_H__
#define __LED_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define PHY_LED2_RCC 0X50000A28
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR 0X50006014
#define PHY_LED3_RCC 0X50000A28
//风扇
#define PHY_FAN_MODER 0X50006000
#define PHY_FAN_ODR 0X50006014
#define PHY_FAN_RCC 0X50000A28
//蜂鸣器
#define PHY_BUZZER_MODER 0X50003000
#define PHY_BUZZER_ODR 0X50003014
#define PHY_BUZZER_RCC 0X50000A28
//马达
#define PHY_MOTOR_MODER 0X50007000
#define PHY_MOTOR_ODR 0X50007014
#define PHY_MOTOR_RCC 0X50000A28
//功能码
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#define FAN_ON _IO('f',1)
#define FAN_OFF _IO('f',0)
#define BUZZER_ON _IO('b',1)
#define BUZZER_OFF _IO('b',0)
#define MOTOR_ON _IO('m',1)
#define MOTOR_OFF _IO('m',0)
#endif
应用层程序:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int led1_fd, led2_fd, led3_fd;
// 打开led设备文件
led1_fd = open("/dev/mycdev0", O_RDWR);
if (led1_fd < 0)
{
printf("打开led1设备文件失败\n");
exit(-1);
}
led2_fd = open("/dev/mycdev1", O_RDWR);
if (led2_fd < 0)
{
printf("打开led2设备文件失败\n");
exit(-1);
}
led3_fd = open("/dev/mycdev2", O_RDWR);
if (led3_fd < 0)
{
printf("打开led3设备文件失败\n");
exit(-1);
}
int a, b;
while (1)
{
printf("请选择要控制的器件: 1(led灯1) 2 (led灯2) 3 (led灯3) ");
scanf("%d", &a);
printf("请输入指令:0(关闭) 1 (打开)");
scanf("%d", &b);
switch (b)
{
case 1:
switch (a)
{
case 1:
ioctl(led1_fd, LED_ON, a);
break;
case 2:
ioctl(led2_fd, LED_ON, a);
break;
case 3:
ioctl(led3_fd, LED_ON, a);
break;
}
break;
case 0:
switch (a)
{
case 1:
ioctl(led1_fd, LED_OFF, a);
break;
case 2:
ioctl(led2_fd, LED_OFF, a);
break;
case 3:
ioctl(led3_fd, LED_OFF, a);
break;
}
break;
}
}
close(led1_fd);
close(led2_fd);
close(led3_fd);
return 0;
}
驱动程序:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include "head.h"
struct cdev* mycdev;
struct class* cls;
struct device* dev_d;
unsigned int major = 500;
unsigned int minor = 0;
dev_t devno;
char kbuf[128] = {0};
wait_queue_head_t wq_head;
int condition = 0;
struct device_node *dev,*led_dev;
unsigned int irqno1,irqno2,irqno3;
struct gpio_desc* gpiono1,*gpiono2,*gpiono3;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
int a = inode->i_rdev;
file->private_data = (void *)MINOR(a);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mychrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{ unsigned int a;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
a = (unsigned int)file->private_data;
switch(a)
{
case 0:
if(cmd == LED_ON){
gpiod_set_value(gpiono1,1);
}
else{
gpiod_set_value(gpiono1,0);
}
break;
case 1:
if(cmd == LED_ON){
gpiod_set_value(gpiono2,1);
}
else{
gpiod_set_value(gpiono2,0);
}
break;
case 2:
if(cmd == LED_ON){
gpiod_set_value(gpiono3,1);
}
else{
gpiod_set_value(gpiono3,0);
}
break;
}
return 0;
}
irqreturn_t myirq_handler(int irq,void *dev)
{
gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
return IRQ_HANDLED;
}
irqreturn_t myirq2_handler(int irq,void *dev)
{
gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
return IRQ_HANDLED;
}
irqreturn_t myirq3_handler(int irq,void *dev)
{
gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
return IRQ_HANDLED;
}
//对设备文件进行操作的结构体
struct file_operations fops = {
.open = mycdev_open,//打开
.unlocked_ioctl = mychrdev_ioctl,//硬件功能的选择
.release = mycdev_close,//关闭
};
static int __init mycdev_init(void)
{
int i;
major = register_chrdev(0,"mycdev",&fops);
if(major<0){
printk("注册设备驱动失败\n");
return major;
}
printk("注册设备驱动成功major=%d\n",major);
//************自动申请设备节点*********************
//1、向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if(IS_ERR(cls)){
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 2、向上提交设备节点信息
for (i = 0; i < 3; i++)
{
dev_d = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
if (IS_ERR(dev_d))
{
printk("提交led设备信息失败\n");
return -PTR_ERR(dev_d);
}
}
printk("提交设备信息成功\n");
/***********************************/
//解析设备树的节点
int ret;
dev = of_find_node_by_name(NULL,"myirq");
if(dev == NULL){
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析myirq设备树节点成功\n");
led_dev = of_find_node_by_name(NULL,"leds");
if(led_dev == NULL){
printk("解析led设备树节点失败\n");
return -EIO;
}
printk("解析led设备树节点成功\n");
gpiono1 = gpiod_get_from_of_node(led_dev,"led1-gpios",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1)){
printk("申请gpio对象失败\n");
return -PTR_ERR(gpiono1);
}
gpiono2 = gpiod_get_from_of_node(led_dev,"led2-gpios",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2)){
printk("申请gpio2对象失败\n");
return -PTR_ERR(gpiono2);
}
gpiono3 = gpiod_get_from_of_node(led_dev,"led3-gpios",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3)){
printk("申请gpio3对象失败\n");
return -PTR_ERR(gpiono3);
}
//根据设备树节点解析出软中断号
irqno1 = irq_of_parse_and_map(dev,0);
if(!irqno1){
printk("解析led1软中断号失败\n");
return -ENXIO;
}
printk("解析软led1中断号成功irqno=%d\n",irqno1);
irqno2 = irq_of_parse_and_map(dev,1);
if(!irqno2){
printk("解析led2软中断号失败\n");
return -ENXIO;
}
printk("解析led2软中断号成功irqno=%d\n",irqno2);
irqno3 = irq_of_parse_and_map(dev,2);
if(!irqno3){
printk("解析led3软中断号失败\n");
return -ENXIO;
}
printk("解析led3软中断号成功irqno=%d\n",irqno3);
//注册中断
ret = request_irq(irqno1,myirq_handler,IRQF_TRIGGER_FALLING,"key1",NULL);
if(ret){
printk("key1软中断号注册失败\n");
return ret;
}
printk("key1软中断号注册成功\n");
ret = request_irq(irqno2,myirq2_handler,IRQF_TRIGGER_FALLING,"key2",NULL);
if(ret){
printk("key2软中断号注册失败\n");
return ret;
}
printk("key2软中断号注册成功\n");
ret = request_irq(irqno3,myirq3_handler,IRQF_TRIGGER_FALLING,"key3",NULL);
if(ret){
printk("key3软中断号注册失败\n");
return ret;
}
printk("key3软中断号注册成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
//灭灯
gpiod_set_value(gpiono1,0);
//释放gpio编号
gpiod_put(gpiono1);
//灭灯
gpiod_set_value(gpiono2,0);
//释放gpio编号
gpiod_put(gpiono2);
//灭灯
gpiod_set_value(gpiono3,0);
//释放gpio编号
gpiod_put(gpiono3);
//注销中断号
free_irq(irqno1,NULL);
free_irq(irqno2,NULL);
free_irq(irqno3,NULL);
//销毁节点信息
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
//销毁目录信息
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");