字符设备程序实列二-查询按键值,按键按下相应的LED灯亮,按键松开相应的LED灯灭

按照实例一,实现了从应用程序空间向内核空间传递数据,这一例实现从内核读取按键值到应用空间,然后把刚刚保存在应用空间按键值写到内核空间,内核空间按键值来操作对应的LED

驱动源码:keys_leds.c

驱动源码Makefile

测试源码: keys_leds_test.c

keys_leds.c:

/***************************************************************

*	filename:	keys_leds.c
*	description:	无按键按下,熄灭全部LED,按键按下,点亮相应LED,松开熄灭相应LED
*	author:		xyc
*	create time:	2014/6/2
*	version:1
*	modify info:
******************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm-arm/io.h>
#include <asm-arm/uaccess.h>

static int KEYS_LEDS_MAJOR;
static struct class *keys_leds;
static struct class_device *keys_leds_dev;
static volatile unsigned long *gpfcon=NULL;
static volatile unsigned long *gpfdat=NULL;

static int keys_leds_open(struct inode *inode, struct file *file)
{
	/*配置按键为输入,LED为输出*/

	*gpfcon &= ~(0x3<<(4*2)) & (~(0x3<<(0*2))) & (~(0x3<<(5*2)) ) & (~(0x3<<(2*2)));
	*gpfcon |= 0x1<<(4*2) |(0x0<<(0*2)) | (0x1<<(5*2) )|(0x0<<(2*2));
	*gpfdat |=1<<4|1<<5;	
	return 0;
}

static ssize_t keys_leds_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
	char val =0;//初始化为0不要忘
	/*记录按下的键*/
	if( !(*gpfdat &a
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值