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.

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