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.

358 lines
8.3 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 <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 matrix_init(void)
  78. {
  79. debug_enable = true;
  80. debug_matrix = true;
  81. debug_mouse = true;
  82. // initialize row and col
  83. unselect_rows();
  84. init_cols();
  85. setPinOutput(B0);
  86. setPinOutput(D5);
  87. writePinHigh(D5);
  88. writePinHigh(B0);
  89. // initialize matrix state: all keys off
  90. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  91. matrix[i] = 0;
  92. matrix_debouncing[i] = 0;
  93. }
  94. is_master = has_usb();
  95. matrix_init_quantum();
  96. }
  97. uint8_t _matrix_scan(void)
  98. {
  99. // Right hand is stored after the left in the matirx so, we need to offset it
  100. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  101. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  102. select_row(i);
  103. _delay_us(30); // without this wait read unstable value.
  104. matrix_row_t cols = read_cols();
  105. if (matrix_debouncing[i+offset] != cols) {
  106. matrix_debouncing[i+offset] = cols;
  107. debouncing = DEBOUNCE;
  108. }
  109. unselect_rows();
  110. }
  111. if (debouncing) {
  112. if (--debouncing) {
  113. _delay_ms(1);
  114. } else {
  115. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  116. matrix[i+offset] = matrix_debouncing[i+offset];
  117. }
  118. }
  119. }
  120. return 1;
  121. }
  122. #ifdef USE_MATRIX_I2C
  123. // Get rows from other half over i2c
  124. int i2c_transaction(void) {
  125. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  126. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  127. if (err) goto i2c_error;
  128. // start of matrix stored at 0x00
  129. err = i2c_master_write(0x00);
  130. if (err) goto i2c_error;
  131. // Start read
  132. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  133. if (err) goto i2c_error;
  134. if (!err) {
  135. int i;
  136. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  137. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  138. }
  139. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  140. i2c_master_stop();
  141. } else {
  142. i2c_error: // the cable is disconnceted, or something else went wrong
  143. i2c_reset_state();
  144. return err;
  145. }
  146. return 0;
  147. }
  148. #else // USE_SERIAL
  149. int serial_transaction(int master_changed) {
  150. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  151. #ifdef SERIAL_USE_MULTI_TRANSACTION
  152. int ret=serial_update_buffers(master_changed);
  153. #else
  154. int ret=serial_update_buffers();
  155. #endif
  156. if (ret ) {
  157. if(ret==2) writePinLow(B0);
  158. return 1;
  159. }
  160. writePinHigh(B0);
  161. memcpy(&matrix[slaveOffset],
  162. (void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH);
  163. return 0;
  164. }
  165. #endif
  166. uint8_t matrix_scan(void)
  167. {
  168. if (is_master) {
  169. matrix_master_scan();
  170. }else{
  171. matrix_slave_scan();
  172. int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
  173. memcpy(&matrix[offset],
  174. (void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH);
  175. matrix_scan_quantum();
  176. }
  177. return 1;
  178. }
  179. uint8_t matrix_master_scan(void) {
  180. int ret = _matrix_scan();
  181. int mchanged = 1;
  182. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  183. #ifdef USE_MATRIX_I2C
  184. // for (int i = 0; i < ROWS_PER_HAND; ++i) {
  185. /* i2c_slave_buffer[i] = matrix[offset+i]; */
  186. // i2c_slave_buffer[i] = matrix[offset+i];
  187. // }
  188. #else // USE_SERIAL
  189. #ifdef SERIAL_USE_MULTI_TRANSACTION
  190. mchanged = memcmp((void *)serial_master_buffer,
  191. &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
  192. #endif
  193. memcpy((void *)serial_master_buffer,
  194. &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
  195. #endif
  196. #ifdef USE_MATRIX_I2C
  197. if( i2c_transaction() ) {
  198. #else // USE_SERIAL
  199. if( serial_transaction(mchanged) ) {
  200. #endif
  201. // turn on the indicator led when halves are disconnected
  202. writePinLow(D5);
  203. error_count++;
  204. if (error_count > ERROR_DISCONNECT_COUNT) {
  205. // reset other half if disconnected
  206. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  207. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  208. matrix[slaveOffset+i] = 0;
  209. }
  210. }
  211. } else {
  212. // turn off the indicator led on no error
  213. writePinHigh(D5);
  214. error_count = 0;
  215. }
  216. matrix_scan_quantum();
  217. return ret;
  218. }
  219. void matrix_slave_scan(void) {
  220. _matrix_scan();
  221. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  222. #ifdef USE_MATRIX_I2C
  223. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  224. /* i2c_slave_buffer[i] = matrix[offset+i]; */
  225. i2c_slave_buffer[i] = matrix[offset+i];
  226. }
  227. #else // USE_SERIAL
  228. #ifdef SERIAL_USE_MULTI_TRANSACTION
  229. int change = 0;
  230. #endif
  231. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  232. #ifdef SERIAL_USE_MULTI_TRANSACTION
  233. if( serial_slave_buffer[i] != matrix[offset+i] )
  234. change = 1;
  235. #endif
  236. serial_slave_buffer[i] = matrix[offset+i];
  237. }
  238. #ifdef SERIAL_USE_MULTI_TRANSACTION
  239. slave_buffer_change_count += change;
  240. #endif
  241. #endif
  242. }
  243. bool matrix_is_modified(void)
  244. {
  245. if (debouncing) return false;
  246. return true;
  247. }
  248. inline
  249. bool matrix_is_on(uint8_t row, uint8_t col)
  250. {
  251. return (matrix[row] & ((matrix_row_t)1<<col));
  252. }
  253. inline
  254. matrix_row_t matrix_get_row(uint8_t row)
  255. {
  256. return matrix[row];
  257. }
  258. void matrix_print(void)
  259. {
  260. print("\nr/c 0123456789ABCDEF\n");
  261. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  262. print_hex8(row); print(": ");
  263. print_bin_reverse16(matrix_get_row(row));
  264. print("\n");
  265. }
  266. }
  267. uint8_t matrix_key_count(void)
  268. {
  269. uint8_t count = 0;
  270. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  271. count += bitpop16(matrix[i]);
  272. }
  273. return count;
  274. }
  275. static void init_cols(void)
  276. {
  277. for(int x = 0; x < MATRIX_COLS; x++) {
  278. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  279. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  280. }
  281. }
  282. static matrix_row_t read_cols(void)
  283. {
  284. matrix_row_t result = 0;
  285. for(int x = 0; x < MATRIX_COLS; x++) {
  286. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  287. }
  288. return result;
  289. }
  290. static void unselect_rows(void)
  291. {
  292. for(int x = 0; x < ROWS_PER_HAND; x++) {
  293. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  294. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  295. }
  296. }
  297. static void select_row(uint8_t row)
  298. {
  299. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  300. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  301. }