matrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "debounce.h"
  28. #include QMK_KEYBOARD_H
  29. #ifdef DEBUG_MATRIX_SCAN_RATE
  30. # include "timer.h"
  31. #endif
  32. #ifdef BALLER
  33. #include <avr/interrupt.h>
  34. #include "pointing_device.h"
  35. #endif
  36. #ifndef DEBOUNCE
  37. # define DEBOUNCE 5
  38. #endif
  39. // MCP Pin Defs
  40. #define RROW1 (1<<3)
  41. #define RROW2 (1<<2)
  42. #define RROW3 (1<<1)
  43. #define RROW4 (1<<0)
  44. #define COL0 (1<<0)
  45. #define COL1 (1<<1)
  46. #define COL2 (1<<2)
  47. #define COL3 (1<<3)
  48. #define COL4 (1<<4)
  49. #define COL5 (1<<5)
  50. #define COL6 (1<<6)
  51. // ATmega pin defs
  52. #define ROW1 (1<<6)
  53. #define ROW2 (1<<5)
  54. #define ROW3 (1<<4)
  55. #define ROW4 (1<<1)
  56. #define COL7 (1<<0)
  57. #define COL8 (1<<1)
  58. #define COL9 (1<<2)
  59. #define COL10 (1<<3)
  60. #define COL11 (1<<2)
  61. #define COL12 (1<<3)
  62. #define COL13 (1<<6)
  63. //Trackball pin defs
  64. #define TRKUP (1<<4)
  65. #define TRKDN (1<<5)
  66. #define TRKLT (1<<6)
  67. #define TRKRT (1<<7)
  68. #define TRKBTN (1<<6)
  69. // Multiple for mouse moves
  70. #ifndef TRKSTEP
  71. #define TRKSTEP 20
  72. #endif
  73. // multiple for mouse scroll
  74. #ifndef SCROLLSTEP
  75. #define SCROLLSTEP 5
  76. #endif
  77. // bit masks
  78. #define BMASK (COL7 | COL8 | COL9 | COL10)
  79. #define CMASK (COL13)
  80. #define DMASK (COL11 | COL12)
  81. #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
  82. #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
  83. #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
  84. #define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
  85. // Trackball interrupts accumulate over here. Processed on scan
  86. // Stores prev state of mouse, high bits store direction
  87. uint8_t trkState = 0;
  88. uint8_t trkBtnState = 0;
  89. volatile uint8_t tbUpCnt = 0;
  90. volatile uint8_t tbDnCnt = 0;
  91. volatile uint8_t tbLtCnt = 0;
  92. volatile uint8_t tbRtCnt = 0;
  93. /* matrix state(1:on, 0:off) */
  94. static matrix_row_t matrix[MATRIX_ROWS];
  95. /*
  96. * matrix state(1:on, 0:off)
  97. * contains the raw values without debounce filtering of the last read cycle.
  98. */
  99. static matrix_row_t raw_matrix[MATRIX_ROWS];
  100. // Debouncing: store for each key the number of scans until it's eligible to
  101. // change. When scanning the matrix, ignore any changes in keys that have
  102. // already changed in the last DEBOUNCE scans.
  103. static matrix_row_t read_cols(uint8_t row);
  104. static void init_cols(void);
  105. static void unselect_rows(void);
  106. static void select_row(uint8_t row);
  107. static void enableInterrupts(void);
  108. static uint8_t mcp23018_reset_loop;
  109. // static uint16_t mcp23018_reset_loop;
  110. #ifdef DEBUG_MATRIX_SCAN_RATE
  111. uint32_t matrix_timer;
  112. uint32_t matrix_scan_count;
  113. #endif
  114. __attribute__ ((weak)) void matrix_init_user(void) {}
  115. __attribute__ ((weak)) void matrix_scan_user(void) {}
  116. __attribute__ ((weak))
  117. void matrix_init_kb(void) {
  118. matrix_init_user();
  119. }
  120. __attribute__ ((weak))
  121. void matrix_scan_kb(void) {
  122. matrix_scan_user();
  123. }
  124. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  125. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  126. void matrix_init(void) {
  127. // initialize row and col
  128. mcp23018_status = init_mcp23018();
  129. unselect_rows();
  130. init_cols();
  131. // initialize matrix state: all keys off
  132. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  133. matrix[i] = 0;
  134. raw_matrix[i] = 0;
  135. }
  136. #ifdef DEBUG_MATRIX_SCAN_RATE
  137. matrix_timer = timer_read32();
  138. matrix_scan_count = 0;
  139. #endif
  140. debounce_init(MATRIX_ROWS);
  141. matrix_init_quantum();
  142. }
  143. void matrix_power_up(void) {
  144. mcp23018_status = init_mcp23018();
  145. unselect_rows();
  146. init_cols();
  147. // initialize matrix state: all keys off
  148. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  149. matrix[i] = 0;
  150. }
  151. #ifdef DEBUG_MATRIX_SCAN_RATE
  152. matrix_timer = timer_read32();
  153. matrix_scan_count = 0;
  154. #endif
  155. }
  156. // Reads and stores a row, returning
  157. // whether a change occurred.
  158. static inline bool store_raw_matrix_row(uint8_t index) {
  159. matrix_row_t temp = read_cols(index);
  160. if (raw_matrix[index] != temp) {
  161. raw_matrix[index] = temp;
  162. return true;
  163. }
  164. return false;
  165. }
  166. uint8_t matrix_scan(void) {
  167. // TODO: Find what is trashing interrupts
  168. enableInterrupts();
  169. // First we handle the mouse inputs
  170. #ifdef BALLER
  171. uint8_t pBtn = PINE & TRKBTN;
  172. #ifdef DEBUG_BALLER
  173. // Compare to previous, mod report
  174. if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
  175. xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
  176. #endif
  177. // Modify the report
  178. report_mouse_t pRprt = pointing_device_get_report();
  179. // Scroll by default, move on layer
  180. if (layer_state == 0) {
  181. pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
  182. pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
  183. pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
  184. pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
  185. } else {
  186. pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
  187. pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
  188. pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
  189. pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
  190. }
  191. #ifdef DEBUG_BALLER
  192. if (pRprt.x != 0 || pRprt.y != 0)
  193. xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
  194. #endif
  195. if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
  196. if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
  197. // Save state, push update
  198. if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
  199. pointing_device_set_report(pRprt);
  200. trkBtnState = pBtn;
  201. #endif
  202. // Then the keyboard
  203. if (mcp23018_status) { // if there was an error
  204. if (++mcp23018_reset_loop == 0) {
  205. // if (++mcp23018_reset_loop >= 1300) {
  206. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  207. // this will be approx bit more frequent than once per second
  208. print("trying to reset mcp23018\n");
  209. mcp23018_status = init_mcp23018();
  210. if (mcp23018_status) {
  211. print("left side not responding\n");
  212. } else {
  213. print("left side attached\n");
  214. }
  215. }
  216. }
  217. #ifdef DEBUG_MATRIX_SCAN_RATE
  218. matrix_scan_count++;
  219. uint32_t timer_now = timer_read32();
  220. if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
  221. print("matrix scan frequency: ");
  222. pdec(matrix_scan_count);
  223. print("\n");
  224. matrix_timer = timer_now;
  225. matrix_scan_count = 0;
  226. }
  227. #endif
  228. bool changed = false;
  229. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  230. // select rows from left and right hands
  231. uint8_t left_index = i;
  232. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  233. select_row(left_index);
  234. select_row(right_index);
  235. // we don't need a 30us delay anymore, because selecting a
  236. // left-hand row requires more than 30us for i2c.
  237. changed |= store_raw_matrix_row(left_index);
  238. changed |= store_raw_matrix_row(right_index);
  239. unselect_rows();
  240. }
  241. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  242. matrix_scan_quantum();
  243. enableInterrupts();
  244. #ifdef DEBUG_MATRIX
  245. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  246. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  247. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  248. #endif
  249. return 1;
  250. }
  251. bool matrix_is_modified(void) // deprecated and evidently not called.
  252. {
  253. return true;
  254. }
  255. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  256. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  257. void matrix_print(void) {
  258. print("\nr/c 0123456789ABCDEF\n");
  259. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  260. phex(row); print(": ");
  261. pbin_reverse16(matrix_get_row(row));
  262. print("\n");
  263. }
  264. }
  265. uint8_t matrix_key_count(void) {
  266. uint8_t count = 0;
  267. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  268. count += bitpop16(matrix[i]);
  269. }
  270. return count;
  271. }
  272. // Remember this means ROWS
  273. static void init_cols(void) {
  274. // init on mcp23018
  275. // not needed, already done as part of init_mcp23018()
  276. // Input with pull-up(DDR:0, PORT:1)
  277. DDRF &= ~FMASK;
  278. PORTF |= FMASK;
  279. }
  280. static matrix_row_t read_cols(uint8_t row) {
  281. if (row < 7) {
  282. if (mcp23018_status) { // if there was an error
  283. return 0;
  284. } else {
  285. uint8_t data = 0;
  286. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  287. mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
  288. mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
  289. mcp23018_status = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  290. data = ~((uint8_t)mcp23018_status);
  291. mcp23018_status = I2C_STATUS_SUCCESS;
  292. out:
  293. i2c_stop();
  294. #ifdef DEBUG_MATRIX
  295. if (data != 0x00) xprintf("I2C: %d\n", data);
  296. #endif
  297. return data;
  298. }
  299. } else {
  300. /* read from teensy
  301. * bitmask is 0b0111001, but we want the lower four
  302. * we'll return 1s for the top two, but that's harmless.
  303. */
  304. // So I need to confuckulate all this
  305. //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
  306. //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  307. return ~(
  308. (((PINF & ROW4) >> 1)
  309. | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
  310. & 0xF);
  311. }
  312. }
  313. // Row pin configuration
  314. static void unselect_rows(void)
  315. {
  316. // no need to unselect on mcp23018, because the select step sets all
  317. // the other row bits high, and it's not changing to a different
  318. // direction
  319. // Hi-Z(DDR:0, PORT:0) to unselect
  320. DDRB &= ~(BMASK | TRKMASK);
  321. PORTB &= ~(BMASK);
  322. DDRC &= ~CMASK;
  323. PORTC &= ~CMASK;
  324. DDRD &= ~DMASK;
  325. PORTD &= ~DMASK;
  326. // Fix trashing of DDRB for TB
  327. PORTB |= TRKMASK;
  328. }
  329. static void select_row(uint8_t row)
  330. {
  331. if (row < 7) {
  332. // select on mcp23018
  333. if (mcp23018_status) { // do nothing on error
  334. } else { // set active row low : 0 // set other rows hi-Z : 1
  335. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  336. mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
  337. mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (mcp23018_status) goto out;
  338. out:
  339. i2c_stop();
  340. }
  341. } else {
  342. // Output low(DDR:1, PORT:0) to select
  343. switch (row) {
  344. case 7:
  345. DDRB |= COL7;
  346. PORTB &= ~COL7;
  347. break;
  348. case 8:
  349. DDRB |= COL8;
  350. PORTB &= ~COL8;
  351. break;
  352. case 9:
  353. DDRB |= COL9;
  354. PORTB &= ~COL9;
  355. break;
  356. case 10:
  357. DDRB |= COL10;
  358. PORTB &= ~COL10;
  359. break;
  360. case 11:
  361. DDRD |= COL11;
  362. PORTD &= ~COL11;
  363. break;
  364. case 12:
  365. DDRD |= COL12;
  366. PORTD &= ~COL12;
  367. break;
  368. case 13:
  369. DDRC |= COL13;
  370. PORTC &= ~COL13;
  371. break;
  372. }
  373. }
  374. }
  375. // Trackball Interrupts
  376. static void enableInterrupts(void) {
  377. #ifdef BALLER
  378. // Set interrupt mask
  379. // Set port defs
  380. DDRB &= ~TRKMASK;
  381. PORTB |= TRKMASK;
  382. DDRE &= ~TRKBTN;
  383. PORTE |= TRKBTN;
  384. // Interrupt shenanigans
  385. //EIMSK |= (1 << PCIE0);
  386. PCMSK0 |= TRKMASK;
  387. PCICR |= (1 << PCIE0);
  388. sei();
  389. #endif
  390. return;
  391. }
  392. #ifdef BALLER
  393. ISR (PCINT0_vect) {
  394. // Don't get fancy, we're in a interrupt here
  395. // PCINT reports a interrupt for a change on the bus
  396. // We hand the button at scantime for debounce
  397. volatile uint8_t pState = PINB & TRKMASK;
  398. if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
  399. if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
  400. if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
  401. if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
  402. trkState = pState;
  403. }
  404. #endif