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

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