matrix.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright 2012-2017 Jun Wako, Jack Humbert
  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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #if defined(__AVR__)
  17. #include <avr/io.h>
  18. #endif
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "timer.h"
  25. #include "sx60.h"
  26. /* Set 0 if debouncing isn't needed */
  27. #ifndef DEBOUNCING_DELAY
  28. # define DEBOUNCING_DELAY 5
  29. #endif
  30. #if (DEBOUNCING_DELAY > 0)
  31. static uint16_t debouncing_time;
  32. static bool debouncing = false;
  33. #endif
  34. #if (MATRIX_COLS <= 8)
  35. # define print_matrix_header() print("\nr/c 01234567\n")
  36. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  37. # define matrix_bitpop(i) bitpop(matrix[i])
  38. # define ROW_SHIFTER ((uint8_t)1)
  39. #elif (MATRIX_COLS <= 16)
  40. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  41. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  42. # define matrix_bitpop(i) bitpop16(matrix[i])
  43. # define ROW_SHIFTER ((uint16_t)1)
  44. #elif (MATRIX_COLS <= 32)
  45. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  46. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  47. # define matrix_bitpop(i) bitpop32(matrix[i])
  48. # define ROW_SHIFTER ((uint32_t)1)
  49. #endif
  50. #ifdef MATRIX_MASKED
  51. extern const matrix_row_t matrix_mask[];
  52. #endif
  53. static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
  54. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  55. /* matrix state(1:on, 0:off) */
  56. static matrix_row_t matrix[MATRIX_ROWS];
  57. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  58. static uint8_t mcp23018_reset_loop;
  59. static void init_cols(void);
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  61. static void unselect_rows(void);
  62. static void select_row(uint8_t row);
  63. __attribute__ ((weak))
  64. void matrix_init_quantum(void) {
  65. matrix_init_kb();
  66. }
  67. __attribute__ ((weak))
  68. void matrix_scan_quantum(void) {
  69. matrix_scan_kb();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_init_kb(void) {
  73. matrix_init_user();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_kb(void) {
  77. matrix_scan_user();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_init_user(void) {
  81. }
  82. __attribute__ ((weak))
  83. void matrix_scan_user(void) {
  84. }
  85. inline
  86. uint8_t matrix_rows(void) {
  87. return MATRIX_ROWS;
  88. }
  89. inline
  90. uint8_t matrix_cols(void) {
  91. return MATRIX_COLS;
  92. }
  93. void matrix_init(void) {
  94. /* To use PORTF disable JTAG with writing JTD bit twice within four cycles. */
  95. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
  96. MCUCR |= _BV(JTD);
  97. MCUCR |= _BV(JTD);
  98. #endif
  99. mcp23018_status = true;
  100. /* initialize row and col */
  101. unselect_rows();
  102. init_cols();
  103. /* initialize matrix state: all keys off */
  104. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  105. matrix[i] = 0;
  106. matrix_debouncing[i] = 0;
  107. }
  108. matrix_init_quantum();
  109. }
  110. uint8_t matrix_scan(void)
  111. {
  112. if (mcp23018_status) {
  113. /* if there was an error */
  114. if (++mcp23018_reset_loop == 0) {
  115. /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  116. this will be approx bit more frequent than once per second */
  117. print("trying to reset mcp23018\n");
  118. mcp23018_status = init_mcp23018();
  119. if (mcp23018_status) {
  120. print("left side not responding\n");
  121. } else {
  122. print("left side attached\n");
  123. }
  124. }
  125. }
  126. /* Set row, read cols */
  127. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  128. # if (DEBOUNCING_DELAY > 0)
  129. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  130. if (matrix_changed) {
  131. debouncing = true;
  132. debouncing_time = timer_read();
  133. }
  134. # else
  135. read_cols_on_row(matrix, current_row);
  136. # endif
  137. }
  138. # if (DEBOUNCING_DELAY > 0)
  139. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  140. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  141. matrix[i] = matrix_debouncing[i];
  142. }
  143. debouncing = false;
  144. }
  145. # endif
  146. matrix_scan_quantum();
  147. return 1;
  148. }
  149. bool matrix_is_modified(void)
  150. {
  151. #if (DEBOUNCING_DELAY > 0)
  152. if (debouncing) return false;
  153. #endif
  154. return true;
  155. }
  156. inline
  157. bool matrix_is_on(uint8_t row, uint8_t col)
  158. {
  159. return (matrix[row] & ((matrix_row_t)1<col));
  160. }
  161. inline
  162. matrix_row_t matrix_get_row(uint8_t row)
  163. {
  164. /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  165. switch blocker installed and the switch is always pressed. */
  166. #ifdef MATRIX_MASKED
  167. return matrix[row] & matrix_mask[row];
  168. #else
  169. return matrix[row];
  170. #endif
  171. }
  172. void matrix_print(void)
  173. {
  174. print_matrix_header();
  175. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  176. phex(row); print(": ");
  177. print_matrix_row(row);
  178. print("\n");
  179. }
  180. }
  181. uint8_t matrix_key_count(void)
  182. {
  183. uint8_t count = 0;
  184. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  185. count += matrix_bitpop(i);
  186. }
  187. return count;
  188. }
  189. static void init_cols(void)
  190. {
  191. for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
  192. uint8_t pin = col_pins[x];
  193. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  194. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  195. }
  196. }
  197. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  198. {
  199. /* Store last value of row prior to reading */
  200. matrix_row_t last_row_value = current_matrix[current_row];
  201. /* Clear data in matrix row */
  202. current_matrix[current_row] = 0;
  203. /* Select row and wait for row selecton to stabilize */
  204. select_row(current_row);
  205. wait_us(30);
  206. if (mcp23018_status) {
  207. /* if there was an error */
  208. return 0;
  209. } else {
  210. uint16_t data = 0;
  211. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  212. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  213. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  214. data = i2c_readNak();
  215. data = ~data;
  216. out:
  217. i2c_stop();
  218. current_matrix[current_row] |= (data << 8);
  219. }
  220. /* For each col... */
  221. for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
  222. /* Select the col pin to read (active low) */
  223. uint8_t pin = col_pins[col_index];
  224. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  225. /* Populate the matrix row with the state of the col pin */
  226. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  227. }
  228. /* Unselect row */
  229. unselect_rows();
  230. return (last_row_value != current_matrix[current_row]);
  231. }
  232. static void select_row(uint8_t row)
  233. {
  234. if (mcp23018_status) {
  235. /* if there was an error do nothing */
  236. } else {
  237. /* set active row low : 0
  238. set active row output : 1
  239. set other rows hi-Z : 1 */
  240. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  241. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  242. mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
  243. out:
  244. i2c_stop();
  245. }
  246. uint8_t pin = row_pins[row];
  247. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
  248. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
  249. }
  250. static void unselect_rows(void)
  251. {
  252. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  253. uint8_t pin = row_pins[x];
  254. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  255. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  256. }
  257. }