Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "timer.h"
  27. #include "matrix.h"
  28. #include "i2c.h"
  29. #include "serial.h"
  30. #include "split-util.h"
  31. #include "pro-micro.h"
  32. #include "config.h"
  33. #include "rgblight.h"
  34. #include "pin_defs.h"
  35. #ifndef DEBOUNCE
  36. # define DEBOUNCE 5
  37. #endif
  38. #define ERROR_DISCONNECT_COUNT 5
  39. #define I2C_MATRIX_ADDR 0x00
  40. #define I2C_LED_ADDR ROWS_PER_HAND
  41. static uint8_t debouncing = DEBOUNCE;
  42. static uint8_t error_count = 0;
  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 const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  47. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  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. inline
  53. uint8_t matrix_rows(void)
  54. {
  55. return MATRIX_ROWS;
  56. }
  57. inline
  58. uint8_t matrix_cols(void)
  59. {
  60. return MATRIX_COLS;
  61. }
  62. void matrix_init(void)
  63. {
  64. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  65. MCUCR |= (1<<JTD);
  66. MCUCR |= (1<<JTD);
  67. debug_enable = true;
  68. debug_matrix = true;
  69. debug_mouse = true;
  70. // initialize row and col
  71. unselect_rows();
  72. init_cols();
  73. TX_RX_LED_INIT;
  74. //Turn LEDs off by default
  75. RXLED0;
  76. TXLED0;
  77. rgblight_init();
  78. // initialize matrix state: all keys off
  79. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  80. matrix[i] = 0;
  81. matrix_debouncing[i] = 0;
  82. }
  83. }
  84. uint8_t _matrix_scan(void)
  85. {
  86. // Right hand is stored after the left in the matirx so, we need to offset it
  87. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  88. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  89. select_row(i);
  90. _delay_us(30); // without this wait read unstable value.
  91. matrix_row_t cols = read_cols();
  92. if (matrix_debouncing[i+offset] != cols) {
  93. matrix_debouncing[i+offset] = cols;
  94. debouncing = DEBOUNCE;
  95. }
  96. unselect_rows();
  97. }
  98. if (debouncing) {
  99. if (--debouncing) {
  100. _delay_ms(1);
  101. } else {
  102. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  103. matrix[i+offset] = matrix_debouncing[i+offset];
  104. }
  105. }
  106. }
  107. return 1;
  108. }
  109. // Get rows from other half over i2c
  110. int i2c_transaction(void) {
  111. bool err = false;
  112. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  113. err = i2c_master_read(
  114. SLAVE_I2C_ADDRESS, // i2c address of other half
  115. I2C_MATRIX_ADDR, // read the slaves matrix data
  116. matrix+slaveOffset, // store in correct position in master's matrix
  117. ROWS_PER_HAND // number of bytes to read
  118. );
  119. #ifdef I2C_WRITE_TEST_CODE
  120. // controls the RX led on the slave and toggles it every second
  121. uint8_t test_data = (timer_read() / 1000) % 2;
  122. err |= i2c_master_write(
  123. SLAVE_I2C_ADDRESS, // i2c address of other half
  124. I2C_LED_ADDR, // address for led control
  125. &test_data, // data to send
  126. sizeof(test_data) // size of test data
  127. );
  128. #endif
  129. return err;
  130. }
  131. int serial_transaction(void) {
  132. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  133. if (serial_update_buffers()) {
  134. return 1;
  135. }
  136. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  137. matrix[slaveOffset+i] = serial_slave_buffer[i];
  138. }
  139. return 0;
  140. }
  141. uint8_t matrix_scan(void)
  142. {
  143. int ret = _matrix_scan();
  144. #ifdef USE_I2C
  145. if( i2c_transaction() ) {
  146. #else
  147. if( serial_transaction() ) {
  148. #endif
  149. // turn on the indicator led when halves are disconnected
  150. TXLED1;
  151. error_count++;
  152. if (error_count > ERROR_DISCONNECT_COUNT) {
  153. // reset other half if disconnected
  154. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  155. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  156. matrix[slaveOffset+i] = 0;
  157. }
  158. }
  159. } else {
  160. // turn off the indicator led on no error
  161. TXLED0;
  162. error_count = 0;
  163. }
  164. return ret;
  165. }
  166. void matrix_slave_scan(void) {
  167. _matrix_scan();
  168. int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
  169. #ifdef USE_I2C
  170. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  171. i2c_slave_write(I2C_MATRIX_ADDR+i, matrix[offset+i]);
  172. }
  173. #ifdef I2C_WRITE_TEST_CODE
  174. // control the pro micro RX LED based on what the
  175. // i2c master has sent us
  176. uint8_t led_state = i2c_slave_read(I2C_LED_ADDR);
  177. if (led_state == 1) {
  178. RXLED1;
  179. } else if(led_state == 0) {
  180. RXLED0;
  181. }
  182. #endif
  183. #else
  184. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  185. serial_slave_buffer[i] = matrix[offset+i];
  186. }
  187. #endif
  188. }
  189. bool matrix_is_modified(void)
  190. {
  191. if (debouncing) return false;
  192. return true;
  193. }
  194. inline
  195. bool matrix_is_on(uint8_t row, uint8_t col)
  196. {
  197. return (matrix[row] & ((matrix_row_t)1<<col));
  198. }
  199. inline
  200. matrix_row_t matrix_get_row(uint8_t row)
  201. {
  202. return matrix[row];
  203. }
  204. void matrix_print(void)
  205. {
  206. print("\nr/c 0123456789ABCDEF\n");
  207. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  208. phex(row); print(": ");
  209. pbin_reverse16(matrix_get_row(row));
  210. print("\n");
  211. }
  212. }
  213. uint8_t matrix_key_count(void)
  214. {
  215. uint8_t count = 0;
  216. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  217. count += bitpop16(matrix[i]);
  218. }
  219. return count;
  220. }
  221. static void init_cols(void)
  222. {
  223. for(int x = 0; x < MATRIX_COLS; x++) {
  224. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  225. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  226. }
  227. }
  228. static matrix_row_t read_cols(void)
  229. {
  230. matrix_row_t result = 0;
  231. for(int x = 0; x < MATRIX_COLS; x++) {
  232. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  233. }
  234. return result;
  235. }
  236. static void unselect_rows(void)
  237. {
  238. for(int x = 0; x < ROWS_PER_HAND; x++) {
  239. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  240. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  241. }
  242. }
  243. static void select_row(uint8_t row)
  244. {
  245. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  246. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  247. }