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.

168 lines
5.3 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. /*
  26. * col: { B11, B10, B2, B1, A7, B0 }
  27. * row: { A10, A9, A8, B15, C13, C14, C15, A2 }
  28. */
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_COLS];
  32. static bool debouncing = false;
  33. static uint16_t debouncing_time = 0;
  34. __attribute__((weak)) void matrix_init_user(void) {}
  35. __attribute__((weak)) void matrix_scan_user(void) {}
  36. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  37. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  38. void matrix_init(void) {
  39. printf("matrix init\n");
  40. // debug_matrix = true;
  41. // actual matrix setup
  42. palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
  44. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  45. palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
  46. palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL);
  47. palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
  48. palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLDOWN);
  49. palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLDOWN);
  50. palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLDOWN);
  51. palSetPadMode(GPIOB, 15, PAL_MODE_INPUT_PULLDOWN);
  52. palSetPadMode(GPIOC, 13, PAL_MODE_INPUT_PULLDOWN);
  53. palSetPadMode(GPIOC, 14, PAL_MODE_INPUT_PULLDOWN);
  54. palSetPadMode(GPIOC, 15, PAL_MODE_INPUT_PULLDOWN);
  55. palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN);
  56. palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN);
  57. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
  58. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  59. memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t));
  60. matrix_init_quantum();
  61. }
  62. uint8_t matrix_scan(void) {
  63. // actual matrix
  64. for (int col = 0; col < MATRIX_COLS; col++) {
  65. matrix_row_t data = 0;
  66. // strobe col { B11, B10, B2, B1, A7, B0 }
  67. switch (col) {
  68. case 0:
  69. palSetPad(GPIOB, 11);
  70. break;
  71. case 1:
  72. palSetPad(GPIOB, 10);
  73. break;
  74. case 2:
  75. palSetPad(GPIOB, 2);
  76. break;
  77. case 3:
  78. palSetPad(GPIOB, 1);
  79. break;
  80. case 4:
  81. palSetPad(GPIOA, 7);
  82. break;
  83. case 5:
  84. palSetPad(GPIOB, 0);
  85. break;
  86. }
  87. // need wait to settle pin state
  88. wait_us(20);
  89. // read row data { A10, A9, A8, B15, C13, C14, C15, A2 }
  90. 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));
  91. // unstrobe col { B11, B10, B2, B1, A7, B0 }
  92. switch (col) {
  93. case 0:
  94. palClearPad(GPIOB, 11);
  95. break;
  96. case 1:
  97. palClearPad(GPIOB, 10);
  98. break;
  99. case 2:
  100. palClearPad(GPIOB, 2);
  101. break;
  102. case 3:
  103. palClearPad(GPIOB, 1);
  104. break;
  105. case 4:
  106. palClearPad(GPIOA, 7);
  107. break;
  108. case 5:
  109. palClearPad(GPIOB, 0);
  110. break;
  111. }
  112. if (matrix_debouncing[col] != data) {
  113. matrix_debouncing[col] = data;
  114. debouncing = true;
  115. debouncing_time = timer_read();
  116. }
  117. }
  118. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  119. for (int row = 0; row < MATRIX_ROWS; row++) {
  120. matrix[row] = 0;
  121. for (int col = 0; col < MATRIX_COLS; col++) {
  122. matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col);
  123. }
  124. }
  125. debouncing = false;
  126. }
  127. matrix_scan_quantum();
  128. return 1;
  129. }
  130. bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); }
  131. matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  132. void matrix_print(void) {
  133. printf("\nr/c 01234567\n");
  134. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  135. printf("%X0: ", row);
  136. matrix_row_t data = matrix_get_row(row);
  137. for (int col = 0; col < MATRIX_COLS; col++) {
  138. if (data & (1 << col))
  139. printf("1");
  140. else
  141. printf("0");
  142. }
  143. printf("\n");
  144. }
  145. }