Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * scan matrix
  3. */
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include "print.h"
  9. #include "util.h"
  10. #include "debug.h"
  11. #include "ps2.h"
  12. #include "usb_keyboard.h"
  13. #include "matrix_skel.h"
  14. #if (MATRIX_COLS > 16)
  15. # error "MATRIX_COLS must not exceed 16"
  16. #endif
  17. #if (MATRIX_ROWS > 255)
  18. # error "MATRIX_ROWS must not exceed 255"
  19. #endif
  20. /*
  21. * Matrix usage:
  22. * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
  23. * Hmm, It is very sparse and not efficient :(
  24. *
  25. * 8bit
  26. * ---------
  27. * 0| |
  28. * :| XX | 00-7F for normal codes(without E0-prefix)
  29. * f|_________|
  30. * 10| |
  31. * :| E0 XX | 80-FF for E0-prefix codes(use (XX|0x80) as code)
  32. * 1f| |
  33. * ---------
  34. * exceptions:
  35. * 83: F8[0x83](normal codes but > 0x7F)
  36. * FC: PrintScreen[E0 7C or 84]
  37. * FE: Puause
  38. */
  39. #define F8 (0x83)
  40. #define PRINT_SCREEN (0xFC)
  41. #define PAUSE (0xFE)
  42. #define ROW(code) (code>>3)
  43. #define COL(code) (code&0x07)
  44. static bool is_modified = false;
  45. // matrix state buffer(1:on, 0:off)
  46. #if (MATRIX_COLS <= 8)
  47. static uint8_t matrix[MATRIX_ROWS];
  48. #else
  49. static uint16_t matrix[MATRIX_ROWS];
  50. #endif
  51. #ifdef MATRIX_HAS_GHOST
  52. static bool matrix_has_ghost_in_row(uint8_t row);
  53. #endif
  54. static void matrix_make(uint8_t code);
  55. static void matrix_break(uint8_t code);
  56. static void ps2_reset(void);
  57. static void ps2_set_leds(uint8_t leds);
  58. inline
  59. uint8_t matrix_rows(void)
  60. {
  61. return MATRIX_ROWS;
  62. }
  63. inline
  64. uint8_t matrix_cols(void)
  65. {
  66. return MATRIX_COLS;
  67. }
  68. void matrix_init(void)
  69. {
  70. ps2_host_init();
  71. _delay_ms(1000);
  72. // flush LEDs
  73. /*
  74. ps2_set_leds(1<<PS2_LED_NUM_LOCK);
  75. _delay_ms(100);
  76. ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK);
  77. _delay_ms(100);
  78. ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK|1<<PS2_LED_SCROLL_LOCK);
  79. _delay_ms(300);
  80. ps2_set_leds(0x00);
  81. */
  82. // initialize matrix state: all keys off
  83. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  84. return;
  85. }
  86. /*
  87. * PS/2 Scan Code Set 2: Exceptional Handling
  88. *
  89. * There are several keys to be handled exceptionally.
  90. * The scan code for these keys are varied or prefix/postfix'd
  91. * depending on modifier key state.
  92. *
  93. * References:
  94. * http://www.microsoft.com/whdc/archive/scancode.mspx
  95. * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
  96. *
  97. *
  98. * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
  99. * Num Lock: off
  100. * modifiers | make | break
  101. * ----------+---------------------------+----------------------
  102. * Ohter | <make> | <break>
  103. * LShift | E0 F0 12 <make> | <break> E0 12
  104. * RShift | E0 F0 59 <make> | <break> E0 59
  105. * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
  106. *
  107. * Num Lock: on
  108. * modifiers | make | break
  109. * ----------+---------------------------+----------------------
  110. * Other | E0 12 <make> | <break> E0 F0 12
  111. * Shift'd | <make> | <break>
  112. *
  113. * Handling: ignore these prefix/postfix codes
  114. *
  115. *
  116. * Keypad-/:
  117. * modifiers | make | break
  118. * ----------+---------------------------+----------------------
  119. * Ohter | <make> | <break>
  120. * LShift | E0 F0 12 <make> | <break> E0 12
  121. * RShift | E0 F0 59 <make> | <break> E0 59
  122. * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
  123. *
  124. * Handling: ignore these prefix/postfix codes
  125. *
  126. *
  127. * PrintScreen:
  128. * With hoding down modifiers, the scan code is sent as following:
  129. *
  130. * modifiers | make | break
  131. * ----------+--------------+-----------------------------------
  132. * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
  133. * Shift'd | E0 7C | E0 F0 7C
  134. * Control'd | E0 7C | E0 F0 7C
  135. * Alt'd | 84 | F0 84
  136. *
  137. * Handling: ignore prefix/postfix codes and treat both scan code
  138. * E0 7C and 84 as PrintScreen.
  139. *
  140. * Pause:
  141. * With hoding down modifiers, the scan code is sent as following:
  142. *
  143. * modifiers | make(no break code)
  144. * ----------+--------------------------------------------------
  145. * no mods | E1 14 77 E1 F0 14 F0 77
  146. * Control'd | E0 7E E0 F0 7E
  147. *
  148. * Handling: treat these two code sequence as Pause
  149. *
  150. */
  151. uint8_t matrix_scan(void)
  152. {
  153. static enum {
  154. INIT,
  155. F0,
  156. E0,
  157. E0_F0,
  158. // states for Pause/Break
  159. E1,
  160. E1_14,
  161. E1_14_77,
  162. E1_14_77_E1,
  163. E1_14_77_E1_F0,
  164. E1_14_77_E1_F0_14,
  165. E1_14_77_E1_F0_14_F0,
  166. } state = INIT;
  167. is_modified = false;
  168. // Pause/Break off(PS/2 has no break for this key)
  169. if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
  170. matrix_break(PAUSE);
  171. }
  172. uint8_t code;
  173. while ((code = ps2_host_recv())) {
  174. //debug_hex(code); debug(" ");
  175. switch (state) {
  176. case INIT:
  177. switch (code) {
  178. case 0xE0: // 2byte make
  179. state = E0;
  180. break;
  181. case 0xF0: // break code
  182. state = F0;
  183. break;
  184. case 0xE1: // Pause/Break
  185. state = E1;
  186. break;
  187. case 0x83: // F8
  188. matrix_make(F8);
  189. state = INIT;
  190. break;
  191. case 0x84: // PrintScreen
  192. matrix_make(PRINT_SCREEN);
  193. state = INIT;
  194. break;
  195. default: // normal key make
  196. if (code < 0x80) {
  197. matrix_make(code);
  198. } else {
  199. debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
  200. }
  201. state = INIT;
  202. }
  203. break;
  204. case E0:
  205. switch (code) {
  206. case 0x12: // postfix/postfix code for exceptional keys
  207. case 0x59: // postfix/postfix code for exceptional keys
  208. // ignore
  209. state = INIT;
  210. break;
  211. case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
  212. matrix_make(PAUSE);
  213. state = INIT;
  214. break;
  215. case 0xF0: // E0 break
  216. state = E0_F0;
  217. break;
  218. default: // E0 make
  219. if (code < 0x80) {
  220. matrix_make(code|0x80);
  221. } else {
  222. debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
  223. }
  224. state = INIT;
  225. }
  226. break;
  227. case F0:
  228. switch (code) {
  229. case 0x83:
  230. matrix_break(F8);
  231. state = INIT;
  232. break;
  233. case 0x84:
  234. matrix_break(PRINT_SCREEN);
  235. state = INIT;
  236. break;
  237. default:
  238. if (code < 0x80) {
  239. matrix_break(code);
  240. } else {
  241. debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
  242. }
  243. state = INIT;
  244. }
  245. break;
  246. case E0_F0: // E0 break
  247. switch (code) {
  248. case 0x12: // postfix/postfix code for exceptional keys
  249. case 0x59: // postfix/postfix code for exceptional keys
  250. case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
  251. // ignore
  252. state = INIT;
  253. break;
  254. default:
  255. if (code < 0x80) {
  256. matrix_break(code|0x80);
  257. } else {
  258. debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
  259. }
  260. state = INIT;
  261. }
  262. break;
  263. /* Pause */
  264. case E1:
  265. switch (code) {
  266. case 0x14:
  267. state = E1_14;
  268. break;
  269. default:
  270. state = INIT;
  271. }
  272. break;
  273. case E1_14:
  274. switch (code) {
  275. case 0x77:
  276. state = E1_14_77;
  277. break;
  278. default:
  279. state = INIT;
  280. }
  281. break;
  282. case E1_14_77:
  283. switch (code) {
  284. case 0xE1:
  285. state = E1_14_77_E1;
  286. break;
  287. default:
  288. state = INIT;
  289. }
  290. break;
  291. case E1_14_77_E1:
  292. switch (code) {
  293. case 0xF0:
  294. state = E1_14_77_E1_F0;
  295. break;
  296. default:
  297. state = INIT;
  298. }
  299. break;
  300. case E1_14_77_E1_F0:
  301. switch (code) {
  302. case 0x14:
  303. state = E1_14_77_E1_F0_14;
  304. break;
  305. default:
  306. state = INIT;
  307. }
  308. break;
  309. case E1_14_77_E1_F0_14:
  310. switch (code) {
  311. case 0xF0:
  312. state = E1_14_77_E1_F0_14_F0;
  313. break;
  314. default:
  315. state = INIT;
  316. }
  317. break;
  318. case E1_14_77_E1_F0_14_F0:
  319. switch (code) {
  320. case 0x77:
  321. matrix_make(PAUSE);
  322. state = INIT;
  323. break;
  324. default:
  325. state = INIT;
  326. }
  327. break;
  328. default:
  329. state = INIT;
  330. }
  331. }
  332. // handle LED indicators
  333. /*
  334. static uint8_t prev_leds = 0;
  335. if (prev_leds != usb_keyboard_leds) {
  336. uint8_t leds = 0;
  337. if (usb_keyboard_leds&(1<<USB_LED_SCROLL_LOCK))
  338. leds |= (1<<PS2_LED_SCROLL_LOCK);
  339. if (usb_keyboard_leds&(1<<USB_LED_NUM_LOCK))
  340. leds |= (1<<PS2_LED_NUM_LOCK);
  341. if (usb_keyboard_leds&(1<<USB_LED_CAPS_LOCK))
  342. leds |= (1<<PS2_LED_CAPS_LOCK);
  343. ps2_set_leds(leds);
  344. prev_leds = usb_keyboard_leds;
  345. }
  346. */
  347. return 1;
  348. }
  349. bool matrix_is_modified(void)
  350. {
  351. return is_modified;
  352. }
  353. inline
  354. bool matrix_has_ghost(void)
  355. {
  356. #ifdef MATRIX_HAS_GHOST
  357. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  358. if (matrix_has_ghost_in_row(i))
  359. return true;
  360. }
  361. #endif
  362. return false;
  363. }
  364. inline
  365. bool matrix_is_on(uint8_t row, uint8_t col)
  366. {
  367. return (matrix[row] & (1<<col));
  368. }
  369. inline
  370. #if (MATRIX_COLS <= 8)
  371. uint8_t matrix_get_row(uint8_t row)
  372. #else
  373. uint16_t matrix_get_row(uint8_t row)
  374. #endif
  375. {
  376. return matrix[row];
  377. }
  378. void matrix_print(void)
  379. {
  380. #if (MATRIX_COLS <= 8)
  381. print("\nr/c 01234567\n");
  382. #else
  383. print("\nr/c 0123456789ABCDEF\n");
  384. #endif
  385. for (uint8_t row = 0; row < matrix_rows(); row++) {
  386. phex(row); print(": ");
  387. #if (MATRIX_COLS <= 8)
  388. pbin_reverse(matrix_get_row(row));
  389. #else
  390. pbin_reverse16(matrix_get_row(row));
  391. #endif
  392. #ifdef MATRIX_HAS_GHOST
  393. if (matrix_has_ghost_in_row(row)) {
  394. print(" <ghost");
  395. }
  396. #endif
  397. print("\n");
  398. }
  399. }
  400. uint8_t matrix_key_count(void)
  401. {
  402. uint8_t count = 0;
  403. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  404. #if (MATRIX_COLS <= 8)
  405. count += bitpop(matrix[i]);
  406. #else
  407. count += bitpop16(matrix[i]);
  408. #endif
  409. }
  410. return count;
  411. }
  412. #ifdef MATRIX_HAS_GHOST
  413. inline
  414. static bool matrix_has_ghost_in_row(uint8_t row)
  415. {
  416. // no ghost exists in case less than 2 keys on
  417. if (((matrix[row] - 1) & matrix[row]) == 0)
  418. return false;
  419. // ghost exists in case same state as other row
  420. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  421. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  422. return true;
  423. }
  424. return false;
  425. }
  426. #endif
  427. inline
  428. static void matrix_make(uint8_t code)
  429. {
  430. if (!matrix_is_on(ROW(code), COL(code))) {
  431. matrix[ROW(code)] |= 1<<COL(code);
  432. is_modified = true;
  433. }
  434. }
  435. inline
  436. static void matrix_break(uint8_t code)
  437. {
  438. if (matrix_is_on(ROW(code), COL(code))) {
  439. matrix[ROW(code)] &= ~(1<<COL(code));
  440. is_modified = true;
  441. }
  442. }
  443. static void ps2_reset(void)
  444. {
  445. ps2_host_send(0xFF);
  446. if (ps2_host_recv() != 0xFA) return;
  447. _delay_ms(1000);
  448. if (ps2_host_recv() != 0xAA) return;
  449. }
  450. static void ps2_set_leds(uint8_t leds)
  451. {
  452. ps2_host_send(0xED);
  453. if (ps2_host_recv() != 0xFA) return; // 0xFA
  454. ps2_host_send(leds);
  455. if (ps2_host_recv() != 0xFA) return; // 0xFA
  456. }