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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. code = ps2_host_recv();
  174. if (code == 0x00) return 0;
  175. //while ((code = ps2_host_recv())) {
  176. //phex(code); print(" ");
  177. switch (state) {
  178. case INIT:
  179. switch (code) {
  180. case 0xE0: // 2byte make
  181. state = E0;
  182. break;
  183. case 0xF0: // break code
  184. state = F0;
  185. break;
  186. case 0xE1: // Pause/Break
  187. state = E1;
  188. break;
  189. case 0x83: // F8
  190. matrix_make(F8);
  191. state = INIT;
  192. break;
  193. case 0x84: // PrintScreen
  194. matrix_make(PRINT_SCREEN);
  195. state = INIT;
  196. break;
  197. default: // normal key make
  198. if (code < 0x80) {
  199. matrix_make(code);
  200. } else {
  201. debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
  202. }
  203. state = INIT;
  204. }
  205. break;
  206. case E0:
  207. switch (code) {
  208. case 0x12: // postfix/postfix code for exceptional keys
  209. case 0x59: // postfix/postfix code for exceptional keys
  210. // ignore
  211. state = INIT;
  212. break;
  213. case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
  214. matrix_make(PAUSE);
  215. state = INIT;
  216. break;
  217. case 0xF0: // E0 break
  218. state = E0_F0;
  219. break;
  220. default: // E0 make
  221. if (code < 0x80) {
  222. matrix_make(code|0x80);
  223. } else {
  224. debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
  225. }
  226. state = INIT;
  227. }
  228. break;
  229. case F0:
  230. switch (code) {
  231. case 0x83:
  232. matrix_break(F8);
  233. state = INIT;
  234. break;
  235. case 0x84:
  236. matrix_break(PRINT_SCREEN);
  237. state = INIT;
  238. break;
  239. default:
  240. if (code < 0x80) {
  241. matrix_break(code);
  242. } else {
  243. debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
  244. }
  245. state = INIT;
  246. }
  247. break;
  248. case E0_F0: // E0 break
  249. switch (code) {
  250. case 0x12: // postfix/postfix code for exceptional keys
  251. case 0x59: // postfix/postfix code for exceptional keys
  252. case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
  253. // ignore
  254. state = INIT;
  255. break;
  256. default:
  257. if (code < 0x80) {
  258. matrix_break(code|0x80);
  259. } else {
  260. debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
  261. }
  262. state = INIT;
  263. }
  264. break;
  265. /* Pause */
  266. case E1:
  267. switch (code) {
  268. case 0x14:
  269. state = E1_14;
  270. break;
  271. default:
  272. state = INIT;
  273. }
  274. break;
  275. case E1_14:
  276. switch (code) {
  277. case 0x77:
  278. state = E1_14_77;
  279. break;
  280. default:
  281. state = INIT;
  282. }
  283. break;
  284. case E1_14_77:
  285. switch (code) {
  286. case 0xE1:
  287. state = E1_14_77_E1;
  288. break;
  289. default:
  290. state = INIT;
  291. }
  292. break;
  293. case E1_14_77_E1:
  294. switch (code) {
  295. case 0xF0:
  296. state = E1_14_77_E1_F0;
  297. break;
  298. default:
  299. state = INIT;
  300. }
  301. break;
  302. case E1_14_77_E1_F0:
  303. switch (code) {
  304. case 0x14:
  305. state = E1_14_77_E1_F0_14;
  306. break;
  307. default:
  308. state = INIT;
  309. }
  310. break;
  311. case E1_14_77_E1_F0_14:
  312. switch (code) {
  313. case 0xF0:
  314. state = E1_14_77_E1_F0_14_F0;
  315. break;
  316. default:
  317. state = INIT;
  318. }
  319. break;
  320. case E1_14_77_E1_F0_14_F0:
  321. switch (code) {
  322. case 0x77:
  323. matrix_make(PAUSE);
  324. state = INIT;
  325. break;
  326. default:
  327. state = INIT;
  328. }
  329. break;
  330. default:
  331. state = INIT;
  332. }
  333. //}
  334. //print("|");
  335. // handle LED indicators
  336. /*
  337. static uint8_t prev_leds = 0;
  338. if (prev_leds != usb_keyboard_leds) {
  339. uint8_t leds = 0;
  340. if (usb_keyboard_leds&(1<<USB_LED_SCROLL_LOCK))
  341. leds |= (1<<PS2_LED_SCROLL_LOCK);
  342. if (usb_keyboard_leds&(1<<USB_LED_NUM_LOCK))
  343. leds |= (1<<PS2_LED_NUM_LOCK);
  344. if (usb_keyboard_leds&(1<<USB_LED_CAPS_LOCK))
  345. leds |= (1<<PS2_LED_CAPS_LOCK);
  346. ps2_set_leds(leds);
  347. prev_leds = usb_keyboard_leds;
  348. }
  349. */
  350. return 1;
  351. }
  352. bool matrix_is_modified(void)
  353. {
  354. return is_modified;
  355. }
  356. inline
  357. bool matrix_has_ghost(void)
  358. {
  359. #ifdef MATRIX_HAS_GHOST
  360. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  361. if (matrix_has_ghost_in_row(i))
  362. return true;
  363. }
  364. #endif
  365. return false;
  366. }
  367. inline
  368. bool matrix_is_on(uint8_t row, uint8_t col)
  369. {
  370. return (matrix[row] & (1<<col));
  371. }
  372. inline
  373. #if (MATRIX_COLS <= 8)
  374. uint8_t matrix_get_row(uint8_t row)
  375. #else
  376. uint16_t matrix_get_row(uint8_t row)
  377. #endif
  378. {
  379. return matrix[row];
  380. }
  381. void matrix_print(void)
  382. {
  383. #if (MATRIX_COLS <= 8)
  384. print("\nr/c 01234567\n");
  385. #else
  386. print("\nr/c 0123456789ABCDEF\n");
  387. #endif
  388. for (uint8_t row = 0; row < matrix_rows(); row++) {
  389. phex(row); print(": ");
  390. #if (MATRIX_COLS <= 8)
  391. pbin_reverse(matrix_get_row(row));
  392. #else
  393. pbin_reverse16(matrix_get_row(row));
  394. #endif
  395. #ifdef MATRIX_HAS_GHOST
  396. if (matrix_has_ghost_in_row(row)) {
  397. print(" <ghost");
  398. }
  399. #endif
  400. print("\n");
  401. }
  402. }
  403. uint8_t matrix_key_count(void)
  404. {
  405. uint8_t count = 0;
  406. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  407. #if (MATRIX_COLS <= 8)
  408. count += bitpop(matrix[i]);
  409. #else
  410. count += bitpop16(matrix[i]);
  411. #endif
  412. }
  413. return count;
  414. }
  415. #ifdef MATRIX_HAS_GHOST
  416. inline
  417. static bool matrix_has_ghost_in_row(uint8_t row)
  418. {
  419. // no ghost exists in case less than 2 keys on
  420. if (((matrix[row] - 1) & matrix[row]) == 0)
  421. return false;
  422. // ghost exists in case same state as other row
  423. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  424. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  425. return true;
  426. }
  427. return false;
  428. }
  429. #endif
  430. inline
  431. static void matrix_make(uint8_t code)
  432. {
  433. if (!matrix_is_on(ROW(code), COL(code))) {
  434. matrix[ROW(code)] |= 1<<COL(code);
  435. is_modified = true;
  436. //print("matrix_make: "); phex(code); print("\n");
  437. }
  438. }
  439. inline
  440. static void matrix_break(uint8_t code)
  441. {
  442. if (matrix_is_on(ROW(code), COL(code))) {
  443. matrix[ROW(code)] &= ~(1<<COL(code));
  444. is_modified = true;
  445. //print("matrix_break: "); phex(code); print("\n");
  446. }
  447. }
  448. static void ps2_reset(void)
  449. {
  450. ps2_host_send(0xFF);
  451. if (ps2_host_recv() != 0xFA) return;
  452. _delay_ms(1000);
  453. if (ps2_host_recv() != 0xAA) return;
  454. }
  455. static void ps2_set_leds(uint8_t leds)
  456. {
  457. ps2_host_send(0xED);
  458. if (ps2_host_recv() != 0xFA) return; // 0xFA
  459. ps2_host_send(leds);
  460. if (ps2_host_recv() != 0xFA) return; // 0xFA
  461. }