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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 7.6KB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "timer.h"
  25. #include "matrix.h"
  26. // Timer resolution check
  27. #if (1000000/TIMER_RAW_FREQ > 20)
  28. # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
  29. #endif
  30. #if (MATRIX_COLS > 16)
  31. # error "MATRIX_COLS must not exceed 16"
  32. #endif
  33. #if (MATRIX_ROWS > 255)
  34. # error "MATRIX_ROWS must not exceed 255"
  35. #endif
  36. // matrix state buffer(1:on, 0:off)
  37. #if (MATRIX_COLS <= 8)
  38. static uint8_t *matrix;
  39. static uint8_t *matrix_prev;
  40. static uint8_t _matrix0[MATRIX_ROWS];
  41. static uint8_t _matrix1[MATRIX_ROWS];
  42. #else
  43. static uint16_t *matrix;
  44. static uint16_t *matrix_prev;
  45. static uint16_t _matrix0[MATRIX_ROWS];
  46. static uint16_t _matrix1[MATRIX_ROWS];
  47. #endif
  48. // HHKB has no ghost and no bounce.
  49. #ifdef MATRIX_HAS_GHOST
  50. static bool matrix_has_ghost_in_row(uint8_t row);
  51. #endif
  52. // Matrix I/O ports
  53. //
  54. // row: HC4051[A,B,C] selects scan row0-7
  55. // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
  56. // key: on: 0/off: 1
  57. // prev: unknown: output previous key state(negated)?
  58. #ifdef HOST_PJRC
  59. // Ports for Teensy
  60. // row: PB0-2
  61. // col: PB3-5,6
  62. // key: PE6(pull-uped)
  63. // prev: PE7
  64. #define KEY_INIT() do { \
  65. DDRB |= 0x7F; \
  66. DDRE |= (1<<7); \
  67. DDRE &= ~(1<<6); \
  68. PORTE |= (1<<6); \
  69. } while (0)
  70. #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
  71. (((COL) & 0x07)<<3) | \
  72. ((ROW) & 0x07))
  73. #define KEY_ENABLE() (PORTB &= ~(1<<6))
  74. #define KEY_UNABLE() (PORTB |= (1<<6))
  75. #define KEY_STATE() (PINE & (1<<6))
  76. #define KEY_PREV_ON() (PORTE |= (1<<7))
  77. #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
  78. #define KEY_POWER_ON()
  79. #define KEY_POWER_OFF()
  80. #else
  81. // Ports for V-USB
  82. // key: PB0(pull-uped)
  83. // prev: PB1
  84. // row: PB2-4
  85. // col: PC0-2,3
  86. // power: PB5(Low:on/Hi-z:off)
  87. #define KEY_INIT() do { \
  88. DDRB |= 0x3E; \
  89. DDRB &= ~(1<<0); \
  90. PORTB |= 1<<0; \
  91. DDRC |= 0x0F; \
  92. KEY_UNABLE(); \
  93. KEY_PREV_OFF(); \
  94. } while (0)
  95. #define KEY_SELECT(ROW, COL) do { \
  96. PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
  97. PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
  98. } while (0)
  99. #define KEY_ENABLE() (PORTC &= ~(1<<3))
  100. #define KEY_UNABLE() (PORTC |= (1<<3))
  101. #define KEY_STATE() (PINB & (1<<0))
  102. #define KEY_PREV_ON() (PORTB |= (1<<1))
  103. #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
  104. // Power supply switching
  105. #define KEY_POWER_ON() do { \
  106. KEY_INIT(); \
  107. PORTB &= ~(1<<5); \
  108. _delay_us(200); \
  109. } while (0)
  110. #define KEY_POWER_OFF() do { \
  111. DDRB &= ~0x3F; \
  112. PORTB &= ~0x3F; \
  113. DDRC &= ~0x0F; \
  114. PORTC &= ~0x0F; \
  115. } while (0)
  116. #endif
  117. inline
  118. uint8_t matrix_rows(void)
  119. {
  120. return MATRIX_ROWS;
  121. }
  122. inline
  123. uint8_t matrix_cols(void)
  124. {
  125. return MATRIX_COLS;
  126. }
  127. void matrix_init(void)
  128. {
  129. KEY_INIT();
  130. // initialize matrix state: all keys off
  131. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  132. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  133. matrix = _matrix0;
  134. matrix_prev = _matrix1;
  135. }
  136. uint8_t matrix_scan(void)
  137. {
  138. uint8_t *tmp;
  139. tmp = matrix_prev;
  140. matrix_prev = matrix;
  141. matrix = tmp;
  142. KEY_POWER_ON();
  143. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  144. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  145. KEY_SELECT(row, col);
  146. _delay_us(40);
  147. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  148. if (matrix_prev[row] & (1<<col)) {
  149. KEY_PREV_ON();
  150. }
  151. _delay_us(7);
  152. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  153. // If V-USB interrupts in this section we could lose 40us or so
  154. // and would read invalid value from KEY_STATE.
  155. uint8_t last = TIMER_RAW;
  156. KEY_ENABLE();
  157. // Wait for KEY_STATE outputs its value.
  158. // 1us was ok on one HHKB, but not worked on another.
  159. _delay_us(10);
  160. if (KEY_STATE()) {
  161. matrix[row] &= ~(1<<col);
  162. } else {
  163. matrix[row] |= (1<<col);
  164. }
  165. // Ignore if this code region execution time elapses more than 20us.
  166. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  167. matrix[row] = matrix_prev[row];
  168. }
  169. KEY_PREV_OFF();
  170. KEY_UNABLE();
  171. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  172. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  173. _delay_us(150);
  174. }
  175. }
  176. KEY_POWER_OFF();
  177. return 1;
  178. }
  179. bool matrix_is_modified(void)
  180. {
  181. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  182. if (matrix[i] != matrix_prev[i])
  183. return true;
  184. }
  185. return false;
  186. }
  187. inline
  188. bool matrix_has_ghost(void)
  189. {
  190. #ifdef MATRIX_HAS_GHOST
  191. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  192. if (matrix_has_ghost_in_row(i))
  193. return true;
  194. }
  195. #endif
  196. return false;
  197. }
  198. inline
  199. bool matrix_is_on(uint8_t row, uint8_t col)
  200. {
  201. return (matrix[row] & (1<<col));
  202. }
  203. inline
  204. #if (MATRIX_COLS <= 8)
  205. uint8_t matrix_get_row(uint8_t row)
  206. #else
  207. uint16_t matrix_get_row(uint8_t row)
  208. #endif
  209. {
  210. return matrix[row];
  211. }
  212. void matrix_print(void)
  213. {
  214. #if (MATRIX_COLS <= 8)
  215. print("\nr/c 01234567\n");
  216. #else
  217. print("\nr/c 0123456789ABCDEF\n");
  218. #endif
  219. for (uint8_t row = 0; row < matrix_rows(); row++) {
  220. phex(row); print(": ");
  221. #if (MATRIX_COLS <= 8)
  222. pbin_reverse(matrix_get_row(row));
  223. #else
  224. pbin_reverse16(matrix_get_row(row));
  225. #endif
  226. #ifdef MATRIX_HAS_GHOST
  227. if (matrix_has_ghost_in_row(row)) {
  228. print(" <ghost");
  229. }
  230. #endif
  231. print("\n");
  232. }
  233. }
  234. uint8_t matrix_key_count(void)
  235. {
  236. uint8_t count = 0;
  237. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  238. #if (MATRIX_COLS <= 8)
  239. count += bitpop(matrix[i]);
  240. #else
  241. count += bitpop16(matrix[i]);
  242. #endif
  243. }
  244. return count;
  245. }
  246. #ifdef MATRIX_HAS_GHOST
  247. inline
  248. static bool matrix_has_ghost_in_row(uint8_t row)
  249. {
  250. // no ghost exists in case less than 2 keys on
  251. if (((matrix[row] - 1) & matrix[row]) == 0)
  252. return false;
  253. // ghost exists in case same state as other row
  254. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  255. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  256. return true;
  257. }
  258. return false;
  259. }
  260. #endif