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.

445 lines
12 KiB

  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 (1<<3)
  34. #define RROW2 (1<<2)
  35. #define RROW3 (1<<1)
  36. #define RROW4 (1<<0)
  37. #define COL0 (1<<0)
  38. #define COL1 (1<<1)
  39. #define COL2 (1<<2)
  40. #define COL3 (1<<3)
  41. #define COL4 (1<<4)
  42. #define COL5 (1<<5)
  43. #define COL6 (1<<6)
  44. // ATmega pin defs
  45. #define ROW1 (1<<6)
  46. #define ROW2 (1<<5)
  47. #define ROW3 (1<<4)
  48. #define ROW4 (1<<1)
  49. #define COL7 (1<<0)
  50. #define COL8 (1<<1)
  51. #define COL9 (1<<2)
  52. #define COL10 (1<<3)
  53. #define COL11 (1<<2)
  54. #define COL12 (1<<3)
  55. #define COL13 (1<<6)
  56. //Trackball pin defs
  57. #define TRKUP (1<<4)
  58. #define TRKDN (1<<5)
  59. #define TRKLT (1<<6)
  60. #define TRKRT (1<<7)
  61. #define TRKBTN (1<<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. bool matrix_is_modified(void) // deprecated and evidently not called.
  222. {
  223. return true;
  224. }
  225. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  226. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  227. void matrix_print(void) {
  228. print("\nr/c 0123456789ABCDEF\n");
  229. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  230. phex(row); print(": ");
  231. pbin_reverse16(matrix_get_row(row));
  232. print("\n");
  233. }
  234. }
  235. uint8_t matrix_key_count(void) {
  236. uint8_t count = 0;
  237. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  238. count += bitpop16(matrix[i]);
  239. }
  240. return count;
  241. }
  242. // Remember this means ROWS
  243. static void init_cols(void) {
  244. // init on mcp23018
  245. // not needed, already done as part of init_mcp23018()
  246. // Input with pull-up(DDR:0, PORT:1)
  247. DDRF &= ~FMASK;
  248. PORTF |= FMASK;
  249. }
  250. static matrix_row_t read_cols(uint8_t row) {
  251. if (row < 7) {
  252. if (mcp23018_status) { // if there was an error
  253. return 0;
  254. } else {
  255. uint8_t data = 0;
  256. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  257. mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
  258. mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
  259. mcp23018_status = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  260. data = ~((uint8_t)mcp23018_status);
  261. mcp23018_status = I2C_STATUS_SUCCESS;
  262. out:
  263. i2c_stop();
  264. #ifdef DEBUG_MATRIX
  265. if (data != 0x00) xprintf("I2C: %d\n", data);
  266. #endif
  267. return data;
  268. }
  269. } else {
  270. /* read from teensy
  271. * bitmask is 0b0111001, but we want the lower four
  272. * we'll return 1s for the top two, but that's harmless.
  273. */
  274. // So I need to confuckulate all this
  275. //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
  276. //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  277. return ~(
  278. (((PINF & ROW4) >> 1)
  279. | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
  280. & 0xF);
  281. }
  282. }
  283. // Row pin configuration
  284. static void unselect_rows(void)
  285. {
  286. // no need to unselect on mcp23018, because the select step sets all
  287. // the other row bits high, and it's not changing to a different
  288. // direction
  289. // Hi-Z(DDR:0, PORT:0) to unselect
  290. DDRB &= ~(BMASK | TRKMASK);
  291. PORTB &= ~(BMASK);
  292. DDRC &= ~CMASK;
  293. PORTC &= ~CMASK;
  294. DDRD &= ~DMASK;
  295. PORTD &= ~DMASK;
  296. // Fix trashing of DDRB for TB
  297. PORTB |= TRKMASK;
  298. }
  299. static void select_row(uint8_t row)
  300. {
  301. if (row < 7) {
  302. // select on mcp23018
  303. if (mcp23018_status) { // do nothing on error
  304. } else { // set active row low : 0 // set other rows hi-Z : 1
  305. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  306. mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
  307. mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (mcp23018_status) goto out;
  308. out:
  309. i2c_stop();
  310. }
  311. } else {
  312. // Output low(DDR:1, PORT:0) to select
  313. switch (row) {
  314. case 7:
  315. DDRB |= COL7;
  316. PORTB &= ~COL7;
  317. break;
  318. case 8:
  319. DDRB |= COL8;
  320. PORTB &= ~COL8;
  321. break;
  322. case 9:
  323. DDRB |= COL9;
  324. PORTB &= ~COL9;
  325. break;
  326. case 10:
  327. DDRB |= COL10;
  328. PORTB &= ~COL10;
  329. break;
  330. case 11:
  331. DDRD |= COL11;
  332. PORTD &= ~COL11;
  333. break;
  334. case 12:
  335. DDRD |= COL12;
  336. PORTD &= ~COL12;
  337. break;
  338. case 13:
  339. DDRC |= COL13;
  340. PORTC &= ~COL13;
  341. break;
  342. }
  343. }
  344. }
  345. // Trackball Interrupts
  346. static void enableInterrupts(void) {
  347. #ifdef BALLER
  348. // Set interrupt mask
  349. // Set port defs
  350. DDRB &= ~TRKMASK;
  351. PORTB |= TRKMASK;
  352. DDRE &= ~TRKBTN;
  353. PORTE |= TRKBTN;
  354. // Interrupt shenanigans
  355. //EIMSK |= (1 << PCIE0);
  356. PCMSK0 |= TRKMASK;
  357. PCICR |= (1 << PCIE0);
  358. sei();
  359. #endif
  360. return;
  361. }
  362. #ifdef BALLER
  363. ISR (PCINT0_vect) {
  364. // Don't get fancy, we're in a interrupt here
  365. // PCINT reports a interrupt for a change on the bus
  366. // We hand the button at scantime for debounce
  367. volatile uint8_t pState = PINB & TRKMASK;
  368. if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
  369. if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
  370. if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
  371. if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
  372. trkState = pState;
  373. }
  374. #endif