matrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. Note for ErgoDox EZ customizers: Here be dragons!
  3. This is not a file you want to be messing with.
  4. All of the interesting stuff for you is under keymaps/ :)
  5. Love, Erez
  6. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * scan matrix
  20. */
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <avr/io.h>
  24. #include "wait.h"
  25. #include "action_layer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30. #include QMK_KEYBOARD_H
  31. #ifdef DEBUG_MATRIX_SCAN_RATE
  32. #include "timer.h"
  33. #endif
  34. /*
  35. * This constant define not debouncing time in msecs, but amount of matrix
  36. * scan loops which should be made to get stable debounced results.
  37. *
  38. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  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. * However, some switches seem to have higher debouncing requirements, or
  43. * something else might be wrong. (Also, the scan speed has improved since
  44. * that comment was written.)
  45. */
  46. #ifndef DEBOUNCE
  47. # define DEBOUNCE 5
  48. #endif
  49. /* matrix state(1:on, 0:off) */
  50. static matrix_row_t matrix[MATRIX_ROWS];
  51. /*
  52. * matrix state(1:on, 0:off)
  53. * contains the raw values without debounce filtering of the last read cycle.
  54. */
  55. static matrix_row_t raw_matrix[MATRIX_ROWS];
  56. // Debouncing: store for each key the number of scans until it's eligible to
  57. // change. When scanning the matrix, ignore any changes in keys that have
  58. // already changed in the last DEBOUNCE scans.
  59. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  60. static matrix_row_t read_cols(uint8_t row);
  61. static void init_cols(void);
  62. static void unselect_rows(void);
  63. static void select_row(uint8_t row);
  64. static uint8_t mcp23018_reset_loop;
  65. // static uint16_t mcp23018_reset_loop;
  66. #ifdef DEBUG_MATRIX_SCAN_RATE
  67. uint32_t matrix_timer;
  68. uint32_t matrix_scan_count;
  69. #endif
  70. __attribute__ ((weak))
  71. void matrix_init_user(void) {}
  72. __attribute__ ((weak))
  73. void matrix_scan_user(void) {}
  74. __attribute__ ((weak))
  75. void matrix_init_kb(void) {
  76. matrix_init_user();
  77. }
  78. __attribute__ ((weak))
  79. void matrix_scan_kb(void) {
  80. matrix_scan_user();
  81. }
  82. inline
  83. uint8_t matrix_rows(void)
  84. {
  85. return MATRIX_ROWS;
  86. }
  87. inline
  88. uint8_t matrix_cols(void)
  89. {
  90. return MATRIX_COLS;
  91. }
  92. void matrix_init(void)
  93. {
  94. // initialize row and col
  95. mcp23018_status = init_mcp23018();
  96. unselect_rows();
  97. init_cols();
  98. // initialize matrix state: all keys off
  99. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  100. matrix[i] = 0;
  101. raw_matrix[i] = 0;
  102. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  103. debounce_matrix[i * MATRIX_COLS + j] = 0;
  104. }
  105. }
  106. #ifdef DEBUG_MATRIX_SCAN_RATE
  107. matrix_timer = timer_read32();
  108. matrix_scan_count = 0;
  109. #endif
  110. matrix_init_quantum();
  111. }
  112. void matrix_power_up(void) {
  113. mcp23018_status = init_mcp23018();
  114. unselect_rows();
  115. init_cols();
  116. // initialize matrix state: all keys off
  117. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  118. matrix[i] = 0;
  119. }
  120. #ifdef DEBUG_MATRIX_SCAN_RATE
  121. matrix_timer = timer_read32();
  122. matrix_scan_count = 0;
  123. #endif
  124. }
  125. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  126. // eligible to change in this scan.
  127. matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
  128. matrix_row_t result = 0;
  129. matrix_row_t change = rawcols ^ raw_matrix[row];
  130. raw_matrix[row] = rawcols;
  131. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  132. if (debounce_matrix[row * MATRIX_COLS + i]) {
  133. --debounce_matrix[row * MATRIX_COLS + i];
  134. } else {
  135. result |= (1 << i);
  136. }
  137. if (change & (1 << i)) {
  138. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  139. }
  140. }
  141. return result;
  142. }
  143. matrix_row_t debounce_read_cols(uint8_t row) {
  144. // Read the row without debouncing filtering and store it for later usage.
  145. matrix_row_t cols = read_cols(row);
  146. // Get the Debounce mask.
  147. matrix_row_t mask = debounce_mask(cols, row);
  148. // debounce the row and return the result.
  149. return (cols & mask) | (matrix[row] & ~mask);;
  150. }
  151. uint8_t matrix_scan(void)
  152. {
  153. if (mcp23018_status) { // if there was an error
  154. if (++mcp23018_reset_loop == 0) {
  155. // if (++mcp23018_reset_loop >= 1300) {
  156. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  157. // this will be approx bit more frequent than once per second
  158. print("trying to reset mcp23018\n");
  159. mcp23018_status = init_mcp23018();
  160. if (mcp23018_status) {
  161. print("left side not responding\n");
  162. } else {
  163. print("left side attached\n");
  164. ergodox_blink_all_leds();
  165. }
  166. }
  167. }
  168. #ifdef DEBUG_MATRIX_SCAN_RATE
  169. matrix_scan_count++;
  170. uint32_t timer_now = timer_read32();
  171. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  172. print("matrix scan frequency: ");
  173. pdec(matrix_scan_count);
  174. print("\n");
  175. matrix_timer = timer_now;
  176. matrix_scan_count = 0;
  177. }
  178. #endif
  179. #ifdef LEFT_LEDS
  180. mcp23018_status = ergodox_left_leds_update();
  181. #endif // LEFT_LEDS
  182. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  183. select_row(i);
  184. // and select on left hand
  185. select_row(i + MATRIX_ROWS_PER_SIDE);
  186. // we don't need a 30us delay anymore, because selecting a
  187. // left-hand row requires more than 30us for i2c.
  188. // grab cols from left hand
  189. matrix[i] = debounce_read_cols(i);
  190. // grab cols from right hand
  191. matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
  192. unselect_rows();
  193. }
  194. matrix_scan_quantum();
  195. return 1;
  196. }
  197. bool matrix_is_modified(void) // deprecated and evidently not called.
  198. {
  199. return true;
  200. }
  201. inline
  202. bool matrix_is_on(uint8_t row, uint8_t col)
  203. {
  204. return (matrix[row] & ((matrix_row_t)1<<col));
  205. }
  206. inline
  207. matrix_row_t matrix_get_row(uint8_t row)
  208. {
  209. return matrix[row];
  210. }
  211. void matrix_print(void)
  212. {
  213. print("\nr/c 0123456789ABCDEF\n");
  214. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  215. phex(row); print(": ");
  216. pbin_reverse16(matrix_get_row(row));
  217. print("\n");
  218. }
  219. }
  220. uint8_t matrix_key_count(void)
  221. {
  222. uint8_t count = 0;
  223. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  224. count += bitpop16(matrix[i]);
  225. }
  226. return count;
  227. }
  228. /* Column pin configuration
  229. *
  230. * Teensy
  231. * col: 0 1 2 3 4 5
  232. * pin: F0 F1 F4 F5 F6 F7
  233. *
  234. * MCP23018
  235. * col: 0 1 2 3 4 5
  236. * pin: B5 B4 B3 B2 B1 B0
  237. */
  238. static void init_cols(void)
  239. {
  240. // init on mcp23018
  241. // not needed, already done as part of init_mcp23018()
  242. // init on teensy
  243. // Input with pull-up(DDR:0, PORT:1)
  244. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  245. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  246. }
  247. static matrix_row_t read_cols(uint8_t row)
  248. {
  249. if (row < 7) {
  250. if (mcp23018_status) { // if there was an error
  251. return 0;
  252. } else {
  253. uint8_t data = 0;
  254. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  255. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  256. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  257. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  258. data = ~((uint8_t)mcp23018_status);
  259. mcp23018_status = I2C_STATUS_SUCCESS;
  260. out:
  261. i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
  262. return data;
  263. }
  264. } else {
  265. /* read from teensy
  266. * bitmask is 0b11110011, but we want those all
  267. * in the lower six bits.
  268. * we'll return 1s for the top two, but that's harmless.
  269. */
  270. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  271. }
  272. }
  273. /* Row pin configuration
  274. *
  275. * Teensy
  276. * row: 7 8 9 10 11 12 13
  277. * pin: B0 B1 B2 B3 D2 D3 C6
  278. *
  279. * MCP23018
  280. * row: 0 1 2 3 4 5 6
  281. * pin: A0 A1 A2 A3 A4 A5 A6
  282. */
  283. static void unselect_rows(void)
  284. {
  285. // no need to unselect on mcp23018, because the select step sets all
  286. // the other row bits high, and it's not changing to a different
  287. // direction
  288. // unselect on teensy
  289. // Hi-Z(DDR:0, PORT:0) to unselect
  290. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  291. PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  292. DDRD &= ~(1<<2 | 1<<3);
  293. PORTD &= ~(1<<2 | 1<<3);
  294. DDRC &= ~(1<<6);
  295. PORTC &= ~(1<<6);
  296. }
  297. static void select_row(uint8_t row)
  298. {
  299. if (row < 7) {
  300. // select on mcp23018
  301. if (mcp23018_status) { // if there was an error
  302. // do nothing
  303. } else {
  304. // set active row low : 0
  305. // set other rows hi-Z : 1
  306. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  307. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  308. mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  309. out:
  310. i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
  311. }
  312. } else {
  313. // select on teensy
  314. // Output low(DDR:1, PORT:0) to select
  315. switch (row) {
  316. case 7:
  317. DDRB |= (1<<0);
  318. PORTB &= ~(1<<0);
  319. break;
  320. case 8:
  321. DDRB |= (1<<1);
  322. PORTB &= ~(1<<1);
  323. break;
  324. case 9:
  325. DDRB |= (1<<2);
  326. PORTB &= ~(1<<2);
  327. break;
  328. case 10:
  329. DDRB |= (1<<3);
  330. PORTB &= ~(1<<3);
  331. break;
  332. case 11:
  333. DDRD |= (1<<2);
  334. PORTD &= ~(1<<2);
  335. break;
  336. case 12:
  337. DDRD |= (1<<3);
  338. PORTD &= ~(1<<3);
  339. break;
  340. case 13:
  341. DDRC |= (1<<6);
  342. PORTC &= ~(1<<6);
  343. break;
  344. }
  345. }
  346. }