matrix.c 9.5 KB

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