matrix.c 13 KB

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