linux子系统led,Linux输入子系统(input subsystem)之按键输入和LED控制

实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭。

1.代码

input_subsys_drv.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

struct pin_desc{

int irq;

char *name;

unsigned int pin;

unsigned int key_val;

};

struct pin_desc pins_desc[4] = {

{IRQ_EINT0,  "S2", S3C2410_GPF0,  KEY_L},

{IRQ_EINT2,  "S3", S3C2410_GPF2,  KEY_S},

{IRQ_EINT11, "S4", S3C2410_GPG3,  KEY_ENTER},

{IRQ_EINT19, "S5",  S3C2410_GPG11, KEY_LEFTSHIFT},

};

static struct input_dev *input_subsys_dev;

static struct pin_desc *irq_pd;

static struct timer_list buttons_timer;

static irqreturn_t buttons_irq(int irq, void *dev_id)

{

/* [cgw]: 按键IO发生边沿中断时重新设置定时间隔

* 用于按键消抖

*/

irq_pd = (struct pin_desc *)dev_id;

buttons_timer.data = irq_pd->pin;

mod_timer(&buttons_timer, jiffies+HZ/100);

return IRQ_RETVAL(IRQ_HANDLED);

}

static void buttons_timer_function(unsigned long data)

{

struct pin_desc * pindesc = irq_pd;

unsigned int pinval;

if (!pindesc)

return;

/* [cgw]: 获取按键IO状态 */

pinval = s3c2410_gpio_getpin((unsigned int)data);

/* [cgw]: 根据按键IO状态上报按键事件 */

if (pinval)

{

/* [cgw]: 上报按键弹起 */

input_report_key(input_subsys_dev, pindesc->key_val, 0);

//input_sync(input_subsys_dev);

}

else

{

/* [cgw]: 上报按键按下 */

input_report_key(input_subsys_dev, pindesc->key_val, 1);

//input_sync(input_subsys_dev);

}

//printk("timer occur!\n");

}

static int led_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)

{

printk("led event!\n");

printk("value: 0x%x\n", value);

/* [cgw]: 根据应用程序下发的LED控制事件

* 亮灭LED

*/

//if (code == SND_BELL) {

if (code == LED_MUTE) {

if (value == 0xAA) {

/* [cgw]: 点亮 */

s3c2410_gpio_setpin(S3C2410_GPF4, 0);

} else if (value == 0xEE) {

/* [cgw]: 熄灭 */

s3c2410_gpio_setpin(S3C2410_GPF4, 1);

}

return 0;

}

return -1;

}

int input_subsys_open(struct input_dev *dev)

{

int i, retval;

/* [cgw]: 设置按键IO为中断输入 */

s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0);

s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);

s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11);

s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19);

/* [cgw]: 设置LED IO为输出,初始为熄灭LED */

s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);

s3c2410_gpio_setpin(S3C2410_GPF4, 1);

s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);

s3c2410_gpio_setpin(S3C2410_GPF5, 1);

s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);

s3c2410_gpio_setpin(S3C2410_GPF5, 1);

/* [cgw]: 为按键IO分配中断线 */

for (i = 0; i < 4; i++)

{

retval = request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]);

}

printk("input subsys open!\n");

return 0;

}

static int input_subsys_init(void)

{

/* [cgw]: 分配一个输入设备 */

input_subsys_dev = input_allocate_device();

input_subsys_dev->name = "input_subsys_dev";

/* [cgw]: 设置支持的事件类型 */

set_bit(EV_KEY, input_subsys_dev->evbit);

//set_bit(EV_REP, input_subsys_dev->evbit);

set_bit(EV_LED, input_subsys_dev->evbit);

//set_bit(EV_SND, input_subsys_dev->evbit);

/* [cgw]: 设置支持的事件码 */

set_bit(KEY_L, input_subsys_dev->keybit);

set_bit(KEY_S, input_subsys_dev->keybit);

set_bit(KEY_ENTER, input_subsys_dev->keybit);

set_bit(KEY_LEFTSHIFT, input_subsys_dev->keybit);

set_bit(LED_MUTE, input_subsys_dev->ledbit);

//set_bit(SND_BELL, input_subsys_dev->sndbit);

/* [cgw]: 分配输入设备的open方法,用户操作open(/dev/xxx, ...)时调用 */

input_subsys_dev->open = input_subsys_open;

/* [cgw]: 分配输入设备的event方法,用户在应用程序write()时 */

input_subsys_dev->event = led_event;

/* [cgw]: 注册输入设备 */

input_register_device(input_subsys_dev);

/* [cgw]: 初始化定时器,用于按键消抖 */

init_timer(&buttons_timer);

buttons_timer.function = buttons_timer_function;

add_timer(&buttons_timer);

printk("input subsys init!\n");

return 0;

}

static void input_subsys_exit(void)

{

int i;

/* [cgw]: 释放按键IO中断 */

for (i = 0; i < 4; i++)

{

free_irq(pins_desc[i].irq, &pins_desc[i]);

}

/* [cgw]: 删除定时器 */

del_timer(&buttons_timer);

/* [cgw]: 注销输入设备 */

input_unregister_device(input_subsys_dev);

/* [cgw]: 释放输入设备内存空间 */

input_free_device(input_subsys_dev);

}

module_init(input_subsys_init);

module_exit(input_subsys_exit);

MODULE_LICENSE("GPL");

input_subsys_test.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int fd;

void my_signal_fun(int signum)

{

struct input_event buttons_event, leds_event;

/* [cgw]: 异步通知产生时返回的数据 */

read(fd, &buttons_event, sizeof(struct input_event));

/* [cgw]: 打印事件类型,事件码,事件值 */

printf("type: 0x%x code: 0x%x value: 0x%x\n",

buttons_event.type,

buttons_event.code,

buttons_event.value);

/* [cgw]: 返回的是KEY_L或KEY_S值 */

if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {

/* [cgw]: 按键弹起 */

if (buttons_event.value == 0) {

/* [cgw]: 构造一个EV_LED事件 */

//leds_event.type = EV_SND;

leds_event.type = EV_LED;

//leds_event.code = SND_BELL;

leds_event.code = LED_MUTE;

/* [cgw]: KEY_L和KEY_S控制LED的亮灭 */

if (buttons_event.code == KEY_L) {

leds_event.value = 0xAA;

} else if (buttons_event.code == KEY_S) {

leds_event.value = 0xEE;

}

/* [cgw]: 发送LED控制事件 */

write(fd, &leds_event, sizeof(struct input_event));

printf("led write!\n");

}

}

}

int main(int argc, char **argv)

{

int Oflags;

/* [cgw]: 设置需要处理的信号SIGIO,即输入文件会请求一个SIGIO

* 信号,当有新数据到来这个信号会发给filp->f_owner进程

*/

signal(SIGIO, my_signal_fun);

fd = open("/dev/event1", O_RDWR | O_NONBLOCK);

//printf("fd = 0x%x\n", fd);

if (fd < 0)

{

printf("can't open!\n");

}

/* [cgw]: 根据文件标识符fd,设置能够获得这个文件的进程(owner)

* getpid()获得当前进程ID

*/

fcntl(fd, F_SETOWN, getpid());

/* [cgw]: 获得file->f_flags标志 */

Oflags = fcntl(fd, F_GETFL);

/* [cgw]: 置位FASYNC使能异步通知 */

fcntl(fd, F_SETFL, Oflags | FASYNC);

while (1)

{

/* [cgw]: 休眠 */

sleep(1000);

}

return 0;

}

makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:

make -C $(KERN_DIR) M=`pwd` modules

clean:

make -C $(KERN_DIR) M=`pwd` modules clean

rm -rf modules.order

obj-m    += input_subsys_drv.o

2. 实验

2.1

安装驱动程序:

insmod input_subsys_drv.ko

1 # insmod input_subsys_drv.ko2 input: input_subsys_dev as /class/input/input13 input subsys open!

4 input subsys init!

运行应用程序

./input_subsys_test

# ./input_subsys_test

type: 0x1 code: 0x26 value: 0x1

type: 0x1 code: 0x26 value: 0x0

led event!

value: 0xaa

led write!

type: 0x11 code: 0x7 value: 0xaa

type: 0x1 code: 0x1f value: 0x1

type: 0x1 code: 0x1f value: 0x0

led event!

value: 0xee

led write!

type: 0x11 code: 0x7 value: 0xee

3. 现象分析

按一下按键KEY_L,终端输出:

type: 0x1 code: 0x26 value: 0x1 //按键按下type:0x1 code: 0x26 value: 0x0 //按键弹起ledevent! //应用程序操作write, 发送LED控制事件,调用led_event()value:0xaa //读到的LED亮的命令led write! //返回应用程序继执行type:0x11 code: 0x7 value: 0xaa //因为应用程序发送了事件给驱动程序,驱动把这个事件作为一个输入事件(相当于按键输入事件),同样通过异步通知反馈到应用程序

c4dadd5f6991ccf313c632410749687f.png

The end!

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值