matrix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/wdt.h>
  21. #include <avr/interrupt.h>
  22. #include <util/delay.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. #ifdef USE_MATRIX_I2C
  31. # include "i2c.h"
  32. #else // USE_SERIAL
  33. # include "serial.h"
  34. #endif
  35. #ifndef DEBOUNCE
  36. # define DEBOUNCE 5
  37. #endif
  38. #define ERROR_DISCONNECT_COUNT 5
  39. static uint8_t debouncing = DEBOUNCE;
  40. static const int ROWS_PER_HAND = MATRIX_ROWS/2;
  41. static uint8_t error_count = 0;
  42. uint8_t is_master = 0 ;
  43. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  44. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  45. /* matrix state(1:on, 0:off) */
  46. static matrix_row_t matrix[MATRIX_ROWS];
  47. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  48. static matrix_row_t read_cols(void);
  49. static void init_cols(void);
  50. static void unselect_rows(void);
  51. static void select_row(uint8_t row);
  52. static uint8_t matrix_master_scan(void);
  53. __attribute__ ((weak))
  54. void matrix_init_quantum(void) {
  55. matrix_init_kb();
  56. }
  57. __attribute__ ((weak))
  58. void matrix_scan_quantum(void) {
  59. matrix_scan_kb();
  60. }
  61. __attribute__ ((weak))
  62. void matrix_init_kb(void) {
  63. matrix_init_user();
  64. }
  65. __attribute__ ((weak))
  66. void matrix_scan_kb(void) {
  67. matrix_scan_user();
  68. }
  69. __attribute__ ((weak))
  70. void matrix_init_user(void) {
  71. }
  72. __attribute__ ((weak))
  73. void matrix_scan_user(void) {
  74. }
  75. inline
  76. uint8_t matrix_rows(void)
  77. {
  78. return MATRIX_ROWS;
  79. }
  80. inline
  81. uint8_t matrix_cols(void)
  82. {
  83. return MATRIX_COLS;
  84. }
  85. void matrix_init(void)
  86. {
  87. debug_enable = true;
  88. debug_matrix = true;
  89. debug_mouse = true;
  90. // initialize row and col
  91. unselect_rows();
  92. init_cols();
  93. TX_RX_LED_INIT;
  94. // initialize matrix state: all keys off
  95. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  96. matrix[i] = 0;
  97. matrix_debouncing[i] = 0;
  98. }
  99. is_master = has_usb();
  100. matrix_init_quantum();
  101. }
  102. uint8_t _matrix_scan(void)
  103. {
  104. // Right hand is stored after the left in the matirx so, we need to offset it
  105. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  106. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  107. select_row(i);
  108. _delay_us(30); // without this wait read unstable value.
  109. matrix_row_t cols = read_cols();
  110. if (matrix_debouncing[i+offset] != cols) {
  111. matrix_debouncing[i+offset] = cols;
  112. debouncing = DEBOUNCE;
  113. }
  114. unselect_rows();
  115. }
  116. if (debouncing) {
  117. if (--debouncing) {
  118. _delay_ms(1);
  119. } else {
  120. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  121. matrix[i+offset] = matrix_debouncing[i+offset];
  122. }
  123. }
  124. }
  125. return 1;
  126. }
  127. #ifdef USE_MATRIX_I2C
  128. // Get rows from other half over i2c
  129. int i2c_transaction(void) {
  130. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  131. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  132. if (err) goto i2c_error;
  133. // start of matrix stored at 0x00
  134. err = i2c_master_write(0x00);
  135. if (err) goto i2c_error;
  136. // Start read
  137. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  138. if (err) goto i2c_error;
  139. if (!err) {
  140. int i;
  141. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  142. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  143. }
  144. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  145. i2c_master_stop();
  146. } else {
  147. i2c_error: // the cable is disconnceted, or something else went wrong
  148. i2c_reset_state();
  149. return err;
  150. }
  151. return 0;
  152. }
  153. #else // USE_SERIAL
  154. int serial_transaction(void) {
  155. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  156. int ret=serial_update_buffers();
  157. if (ret ) {
  158. if(ret==2)RXLED1;
  159. return 1;
  160. }
  161. RXLED0;
  162. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  163. matrix[slaveOffset+i] = serial_slave_buffer[i];
  164. }
  165. return 0;
  166. }
  167. #endif
  168. uint8_t matrix_scan(void)
  169. {
  170. if (is_master) {
  171. matrix_master_scan();
  172. }else{
  173. matrix_slave_scan();
  174. // if(serial_slave_DATA_CORRUPT()){
  175. // TXLED0;
  176. int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
  177. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  178. matrix[offset+i] = serial_master_buffer[i];
  179. }
  180. // }else{
  181. // TXLED1;
  182. // }
  183. matrix_scan_quantum();
  184. }
  185. return 1;
  186. }
  187. uint8_t matrix_master_scan(void) {
  188. int ret = _matrix_scan();
  189. #ifndef KEYBOARD_helix_rev1
  190. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  191. #ifdef USE_MATRIX_I2C
  192. // for (int i = 0; i < ROWS_PER_HAND; ++i) {
  193. /* i2c_slave_buffer[i] = matrix[offset+i]; */
  194. // i2c_slave_buffer[i] = matrix[offset+i];
  195. // }
  196. #else // USE_SERIAL
  197. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  198. serial_master_buffer[i] = matrix[offset+i];
  199. }
  200. #endif
  201. #endif
  202. #ifdef USE_MATRIX_I2C
  203. if( i2c_transaction() ) {
  204. #else // USE_SERIAL
  205. if( serial_transaction() ) {
  206. #endif
  207. // turn on the indicator led when halves are disconnected
  208. TXLED1;
  209. error_count++;
  210. if (error_count > ERROR_DISCONNECT_COUNT) {
  211. // reset other half if disconnected
  212. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  213. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  214. matrix[slaveOffset+i] = 0;
  215. }
  216. }
  217. } else {
  218. // turn off the indicator led on no error
  219. TXLED0;
  220. error_count = 0;
  221. }
  222. matrix_scan_quantum();
  223. return ret;
  224. }
  225. void matrix_slave_scan(void) {
  226. _matrix_scan();
  227. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  228. #ifdef USE_MATRIX_I2C
  229. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  230. /* i2c_slave_buffer[i] = matrix[offset+i]; */
  231. i2c_slave_buffer[i] = matrix[offset+i];
  232. }
  233. #else // USE_SERIAL
  234. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  235. serial_slave_buffer[i] = matrix[offset+i];
  236. }
  237. #endif
  238. }
  239. bool matrix_is_modified(void)
  240. {
  241. if (debouncing) return false;
  242. return true;
  243. }
  244. inline
  245. bool matrix_is_on(uint8_t row, uint8_t col)
  246. {
  247. return (matrix[row] & ((matrix_row_t)1<<col));
  248. }
  249. inline
  250. matrix_row_t matrix_get_row(uint8_t row)
  251. {
  252. return matrix[row];
  253. }
  254. void matrix_print(void)
  255. {
  256. print("\nr/c 0123456789ABCDEF\n");
  257. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  258. phex(row); print(": ");
  259. pbin_reverse16(matrix_get_row(row));
  260. print("\n");
  261. }
  262. }
  263. uint8_t matrix_key_count(void)
  264. {
  265. uint8_t count = 0;
  266. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  267. count += bitpop16(matrix[i]);
  268. }
  269. return count;
  270. }
  271. static void init_cols(void)
  272. {
  273. for(int x = 0; x < MATRIX_COLS; x++) {
  274. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  275. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  276. }
  277. }
  278. static matrix_row_t read_cols(void)
  279. {
  280. matrix_row_t result = 0;
  281. for(int x = 0; x < MATRIX_COLS; x++) {
  282. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  283. }
  284. return result;
  285. }
  286. static void unselect_rows(void)
  287. {
  288. for(int x = 0; x < ROWS_PER_HAND; x++) {
  289. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  290. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  291. }
  292. }
  293. static void select_row(uint8_t row)
  294. {
  295. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  296. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  297. }