matrix.c 5.1 KB

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