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.

517 lines
14 KiB

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