matrix.c 7.7 KB

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