matrix.c 11 KB

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