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

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