matrix.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "split_util.h"
  26. #include "pro_micro.h"
  27. #include "config.h"
  28. #include "timer.h"
  29. #include "split_flags.h"
  30. #ifdef RGBLIGHT_ENABLE
  31. # include "rgblight.h"
  32. #endif
  33. #ifdef BACKLIGHT_ENABLE
  34. # include "backlight.h"
  35. extern backlight_config_t backlight_config;
  36. #endif
  37. #if defined(USE_I2C) || defined(EH)
  38. # include "i2c.h"
  39. #else // USE_SERIAL
  40. # include "serial.h"
  41. #endif
  42. #ifndef DEBOUNCING_DELAY
  43. # define DEBOUNCING_DELAY 5
  44. #endif
  45. #if (DEBOUNCING_DELAY > 0)
  46. static uint16_t debouncing_time;
  47. static bool debouncing = false;
  48. #endif
  49. #if (MATRIX_COLS <= 8)
  50. # define print_matrix_header() print("\nr/c 01234567\n")
  51. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  52. # define matrix_bitpop(i) bitpop(matrix[i])
  53. # define ROW_SHIFTER ((uint8_t)1)
  54. #else
  55. # error "Currently only supports 8 COLS"
  56. #endif
  57. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  58. #define ERROR_DISCONNECT_COUNT 5
  59. #define ROWS_PER_HAND (MATRIX_ROWS/2)
  60. static uint8_t error_count = 0;
  61. #if ((DIODE_DIRECTION == COL2ROW) || (DIODE_DIRECTION == ROW2COL))
  62. static uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  63. static uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  64. #elif (DIODE_DIRECTION == CUSTOM_MATRIX)
  65. static uint8_t row_col_pins[MATRIX_ROWS][MATRIX_COLS] = MATRIX_ROW_COL_PINS;
  66. #endif
  67. /* matrix state(1:on, 0:off) */
  68. static matrix_row_t matrix[MATRIX_ROWS];
  69. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  70. #if (DIODE_DIRECTION == COL2ROW)
  71. static void init_cols(void);
  72. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  73. static void unselect_rows(void);
  74. static void select_row(uint8_t row);
  75. static void unselect_row(uint8_t row);
  76. #elif (DIODE_DIRECTION == ROW2COL)
  77. static void init_rows(void);
  78. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  79. static void unselect_cols(void);
  80. static void unselect_col(uint8_t col);
  81. static void select_col(uint8_t col);
  82. #elif (DIODE_DIRECTION == CUSTOM_MATRIX)
  83. static void init_cols_rows(void);
  84. static bool read_cols(matrix_row_t current_matrix[], uint8_t current_row);
  85. #endif
  86. __attribute__ ((weak))
  87. void matrix_init_kb(void) {
  88. matrix_init_user();
  89. }
  90. __attribute__ ((weak))
  91. void matrix_scan_kb(void) {
  92. matrix_scan_user();
  93. }
  94. __attribute__ ((weak))
  95. void matrix_init_user(void) {
  96. }
  97. __attribute__ ((weak))
  98. void matrix_scan_user(void) {
  99. }
  100. __attribute__ ((weak))
  101. void matrix_slave_scan_user(void) {
  102. }
  103. inline
  104. uint8_t matrix_rows(void)
  105. {
  106. return MATRIX_ROWS;
  107. }
  108. inline
  109. uint8_t matrix_cols(void)
  110. {
  111. return MATRIX_COLS;
  112. }
  113. void matrix_init(void)
  114. {
  115. #ifdef DISABLE_JTAG
  116. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  117. MCUCR |= (1<<JTD);
  118. MCUCR |= (1<<JTD);
  119. #endif
  120. debug_enable = true;
  121. debug_matrix = true;
  122. debug_mouse = true;
  123. // Set pinout for right half if pinout for that half is defined
  124. if (!isLeftHand) {
  125. #ifdef MATRIX_ROW_PINS_RIGHT
  126. const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  127. for (uint8_t i = 0; i < MATRIX_ROWS; i++)
  128. row_pins[i] = row_pins_right[i];
  129. #endif
  130. #ifdef MATRIX_COL_PINS_RIGHT
  131. const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  132. for (uint8_t i = 0; i < MATRIX_COLS; i++)
  133. col_pins[i] = col_pins_right[i];
  134. #endif
  135. }
  136. // initialize row and col
  137. #if (DIODE_DIRECTION == COL2ROW)
  138. unselect_rows();
  139. init_cols();
  140. #elif (DIODE_DIRECTION == ROW2COL)
  141. unselect_cols();
  142. init_rows();
  143. #elif (DIODE_DIRECTION == CUSTOM_MATRIX)
  144. init_cols_rows();
  145. #endif
  146. // initialize matrix state: all keys off
  147. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  148. matrix[i] = 0;
  149. matrix_debouncing[i] = 0;
  150. }
  151. matrix_init_quantum();
  152. }
  153. uint8_t _matrix_scan(void)
  154. {
  155. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  156. #if (DIODE_DIRECTION == COL2ROW)
  157. // Set row, read cols
  158. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  159. # if (DEBOUNCING_DELAY > 0)
  160. bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
  161. if (matrix_changed) {
  162. debouncing = true;
  163. debouncing_time = timer_read();
  164. }
  165. # else
  166. read_cols_on_row(matrix+offset, current_row);
  167. # endif
  168. }
  169. #elif (DIODE_DIRECTION == ROW2COL)
  170. // Set col, read rows
  171. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  172. # if (DEBOUNCING_DELAY > 0)
  173. bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
  174. if (matrix_changed) {
  175. debouncing = true;
  176. debouncing_time = timer_read();
  177. }
  178. # else
  179. read_rows_on_col(matrix+offset, current_col);
  180. # endif
  181. }
  182. #elif (DIODE_DIRECTION == CUSTOM_MATRIX)
  183. // Set row, read cols
  184. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  185. # if (DEBOUNCING_DELAY > 0)
  186. bool matrix_changed = read_cols(matrix_debouncing+offset, current_row);
  187. if (matrix_changed) {
  188. debouncing = true;
  189. debouncing_time = timer_read();
  190. }
  191. # else
  192. read_cols(matrix+offset, current_row);
  193. # endif
  194. }
  195. #endif
  196. # if (DEBOUNCING_DELAY > 0)
  197. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  198. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  199. matrix[i+offset] = matrix_debouncing[i+offset];
  200. }
  201. debouncing = false;
  202. }
  203. # endif
  204. return 1;
  205. }
  206. #if defined(USE_I2C) || defined(EH)
  207. // Get rows from other half over i2c
  208. int i2c_transaction(void) {
  209. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  210. int err = 0;
  211. // write backlight info
  212. #ifdef BACKLIGHT_ENABLE
  213. if (BACKLIT_DIRTY) {
  214. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  215. if (err) goto i2c_error;
  216. // Backlight location
  217. err = i2c_master_write(I2C_BACKLIT_START);
  218. if (err) goto i2c_error;
  219. // Write backlight
  220. i2c_master_write(get_backlight_level());
  221. BACKLIT_DIRTY = false;
  222. }
  223. #endif
  224. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  225. if (err) goto i2c_error;
  226. // start of matrix stored at I2C_KEYMAP_START
  227. err = i2c_master_write(I2C_KEYMAP_START);
  228. if (err) goto i2c_error;
  229. // Start read
  230. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  231. if (err) goto i2c_error;
  232. if (!err) {
  233. int i;
  234. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  235. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  236. }
  237. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  238. i2c_master_stop();
  239. } else {
  240. i2c_error: // the cable is disconnceted, or something else went wrong
  241. i2c_reset_state();
  242. return err;
  243. }
  244. #ifdef RGBLIGHT_ENABLE
  245. if (RGB_DIRTY) {
  246. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  247. if (err) goto i2c_error;
  248. // RGB Location
  249. err = i2c_master_write(I2C_RGB_START);
  250. if (err) goto i2c_error;
  251. uint32_t dword = eeconfig_read_rgblight();
  252. // Write RGB
  253. err = i2c_master_write_data(&dword, 4);
  254. if (err) goto i2c_error;
  255. RGB_DIRTY = false;
  256. i2c_master_stop();
  257. }
  258. #endif
  259. return 0;
  260. }
  261. #else // USE_SERIAL
  262. int serial_transaction(void) {
  263. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  264. if (serial_update_buffers()) {
  265. return 1;
  266. }
  267. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  268. matrix[slaveOffset+i] = serial_slave_buffer[i];
  269. }
  270. #ifdef RGBLIGHT_ENABLE
  271. // Code to send RGB over serial goes here (not implemented yet)
  272. #endif
  273. #ifdef BACKLIGHT_ENABLE
  274. // Write backlight level for slave to read
  275. serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0;
  276. #endif
  277. return 0;
  278. }
  279. #endif
  280. uint8_t matrix_scan(void)
  281. {
  282. uint8_t ret = _matrix_scan();
  283. #if defined(USE_I2C) || defined(EH)
  284. if( i2c_transaction() ) {
  285. #else // USE_SERIAL
  286. if( serial_transaction() ) {
  287. #endif
  288. error_count++;
  289. if (error_count > ERROR_DISCONNECT_COUNT) {
  290. // reset other half if disconnected
  291. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  292. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  293. matrix[slaveOffset+i] = 0;
  294. }
  295. }
  296. } else {
  297. error_count = 0;
  298. }
  299. matrix_scan_quantum();
  300. return ret;
  301. }
  302. void matrix_slave_scan(void) {
  303. _matrix_scan();
  304. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  305. #if defined(USE_I2C) || defined(EH)
  306. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  307. i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
  308. }
  309. #else // USE_SERIAL
  310. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  311. serial_slave_buffer[i] = matrix[offset+i];
  312. }
  313. #endif
  314. matrix_slave_scan_user();
  315. }
  316. bool matrix_is_modified(void)
  317. {
  318. if (debouncing) return false;
  319. return true;
  320. }
  321. inline
  322. bool matrix_is_on(uint8_t row, uint8_t col)
  323. {
  324. return (matrix[row] & ((matrix_row_t)1<<col));
  325. }
  326. inline
  327. matrix_row_t matrix_get_row(uint8_t row)
  328. {
  329. return matrix[row];
  330. }
  331. void matrix_print(void)
  332. {
  333. print("\nr/c 0123456789ABCDEF\n");
  334. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  335. phex(row); print(": ");
  336. pbin_reverse16(matrix_get_row(row));
  337. print("\n");
  338. }
  339. }
  340. uint8_t matrix_key_count(void)
  341. {
  342. uint8_t count = 0;
  343. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  344. count += bitpop16(matrix[i]);
  345. }
  346. return count;
  347. }
  348. #if (DIODE_DIRECTION == COL2ROW)
  349. static void init_cols(void)
  350. {
  351. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  352. uint8_t pin = col_pins[x];
  353. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  354. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  355. }
  356. }
  357. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  358. {
  359. // Store last value of row prior to reading
  360. matrix_row_t last_row_value = current_matrix[current_row];
  361. // Clear data in matrix row
  362. current_matrix[current_row] = 0;
  363. // Select row and wait for row selecton to stabilize
  364. select_row(current_row);
  365. wait_us(30);
  366. // For each col...
  367. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  368. // Select the col pin to read (active low)
  369. uint8_t pin = col_pins[col_index];
  370. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  371. // Populate the matrix row with the state of the col pin
  372. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  373. }
  374. // Unselect row
  375. unselect_row(current_row);
  376. return (last_row_value != current_matrix[current_row]);
  377. }
  378. static void select_row(uint8_t row)
  379. {
  380. uint8_t pin = row_pins[row];
  381. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  382. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  383. }
  384. static void unselect_row(uint8_t row)
  385. {
  386. uint8_t pin = row_pins[row];
  387. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  388. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  389. }
  390. static void unselect_rows(void)
  391. {
  392. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  393. uint8_t pin = row_pins[x];
  394. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  395. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  396. }
  397. }
  398. #elif (DIODE_DIRECTION == ROW2COL)
  399. static void init_rows(void)
  400. {
  401. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  402. uint8_t pin = row_pins[x];
  403. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  404. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  405. }
  406. }
  407. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  408. {
  409. bool matrix_changed = false;
  410. // Select col and wait for col selecton to stabilize
  411. select_col(current_col);
  412. wait_us(30);
  413. // For each row...
  414. for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
  415. {
  416. // Store last value of row prior to reading
  417. matrix_row_t last_row_value = current_matrix[row_index];
  418. // Check row pin state
  419. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  420. {
  421. // Pin LO, set col bit
  422. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  423. }
  424. else
  425. {
  426. // Pin HI, clear col bit
  427. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  428. }
  429. // Determine if the matrix changed state
  430. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  431. {
  432. matrix_changed = true;
  433. }
  434. }
  435. // Unselect col
  436. unselect_col(current_col);
  437. return matrix_changed;
  438. }
  439. static void select_col(uint8_t col)
  440. {
  441. uint8_t pin = col_pins[col];
  442. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  443. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  444. }
  445. static void unselect_col(uint8_t col)
  446. {
  447. uint8_t pin = col_pins[col];
  448. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  449. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  450. }
  451. static void unselect_cols(void)
  452. {
  453. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  454. uint8_t pin = col_pins[x];
  455. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  456. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  457. }
  458. }
  459. #elif (DIODE_DIRECTION == CUSTOM_MATRIX)
  460. static void init_cols_rows(void)
  461. {
  462. for(int row = 0; row < MATRIX_ROWS; row++) {
  463. for(int col = 0; col < MATRIX_COLS; col++) {
  464. uint8_t pin = row_col_pins[row][col];
  465. if(pin == NO_PIN) {
  466. continue;
  467. }
  468. // DDxn set 0 for input
  469. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
  470. // PORTxn set 1 for input/pullup
  471. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
  472. }
  473. }
  474. }
  475. static bool read_cols(matrix_row_t current_matrix[], uint8_t current_row)
  476. {
  477. matrix_row_t last_row_value = current_matrix[current_row];
  478. current_matrix[current_row] = 0;
  479. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  480. uint8_t pin = row_col_pins[current_row][col_index];
  481. if(pin == NO_PIN) {
  482. current_matrix[current_row] |= 0;
  483. }
  484. else {
  485. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  486. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  487. }
  488. }
  489. return (last_row_value != current_matrix[current_row]);
  490. }
  491. #endif