matrix.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. Copyright 2017 Erin Call <hello@erincall.com>
  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. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <avr/io.h>
  21. #include "wait.h"
  22. #include "action_layer.h"
  23. #include "print.h"
  24. #include "debug.h"
  25. #include "util.h"
  26. #include "matrix.h"
  27. #include "dactyl.h"
  28. #include "i2cmaster.h"
  29. #ifdef DEBUG_MATRIX_SCAN_RATE
  30. #include "timer.h"
  31. #endif
  32. /*
  33. * This constant define not debouncing time in msecs, but amount of matrix
  34. * scan loops which should be made to get stable debounced results.
  35. *
  36. * On the Dactyl, the matrix scan rate is relatively low, because
  37. * communicating with the left hand's I/O expander is slower than simply
  38. * selecting local pins.
  39. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  40. * According to Cherry specs, debouncing time is 5 msec.
  41. *
  42. * And so, there is no sense to have DEBOUNCE higher than 2.
  43. */
  44. #ifndef DEBOUNCE
  45. # define DEBOUNCE 5
  46. #endif
  47. /* matrix state(1:on, 0:off) */
  48. static matrix_row_t matrix[MATRIX_ROWS];
  49. // Debouncing: store for each key the number of scans until it's eligible to
  50. // change. When scanning the matrix, ignore any changes in keys that have
  51. // already changed in the last DEBOUNCE scans.
  52. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  53. static matrix_row_t read_cols(uint8_t row);
  54. static void init_cols(void);
  55. static void unselect_rows(void);
  56. static void select_row(uint8_t row);
  57. static uint8_t mcp23018_reset_loop;
  58. #ifdef DEBUG_MATRIX_SCAN_RATE
  59. uint32_t matrix_timer;
  60. uint32_t matrix_scan_count;
  61. #endif
  62. __attribute__ ((weak))
  63. void matrix_init_user(void) {}
  64. __attribute__ ((weak))
  65. void matrix_scan_user(void) {}
  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. inline
  75. uint8_t matrix_rows(void)
  76. {
  77. return MATRIX_ROWS;
  78. }
  79. inline
  80. uint8_t matrix_cols(void)
  81. {
  82. return MATRIX_COLS;
  83. }
  84. void matrix_init(void)
  85. {
  86. // initialize row and col
  87. mcp23018_status = init_mcp23018();
  88. unselect_rows();
  89. init_cols();
  90. // initialize matrix state: all keys off
  91. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  92. matrix[i] = 0;
  93. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  94. debounce_matrix[i * MATRIX_COLS + j] = 0;
  95. }
  96. }
  97. #ifdef DEBUG_MATRIX_SCAN_RATE
  98. matrix_timer = timer_read32();
  99. matrix_scan_count = 0;
  100. #endif
  101. matrix_init_quantum();
  102. }
  103. void matrix_power_up(void) {
  104. mcp23018_status = init_mcp23018();
  105. unselect_rows();
  106. init_cols();
  107. // initialize matrix state: all keys off
  108. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  109. matrix[i] = 0;
  110. }
  111. #ifdef DEBUG_MATRIX_SCAN_RATE
  112. matrix_timer = timer_read32();
  113. matrix_scan_count = 0;
  114. #endif
  115. }
  116. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  117. // eligible to change in this scan.
  118. matrix_row_t debounce_mask(uint8_t row) {
  119. matrix_row_t result = 0;
  120. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  121. if (debounce_matrix[row * MATRIX_COLS + j]) {
  122. --debounce_matrix[row * MATRIX_COLS + j];
  123. } else {
  124. result |= (1 << j);
  125. }
  126. }
  127. return result;
  128. }
  129. // Report changed keys in the given row. Resets the debounce countdowns
  130. // corresponding to each set bit in 'change' to DEBOUNCE.
  131. void debounce_report(matrix_row_t change, uint8_t row) {
  132. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  133. if (change & (1 << i)) {
  134. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  135. }
  136. }
  137. }
  138. uint8_t matrix_scan(void)
  139. {
  140. if (mcp23018_status) { // if there was an error
  141. if (++mcp23018_reset_loop == 0) {
  142. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  143. // this will be approx bit more frequent than once per second
  144. print("trying to reset mcp23018\n");
  145. mcp23018_status = init_mcp23018();
  146. if (mcp23018_status) {
  147. print("left side not responding\n");
  148. } else {
  149. print("left side attached\n");
  150. }
  151. }
  152. }
  153. #ifdef DEBUG_MATRIX_SCAN_RATE
  154. matrix_scan_count++;
  155. uint32_t timer_now = timer_read32();
  156. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  157. print("matrix scan frequency: ");
  158. pdec(matrix_scan_count);
  159. print("\n");
  160. matrix_timer = timer_now;
  161. matrix_scan_count = 0;
  162. }
  163. #endif
  164. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  165. select_row(i);
  166. wait_us(30); // without this wait read unstable value.
  167. matrix_row_t mask = debounce_mask(i);
  168. matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
  169. debounce_report(cols ^ matrix[i], i);
  170. matrix[i] = cols;
  171. unselect_rows();
  172. }
  173. matrix_scan_quantum();
  174. return 1;
  175. }
  176. bool matrix_is_modified(void) // deprecated and evidently not called.
  177. {
  178. return true;
  179. }
  180. inline
  181. bool matrix_is_on(uint8_t row, uint8_t col)
  182. {
  183. return (matrix[row] & ((matrix_row_t)1<<col));
  184. }
  185. inline
  186. matrix_row_t matrix_get_row(uint8_t row)
  187. {
  188. return matrix[row];
  189. }
  190. void matrix_print(void)
  191. {
  192. print("\nr/c 0123456789ABCDEF\n");
  193. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  194. phex(row); print(": ");
  195. pbin_reverse16(matrix_get_row(row));
  196. print("\n");
  197. }
  198. }
  199. uint8_t matrix_key_count(void)
  200. {
  201. uint8_t count = 0;
  202. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  203. count += bitpop16(matrix[i]);
  204. }
  205. return count;
  206. }
  207. /* Column pin configuration
  208. *
  209. * Teensy
  210. * col: 0 1 2 3 4 5
  211. * pin: F0 F1 F4 F5 F6 F7
  212. *
  213. * MCP23018
  214. * col: 0 1 2 3 4 5
  215. * pin: B5 B4 B3 B2 B1 B0
  216. */
  217. static void init_cols(void)
  218. {
  219. // init on mcp23018
  220. // not needed, already done as part of init_mcp23018()
  221. // init on teensy
  222. // Input with pull-up(DDR:0, PORT:1)
  223. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  224. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  225. }
  226. static matrix_row_t read_cols(uint8_t row)
  227. {
  228. if (row < 6) {
  229. if (mcp23018_status) { // if there was an error
  230. return 0;
  231. } else {
  232. uint8_t data = 0;
  233. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  234. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  235. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  236. data = i2c_readNak();
  237. data = ~data;
  238. out:
  239. i2c_stop();
  240. return data;
  241. }
  242. } else {
  243. // read from teensy
  244. return
  245. (PINF&(1<<0) ? 0 : (1<<0)) |
  246. (PINF&(1<<1) ? 0 : (1<<1)) |
  247. (PINF&(1<<4) ? 0 : (1<<2)) |
  248. (PINF&(1<<5) ? 0 : (1<<3)) |
  249. (PINF&(1<<6) ? 0 : (1<<4)) |
  250. (PINF&(1<<7) ? 0 : (1<<5)) ;
  251. }
  252. }
  253. /* Row pin configuration
  254. *
  255. * Teensy
  256. * row: 6 7 8 9 10 11
  257. * pin: B1 B2 B3 D2 D3 C6
  258. *
  259. * MCP23018
  260. * row: 0 1 2 3 4 5
  261. * pin: A0 A1 A2 A3 A4 A5
  262. */
  263. static void unselect_rows(void)
  264. {
  265. // unselect on mcp23018
  266. if (mcp23018_status) { // if there was an error
  267. // do nothing
  268. } else {
  269. // set all rows hi-Z : 1
  270. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  271. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  272. mcp23018_status = i2c_write(0xFF); if (mcp23018_status) goto out;
  273. out:
  274. i2c_stop();
  275. }
  276. // unselect on teensy
  277. // Hi-Z(DDR:0, PORT:0) to unselect
  278. DDRB &= ~(1<<1 | 1<<2 | 1<<3);
  279. PORTB &= ~(1<<1 | 1<<2 | 1<<3);
  280. DDRD &= ~(1<<2 | 1<<3);
  281. PORTD &= ~(1<<2 | 1<<3);
  282. DDRC &= ~(1<<6);
  283. PORTC &= ~(1<<6);
  284. }
  285. /* Row pin configuration
  286. *
  287. * Teensy
  288. * row: 6 7 8 9 10 11
  289. * pin: B1 B2 B3 D2 D3 C6
  290. *
  291. * MCP23018
  292. * row: 0 1 2 3 4 5
  293. * pin: A0 A1 A2 A3 A4 A5
  294. */
  295. static void select_row(uint8_t row)
  296. {
  297. if (row < 6) {
  298. // select on mcp23018
  299. if (mcp23018_status) { // if there was an error
  300. // do nothing
  301. } else {
  302. // set active row low : 0
  303. // set other rows hi-Z : 1
  304. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  305. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  306. mcp23018_status = i2c_write(0xFF & ~(1<<row)); if (mcp23018_status) goto out;
  307. out:
  308. i2c_stop();
  309. }
  310. } else {
  311. // select on teensy
  312. // Output low(DDR:1, PORT:0) to select
  313. switch (row) {
  314. case 6:
  315. DDRB |= (1<<1);
  316. PORTB &= ~(1<<1);
  317. break;
  318. case 7:
  319. DDRB |= (1<<2);
  320. PORTB &= ~(1<<2);
  321. break;
  322. case 8:
  323. DDRB |= (1<<3);
  324. PORTB &= ~(1<<3);
  325. break;
  326. case 9:
  327. DDRD |= (1<<2);
  328. PORTD &= ~(1<<3);
  329. break;
  330. case 10:
  331. DDRD |= (1<<3);
  332. PORTD &= ~(1<<3);
  333. break;
  334. case 11:
  335. DDRC |= (1<<6);
  336. PORTC &= ~(1<<6);
  337. break;
  338. }
  339. }
  340. }