matrix.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. /* Set 0 if debouncing isn't needed */
  26. /*
  27. * This constant define not debouncing time in msecs, but amount of matrix
  28. * scan loops which should be made to get stable debounced results.
  29. *
  30. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  31. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  32. * According to Cherry specs, debouncing time is 5 msec.
  33. *
  34. * And so, there is no sense to have DEBOUNCE higher than 2.
  35. */
  36. #ifndef DEBOUNCING_DELAY
  37. # define DEBOUNCING_DELAY 5
  38. #endif
  39. static uint8_t debouncing = DEBOUNCING_DELAY;
  40. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  41. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  42. /* matrix state(1:on, 0:off) */
  43. static matrix_row_t matrix[MATRIX_ROWS];
  44. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  45. #if DIODE_DIRECTION == ROW2COL
  46. static matrix_row_t matrix_reversed[MATRIX_COLS];
  47. static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
  48. #endif
  49. #if MATRIX_COLS > 16
  50. #define SHIFTER 1UL
  51. #else
  52. #define SHIFTER 1
  53. #endif
  54. static matrix_row_t read_cols(void);
  55. static void init_cols(void);
  56. static void unselect_rows(void);
  57. static void select_row(uint8_t row);
  58. __attribute__ ((weak))
  59. void matrix_init_quantum(void) {
  60. matrix_init_kb();
  61. }
  62. __attribute__ ((weak))
  63. void matrix_scan_quantum(void) {
  64. matrix_scan_kb();
  65. }
  66. __attribute__ ((weak))
  67. void matrix_init_kb(void) {
  68. matrix_init_user();
  69. }
  70. __attribute__ ((weak))
  71. void matrix_scan_kb(void) {
  72. matrix_scan_user();
  73. }
  74. __attribute__ ((weak))
  75. void matrix_init_user(void) {
  76. }
  77. __attribute__ ((weak))
  78. void matrix_scan_user(void) {
  79. }
  80. inline
  81. uint8_t matrix_rows(void) {
  82. return MATRIX_ROWS;
  83. }
  84. inline
  85. uint8_t matrix_cols(void) {
  86. return MATRIX_COLS;
  87. }
  88. // void matrix_power_up(void) {
  89. // #if DIODE_DIRECTION == COL2ROW
  90. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  91. // /* DDRxn */
  92. // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
  93. // toggle_row(r);
  94. // }
  95. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  96. // /* PORTxn */
  97. // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
  98. // }
  99. // #else
  100. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  101. // /* DDRxn */
  102. // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
  103. // toggle_col(c);
  104. // }
  105. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  106. // /* PORTxn */
  107. // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
  108. // }
  109. // #endif
  110. // }
  111. void matrix_init(void) {
  112. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  113. #ifdef __AVR_ATmega32U4__
  114. MCUCR |= _BV(JTD);
  115. MCUCR |= _BV(JTD);
  116. #endif
  117. // initialize row and col
  118. unselect_rows();
  119. init_cols();
  120. // initialize matrix state: all keys off
  121. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  122. matrix[i] = 0;
  123. matrix_debouncing[i] = 0;
  124. }
  125. matrix_init_quantum();
  126. }
  127. uint8_t matrix_scan(void)
  128. {
  129. #if DIODE_DIRECTION == COL2ROW
  130. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  131. select_row(i);
  132. wait_us(30); // without this wait read unstable value.
  133. matrix_row_t cols = read_cols();
  134. if (matrix_debouncing[i] != cols) {
  135. matrix_debouncing[i] = cols;
  136. if (debouncing) {
  137. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  138. }
  139. debouncing = DEBOUNCING_DELAY;
  140. }
  141. unselect_rows();
  142. }
  143. if (debouncing) {
  144. if (--debouncing) {
  145. wait_us(1);
  146. } else {
  147. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  148. matrix[i] = matrix_debouncing[i];
  149. }
  150. }
  151. }
  152. #else
  153. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  154. select_row(i);
  155. wait_us(30); // without this wait read unstable value.
  156. matrix_row_t rows = read_cols();
  157. if (matrix_reversed_debouncing[i] != rows) {
  158. matrix_reversed_debouncing[i] = rows;
  159. if (debouncing) {
  160. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  161. }
  162. debouncing = DEBOUNCING_DELAY;
  163. }
  164. unselect_rows();
  165. }
  166. if (debouncing) {
  167. if (--debouncing) {
  168. wait_us(1);
  169. } else {
  170. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  171. matrix_reversed[i] = matrix_reversed_debouncing[i];
  172. }
  173. }
  174. }
  175. for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
  176. matrix_row_t row = 0;
  177. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  178. row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
  179. }
  180. matrix[y] = row;
  181. }
  182. #endif
  183. matrix_scan_quantum();
  184. return 1;
  185. }
  186. bool matrix_is_modified(void)
  187. {
  188. if (debouncing) return false;
  189. return true;
  190. }
  191. inline
  192. bool matrix_is_on(uint8_t row, uint8_t col)
  193. {
  194. return (matrix[row] & ((matrix_row_t)1<col));
  195. }
  196. inline
  197. matrix_row_t matrix_get_row(uint8_t row)
  198. {
  199. return matrix[row];
  200. }
  201. void matrix_print(void)
  202. {
  203. print("\nr/c 0123456789ABCDEF\n");
  204. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  205. phex(row); print(": ");
  206. pbin_reverse16(matrix_get_row(row));
  207. print("\n");
  208. }
  209. }
  210. uint8_t matrix_key_count(void)
  211. {
  212. uint8_t count = 0;
  213. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  214. count += bitpop16(matrix[i]);
  215. }
  216. return count;
  217. }
  218. static void init_cols(void)
  219. {
  220. #if DIODE_DIRECTION == COL2ROW
  221. for(int x = 0; x < MATRIX_COLS; x++) {
  222. int pin = col_pins[x];
  223. #else
  224. for(int x = 0; x < MATRIX_ROWS; x++) {
  225. int pin = row_pins[x];
  226. #endif
  227. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
  228. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
  229. }
  230. }
  231. static matrix_row_t read_cols(void)
  232. {
  233. matrix_row_t result = 0;
  234. #if DIODE_DIRECTION == COL2ROW
  235. for(int x = 0; x < MATRIX_COLS; x++) {
  236. int pin = col_pins[x];
  237. #else
  238. for(int x = 0; x < MATRIX_ROWS; x++) {
  239. int pin = row_pins[x];
  240. #endif
  241. result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
  242. }
  243. return result;
  244. }
  245. static void unselect_rows(void)
  246. {
  247. #if DIODE_DIRECTION == COL2ROW
  248. for(int x = 0; x < MATRIX_ROWS; x++) {
  249. int pin = row_pins[x];
  250. #else
  251. for(int x = 0; x < MATRIX_COLS; x++) {
  252. int pin = col_pins[x];
  253. #endif
  254. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
  255. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
  256. }
  257. }
  258. static void select_row(uint8_t row)
  259. {
  260. #if DIODE_DIRECTION == COL2ROW
  261. int pin = row_pins[row];
  262. #else
  263. int pin = col_pins[row];
  264. #endif
  265. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
  266. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);
  267. }