You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
7.8 KiB

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