You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. // reading GPIOB (column port) since in mcp23018's sequential mode
  172. // it is addressed directly after writing to GPIOA in select_row()
  173. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);
  174. if (mcp23018_status) goto out;
  175. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);
  176. if (mcp23018_status < 0) goto out;
  177. data = ~((uint8_t)mcp23018_status);
  178. mcp23018_status = I2C_STATUS_SUCCESS;
  179. out:
  180. i2c_stop();
  181. return data;
  182. }
  183. } else {
  184. /* read from teensy
  185. * bitmask is 0b11110011, but we want those all
  186. * in the lower six bits.
  187. * we'll return 1s for the top two, but that's harmless.
  188. */
  189. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  190. }
  191. }
  192. /* Row pin configuration
  193. *
  194. * Teensy
  195. * row: 7 8 9 10 11 12 13
  196. * pin: B0 B1 B2 B3 D2 D3 C6
  197. *
  198. * MCP23018
  199. * row: 0 1 2 3 4 5 6
  200. * pin: A0 A1 A2 A3 A4 A5 A6
  201. */
  202. static void unselect_rows(void) {
  203. // no need to unselect on mcp23018, because the select step sets all
  204. // the other row bits high, and it's not changing to a different
  205. // direction
  206. // unselect on teensy
  207. // Hi-Z(DDR:0, PORT:0) to unselect
  208. DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  209. PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  210. DDRD &= ~(1 << 2 | 1 << 3);
  211. PORTD &= ~(1 << 2 | 1 << 3);
  212. DDRC &= ~(1 << 6);
  213. PORTC &= ~(1 << 6);
  214. }
  215. static void select_row(uint8_t row) {
  216. if (row < 7) {
  217. // select on mcp23018
  218. if (mcp23018_status) { // if there was an error
  219. // do nothing
  220. } else {
  221. // set active row low : 0
  222. // set other rows hi-Z : 1
  223. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
  224. if (mcp23018_status) goto out;
  225. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);
  226. if (mcp23018_status) goto out;
  227. mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT);
  228. if (mcp23018_status) goto out;
  229. out:
  230. i2c_stop();
  231. }
  232. } else {
  233. // select on teensy
  234. // Output low(DDR:1, PORT:0) to select
  235. switch (row) {
  236. case 7:
  237. DDRB |= (1 << 0);
  238. PORTB &= ~(1 << 0);
  239. break;
  240. case 8:
  241. DDRB |= (1 << 1);
  242. PORTB &= ~(1 << 1);
  243. break;
  244. case 9:
  245. DDRB |= (1 << 2);
  246. PORTB &= ~(1 << 2);
  247. break;
  248. case 10:
  249. DDRB |= (1 << 3);
  250. PORTB &= ~(1 << 3);
  251. break;
  252. case 11:
  253. DDRD |= (1 << 2);
  254. PORTD &= ~(1 << 2);
  255. break;
  256. case 12:
  257. DDRD |= (1 << 3);
  258. PORTD &= ~(1 << 3);
  259. break;
  260. case 13:
  261. DDRC |= (1 << 6);
  262. PORTC &= ~(1 << 6);
  263. break;
  264. }
  265. }
  266. }