matrix.c 8.8 KB

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