matrix.c 8.2 KB

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