matrix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "quantum.h"
  18. #ifndef DEBOUNCE
  19. # define DEBOUNCE 5
  20. #endif
  21. typedef uint16_t matrix_col_t;
  22. /*
  23. * col: { B11, B10, B2, B1, A7, B0 }
  24. * row: { A10, A9, A8, B15, C13, C14, C15, A2 }
  25. */
  26. /* matrix state(1:on, 0:off) */
  27. static matrix_row_t matrix[MATRIX_ROWS];
  28. static matrix_col_t matrix_debouncing[MATRIX_COLS];
  29. static bool debouncing = false;
  30. static uint16_t debouncing_time = 0;
  31. __attribute__((weak)) void matrix_init_user(void) {}
  32. __attribute__((weak)) void matrix_scan_user(void) {}
  33. __attribute__((weak)) void matrix_init_kb(void) {
  34. matrix_init_user();
  35. }
  36. __attribute__((weak)) void matrix_scan_kb(void) {
  37. matrix_scan_user();
  38. }
  39. void matrix_init(void) {
  40. dprintf("matrix init\n");
  41. // debug_matrix = true;
  42. // actual matrix setup
  43. palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
  44. palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
  45. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  46. palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
  47. palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL);
  48. palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
  49. palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLDOWN);
  50. palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLDOWN);
  51. palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLDOWN);
  52. palSetPadMode(GPIOB, 15, PAL_MODE_INPUT_PULLDOWN);
  53. palSetPadMode(GPIOC, 13, PAL_MODE_INPUT_PULLDOWN);
  54. palSetPadMode(GPIOC, 14, PAL_MODE_INPUT_PULLDOWN);
  55. palSetPadMode(GPIOC, 15, PAL_MODE_INPUT_PULLDOWN);
  56. palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN);
  57. palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN);
  58. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
  59. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  60. memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_col_t));
  61. matrix_init_kb();
  62. }
  63. uint8_t matrix_scan(void) {
  64. // actual matrix
  65. for (int col = 0; col < MATRIX_COLS; col++) {
  66. matrix_col_t data = 0;
  67. // strobe col { B11, B10, B2, B1, A7, B0 }
  68. switch (col) {
  69. case 0:
  70. palSetPad(GPIOB, 11);
  71. break;
  72. case 1:
  73. palSetPad(GPIOB, 10);
  74. break;
  75. case 2:
  76. palSetPad(GPIOB, 2);
  77. break;
  78. case 3:
  79. palSetPad(GPIOB, 1);
  80. break;
  81. case 4:
  82. palSetPad(GPIOA, 7);
  83. break;
  84. case 5:
  85. palSetPad(GPIOB, 0);
  86. break;
  87. }
  88. // need wait to settle pin state
  89. wait_us(20);
  90. // read row data { A10, A9, A8, B15, C13, C14, C15, A2 }
  91. data = ((palReadPad(GPIOA, 10) << 0) | (palReadPad(GPIOA, 9) << 1) | (palReadPad(GPIOA, 8) << 2) | (palReadPad(GPIOB, 15) << 3) | (palReadPad(GPIOC, 13) << 4) | (palReadPad(GPIOC, 14) << 5) | (palReadPad(GPIOC, 15) << 6) | (palReadPad(GPIOA, 2) << 7) | (palReadPad(GPIOA, 3) << 8) | (palReadPad(GPIOA, 6) << 9));
  92. // unstrobe col { B11, B10, B2, B1, A7, B0 }
  93. switch (col) {
  94. case 0:
  95. palClearPad(GPIOB, 11);
  96. break;
  97. case 1:
  98. palClearPad(GPIOB, 10);
  99. break;
  100. case 2:
  101. palClearPad(GPIOB, 2);
  102. break;
  103. case 3:
  104. palClearPad(GPIOB, 1);
  105. break;
  106. case 4:
  107. palClearPad(GPIOA, 7);
  108. break;
  109. case 5:
  110. palClearPad(GPIOB, 0);
  111. break;
  112. }
  113. if (matrix_debouncing[col] != data) {
  114. matrix_debouncing[col] = data;
  115. debouncing = true;
  116. debouncing_time = timer_read();
  117. }
  118. }
  119. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  120. for (int row = 0; row < MATRIX_ROWS; row++) {
  121. matrix[row] = 0;
  122. for (int col = 0; col < MATRIX_COLS; col++) {
  123. matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col);
  124. }
  125. }
  126. debouncing = false;
  127. }
  128. matrix_scan_kb();
  129. return 1;
  130. }
  131. bool matrix_is_on(uint8_t row, uint8_t col) {
  132. return (matrix[row] & (1 << col));
  133. }
  134. matrix_row_t matrix_get_row(uint8_t row) {
  135. return matrix[row];
  136. }
  137. void matrix_print(void) {
  138. dprintf("\nr/c 01234567\n");
  139. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  140. dprintf("%X0: ", row);
  141. matrix_row_t data = matrix_get_row(row);
  142. for (int col = 0; col < MATRIX_COLS; col++) {
  143. if (data & (1 << col))
  144. dprintf("1");
  145. else
  146. dprintf("0");
  147. }
  148. dprintf("\n");
  149. }
  150. }