EE_BeagleBone_Cape之矩阵键盘
1. 概述
这篇文档是关于EE_BeagleBone_Cape 4x4矩阵键盘的设计相关,欢迎大伙拍砖
2. 矩阵键盘硬件连接
具体使用了以下几个管脚: - gpio0[15] Row1
gpio3[21] Row2
gpio0[14] Row3
gpio3[19] Row4
gpio3[17] Col1
gpio3[15] Col2
gpio3[16] Col3
gpio3[14] Col4
复制代码
3. 矩阵键盘设计点滴
1. 注意我这个4x4的矩阵键盘没有外接电阻哦,所以作为输入的引脚做好拉地,配置成AM33XX_PIN_INPUT_PULLDOWN
2. 注意Row和Col的引脚输入输出关系不要定义反了,Row为input,Col为output。我开始的时候定义反了搞死都出不来结果。
4. 代码相关
4.1 Pinmux
看看4x4键盘涉及到的几个管脚的定义吧,如下所示:- /* Matrix GPIO Keypad Support for profile-0 only: TODO */
/* pinmux for keypad device */
static struct pinmux_config matrix_keypad_pin_mux[] = {
{"uart1_txd.gpio0_15", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT_PULLDOWN},
{"mcasp0_ahclkx.gpio3_21", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT_PULLDOWN},
{"uart1_rxd.gpio0_14", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT_PULLDOWN},
{"mcasp0_fsr.gpio3_19", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT_PULLDOWN},
{"mcasp0_ahclkr.gpio3_17", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
{"mcasp0_fsx.gpio3_15", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
{"mcasp0_axr0.gpio3_16", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
{"mcasp0_aclkx.gpio3_14", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
{NULL, 0},
};
/* Keys mapping */
static const uint32_t am335x_evm_matrix_keys[] = {
KEY(0, 0, KEY_A),
KEY(1, 0, KEY_3),
KEY(2, 0, KEY_2),
KEY(3, 0, KEY_1),
KEY(0, 1, KEY_B),
KEY(1, 1, KEY_6),
KEY(2, 1, KEY_5),
KEY(3, 1, KEY_4),
KEY(0, 2, KEY_C),
KEY(1, 2, KEY_9),
KEY(2, 2, KEY_8),
KEY(3, 2, KEY_7),
KEY(0, 3, KEY_D),
KEY(1, 3, KEY_OK),
KEY(2, 3, KEY_0),
KEY(3, 3, KEY_DELETE),
};
const struct matrix_keymap_data am335x_evm_keymap_data = {
.keymap = am335x_evm_matrix_keys,
.keymap_size = ARRAY_SIZE(am335x_evm_matrix_keys),
};
static const unsigned int am335x_evm_keypad_row_gpios[] = {
GPIO_TO_PIN(0, 15), GPIO_TO_PIN(3, 21), GPIO_TO_PIN(0, 14), GPIO_TO_PIN(3, 19)
};
static const unsigned int am335x_evm_keypad_col_gpios[] = {
GPIO_TO_PIN(3, 17), GPIO_TO_PIN(3, 15), GPIO_TO_PIN(3, 16), GPIO_TO_PIN(3, 14)
};
static struct matrix_keypad_platform_data am335x_evm_keypad_platform_data = {
.keymap_data = &am335x_evm_keymap_data,
.row_gpios = am335x_evm_keypad_row_gpios,
.num_row_gpios = ARRAY_SIZE(am335x_evm_keypad_row_gpios),
.col_gpios = am335x_evm_keypad_col_gpios,
.num_col_gpios = ARRAY_SIZE(am335x_evm_keypad_col_gpios),
.active_low = false,
.debounce_ms = 5,
.col_scan_delay_us = 2,
};
static struct platform_device am335x_evm_keyboard = {
.name = "matrix-keypad",
.id = -1,
.dev = {
.platform_data = &am335x_evm_keypad_platform_data,
},
};
static void matrix_keypad_init(int evm_id, int profile)
{
int err;
setup_pin_mux(matrix_keypad_pin_mux);
err = platform_device_register(&am335x_evm_keyboard);
if (err) {
pr_err("failed to register matrix keypad (4x4) device\n");
}
}
复制代码4.2 初始化- /* Beaglebone Rev A3 and after */
static struct evm_dev_cfg beaglebone_dev_cfg[] = {
{tps65217_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{mii1_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{usb0_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{usb1_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{mmc0_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{i2c1_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{i2c2_init, DEV_ON_BASEBOARD, PROFILE_NONE},
{lcdc_init, DEV_ON_BASEBOARD, (PROFILE_0 | PROFILE_1 |
PROFILE_2 | PROFILE_7) },
{evm_nand_init, DEV_ON_BASEBOARD,
(PROFILE_ALL & ~PROFILE_2 & ~PROFILE_3)},
{matrix_keypad_init, DEV_ON_BASEBOARD, PROFILE_0},
{NULL, 0, 0},
};
复制代码
看看include\linux\Input.h关于键值的定义吧- #define KEY_1 2
#define KEY_2 3
#define KEY_3 4
#define KEY_4 5
#define KEY_5 6
#define KEY_6 7
#define KEY_7 8
#define KEY_8 9
#define KEY_9 10
#define KEY_0 11
复制代码
4.3 测试程序
从网络上找个一个测试程序,能用!
#include
#include
#include
#include
#include
#include
int fd;
int loop_flag = 1;
void exit_loop()
{
loop_flag--;
}
void deal_int(int signum)
{
if(fd > 0)
close(fd);
exit_loop();
}
int main()
{
struct input_event evt;
fd = open("/dev/input/event0",O_RDWR);
if(fd < 0)
printf("Can not open\n");
while(loop_flag > 0)
{
read(fd,&evt,sizeof(struct input_event));
switch(evt.type)
{
case EV_KEY:
printf("Key -->\nCode: %d\nValue: %d\n",evt.code,evt.value);
break;
case EV_MSC:
printf("MSC -->\nCode: %d\nValue: %d\n",evt.code,evt.value);
break;
case EV_SYN:
printf("<-- \n");
default:
printf("...\n");
}
}
return 0;
}
复制代码
4.4 测试结果- MSC -->
Code: 4
Value: 12
Key -->
Code: 2 // 1
Value: 1
<--
...
MSC -->
Code: 4
Value: 12
Key -->
Code: 2
Value: 0
<--
...
MSC -->
Code: 4
Value: 9
Key -->
Code: 6 // 5
Value: 1
<--
...
MSC -->
Code: 4
Value: 9
Key -->
Code: 6
Value: 0
<--
...
MSC -->
Code: 4
Value: 6
Key -->
Code: 10 // 9
Value: 1
<--
...
MSC -->
Code: 4
Value: 6
Key -->
Code: 10
Value: 0
<--
...
|