matrix.c 12 KB

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