博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gpio matrix keypad linux driver
阅读量:4058 次
发布时间:2019-05-25

本文共 8432 字,大约阅读时间需要 28 分钟。

Cross-Referenced and Code

[ source navigation ] [ ] [ ] [ ] [ ]

Version: [  ][  ][  ][ 2.6.31.13 ] Architecture:[ i386 ]

/*    *  GPIO driven matrix keyboard driver    *    *  Copyright (c) 2008 Marek Vasut 
* * Based on corgikbd.c * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */ #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct { const struct *; struct *; unsigned short *keycodes; unsigned int row_shift; last_key_state[]; struct work; scan_pending; stopped; ; }; /* * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause * minmal side effect when scanning other columns, here it is configured to * be input, and it should work on most platforms. */ static void (const struct *, int col, ) { level_on = !->active_low; if () { (->[col], level_on); } else { (->[col], !level_on); (->[col]); } } static void (const struct *, int col, ) { (, col, ); if ( && ->col_scan_delay_us) (->col_scan_delay_us); } static void (const struct *, ) { int col; for (col = 0; col < ->num_col_gpios; col++) (, col, ); } static (const struct *, int row) { return (->[row]) ? !->active_low : ->active_low; } static void (struct *keypad) { const struct * = keypad->; int ; for ( = 0; < ->num_row_gpios; ++) //使能中断。 ((->[])); } static void (struct *keypad) { const struct * = keypad->; int ; for ( = 0; < ->num_row_gpios; ++) 这个是关中断,记得以前使用disable_irq()时有时候会出错。 所以使用disable_irq_nosync()会更好友好一些。 ((->[])); } 使用input子系统的一般流程为:input_allocate_device()申请一个input_dev设备――>初始化该input_dev――>input_register_device()向子系统注册该设备――>中断时input_event()向子系 统报告事件。 /* * This gets the keys from keyboard and reports it to input subsystem */ 这就是延后的工作的函数体,依次进行列行扫描来判断哪个按键被按下,然后使用report_key()向 input core提交按键事件。 static void (struct *work) { struct *keypad = (work, struct , work.work); struct * = keypad->; const struct * = keypad->; []; int row, col, ; /* de-activate all columns for scanning */ (, ); (, 0, sizeof()); /* assert each column and read the row status out */ for (col = 0; col < ->num_col_gpios; col++) { (, col, ); for (row = 0; row < ->num_row_gpios; row++) [col] |= (, row) ? (1 << row) : 0; (, col, ); } for (col = 0; col < ->num_col_gpios; col++) { bits_changed; bits_changed = keypad->last_key_state[col] ^ [col]; if (bits_changed == 0) continue; for (row = 0; row < ->num_row_gpios; row++) { if ((bits_changed & (1 << row)) == 0) continue; = (row, col, keypad->row_shift); //提交输入事件 (, , , ); //提交按键值。 (, keypad->keycodes[], [col] & (1 << row)); } } 这个函数难道是flush input core让它立即向上层提交。 (); (keypad->last_key_state, , sizeof()); (, ); /* Enable IRQs again */ (&keypad->); keypad->scan_pending = ; (keypad); (&keypad->); } 中断函数。 static (int , void *) { 指针强制转换,送入的irq为当前发生中断的中断号 id为在使用request_irq()注册时输入的参数,可以在中断上下文中使用。 struct *keypad = ; unsigned long ; (&keypad->, ); /* * See if another IRQ beaten us to it and scheduled the * scan already. In that case we should not try to * disable IRQs again. */ if ((keypad->scan_pending || keypad->stopped)) goto ; (keypad); keypad->scan_pending = ; //中断中并不直接读取gpio,直接调度一个延时的任务, //相当于中断上半部分和下半部分, //上半部分:在中断处理函数中进行一些操作,然后调度一个延后的工作队列。 //下半部分:在工作队列中完成真正的操作。 //这样的好处是,尽可能少的占用中断的时间。 (&keypad->work, (keypad->->debounce_ms)); : (&keypad->, ); return ; } static int (struct *) { //获取保存在输出设备中的指针。 struct *keypad = (); keypad->stopped = ; (); /* * Schedule an immediate key scan to capture current key state; * columns will be activated and IRQs be enabled after the scan. */ //调用一个延时的任务用于扫描矩阵键盘。 (&keypad->work, 0); return 0; } static void (struct *) { struct *keypad = (); keypad->stopped = ; (); (&keypad->work.work); /* * matrix_keypad_scan() will leave IRQs enabled; * we should disable them now. */ (keypad); } 这个应该是由内核输出的宏,在内核中启动电源管理就会定义这个宏PowerManagement #ifdef static int (struct *, ) { struct *keypad = (); const struct * = keypad->; int ; (keypad->); if ((&->)) for ( = 0; < ->num_row_gpios; ++) ((->[])); return 0; } static int (struct *) { struct *keypad = (); const struct * = keypad->; int ; if ((&->)) for ( = 0; < ->num_row_gpios; ++) ((->[])); (keypad->); return 0; } #else 如果没有电源功能,则这2个函数为空。 #define #define #endif 初始化连接的GPIO引脚寄存器,包括:方向:输入还是输出,向内核申请GPIO,表示 这段区域我正在使用,别人不能用了。防止多人使用出现冲突错误。 同时完成申请中断的操作。 static int (struct *, struct *keypad) { const struct * = keypad->; int , = -; /* initialized strobe lines as outputs, activated */ for ( = 0; < ->num_col_gpios; ++) { = (->[], "matrix_kbd_col"); if () { (&->, "failed to request GPIO%d for COL%d\n", ->[], ); goto err_free_cols; } (->[], !->active_low); } for ( = 0; < ->num_row_gpios; ++) { = (->[], "matrix_kbd_row"); if () { (&->, "failed to request GPIO%d for ROW%d\n", ->[], ); goto err_free_rows; } (->[]); } for ( = 0; < ->num_row_gpios; ++) { = ((->[]), , | | , "matrix-keypad", keypad); if () { (&->, "Unable to acquire interrupt for GPIO line %i\n", ->[]); goto err_free_irqs; } } /* initialized as disabled - enabled by input->open */ (keypad); return 0; err_free_irqs: while (-- >= 0) ((->[]), keypad); = ->num_row_gpios; err_free_rows: while (-- >= 0) (->[]); = ->num_col_gpios; err_free_cols: while (-- >= 0) (->[]); return ; } 这个函数应该是platform_device跟platform_driver匹配时被调用。 static int (struct *) { const struct *; const struct *keymap_data; struct *keypad; struct *;//内核输入设备结构体 unsigned short *keycodes; unsigned int row_shift; int ; int ; 获取平台设备的数据,这个应该是由platform_device提供, 由platform_driver使用。 = ->.; if (!) { (&->, "no platform data defined\n"); return -; } keymap_data = ->keymap_data; if (!keymap_data) { (&->, "no keymap data defined\n"); return -; } row_shift = (->num_col_gpios); 申请keypad设备结构体,为了防止指针丢失, 一会儿会将其保存在platform_device中的 私有指针域中。 keypad = (sizeof(struct ), ); 这个指针会保存的keypad结构体中,作为二级指针。(见下面). keycodes = ((->num_row_gpios << row_shift) * sizeof(*keycodes), ); 内核的输入子系统接口,内部应该就是kmalloc一个结构体。 = (); if (!keypad || !keycodes || !) { = -; goto err_free_mem; } 上面是申请内存,下面将这些值保存在设备结构体中. keypad-> = ;内核输入设备结构体(保存这个指针不能丢失) keypad-> = ; keypad->keycodes = keycodes; keypad->row_shift = row_shift; keypad->stopped = ; 初始化延后的工作队列,当该工作被调度时将调用 函数。 (&keypad->work, );延时的任务 (&keypad->);自旋锁不可睡眠,难道在中断中用?? 这里还是比较关键的,初始化输入设备结构。Linux中输入设备的事件类型有(这里只列出了常用的一些,更多请看linux/input.h中): EV_SYN 0x00 同步事件EV_KEY 0x01 按键事件EV_REL 0x02 相对坐标EV_ABS 0x03 绝对坐标EV_MSC 0x04 其它EV_LED 0x11 LEDEV_SND 0x12 声音EV_REP 0x14 RepeatEV_FF 0x15 力反馈 ~~~~~~~~~~~~~~~~~~~~~~~~EV_PWR 电源EV_FF_STATUS 状态 -> = ->; ->.bustype = ; ->. = &->; 事件类型:按键和重复 ->evbit[0] = () | (); -> = ; -> = ; ->keycode = keycodes; ->keycodesize = sizeof(*keycodes); ->keycodemax = ->num_row_gpios << keypad->row_shift; for ( = 0; < keymap_data->keymap_size; ++) { unsigned int = keymap_data->[]; unsigned int row = (); unsigned int col = (); unsigned short = (); keycodes[(row, col, row_shift)] = ; (, ->keybit); } (, ->keybit); (, , ); (, keypad); = (, keypad); if () goto err_free_mem; 这里是标准的注册输入设备函数,属于内核API。 = (keypad->); if () goto err_free_mem; //电源管理PM的功能,设置设备能否可唤醒以及是否唤醒 (&->, ->); //这里将临时申请的内存keypad保存到platform_device平台设备结构体中,防止丢失。 //往前看可以看到,keypad是使用kmalloc()申请的。如果我们不找个地方保存起来的话, //就会丢失,造成野指针。 (, keypad); return 0; err_free_mem: (); (keycodes); (keypad); return ; } static int (struct *) { //在remove()函数中,我们从平台设备结构体中提取我们保存的指针。 //来获取我们的信息,并做释放操作。 //并手工释放内存。 struct *keypad = (); //从保存的设备结构体中提取下一级提针。 const struct * = keypad->; int ; (&->, 0); for ( = 0; < ->num_row_gpios; ++) { //释放中断 ((->[]), keypad); //释放GPIO. (->[]); } for ( = 0; < ->num_col_gpios; ++) (->[]); 注销输入设备。 (keypad->); //为了防止误操作,将平台设备的数据域设置为0。 (, ); (keypad->keycodes); //platform_set_drvdata()和platform_get_drvdata()只是负责保存指针。 //到最后还是需要手工释放内存的。 (keypad); return 0; } static struct = { . = , . = (), . = , .resume = ,这是PM电源管理的功能。 休眠时内核会依次调用驱动链表上所有驱动的suspend() 函数,在这里应该将硬件设置为休眠低功耗状态。 当内核恢复时,也会依次调用所有驱动的resume()函数, 这里就必须将硬件设置为正常工作模式,退出休眠。 . = { . = "matrix-keypad",这里的name很重要,因为只有 平台驱动跟平台设备的name匹配时才会调用驱动的probe()。 .owner = , }, }; static int (void) { 注册平台设备,这类GPIO矩阵键盘归为平台设备,属于SoC吗​? 如果这样的话,那么是否还得注册一个platform_device, 因为在调试中发现只有platform_driver跟platform_device匹配时,才会 调用platform_driver中的probe()函数,才有完成初始化的工作。 return (&); } static void (void) { (&); } (); (); ("Marek Vasut
"); ("GPIO Driven Matrix Keypad Driver"); ("GPL v2"); ("platform:matrix-keypad");

  This page wasautomatically generated by the .

转载地址:http://cdzji.baihongyu.com/

你可能感兴趣的文章
io口的作用
查看>>
IO口的作用
查看>>
上拉电阻和下拉电阻
查看>>
端口和引脚的区别
查看>>
外部中断的使用
查看>>
STM32系列ARM单片机介绍
查看>>
JSON入门指南
查看>>
JSP
查看>>
STM32固件库命名规则
查看>>
串口的工作原理
查看>>
STM32的NVIC理解
查看>>
STM32外部中断的错误
查看>>
图片拉伸函数详解
查看>>
系统相册和拍照
查看>>
FMDB开启事务
查看>>
coredata使用代码实现
查看>>
autolayout的使用原理及代码实现
查看>>
Xcode各种版本下载
查看>>
Xcode编程问题小结
查看>>
UIView的使用setNeedsDisplay
查看>>