matrix.c 13 KB

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