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.

254 lines
7.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
  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 = 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_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  128. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  129. data = ~((uint8_t)mcp23018_status);
  130. mcp23018_status = I2C_STATUS_SUCCESS;
  131. out:
  132. i2c_stop();
  133. return data;
  134. }
  135. } else {
  136. /* read from teensy
  137. * bitmask is 0b11110011, but we want those all
  138. * in the lower six bits.
  139. * we'll return 1s for the top two, but that's harmless.
  140. */
  141. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  142. }
  143. }
  144. /* Row pin configuration
  145. *
  146. * Teensy
  147. * row: 7 8 9 10 11 12 13
  148. * pin: B0 B1 B2 B3 D2 D3 C6
  149. *
  150. * MCP23018
  151. * row: 0 1 2 3 4 5 6
  152. * pin: A0 A1 A2 A3 A4 A5 A6
  153. */
  154. static void unselect_rows(void) {
  155. // no need to unselect on mcp23018, because the select step sets all
  156. // the other row bits high, and it's not changing to a different
  157. // direction
  158. // unselect on teensy
  159. setPinInput(B0);
  160. setPinInput(B1);
  161. setPinInput(B2);
  162. setPinInput(B3);
  163. setPinInput(D2);
  164. setPinInput(D3);
  165. setPinInput(C6);
  166. }
  167. static void select_row(uint8_t row) {
  168. if (row < 7) {
  169. // select on mcp23018
  170. if (!mcp23018_status) {
  171. // set active row low : 0
  172. // set other rows hi-Z : 1
  173. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  174. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  175. mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  176. out:
  177. i2c_stop();
  178. }
  179. } else {
  180. // select on teensy
  181. // Output low(DDR:1, PORT:0) to select
  182. switch (row) {
  183. case 7:
  184. setPinOutput(B0);
  185. writePinLow(B0);
  186. break;
  187. case 8:
  188. setPinOutput(B1);
  189. writePinLow(B1);
  190. break;
  191. case 9:
  192. setPinOutput(B2);
  193. writePinLow(B2);
  194. break;
  195. case 10:
  196. setPinOutput(B3);
  197. writePinLow(B3);
  198. break;
  199. case 11:
  200. setPinOutput(D2);
  201. writePinLow(D2);
  202. break;
  203. case 12:
  204. setPinOutput(D3);
  205. writePinLow(D3);
  206. break;
  207. case 13:
  208. setPinOutput(C6);
  209. writePinLow(C6);
  210. break;
  211. }
  212. }
  213. }
  214. // DO NOT REMOVE
  215. // Needed for proper wake/sleep
  216. void matrix_power_up(void) {
  217. mcp23018_status = init_mcp23018();
  218. unselect_rows();
  219. init_cols();
  220. // initialize matrix state: all keys off
  221. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  222. matrix[i] = 0;
  223. }
  224. }