matrix.c 10 KB

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