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.

170 lines
5.4 KiB

  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 <stdint.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include "hal.h"
  21. #include "timer.h"
  22. #include "wait.h"
  23. #include "debug.h"
  24. #include "matrix.h"
  25. typedef uint16_t matrix_col_t;
  26. /*
  27. * col: { B11, B10, B2, B1, A7, B0 }
  28. * row: { A10, A9, A8, B15, C13, C14, C15, A2 }
  29. */
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t matrix[MATRIX_ROWS];
  32. static matrix_col_t matrix_debouncing[MATRIX_COLS];
  33. static bool debouncing = false;
  34. static uint16_t debouncing_time = 0;
  35. __attribute__((weak)) void matrix_init_user(void) {}
  36. __attribute__((weak)) void matrix_scan_user(void) {}
  37. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  38. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  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_quantum();
  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_quantum();
  129. return 1;
  130. }
  131. bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); }
  132. matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  133. void matrix_print(void) {
  134. dprintf("\nr/c 01234567\n");
  135. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  136. dprintf("%X0: ", row);
  137. matrix_row_t data = matrix_get_row(row);
  138. for (int col = 0; col < MATRIX_COLS; col++) {
  139. if (data & (1 << col))
  140. dprintf("1");
  141. else
  142. dprintf("0");
  143. }
  144. dprintf("\n");
  145. }
  146. }