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.

309 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. }
  100. }
  101. }
  102. #ifdef LEFT_LEDS
  103. mcp23018_status = ergodox_left_leds_update();
  104. #endif // LEFT_LEDS
  105. bool changed = false;
  106. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  107. // select rows from left and right hands
  108. uint8_t left_index = i;
  109. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  110. select_row(left_index);
  111. select_row(right_index);
  112. // we don't need a 30us delay anymore, because selecting a
  113. // left-hand row requires more than 30us for i2c.
  114. changed |= store_raw_matrix_row(left_index);
  115. changed |= store_raw_matrix_row(right_index);
  116. unselect_rows();
  117. }
  118. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  119. matrix_scan_quantum();
  120. return 1;
  121. }
  122. bool matrix_is_modified(void) // deprecated and evidently not called.
  123. {
  124. return true;
  125. }
  126. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  127. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  128. void matrix_print(void) {
  129. print("\nr/c 0123456789ABCDEF\n");
  130. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  131. phex(row);
  132. print(": ");
  133. pbin_reverse16(matrix_get_row(row));
  134. print("\n");
  135. }
  136. }
  137. uint8_t matrix_key_count(void) {
  138. uint8_t count = 0;
  139. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  140. count += bitpop16(matrix[i]);
  141. }
  142. return count;
  143. }
  144. /* Column pin configuration
  145. *
  146. * Teensy
  147. * col: 0 1 2 3 4 5
  148. * pin: F0 F1 F4 F5 F6 F7
  149. *
  150. * MCP23018
  151. * col: 0 1 2 3 4 5
  152. * pin: B5 B4 B3 B2 B1 B0
  153. */
  154. static void init_cols(void) {
  155. // init on mcp23018
  156. // not needed, already done as part of init_mcp23018()
  157. // init on teensy
  158. // Input with pull-up(DDR:0, PORT:1)
  159. DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
  160. PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
  161. }
  162. static matrix_row_t read_cols(uint8_t row) {
  163. if (row < 7) {
  164. if (mcp23018_status) { // if there was an error
  165. return 0;
  166. } else {
  167. uint8_t data = 0;
  168. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
  169. if (mcp23018_status) goto out;
  170. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);
  171. if (mcp23018_status) goto out;
  172. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);
  173. if (mcp23018_status) goto out;
  174. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);
  175. if (mcp23018_status < 0) goto out;
  176. data = ~((uint8_t)mcp23018_status);
  177. mcp23018_status = I2C_STATUS_SUCCESS;
  178. out:
  179. i2c_stop();
  180. return data;
  181. }
  182. } else {
  183. /* read from teensy
  184. * bitmask is 0b11110011, but we want those all
  185. * in the lower six bits.
  186. * we'll return 1s for the top two, but that's harmless.
  187. */
  188. return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  189. }
  190. }
  191. /* Row pin configuration
  192. *
  193. * Teensy
  194. * row: 7 8 9 10 11 12 13
  195. * pin: B0 B1 B2 B3 D2 D3 C6
  196. *
  197. * MCP23018
  198. * row: 0 1 2 3 4 5 6
  199. * pin: A0 A1 A2 A3 A4 A5 A6
  200. */
  201. static void unselect_rows(void) {
  202. // no need to unselect on mcp23018, because the select step sets all
  203. // the other row bits high, and it's not changing to a different
  204. // direction
  205. // unselect on teensy
  206. // Hi-Z(DDR:0, PORT:0) to unselect
  207. DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  208. PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
  209. DDRD &= ~(1 << 2 | 1 << 3);
  210. PORTD &= ~(1 << 2 | 1 << 3);
  211. DDRC &= ~(1 << 6);
  212. PORTC &= ~(1 << 6);
  213. }
  214. static void select_row(uint8_t row) {
  215. if (row < 7) {
  216. // select on mcp23018
  217. if (mcp23018_status) { // if there was an error
  218. // do nothing
  219. } else {
  220. // set active row low : 0
  221. // set other rows hi-Z : 1
  222. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
  223. if (mcp23018_status) goto out;
  224. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);
  225. if (mcp23018_status) goto out;
  226. mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT);
  227. if (mcp23018_status) goto out;
  228. out:
  229. i2c_stop();
  230. }
  231. } else {
  232. // select on teensy
  233. // Output low(DDR:1, PORT:0) to select
  234. switch (row) {
  235. case 7:
  236. DDRB |= (1 << 0);
  237. PORTB &= ~(1 << 0);
  238. break;
  239. case 8:
  240. DDRB |= (1 << 1);
  241. PORTB &= ~(1 << 1);
  242. break;
  243. case 9:
  244. DDRB |= (1 << 2);
  245. PORTB &= ~(1 << 2);
  246. break;
  247. case 10:
  248. DDRB |= (1 << 3);
  249. PORTB &= ~(1 << 3);
  250. break;
  251. case 11:
  252. DDRD |= (1 << 2);
  253. PORTD &= ~(1 << 2);
  254. break;
  255. case 12:
  256. DDRD |= (1 << 3);
  257. PORTD &= ~(1 << 3);
  258. break;
  259. case 13:
  260. DDRC |= (1 << 6);
  261. PORTC &= ~(1 << 6);
  262. break;
  263. }
  264. }
  265. }