matrix.c 8.9 KB

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