micro2440利用LEDS与BUTTONS驱动实现按键控制led灯【开发总结】

2023-11-09 12:32

本文主要是介绍micro2440利用LEDS与BUTTONS驱动实现按键控制led灯【开发总结】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近玩友善之臂的micro2440,实现了按键控制led灯亮灭的功能。在这里总结一下,有什么错误希望高手们能指点一下,同时也希望能够帮到刚学驱动的新手。

首先贴出leds驱动程序:

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>#define DEVICE_NAME "leds"static unsigned long led_table [] = {S3C2410_GPB5,S3C2410_GPB6,S3C2410_GPB7,S3C2410_GPB8,
};static unsigned int led_cfg_table [] = {S3C2410_GPB5_OUTP,S3C2410_GPB6_OUTP,S3C2410_GPB7_OUTP,S3C2410_GPB8_OUTP,
};static int sbc2440_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{switch(cmd) {case 0:case 1:if (arg > 4) {return -EINVAL;}s3c2410_gpio_setpin(led_table[arg], !cmd);return 0;default:return -EINVAL;}
}static struct file_operations dev_fops = {.owner	=	THIS_MODULE,.ioctl	=	sbc2440_leds_ioctl,
};static struct miscdevice misc = {.minor = MISC_DYNAMIC_MINOR,.name = DEVICE_NAME,.fops = &dev_fops,
};static int __init dev_init(void)
{int ret;int i;for (i = 0; i < 4; i++) {s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);s3c2410_gpio_setpin(led_table[i], 0);}ret = misc_register(&misc);printk (DEVICE_NAME"\tinitialized\n");return ret;
}static void __exit dev_exit(void)
{misc_deregister(&misc);
}module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

此驱动关键的地方是给出了一个ioctl的设备方法,在应用程序中使用ioctl(leds的fd,cmd,arg)系统调用时,系统会进入内核态并调用sbc2440_leds_ioctl(),以控制灯的亮灭。

下面贴出buttons的驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>#define DEVICE_NAME     "buttons"struct button_irq_desc {int irq;int pin;int pin_setting;int number;char *name;	
};#if !defined (CONFIG_QQ2440_BUTTONS)
static struct button_irq_desc button_irqs [] = {{IRQ_EINT8 , S3C2410_GPG0 ,  S3C2410_GPG0_EINT8  , 0, "KEY0"},{IRQ_EINT11, S3C2410_GPG3 ,  S3C2410_GPG3_EINT11 , 1, "KEY1"},{IRQ_EINT13, S3C2410_GPG5 ,  S3C2410_GPG5_EINT13 , 2, "KEY2"},{IRQ_EINT14, S3C2410_GPG6 ,  S3C2410_GPG6_EINT14 , 3, "KEY3"},{IRQ_EINT15, S3C2410_GPG7 ,  S3C2410_GPG7_EINT15 , 4, "KEY4"},{IRQ_EINT19, S3C2410_GPG11,  S3C2410_GPG11_EINT19, 5, "KEY5"},
};
#else /* means QQ */
static struct button_irq_desc button_irqs [] = {{IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, 0, "KEY0"},{IRQ_EINT11, S3C2410_GPG3,  S3C2410_GPG3_EINT11,  1, "KEY1"},{IRQ_EINT2,  S3C2410_GPF2,  S3C2410_GPF2_EINT2,   2, "KEY2"},{IRQ_EINT0,  S3C2410_GPF0,  S3C2410_GPF0_EINT0,   3, "KEY3"},{       -1,            -1,                 -1,    4, "KEY4"},{       -1,            -1,                 -1,    5, "KEY5"},
};
#endif
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press = 0;static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;int down;// udelay(0);down = !s3c2410_gpio_getpin(button_irqs->pin);if (down != (key_values[button_irqs->number] & 1)) { // Changedkey_values[button_irqs->number] = '0' + down;ev_press = 1;wake_up_interruptible(&button_waitq);}return IRQ_RETVAL(IRQ_HANDLED);
}static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{int i;int err = 0;for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) {continue;}err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH, button_irqs[i].name, (void *)&button_irqs[i]);if (err)break;}if (err) {i--;for (; i >= 0; i--) {if (button_irqs[i].irq < 0) {continue;}disable_irq(button_irqs[i].irq);free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);}return -EBUSY;}ev_press = 1;return 0;
}static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{int i;for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) {continue;}free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);}return 0;
}static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{unsigned long err;if (!ev_press) {if (filp->f_flags & O_NONBLOCK)return -EAGAIN;elsewait_event_interruptible(button_waitq, ev_press);}ev_press = 0;err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));return err ? -EFAULT : min(sizeof(key_values), count);
}static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{unsigned int mask = 0;poll_wait(file, &button_waitq, wait);if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;
}static struct file_operations dev_fops = {.owner   =   THIS_MODULE,.open    =   s3c24xx_buttons_open,.release =   s3c24xx_buttons_close, .read    =   s3c24xx_buttons_read,.poll    =   s3c24xx_buttons_poll,
};static struct miscdevice misc = {.minor = MISC_DYNAMIC_MINOR,.name = DEVICE_NAME,.fops = &dev_fops,
};static int __init dev_init(void)
{int ret;ret = misc_register(&misc);printk (DEVICE_NAME"\tinitialized\n");return ret;
}static void __exit dev_exit(void)
{misc_deregister(&misc);
}module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

该驱动的关键(按键驱动的具体分析请看我的另一篇博客 mini2440_buttons.c驱动程序解读)

1.是s3c24xx_buttons_read的设备方法,在应用程序中使用read(buttons的fs,按键状态数组,读多少位)系统调用时,会进入内核态调用该s3c24xx_buttons_read函数,改函数通过wait _event_interruptible()进入可中断的休眠状态,等待中断的到来

2.是定义了buttons_interrupt中断服务函数,该函数对相应的键值进行处理,并唤醒休眠。


应用程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include<string.h>
int main(void)
{int buttons_fd;char buttons[6] = {'0', '0', '0', '0', '0', '0'};int leds_fd;int save=0;int j = 0;unsigned char s=0;buttons_fd = open("/dev/buttons", 0);if (buttons_fd < 0) {perror("open device buttons");exit(1);}leds_fd = open("/dev/leds",0);if(leds_fd < 0){perror("open device leds");exit(1);}ioctl(leds_fd,0,0);ioctl(leds_fd,0,1);ioctl(leds_fd,0,2);ioctl(leds_fd,0,3);while (1) {// char current_buttons[6]={'0','0','0','0','0','0'};char save[6]={'0','0','0','0','0','0'};int i;//while((strncmp(save,current_buttons,6))==0)//{if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {perror("read buttons:");exit(1);}		//}if((strncmp(save,current_buttons,6))==0)continue;for ( i = 0; i < sizeof (current_buttons) / sizeof (current_buttons[0]); i++) {if (save[i] != current_buttons[i]) {//                 buttons[i] = current_buttons[i];printf("The %d key is pressed!(s=%d)\n",i+1,s);ioctl(leds_fd,(~s)&1,i);}}//  end of for           s=~s;}//end of while(1)close(buttons_fd);close(leds_fd);return 0;
}
每当有按键按下时,current_buttons对应的键值会从0变成1,定位到这个1,并对相应的leds进行控制,通过s取反实现亮灭变化。

这篇关于micro2440利用LEDS与BUTTONS驱动实现按键控制led灯【开发总结】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/376116

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景