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.

128 lines
4.0 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. * JM60
  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: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
  15. * row: { PTB11, PTB10, PTB2, PTB1, PTB0}
  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(GPIOA, 15, PAL_MODE_INPUT_PULLDOWN);
  27. palSetPadMode(GPIOC, 10, PAL_MODE_INPUT_PULLDOWN);
  28. palSetPadMode(GPIOC, 11, PAL_MODE_INPUT_PULLDOWN);
  29. palSetPadMode(GPIOC, 12, PAL_MODE_INPUT_PULLDOWN);
  30. palSetPadMode(GPIOD, 2, PAL_MODE_INPUT_PULLDOWN);
  31. palSetPadMode(GPIOB, 3, PAL_MODE_INPUT_PULLDOWN);
  32. palSetPadMode(GPIOB, 4, PAL_MODE_INPUT_PULLDOWN);
  33. palSetPadMode(GPIOB, 5, PAL_MODE_INPUT_PULLDOWN);
  34. palSetPadMode(GPIOB, 6, PAL_MODE_INPUT_PULLDOWN);
  35. palSetPadMode(GPIOB, 7, PAL_MODE_INPUT_PULLDOWN);
  36. palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_PULLDOWN);
  37. palSetPadMode(GPIOB, 9, PAL_MODE_INPUT_PULLDOWN);
  38. palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN);
  39. palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN);
  40. /* Row(strobe) */
  41. palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
  42. palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
  44. palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
  45. palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
  46. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  47. memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  48. }
  49. uint8_t matrix_scan(void)
  50. {
  51. for (int row = 0; row < MATRIX_ROWS; row++) {
  52. matrix_row_t data = 0;
  53. // strobe row
  54. switch (row) {
  55. case 0: palSetPad(GPIOB, 11); break;
  56. case 1: palSetPad(GPIOB, 10); break;
  57. case 2: palSetPad(GPIOB, 2); break;
  58. case 3: palSetPad(GPIOB, 1); break;
  59. case 4: palSetPad(GPIOB, 0); break;
  60. }
  61. wait_us(20); // need wait to settle pin state
  62. // read col data: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
  63. data = ((palReadPort(GPIOA) & 0x8000UL) >> 15) | // 0
  64. ((palReadPort(GPIOC) & 0x1C00UL) >> 9) | // 1, 2, 3
  65. ((palReadPort(GPIOD) & 0x0004UL) << 2) | // 4
  66. ((palReadPort(GPIOB) & 0x03F8UL) << 2) | // 5, 6, 7, 8, 9, 10, 11
  67. ((palReadPort(GPIOA) & 0x000CUL) << 10); // 12, 13
  68. // un-strobe row
  69. switch (row) {
  70. case 0: palClearPad(GPIOB, 11); break;
  71. case 1: palClearPad(GPIOB, 10); break;
  72. case 2: palClearPad(GPIOB, 2); break;
  73. case 3: palClearPad(GPIOB, 1); break;
  74. case 4: palClearPad(GPIOB, 0); break;
  75. }
  76. if (matrix_debouncing[row] != data) {
  77. matrix_debouncing[row] = data;
  78. debouncing = true;
  79. debouncing_time = timer_read();
  80. }
  81. }
  82. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  83. for (int row = 0; row < MATRIX_ROWS; row++) {
  84. matrix[row] = matrix_debouncing[row];
  85. }
  86. debouncing = false;
  87. }
  88. return 1;
  89. }
  90. bool matrix_is_on(uint8_t row, uint8_t col)
  91. {
  92. return (matrix[row] & (1<<col));
  93. }
  94. matrix_row_t matrix_get_row(uint8_t row)
  95. {
  96. return matrix[row];
  97. }
  98. void matrix_print(void)
  99. {
  100. xprintf("\nr/c 01234567\n");
  101. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  102. xprintf("%X0: ", row);
  103. matrix_row_t data = matrix_get_row(row);
  104. for (int col = 0; col < MATRIX_COLS; col++) {
  105. if (data & (1<<col))
  106. xprintf("1");
  107. else
  108. xprintf("0");
  109. }
  110. xprintf("\n");
  111. }
  112. }