matrix.c 12 KB

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