matrix.c 15 KB

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