matrix.c 8.6 KB

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