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.

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