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

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