matrix.c 14 KB

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