matrix.c 12 KB

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