matrix.c 11 KB

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