logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (5468B)


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