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.

331 lines
8.7 KiB

  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. #include "matrix.h"
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "action_layer.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include QMK_KEYBOARD_H
  24. #ifndef DEBOUNCE
  25. # define DEBOUNCE 5
  26. #endif
  27. // ATmega pin defs
  28. #define ROW1 (1<<5)
  29. #define COL6 (1<<0)
  30. #define COL7 (1<<1)
  31. #define COL8 (1<<2)
  32. #define COL9 (1<<3)
  33. #define COL10 (1<<2)
  34. #define COL11 (1<<3)
  35. // bit masks
  36. #define BMASK (COL7 | COL8 | COL9 | COL6)
  37. #define DMASK (COL10 | COL11)
  38. #define FMASK (ROW1)
  39. /* matrix state(1:on, 0:off) */
  40. static matrix_row_t matrix[MATRIX_ROWS];
  41. /*
  42. * matrix state(1:on, 0:off)
  43. * contains the raw values without debounce filtering of the last read cycle.
  44. */
  45. static matrix_row_t raw_matrix[MATRIX_ROWS];
  46. // Debouncing: store for each key the number of scans until it's eligible to
  47. // change. When scanning the matrix, ignore any changes in keys that have
  48. // already changed in the last DEBOUNCE scans.
  49. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  50. static matrix_row_t read_cols(uint8_t row);
  51. static void init_cols(void);
  52. static void unselect_rows(void);
  53. static void select_row(uint8_t row);
  54. static uint8_t mcp23018_reset_loop;
  55. // static uint16_t mcp23018_reset_loop;
  56. __attribute__ ((weak))
  57. void matrix_init_user(void) {}
  58. __attribute__ ((weak))
  59. void matrix_scan_user(void) {}
  60. __attribute__ ((weak))
  61. void matrix_init_kb(void) {
  62. matrix_init_user();
  63. }
  64. __attribute__ ((weak))
  65. void matrix_scan_kb(void) {
  66. matrix_scan_user();
  67. }
  68. inline
  69. uint8_t matrix_rows(void)
  70. {
  71. return MATRIX_ROWS;
  72. }
  73. inline
  74. uint8_t matrix_cols(void)
  75. {
  76. return MATRIX_COLS;
  77. }
  78. void matrix_init(void)
  79. {
  80. // initialize row and col
  81. mcp23018_status = init_mcp23018();
  82. unselect_rows();
  83. init_cols();
  84. // initialize matrix state: all keys off
  85. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = 0;
  87. raw_matrix[i] = 0;
  88. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  89. debounce_matrix[i * MATRIX_COLS + j] = 0;
  90. }
  91. }
  92. matrix_init_quantum();
  93. }
  94. void matrix_power_up(void) {
  95. mcp23018_status = init_mcp23018();
  96. unselect_rows();
  97. init_cols();
  98. // initialize matrix state: all keys off
  99. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  100. matrix[i] = 0;
  101. }
  102. }
  103. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  104. // eligible to change in this scan.
  105. matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
  106. matrix_row_t result = 0;
  107. matrix_row_t change = rawcols ^ raw_matrix[row];
  108. raw_matrix[row] = rawcols;
  109. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  110. if (debounce_matrix[row * MATRIX_COLS + i]) {
  111. --debounce_matrix[row * MATRIX_COLS + i];
  112. } else {
  113. result |= (1 << i);
  114. }
  115. if (change & (1 << i)) {
  116. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  117. }
  118. }
  119. return result;
  120. }
  121. matrix_row_t debounce_read_cols(uint8_t row) {
  122. // Read the row without debouncing filtering and store it for later usage.
  123. matrix_row_t cols = read_cols(row);
  124. // Get the Debounce mask.
  125. matrix_row_t mask = debounce_mask(cols, row);
  126. // debounce the row and return the result.
  127. return (cols & mask) | (matrix[row] & ~mask);;
  128. }
  129. uint8_t matrix_scan(void)
  130. {
  131. // Then the keyboard
  132. if (mcp23018_status) { // if there was an error
  133. if (++mcp23018_reset_loop == 0) {
  134. // if (++mcp23018_reset_loop >= 1300) {
  135. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  136. // this will be approx bit more frequent than once per second
  137. print("trying to reset mcp23018\n");
  138. mcp23018_status = init_mcp23018();
  139. if (mcp23018_status) {
  140. print("left side not responding\n");
  141. } else {
  142. print("left side attached\n");
  143. }
  144. }
  145. }
  146. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  147. select_row(i);
  148. // and select on left hand
  149. select_row(i + MATRIX_ROWS_PER_SIDE);
  150. // we don't need a 30us delay anymore, because selecting a
  151. // left-hand row requires more than 30us for i2c.
  152. // grab cols from left hand
  153. matrix[i] = debounce_read_cols(i);
  154. // grab cols from right hand
  155. matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
  156. unselect_rows();
  157. }
  158. matrix_scan_quantum();
  159. #ifdef DEBUG_MATRIX
  160. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  161. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  162. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  163. #endif
  164. return 1;
  165. }
  166. bool matrix_is_modified(void) // deprecated and evidently not called.
  167. {
  168. return true;
  169. }
  170. inline
  171. bool matrix_is_on(uint8_t row, uint8_t col)
  172. {
  173. return (matrix[row] & ((matrix_row_t)1<<col));
  174. }
  175. inline
  176. matrix_row_t matrix_get_row(uint8_t row)
  177. {
  178. return matrix[row];
  179. }
  180. void matrix_print(void)
  181. {
  182. print("\nr/c 0123456789ABCDEF\n");
  183. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  184. print_hex8(row); print(": ");
  185. print_bin_reverse16(matrix_get_row(row));
  186. print("\n");
  187. }
  188. }
  189. uint8_t matrix_key_count(void)
  190. {
  191. uint8_t count = 0;
  192. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  193. count += bitpop16(matrix[i]);
  194. }
  195. return count;
  196. }
  197. // Remember this means ROWS
  198. static void init_cols(void)
  199. {
  200. // init on mcp23018
  201. // not needed, already done as part of init_mcp23018()
  202. // Input with pull-up(DDR:0, PORT:1)
  203. DDRF &= ~FMASK;
  204. PORTF |= FMASK;
  205. }
  206. static matrix_row_t read_cols(uint8_t row)
  207. {
  208. if (row < 6) {
  209. if (mcp23018_status) { // if there was an error
  210. return 0;
  211. } else {
  212. uint8_t data = 0;
  213. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  214. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  215. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  216. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  217. data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
  218. mcp23018_status = I2C_STATUS_SUCCESS;
  219. out:
  220. i2c_stop();
  221. #ifdef DEBUG_MATRIX
  222. if (data != 0x00) xprintf("I2C: %d\n", data);
  223. #endif
  224. return data;
  225. }
  226. } else {
  227. // Read using bitmask
  228. return ~((((PINF & ROW1) >> 5)) & 0x1);
  229. }
  230. }
  231. // Row pin configuration
  232. static void unselect_rows(void)
  233. {
  234. // no need to unselect on mcp23018, because the select step sets all
  235. // the other row bits high, and it's not changing to a different
  236. // direction
  237. // Hi-Z(DDR:0, PORT:0) to unselect
  238. DDRB &= ~BMASK;
  239. PORTB &= ~BMASK;
  240. DDRD &= ~DMASK;
  241. PORTD &= ~DMASK;
  242. }
  243. static void select_row(uint8_t row)
  244. {
  245. if (row < 6) {
  246. // select on mcp23018
  247. if (mcp23018_status) { // do nothing on error
  248. // Read using bitmask
  249. } else { // set active row low : 0 // set other rows hi-Z : 1
  250. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  251. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  252. mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  253. out:
  254. i2c_stop();
  255. }
  256. } else {
  257. // Output low(DDR:1, PORT:0) to select
  258. switch (row) {
  259. case 6:
  260. DDRB |= COL6;
  261. PORTB &= ~COL6;
  262. break;
  263. case 7:
  264. DDRB |= COL7;
  265. PORTB &= ~COL7;
  266. break;
  267. case 8:
  268. DDRB |= COL8;
  269. PORTB &= ~COL8;
  270. break;
  271. case 9:
  272. DDRB |= COL9;
  273. PORTB &= ~COL9;
  274. break;
  275. case 10:
  276. DDRD |= COL10;
  277. PORTD &= ~COL10;
  278. break;
  279. case 11:
  280. DDRD |= COL11;
  281. PORTD &= ~COL11;
  282. break;
  283. }
  284. }
  285. }