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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <avr/interrupt.h>
  21. #include <util/delay.h>
  22. #include "print.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #if (MATRIX_COLS > 16)
  26. # error "MATRIX_COLS must not exceed 16"
  27. #endif
  28. #if (MATRIX_ROWS > 255)
  29. # error "MATRIX_ROWS must not exceed 255"
  30. #endif
  31. // matrix state buffer(1:on, 0:off)
  32. #if (MATRIX_COLS <= 8)
  33. static uint8_t *matrix;
  34. static uint8_t *matrix_prev;
  35. static uint8_t _matrix0[MATRIX_ROWS];
  36. static uint8_t _matrix1[MATRIX_ROWS];
  37. #else
  38. static uint16_t *matrix;
  39. static uint16_t *matrix_prev;
  40. static uint16_t _matrix0[MATRIX_ROWS];
  41. static uint16_t _matrix1[MATRIX_ROWS];
  42. #endif
  43. // HHKB has no ghost and no bounce.
  44. #ifdef MATRIX_HAS_GHOST
  45. static bool matrix_has_ghost_in_row(uint8_t row);
  46. #endif
  47. // Matrix I/O ports
  48. //
  49. // row: HC4051[A,B,C] selects scan row0-7
  50. // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
  51. // key: on: 0/off: 1
  52. // prev: unknown: output previous key state(negated)?
  53. #ifdef HOST_PJRC
  54. // Ports for Teensy
  55. // row: PB0-2
  56. // col: PB3-5,6
  57. // key: PE6(pull-uped)
  58. // prev: PE7
  59. #define KEY_INIT() do { \
  60. DDRB |= 0x7F; \
  61. DDRE |= (1<<7); \
  62. DDRE &= ~(1<<6); \
  63. PORTE |= (1<<6); \
  64. } while (0)
  65. #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
  66. (((COL) & 0x07)<<3) | \
  67. ((ROW) & 0x07))
  68. #define KEY_ENABLE() (PORTB &= ~(1<<6))
  69. #define KEY_UNABLE() (PORTB |= (1<<6))
  70. #define KEY_STATE() (PINE & (1<<6))
  71. #define KEY_PREV_ON() (PORTE |= (1<<7))
  72. #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
  73. #else
  74. // Ports for V-USB
  75. // key: PB0(pull-uped)
  76. // prev: PB1
  77. // row: PB2-4
  78. // col: PC0-2,3
  79. #define KEY_INIT() do { \
  80. DDRB |= 0x1E; \
  81. DDRB &= ~(1<<0); \
  82. PORTB |= (1<<0); \
  83. DDRC |= 0x0F; \
  84. } while (0)
  85. #define KEY_SELECT(ROW, COL) do { \
  86. PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
  87. PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
  88. } while (0)
  89. #define KEY_ENABLE() (PORTC &= ~(1<<3))
  90. #define KEY_UNABLE() (PORTC |= (1<<3))
  91. #define KEY_STATE() (PINB & (1<<0))
  92. #define KEY_PREV_ON() (PORTB |= (1<<1))
  93. #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
  94. #endif
  95. inline
  96. uint8_t matrix_rows(void)
  97. {
  98. return MATRIX_ROWS;
  99. }
  100. inline
  101. uint8_t matrix_cols(void)
  102. {
  103. return MATRIX_COLS;
  104. }
  105. void matrix_init(void)
  106. {
  107. KEY_INIT();
  108. // initialize matrix state: all keys off
  109. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  110. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  111. matrix = _matrix0;
  112. matrix_prev = _matrix1;
  113. }
  114. uint8_t matrix_scan(void)
  115. {
  116. uint8_t *tmp;
  117. tmp = matrix_prev;
  118. matrix_prev = matrix;
  119. matrix = tmp;
  120. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  121. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  122. KEY_SELECT(row, col);
  123. _delay_us(40); // from logic analyzer chart
  124. if (matrix_prev[row] & (1<<col)) {
  125. KEY_PREV_ON();
  126. }
  127. _delay_us(7); // from logic analyzer chart
  128. #if HOST_VUSB
  129. // to avoid V-USB interrupt during read key state
  130. uint8_t sreg = SREG;
  131. cli();
  132. #endif
  133. KEY_ENABLE();
  134. _delay_us(10); // from logic analyzer chart
  135. if (KEY_STATE()) {
  136. matrix[row] &= ~(1<<col);
  137. } else {
  138. matrix[row] |= (1<<col);
  139. }
  140. #if HOST_VUSB
  141. SREG = sreg;
  142. #endif
  143. KEY_PREV_OFF();
  144. KEY_UNABLE();
  145. _delay_us(150); // from logic analyzer chart
  146. }
  147. }
  148. return 1;
  149. }
  150. bool matrix_is_modified(void)
  151. {
  152. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  153. if (matrix[i] != matrix_prev[i])
  154. return true;
  155. }
  156. return false;
  157. }
  158. inline
  159. bool matrix_has_ghost(void)
  160. {
  161. #ifdef MATRIX_HAS_GHOST
  162. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  163. if (matrix_has_ghost_in_row(i))
  164. return true;
  165. }
  166. #endif
  167. return false;
  168. }
  169. inline
  170. bool matrix_is_on(uint8_t row, uint8_t col)
  171. {
  172. return (matrix[row] & (1<<col));
  173. }
  174. inline
  175. #if (MATRIX_COLS <= 8)
  176. uint8_t matrix_get_row(uint8_t row)
  177. #else
  178. uint16_t matrix_get_row(uint8_t row)
  179. #endif
  180. {
  181. return matrix[row];
  182. }
  183. void matrix_print(void)
  184. {
  185. #if (MATRIX_COLS <= 8)
  186. print("\nr/c 01234567\n");
  187. #else
  188. print("\nr/c 0123456789ABCDEF\n");
  189. #endif
  190. for (uint8_t row = 0; row < matrix_rows(); row++) {
  191. phex(row); print(": ");
  192. #if (MATRIX_COLS <= 8)
  193. pbin_reverse(matrix_get_row(row));
  194. #else
  195. pbin_reverse16(matrix_get_row(row));
  196. #endif
  197. #ifdef MATRIX_HAS_GHOST
  198. if (matrix_has_ghost_in_row(row)) {
  199. print(" <ghost");
  200. }
  201. #endif
  202. print("\n");
  203. }
  204. }
  205. uint8_t matrix_key_count(void)
  206. {
  207. uint8_t count = 0;
  208. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  209. #if (MATRIX_COLS <= 8)
  210. count += bitpop(matrix[i]);
  211. #else
  212. count += bitpop16(matrix[i]);
  213. #endif
  214. }
  215. return count;
  216. }
  217. #ifdef MATRIX_HAS_GHOST
  218. inline
  219. static bool matrix_has_ghost_in_row(uint8_t row)
  220. {
  221. // no ghost exists in case less than 2 keys on
  222. if (((matrix[row] - 1) & matrix[row]) == 0)
  223. return false;
  224. // ghost exists in case same state as other row
  225. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  226. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  227. return true;
  228. }
  229. return false;
  230. }
  231. #endif