matrix.c 7.1 KB

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