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.

247 lines
6.6 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
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  4. Copyright 2015 ZSA Technology Labs Inc (@zsa)
  5. Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * scan matrix
  19. */
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include <avr/io.h>
  23. #include "wait.h"
  24. #include "action_layer.h"
  25. #include "print.h"
  26. #include "debug.h"
  27. #include "util.h"
  28. #include "matrix.h"
  29. #include "debounce.h"
  30. #include "ergodox_ez.h"
  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. /* matrix state(1:on, 0:off) */
  43. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  44. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  45. static matrix_row_t read_cols(uint8_t row);
  46. static void init_cols(void);
  47. static void unselect_rows(void);
  48. static void select_row(uint8_t row);
  49. static uint8_t mcp23018_reset_loop;
  50. void matrix_init_custom(void) {
  51. // initialize row and col
  52. mcp23018_status = init_mcp23018();
  53. unselect_rows();
  54. init_cols();
  55. }
  56. // Reads and stores a row, returning
  57. // whether a change occurred.
  58. static inline bool store_raw_matrix_row(uint8_t index) {
  59. matrix_row_t temp = 0x3F & read_cols(index);
  60. if (raw_matrix[index] != temp) {
  61. raw_matrix[index] = temp;
  62. return true;
  63. }
  64. return false;
  65. }
  66. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  67. if (mcp23018_status) { // if there was an error
  68. if (++mcp23018_reset_loop == 0) {
  69. print("trying to reset mcp23018\n");
  70. mcp23018_status = init_mcp23018();
  71. if (mcp23018_status) {
  72. print("left side not responding\n");
  73. } else {
  74. print("left side attached\n");
  75. ergodox_blink_all_leds();
  76. #ifdef RGB_MATRIX_ENABLE
  77. rgb_matrix_init(); // re-init driver on reconnect
  78. #endif
  79. }
  80. }
  81. }
  82. #ifdef LEFT_LEDS
  83. mcp23018_status = ergodox_left_leds_update();
  84. #endif // LEFT_LEDS
  85. bool changed = false;
  86. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  87. // select rows from left and right hands
  88. uint8_t left_index = i;
  89. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  90. select_row(left_index);
  91. select_row(right_index);
  92. changed |= store_raw_matrix_row(left_index);
  93. changed |= store_raw_matrix_row(right_index);
  94. unselect_rows();
  95. }
  96. return changed;
  97. }
  98. /* Column pin configuration
  99. *
  100. * Teensy
  101. * col: 0 1 2 3 4 5
  102. * pin: F0 F1 F4 F5 F6 F7
  103. *
  104. * MCP23018
  105. * col: 0 1 2 3 4 5
  106. * pin: B5 B4 B3 B2 B1 B0
  107. */
  108. static void init_cols(void) {
  109. // init on mcp23018
  110. // not needed, already done as part of init_mcp23018()
  111. // init on teensy
  112. setPinInputHigh(F0);
  113. setPinInputHigh(F1);
  114. setPinInputHigh(F4);
  115. setPinInputHigh(F5);
  116. setPinInputHigh(F6);
  117. setPinInputHigh(F7);
  118. }
  119. static matrix_row_t read_cols(uint8_t row) {
  120. if (row < 7) {
  121. if (mcp23018_status) { // if there was an error
  122. return 0;
  123. } else {
  124. uint8_t data = 0;
  125. // reading GPIOB (column port) since in mcp23018's sequential mode
  126. // it is addressed directly after writing to GPIOA in select_row()
  127. mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
  128. return ~data;
  129. }
  130. } else {
  131. /* read from teensy
  132. * bitmask is 0b11110011, but we want those all
  133. * in the lower six bits.
  134. * we'll return 1s for the top two, but that's harmless.
  135. */
  136. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  137. }
  138. }
  139. /* Row pin configuration
  140. *
  141. * Teensy
  142. * row: 7 8 9 10 11 12 13
  143. * pin: B0 B1 B2 B3 D2 D3 C6
  144. *
  145. * MCP23018
  146. * row: 0 1 2 3 4 5 6
  147. * pin: A0 A1 A2 A3 A4 A5 A6
  148. */
  149. static void unselect_rows(void) {
  150. // no need to unselect on mcp23018, because the select step sets all
  151. // the other row bits high, and it's not changing to a different
  152. // direction
  153. // unselect on teensy
  154. setPinInput(B0);
  155. setPinInput(B1);
  156. setPinInput(B2);
  157. setPinInput(B3);
  158. setPinInput(D2);
  159. setPinInput(D3);
  160. setPinInput(C6);
  161. }
  162. static void select_row(uint8_t row) {
  163. if (row < 7) {
  164. // select on mcp23018
  165. if (!mcp23018_status) {
  166. // set active row low : 0
  167. // set other rows hi-Z : 1
  168. uint8_t data;
  169. data = 0xFF & ~(1 << row);
  170. mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
  171. }
  172. } else {
  173. // select on teensy
  174. // Output low(DDR:1, PORT:0) to select
  175. switch (row) {
  176. case 7:
  177. setPinOutput(B0);
  178. writePinLow(B0);
  179. break;
  180. case 8:
  181. setPinOutput(B1);
  182. writePinLow(B1);
  183. break;
  184. case 9:
  185. setPinOutput(B2);
  186. writePinLow(B2);
  187. break;
  188. case 10:
  189. setPinOutput(B3);
  190. writePinLow(B3);
  191. break;
  192. case 11:
  193. setPinOutput(D2);
  194. writePinLow(D2);
  195. break;
  196. case 12:
  197. setPinOutput(D3);
  198. writePinLow(D3);
  199. break;
  200. case 13:
  201. setPinOutput(C6);
  202. writePinLow(C6);
  203. break;
  204. }
  205. }
  206. }
  207. // DO NOT REMOVE
  208. // Needed for proper wake/sleep
  209. void matrix_power_up(void) {
  210. mcp23018_status = init_mcp23018();
  211. unselect_rows();
  212. init_cols();
  213. // initialize matrix state: all keys off
  214. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  215. matrix[i] = 0;
  216. }
  217. }