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.

173 lines
5.1 KiB

  1. /*
  2. * Copyright 2018-2023 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 "gpio.h"
  18. #include "hal_pal.h"
  19. #include "hal_pal_lld.h"
  20. #include "quantum.h"
  21. // STM32-specific watchdog config calculations
  22. // timeout = 31.25us * PR * (RL + 1)
  23. #define _STM32_IWDG_LSI(us) ((us) / 31.25)
  24. #define STM32_IWDG_PR_US(us) (uint8_t)(log(_STM32_IWDG_LSI(us)) / log(2) - 11)
  25. #define STM32_IWDG_PR_MS(s) STM32_IWDG_PR_US(s * 1000.0)
  26. #define STM32_IWDG_PR_S(s) STM32_IWDG_PR_US(s * 1000000.0)
  27. #define _STM32_IWDG_SCALAR(us) (2 << ((uint8_t)STM32_IWDG_PR_US(us) + 1))
  28. #define STM32_IWDG_RL_US(us) (uint64_t)(_STM32_IWDG_LSI(us)) / _STM32_IWDG_SCALAR(us)
  29. #define STM32_IWDG_RL_MS(s) STM32_IWDG_RL_US(s * 1000.0)
  30. #define STM32_IWDG_RL_S(s) STM32_IWDG_RL_US(s * 1000000.0)
  31. /* matrix state(1:on, 0:off) */
  32. static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  33. static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  34. static matrix_row_t matrix_inverted[MATRIX_COLS];
  35. int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  36. uint8_t encoder_state[8] = {0};
  37. int8_t encoder_pulses[8] = {0};
  38. uint8_t encoder_value[8] = {0};
  39. void matrix_init_custom(void) {
  40. // actual matrix setup - cols
  41. for (int i = 0; i < MATRIX_COLS; i++) {
  42. setPinOutput(matrix_col_pins[i]);
  43. }
  44. // rows
  45. for (int i = 0; i < MATRIX_ROWS; i++) {
  46. setPinInputLow(matrix_row_pins[i]);
  47. }
  48. // encoder A & B setup
  49. setPinInputLow(B12);
  50. setPinInputLow(B13);
  51. // setup watchdog timer for 1 second
  52. wdgInit();
  53. static WDGConfig wdgcfg;
  54. wdgcfg.pr = STM32_IWDG_PR_S(1.0);
  55. wdgcfg.rlr = STM32_IWDG_RL_S(1.0);
  56. wdgcfg.winr = STM32_IWDG_WIN_DISABLED;
  57. wdgStart(&WDGD1, &wdgcfg);
  58. }
  59. bool encoder_update(uint8_t index, uint8_t state) {
  60. bool changed = false;
  61. uint8_t i = index;
  62. encoder_pulses[i] += encoder_LUT[state & 0xF];
  63. if (encoder_pulses[i] >= ENCODER_RESOLUTION) {
  64. encoder_value[index]++;
  65. changed = true;
  66. #ifdef ENCODER_MAP_ENABLE
  67. encoder_exec_mapping(index, false);
  68. #else // ENCODER_MAP_ENABLE
  69. encoder_update_kb(index, false);
  70. #endif // ENCODER_MAP_ENABLE
  71. }
  72. if (encoder_pulses[i] <= -ENCODER_RESOLUTION) {
  73. encoder_value[index]--;
  74. changed = true;
  75. #ifdef ENCODER_MAP_ENABLE
  76. encoder_exec_mapping(index, true);
  77. #else // ENCODER_MAP_ENABLE
  78. encoder_update_kb(index, true);
  79. #endif // ENCODER_MAP_ENABLE
  80. }
  81. encoder_pulses[i] %= ENCODER_RESOLUTION;
  82. #ifdef ENCODER_DEFAULT_POS
  83. encoder_pulses[i] = 0;
  84. #endif
  85. return changed;
  86. }
  87. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  88. // reset watchdog
  89. wdgReset(&WDGD1);
  90. bool changed = false;
  91. // actual matrix
  92. for (int col = 0; col < MATRIX_COLS; col++) {
  93. matrix_row_t data = 0;
  94. // strobe col
  95. writePinHigh(matrix_col_pins[col]);
  96. // need wait to settle pin state
  97. wait_us(20);
  98. // read row data
  99. for (int row = 0; row < MATRIX_ROWS; row++) {
  100. data |= (readPin(matrix_row_pins[row]) << row);
  101. }
  102. // unstrobe col
  103. writePinLow(matrix_col_pins[col]);
  104. if (matrix_inverted[col] != data) {
  105. matrix_inverted[col] = data;
  106. }
  107. }
  108. for (int row = 0; row < MATRIX_ROWS; row++) {
  109. matrix_row_t old = current_matrix[row];
  110. current_matrix[row] = 0;
  111. for (int col = 0; col < MATRIX_COLS; col++) {
  112. current_matrix[row] |= ((matrix_inverted[col] & (1 << row) ? 1 : 0) << col);
  113. }
  114. changed |= old != current_matrix[row];
  115. }
  116. // encoder-matrix functionality
  117. // set up C/rows for encoder read
  118. for (int i = 0; i < MATRIX_ROWS; i++) {
  119. setPinOutput(matrix_row_pins[i]);
  120. writePinHigh(matrix_row_pins[i]);
  121. }
  122. // set up A & B for reading
  123. setPinInputHigh(B12);
  124. setPinInputHigh(B13);
  125. for (int i = 0; i < MATRIX_ROWS; i++) {
  126. writePinLow(matrix_row_pins[i]);
  127. wait_us(10);
  128. uint8_t new_status = (palReadPad(GPIOB, 12) << 0) | (palReadPad(GPIOB, 13) << 1);
  129. if ((encoder_state[i] & 0x3) != new_status) {
  130. encoder_state[i] <<= 2;
  131. encoder_state[i] |= new_status;
  132. encoder_update(i, encoder_state[i]);
  133. }
  134. writePinHigh(matrix_row_pins[i]);
  135. }
  136. // revert A & B to matrix state
  137. setPinInputLow(B12);
  138. setPinInputLow(B13);
  139. // revert C/rows to matrix state
  140. for (int i = 0; i < MATRIX_ROWS; i++) {
  141. setPinInputLow(matrix_row_pins[i]);
  142. }
  143. return changed;
  144. }