logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (11358B)


  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))
  101. void matrix_init_kb(void) {
  102. matrix_init_user();
  103. }
  104. __attribute__ ((weak))
  105. void matrix_scan_kb(void) {
  106. matrix_scan_user();
  107. }
  108. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  109. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  110. void matrix_init(void) {
  111. // initialize row and col
  112. mcp23018_status = init_mcp23018();
  113. unselect_rows();
  114. init_cols();
  115. // initialize matrix state: all keys off
  116. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  117. matrix[i] = 0;
  118. raw_matrix[i] = 0;
  119. }
  120. debounce_init(MATRIX_ROWS);
  121. matrix_init_kb();
  122. }
  123. void matrix_power_up(void) {
  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. }
  131. }
  132. // Reads and stores a row, returning
  133. // whether a change occurred.
  134. static inline bool store_raw_matrix_row(uint8_t index) {
  135. matrix_row_t temp = read_cols(index);
  136. if (raw_matrix[index] != temp) {
  137. raw_matrix[index] = temp;
  138. return true;
  139. }
  140. return false;
  141. }
  142. uint8_t matrix_scan(void) {
  143. // TODO: Find what is trashing interrupts
  144. enableInterrupts();
  145. // First we handle the mouse inputs
  146. #ifdef BALLER
  147. uint8_t pBtn = PINE & TRKBTN;
  148. #ifdef DEBUG_BALLER
  149. // Compare to previous, mod report
  150. if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
  151. xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
  152. #endif
  153. // Modify the report
  154. report_mouse_t pRprt = pointing_device_get_report();
  155. // Scroll by default, move on layer
  156. if (layer_state == 0) {
  157. pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
  158. pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
  159. pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
  160. pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
  161. } else {
  162. pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
  163. pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
  164. pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
  165. pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
  166. }
  167. #ifdef DEBUG_BALLER
  168. if (pRprt.x != 0 || pRprt.y != 0)
  169. xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
  170. #endif
  171. if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
  172. if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
  173. // Save state, push update
  174. if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
  175. pointing_device_set_report(pRprt);
  176. trkBtnState = pBtn;
  177. #endif
  178. // Then the keyboard
  179. if (mcp23018_status) { // if there was an error
  180. if (++mcp23018_reset_loop == 0) {
  181. // if (++mcp23018_reset_loop >= 1300) {
  182. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  183. // this will be approx bit more frequent than once per second
  184. print("trying to reset mcp23018\n");
  185. mcp23018_status = init_mcp23018();
  186. if (mcp23018_status) {
  187. print("left side not responding\n");
  188. } else {
  189. print("left side attached\n");
  190. }
  191. }
  192. }
  193. bool changed = false;
  194. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  195. // select rows from left and right hands
  196. uint8_t left_index = i;
  197. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  198. select_row(left_index);
  199. select_row(right_index);
  200. // we don't need a 30us delay anymore, because selecting a
  201. // left-hand row requires more than 30us for i2c.
  202. changed |= store_raw_matrix_row(left_index);
  203. changed |= store_raw_matrix_row(right_index);
  204. unselect_rows();
  205. }
  206. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  207. matrix_scan_kb();
  208. enableInterrupts();
  209. #ifdef DEBUG_MATRIX
  210. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  211. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  212. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  213. #endif
  214. return 1;
  215. }
  216. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  217. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  218. void matrix_print(void) {
  219. print("\nr/c 0123456789ABCDEF\n");
  220. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  221. print_hex8(row); print(": ");
  222. print_bin_reverse16(matrix_get_row(row));
  223. print("\n");
  224. }
  225. }
  226. // Remember this means ROWS
  227. static void init_cols(void) {
  228. // init on mcp23018
  229. // not needed, already done as part of init_mcp23018()
  230. // Input with pull-up(DDR:0, PORT:1)
  231. DDRF &= ~FMASK;
  232. PORTF |= FMASK;
  233. }
  234. static matrix_row_t read_cols(uint8_t row) {
  235. if (row < 7) {
  236. if (mcp23018_status) { // if there was an error
  237. return 0;
  238. } else {
  239. uint8_t data = 0;
  240. mcp23018_status = i2c_read_register(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
  241. #ifdef DEBUG_MATRIX
  242. if (~data != 0x00) xprintf("I2C: %d\n", ~data);
  243. #endif
  244. return ~data;
  245. }
  246. } else {
  247. /* read from teensy
  248. * bitmask is 0b0111001, but we want the lower four
  249. * we'll return 1s for the top two, but that's harmless.
  250. */
  251. // So I need to confuckulate all this
  252. //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
  253. //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
  254. return ~(
  255. (((PINF & ROW4) >> 1)
  256. | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
  257. & 0xF);
  258. }
  259. }
  260. // Row pin configuration
  261. static void unselect_rows(void)
  262. {
  263. // no need to unselect on mcp23018, because the select step sets all
  264. // the other row bits high, and it's not changing to a different
  265. // direction
  266. // Hi-Z(DDR:0, PORT:0) to unselect
  267. DDRB &= ~(BMASK | TRKMASK);
  268. PORTB &= ~(BMASK);
  269. DDRC &= ~CMASK;
  270. PORTC &= ~CMASK;
  271. DDRD &= ~DMASK;
  272. PORTD &= ~DMASK;
  273. // Fix trashing of DDRB for TB
  274. PORTB |= TRKMASK;
  275. }
  276. static void select_row(uint8_t row)
  277. {
  278. if (row < 7) {
  279. // select on mcp23018
  280. if (mcp23018_status) { // do nothing on error
  281. } else { // set active row low : 0 // set other rows hi-Z : 1
  282. uint8_t data = 0xFF & ~(1<<row);
  283. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  284. }
  285. } else {
  286. // Output low(DDR:1, PORT:0) to select
  287. switch (row) {
  288. case 7:
  289. DDRB |= COL7;
  290. PORTB &= ~COL7;
  291. break;
  292. case 8:
  293. DDRB |= COL8;
  294. PORTB &= ~COL8;
  295. break;
  296. case 9:
  297. DDRB |= COL9;
  298. PORTB &= ~COL9;
  299. break;
  300. case 10:
  301. DDRB |= COL10;
  302. PORTB &= ~COL10;
  303. break;
  304. case 11:
  305. DDRD |= COL11;
  306. PORTD &= ~COL11;
  307. break;
  308. case 12:
  309. DDRD |= COL12;
  310. PORTD &= ~COL12;
  311. break;
  312. case 13:
  313. DDRC |= COL13;
  314. PORTC &= ~COL13;
  315. break;
  316. }
  317. }
  318. }
  319. // Trackball Interrupts
  320. static void enableInterrupts(void) {
  321. #ifdef BALLER
  322. // Set interrupt mask
  323. // Set port defs
  324. DDRB &= ~TRKMASK;
  325. PORTB |= TRKMASK;
  326. DDRE &= ~TRKBTN;
  327. PORTE |= TRKBTN;
  328. // Interrupt shenanigans
  329. //EIMSK |= (1 << PCIE0);
  330. PCMSK0 |= TRKMASK;
  331. PCICR |= (1 << PCIE0);
  332. sei();
  333. #endif
  334. return;
  335. }
  336. #ifdef BALLER
  337. ISR (PCINT0_vect) {
  338. // Don't get fancy, we're in a interrupt here
  339. // PCINT reports a interrupt for a change on the bus
  340. // We hand the button at scantime for debounce
  341. volatile uint8_t pState = PINB & TRKMASK;
  342. if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
  343. if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
  344. if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
  345. if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
  346. trkState = pState;
  347. }
  348. #endif