matrix.c 13 KB

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