matrix.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #ifdef DEBUG_MATRIX_SCAN_RATE
  30. #include "matrix_scanrate.h"
  31. #endif
  32. #ifdef USE_I2C
  33. # include "i2c.h"
  34. #else // USE_SERIAL
  35. # error "only i2c supported"
  36. #endif
  37. #ifndef DEBOUNCING_DELAY
  38. # define DEBOUNCING_DELAY 5
  39. #endif
  40. #if (MATRIX_COLS <= 8)
  41. # define print_matrix_header() print("\nr/c 01234567\n")
  42. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  43. # define matrix_bitpop(i) bitpop(matrix[i])
  44. # define ROW_SHIFTER ((uint8_t)1)
  45. #else
  46. # error "Currently only supports 8 COLS"
  47. #endif
  48. #define ERROR_DISCONNECT_COUNT 5
  49. #define ROWS_PER_HAND (MATRIX_ROWS/2)
  50. static uint8_t error_count = 0;
  51. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  52. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  53. /* matrix state(1:on, 0:off) */
  54. static matrix_row_t matrix[MATRIX_ROWS];
  55. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  56. static matrix_row_t* debouncing_matrix_hand_offsetted; //pointer to matrix_debouncing for our hand
  57. static matrix_row_t* matrix_hand_offsetted; // pointer to matrix for our hand
  58. //Debouncing counters
  59. typedef uint8_t debounce_counter_t;
  60. #define DEBOUNCE_COUNTER_MODULO 250
  61. #define DEBOUNCE_COUNTER_INACTIVE 251
  62. static debounce_counter_t debounce_counters[MATRIX_ROWS * MATRIX_COLS];
  63. static debounce_counter_t *debounce_counters_hand_offsetted;
  64. #if (DIODE_DIRECTION == ROW2COL)
  65. error "Only Col2Row supported";
  66. #endif
  67. static void init_cols(void);
  68. static void unselect_rows(void);
  69. static void select_row(uint8_t row);
  70. static void unselect_row(uint8_t row);
  71. static matrix_row_t optimized_col_reader(void);
  72. __attribute__ ((weak))
  73. void matrix_init_kb(void) {
  74. matrix_init_user();
  75. }
  76. __attribute__ ((weak))
  77. void matrix_scan_kb(void) {
  78. matrix_scan_user();
  79. }
  80. __attribute__ ((weak))
  81. void matrix_init_user(void) {
  82. }
  83. __attribute__ ((weak))
  84. void matrix_scan_user(void) {
  85. }
  86. __attribute__ ((weak))
  87. void matrix_slave_scan_user(void) {
  88. }
  89. inline
  90. uint8_t matrix_rows(void)
  91. {
  92. return MATRIX_ROWS;
  93. }
  94. inline
  95. uint8_t matrix_cols(void)
  96. {
  97. return MATRIX_COLS;
  98. }
  99. void matrix_init(void)
  100. {
  101. #ifdef DISABLE_JTAG
  102. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  103. MCUCR |= (1<<JTD);
  104. MCUCR |= (1<<JTD);
  105. #endif
  106. debug_enable = true;
  107. debug_matrix = false;
  108. debug_mouse = false;
  109. // initialize row and col
  110. unselect_rows();
  111. init_cols();
  112. TX_RX_LED_INIT;
  113. // initialize matrix state: all keys off
  114. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  115. matrix[i] = 0;
  116. matrix_debouncing[i] = 0;
  117. }
  118. int my_hand_offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  119. debouncing_matrix_hand_offsetted = matrix_debouncing + my_hand_offset;
  120. matrix_hand_offsetted = matrix + my_hand_offset;
  121. debounce_counters_hand_offsetted = debounce_counters + my_hand_offset;
  122. for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) {
  123. debounce_counters[i] = DEBOUNCE_COUNTER_INACTIVE;
  124. }
  125. matrix_init_quantum();
  126. }
  127. //#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
  128. void update_debounce_counters(uint8_t current_time)
  129. {
  130. debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
  131. for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
  132. {
  133. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  134. {
  135. if (*debounce_pointer != DEBOUNCE_COUNTER_INACTIVE)
  136. {
  137. if (TIMER_DIFF(current_time, *debounce_pointer, DEBOUNCE_COUNTER_MODULO) >=
  138. DEBOUNCING_DELAY) {
  139. *debounce_pointer = DEBOUNCE_COUNTER_INACTIVE;
  140. }
  141. }
  142. debounce_pointer++;
  143. }
  144. }
  145. }
  146. void transfer_matrix_values(uint8_t current_time)
  147. {
  148. //upload from debounce_matrix to final matrix;
  149. debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
  150. for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
  151. {
  152. matrix_row_t row_value = matrix_hand_offsetted[row];
  153. matrix_row_t debounce_value = debouncing_matrix_hand_offsetted[row];
  154. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  155. {
  156. bool final_value = debounce_value & (1 << col);
  157. bool current_value = row_value & (1 << col);
  158. if (*debounce_pointer == DEBOUNCE_COUNTER_INACTIVE
  159. && (current_value != final_value))
  160. {
  161. *debounce_pointer = current_time;
  162. row_value ^= (1 << col);
  163. }
  164. debounce_pointer++;
  165. }
  166. matrix_hand_offsetted[row] = row_value;
  167. }
  168. }
  169. uint8_t _matrix_scan(void)
  170. {
  171. uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
  172. // Set row, read cols
  173. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  174. select_row(current_row);
  175. asm volatile ("nop"); asm volatile("nop");
  176. debouncing_matrix_hand_offsetted[current_row] = optimized_col_reader();
  177. // Unselect row
  178. unselect_row(current_row);
  179. }
  180. update_debounce_counters(current_time);
  181. transfer_matrix_values(current_time);
  182. return 1;
  183. }
  184. // Get rows from other half over i2c
  185. int i2c_transaction(void) {
  186. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  187. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  188. if (err) goto i2c_error;
  189. // start of matrix stored at 0x00
  190. err = i2c_master_write(I2C_KEYMAP_START);
  191. if (err) goto i2c_error;
  192. // Start read
  193. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  194. if (err) goto i2c_error;
  195. if (!err) {
  196. int i;
  197. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  198. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  199. }
  200. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  201. i2c_master_stop();
  202. } else {
  203. i2c_error: // the cable is disconnceted, or something else went wrong
  204. i2c_reset_state();
  205. return err;
  206. }
  207. return 0;
  208. }
  209. uint8_t matrix_scan(void)
  210. {
  211. #ifdef DEBUG_MATRIX_SCAN_RATE
  212. matrix_check_scan_rate();
  213. matrix_time_between_scans();
  214. #endif
  215. uint8_t ret = _matrix_scan();
  216. if( i2c_transaction() ) {
  217. // turn on the indicator led when halves are disconnected
  218. TXLED1;
  219. error_count++;
  220. if (error_count > ERROR_DISCONNECT_COUNT) {
  221. // reset other half if disconnected
  222. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  223. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  224. matrix[slaveOffset+i] = 0;
  225. }
  226. }
  227. } else {
  228. // turn off the indicator led on no error
  229. TXLED0;
  230. error_count = 0;
  231. }
  232. matrix_scan_quantum();
  233. return ret;
  234. }
  235. void matrix_slave_scan(void) {
  236. _matrix_scan();
  237. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  238. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  239. i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
  240. }
  241. matrix_slave_scan_user();
  242. }
  243. inline
  244. bool matrix_is_on(uint8_t row, uint8_t col)
  245. {
  246. return (matrix[row] & ((matrix_row_t)1<<col));
  247. }
  248. inline
  249. matrix_row_t matrix_get_row(uint8_t row)
  250. {
  251. return matrix[row];
  252. }
  253. void matrix_print(void)
  254. {
  255. print("\nr/c 0123456789ABCDEF\n");
  256. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  257. phex(row); print(": ");
  258. pbin_reverse16(matrix_get_row(row));
  259. print("\n");
  260. }
  261. }
  262. uint8_t matrix_key_count(void)
  263. {
  264. uint8_t count = 0;
  265. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  266. count += bitpop16(matrix[i]);
  267. }
  268. return count;
  269. }
  270. static void init_cols(void)
  271. {
  272. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  273. uint8_t pin = col_pins[x];
  274. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  275. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  276. }
  277. }
  278. inline
  279. static matrix_row_t optimized_col_reader(void) {
  280. //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 }
  281. return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) |
  282. (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) |
  283. (PINB & (1 << 3) ? 0 : (ROW_SHIFTER << 2)) |
  284. (PINB & (1 << 1) ? 0 : (ROW_SHIFTER << 3)) |
  285. (PINF & (1 << 7) ? 0 : (ROW_SHIFTER << 4)) |
  286. (PINF & (1 << 6) ? 0 : (ROW_SHIFTER << 5)) |
  287. (PINF & (1 << 5) ? 0 : (ROW_SHIFTER << 6)) |
  288. (PINF & (1 << 4) ? 0 : (ROW_SHIFTER << 7));
  289. }
  290. static void select_row(uint8_t row)
  291. {
  292. uint8_t pin = row_pins[row];
  293. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  294. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  295. }
  296. static void unselect_row(uint8_t row)
  297. {
  298. uint8_t pin = row_pins[row];
  299. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  300. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  301. }
  302. static void unselect_rows(void)
  303. {
  304. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  305. uint8_t pin = row_pins[x];
  306. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  307. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  308. }
  309. }