matrix.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "debounce.h"
  31. #include QMK_KEYBOARD_H
  32. #ifdef DEBUG_MATRIX_SCAN_RATE
  33. # include "timer.h"
  34. #endif
  35. /*
  36. * This constant define not debouncing time in msecs, assuming eager_pr.
  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 raw_matrix[MATRIX_ROWS]; // raw values
  51. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  52. static matrix_row_t read_cols(uint8_t row);
  53. static void init_cols(void);
  54. static void unselect_rows(void);
  55. static void select_row(uint8_t row);
  56. static uint8_t mcp23018_reset_loop;
  57. // static uint16_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)) void matrix_init_user(void) {}
  63. __attribute__((weak)) void matrix_scan_user(void) {}
  64. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  65. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  66. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  67. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  68. void matrix_init(void) {
  69. // initialize row and col
  70. mcp23018_status = init_mcp23018();
  71. unselect_rows();
  72. init_cols();
  73. // initialize matrix state: all keys off
  74. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  75. matrix[i] = 0;
  76. raw_matrix[i] = 0;
  77. }
  78. #ifdef DEBUG_MATRIX_SCAN_RATE
  79. matrix_timer = timer_read32();
  80. matrix_scan_count = 0;
  81. #endif
  82. debounce_init(MATRIX_ROWS);
  83. matrix_init_quantum();
  84. }
  85. void matrix_power_up(void) {
  86. mcp23018_status = init_mcp23018();
  87. unselect_rows();
  88. init_cols();
  89. // initialize matrix state: all keys off
  90. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  91. matrix[i] = 0;
  92. }
  93. #ifdef DEBUG_MATRIX_SCAN_RATE
  94. matrix_timer = timer_read32();
  95. matrix_scan_count = 0;
  96. #endif
  97. }
  98. // Reads and stores a row, returning
  99. // whether a change occurred.
  100. static inline bool store_raw_matrix_row(uint8_t index) {
  101. matrix_row_t temp = read_cols(index);
  102. if (raw_matrix[index] != temp) {
  103. raw_matrix[index] = temp;
  104. return true;
  105. }
  106. return false;
  107. }
  108. uint8_t matrix_scan(void) {
  109. if (mcp23018_status) { // if there was an error
  110. if (++mcp23018_reset_loop == 0) {
  111. // if (++mcp23018_reset_loop >= 1300) {
  112. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  113. // this will be approx bit more frequent than once per second
  114. print("trying to reset mcp23018\n");
  115. mcp23018_status = init_mcp23018();
  116. if (mcp23018_status) {
  117. print("left side not responding\n");
  118. } else {
  119. print("left side attached\n");
  120. ergodox_blink_all_leds();
  121. }
  122. }
  123. }
  124. #ifdef DEBUG_MATRIX_SCAN_RATE
  125. matrix_scan_count++;
  126. uint32_t timer_now = timer_read32();
  127. if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
  128. print("matrix scan frequency: ");
  129. pdec(matrix_scan_count);
  130. print("\n");
  131. matrix_timer = timer_now;
  132. matrix_scan_count = 0;
  133. }
  134. #endif
  135. #ifdef LEFT_LEDS
  136. mcp23018_status = ergodox_left_leds_update();
  137. #endif // LEFT_LEDS
  138. bool changed = false;
  139. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  140. // select rows from left and right hands
  141. uint8_t left_index = i;
  142. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  143. select_row(left_index);
  144. select_row(right_index);
  145. // we don't need a 30us delay anymore, because selecting a
  146. // left-hand row requires more than 30us for i2c.
  147. changed |= store_raw_matrix_row(left_index);
  148. changed |= store_raw_matrix_row(right_index);
  149. unselect_rows();
  150. }
  151. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  152. matrix_scan_quantum();
  153. return 1;
  154. }
  155. bool matrix_is_modified(void) // deprecated and evidently not called.
  156. {
  157. return true;
  158. }
  159. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  160. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  161. void matrix_print(void) {
  162. print("\nr/c 0123456789ABCDEF\n");
  163. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  164. phex(row);
  165. print(": ");
  166. pbin_reverse16(matrix_get_row(row));
  167. print("\n");
  168. }
  169. }
  170. uint8_t matrix_key_count(void) {
  171. uint8_t count = 0;
  172. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  173. count += bitpop16(matrix[i]);
  174. }
  175. return count;
  176. }
  177. /* Column pin configuration
  178. *
  179. * Teensy
  180. * col: 0 1 2 3 4 5
  181. * pin: F0 F1 F4 F5 F6 F7
  182. *
  183. * MCP23018
  184. * col: 0 1 2 3 4 5
  185. * pin: B5 B4 B3 B2 B1 B0
  186. */
  187. static void init_cols(void) {
  188. // init on mcp23018
  189. // not needed, already done as part of init_mcp23018()
  190. // init on teensy
  191. // Input with pull-up(DDR:0, PORT:1)
  192. DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
  193. PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
  194. }
  195. static matrix_row_t read_cols(uint8_t row) {
  196. if (row < 7) {
  197. if (mcp23018_status) { // if there was an error
  198. return 0;
  199. } else {
  200. uint8_t data = 0;
  201. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
  202. if (mcp23018_status) goto out;
  203. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);
  204. if (mcp23018_status) goto out;
  205. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);
  206. if (mcp23018_status) goto out;
  207. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);
  208. if (mcp23018_status < 0) goto out;
  209. data = ~((uint8_t)mcp23018_status);
  210. mcp23018_status = I2C_STATUS_SUCCESS;
  211. out:
  212. i2c_stop();
  213. return data;
  214. }
  215. } else {
  216. /* read from teensy
  217. * bitmask is 0b11110011, but we want those all
  218. * in the lower six bits.
  219. * we'll return 1s for the top two, but that's harmless.
  220. */
  221. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  222. }
  223. }
  224. /* Row pin configuration
  225. *
  226. * Teensy
  227. * row: 7 8 9 10 11 12 13
  228. * pin: B0 B1 B2 B3 D2 D3 C6
  229. *
  230. * MCP23018
  231. * row: 0 1 2 3 4 5 6
  232. * pin: A0 A1 A2 A3 A4 A5 A6
  233. */
  234. static void unselect_rows(void) {
  235. // no need to unselect on mcp23018, because the select step sets all
  236. // the other row bits high, and it's not changing to a different
  237. // direction
  238. // unselect on teensy
  239. // Hi-Z(DDR:0, PORT:0) to unselect
  240. DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  241. PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  242. DDRD &= ~(1 << 2 | 1 << 3);
  243. PORTD &= ~(1 << 2 | 1 << 3);
  244. DDRC &= ~(1 << 6);
  245. PORTC &= ~(1 << 6);
  246. }
  247. static void select_row(uint8_t row) {
  248. if (row < 7) {
  249. // select on mcp23018
  250. if (mcp23018_status) { // if there was an error
  251. // do nothing
  252. } else {
  253. // set active row low : 0
  254. // set other rows hi-Z : 1
  255. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
  256. if (mcp23018_status) goto out;
  257. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);
  258. if (mcp23018_status) goto out;
  259. mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT);
  260. if (mcp23018_status) goto out;
  261. out:
  262. i2c_stop();
  263. }
  264. } else {
  265. // select on teensy
  266. // Output low(DDR:1, PORT:0) to select
  267. switch (row) {
  268. case 7:
  269. DDRB |= (1 << 0);
  270. PORTB &= ~(1 << 0);
  271. break;
  272. case 8:
  273. DDRB |= (1 << 1);
  274. PORTB &= ~(1 << 1);
  275. break;
  276. case 9:
  277. DDRB |= (1 << 2);
  278. PORTB &= ~(1 << 2);
  279. break;
  280. case 10:
  281. DDRB |= (1 << 3);
  282. PORTB &= ~(1 << 3);
  283. break;
  284. case 11:
  285. DDRD |= (1 << 2);
  286. PORTD &= ~(1 << 2);
  287. break;
  288. case 12:
  289. DDRD |= (1 << 3);
  290. PORTD &= ~(1 << 3);
  291. break;
  292. case 13:
  293. DDRC |= (1 << 6);
  294. PORTC &= ~(1 << 6);
  295. break;
  296. }
  297. }
  298. }