Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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