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.

134 lines
4.1 KiB

  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include "hal.h"
  5. #include "timer.h"
  6. #include "wait.h"
  7. #include "print.h"
  8. #include "matrix.h"
  9. /*
  10. * Matt3o's WhiteFox
  11. * Column pins are input with internal pull-down. Row pins are output and strobe with high.
  12. * Key is high or 1 when it turns on.
  13. *
  14. * col: { PTD0, PTD1, PTD4, PTD5, PTD6, PTD7, PTC1, PTC2 }
  15. * row: { PTB2, PTB3, PTB18, PTB19, PTC0, PTC8, PTC9, PTC10, PTC11 }
  16. */
  17. /* matrix state(1:on, 0:off) */
  18. static matrix_row_t matrix[MATRIX_ROWS];
  19. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  20. static bool debouncing = false;
  21. static uint16_t debouncing_time = 0;
  22. void matrix_init(void)
  23. {
  24. //debug_matrix = true;
  25. /* Column(sense) */
  26. palSetPadMode(GPIOD, 0, PAL_MODE_INPUT_PULLDOWN);
  27. palSetPadMode(GPIOD, 1, PAL_MODE_INPUT_PULLDOWN);
  28. palSetPadMode(GPIOD, 4, PAL_MODE_INPUT_PULLDOWN);
  29. palSetPadMode(GPIOD, 5, PAL_MODE_INPUT_PULLDOWN);
  30. palSetPadMode(GPIOD, 6, PAL_MODE_INPUT_PULLDOWN);
  31. palSetPadMode(GPIOD, 7, PAL_MODE_INPUT_PULLDOWN);
  32. palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_PULLDOWN);
  33. palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_PULLDOWN);
  34. /* Row(strobe) */
  35. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  36. palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL);
  37. palSetPadMode(GPIOB, 18, PAL_MODE_OUTPUT_PUSHPULL);
  38. palSetPadMode(GPIOB, 19, PAL_MODE_OUTPUT_PUSHPULL);
  39. palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL);
  40. palSetPadMode(GPIOC, 8, PAL_MODE_OUTPUT_PUSHPULL);
  41. palSetPadMode(GPIOC, 9, PAL_MODE_OUTPUT_PUSHPULL);
  42. palSetPadMode(GPIOC, 10, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOC, 11, PAL_MODE_OUTPUT_PUSHPULL);
  44. memset(matrix, 0, MATRIX_ROWS);
  45. memset(matrix_debouncing, 0, MATRIX_ROWS);
  46. matrix_init_quantum();
  47. }
  48. uint8_t matrix_scan(void)
  49. {
  50. for (int row = 0; row < MATRIX_ROWS; row++) {
  51. matrix_row_t data = 0;
  52. // strobe row
  53. switch (row) {
  54. case 0: palSetPad(GPIOB, 2); break;
  55. case 1: palSetPad(GPIOB, 3); break;
  56. case 2: palSetPad(GPIOB, 18); break;
  57. case 3: palSetPad(GPIOB, 19); break;
  58. case 4: palSetPad(GPIOC, 0); break;
  59. case 5: palSetPad(GPIOC, 8); break;
  60. case 6: palSetPad(GPIOC, 9); break;
  61. case 7: palSetPad(GPIOC, 10); break;
  62. case 8: palSetPad(GPIOC, 11); break;
  63. }
  64. wait_us(20); // need wait to settle pin state
  65. // read col data: { PTD0, PTD1, PTD4, PTD5, PTD6, PTD7, PTC1, PTC2 }
  66. data = ((palReadPort(GPIOC) & 0x06UL) << 5) |
  67. ((palReadPort(GPIOD) & 0xF0UL) >> 2) |
  68. (palReadPort(GPIOD) & 0x03UL);
  69. // un-strobe row
  70. switch (row) {
  71. case 0: palClearPad(GPIOB, 2); break;
  72. case 1: palClearPad(GPIOB, 3); break;
  73. case 2: palClearPad(GPIOB, 18); break;
  74. case 3: palClearPad(GPIOB, 19); break;
  75. case 4: palClearPad(GPIOC, 0); break;
  76. case 5: palClearPad(GPIOC, 8); break;
  77. case 6: palClearPad(GPIOC, 9); break;
  78. case 7: palClearPad(GPIOC, 10); break;
  79. case 8: palClearPad(GPIOC, 11); break;
  80. }
  81. if (matrix_debouncing[row] != data) {
  82. matrix_debouncing[row] = data;
  83. debouncing = true;
  84. debouncing_time = timer_read();
  85. }
  86. }
  87. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  88. for (int row = 0; row < MATRIX_ROWS; row++) {
  89. matrix[row] = matrix_debouncing[row];
  90. }
  91. debouncing = false;
  92. }
  93. matrix_scan_quantum();
  94. return 1;
  95. }
  96. bool matrix_is_on(uint8_t row, uint8_t col)
  97. {
  98. return (matrix[row] & (1<<col));
  99. }
  100. matrix_row_t matrix_get_row(uint8_t row)
  101. {
  102. return matrix[row];
  103. }
  104. void matrix_print(void)
  105. {
  106. xprintf("\nr/c 01234567\n");
  107. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  108. xprintf("%X0: ", row);
  109. matrix_row_t data = matrix_get_row(row);
  110. for (int col = 0; col < MATRIX_COLS; col++) {
  111. if (data & (1<<col))
  112. xprintf("1");
  113. else
  114. xprintf("0");
  115. }
  116. xprintf("\n");
  117. }
  118. }