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.

124 lines
3.7 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. #include <math.h>
  22. // STM32-specific watchdog config calculations
  23. // timeout = 31.25us * PR * (RL + 1)
  24. #define _STM32_IWDG_LSI(us) ((us) / 31.25)
  25. #define STM32_IWDG_PR_US(us) (uint8_t)(log(_STM32_IWDG_LSI(us)) / log(2) - 11)
  26. #define STM32_IWDG_PR_MS(s) STM32_IWDG_PR_US(s * 1000.0)
  27. #define STM32_IWDG_PR_S(s) STM32_IWDG_PR_US(s * 1000000.0)
  28. #define _STM32_IWDG_SCALAR(us) (2 << ((uint8_t)STM32_IWDG_PR_US(us) + 1))
  29. #define STM32_IWDG_RL_US(us) (uint64_t)(_STM32_IWDG_LSI(us)) / _STM32_IWDG_SCALAR(us)
  30. #define STM32_IWDG_RL_MS(s) STM32_IWDG_RL_US(s * 1000.0)
  31. #define STM32_IWDG_RL_S(s) STM32_IWDG_RL_US(s * 1000000.0)
  32. #if !defined(PLANCK_WATCHDOG_TIMEOUT)
  33. # define PLANCK_WATCHDOG_TIMEOUT 1.0
  34. #endif
  35. /* matrix state(1:on, 0:off) */
  36. static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  37. static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  38. static matrix_row_t matrix_inverted[MATRIX_COLS];
  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. writePinLow(matrix_col_pins[i]);
  44. }
  45. // rows
  46. for (int i = 0; i < MATRIX_ROWS; i++) {
  47. setPinInputLow(matrix_row_pins[i]);
  48. }
  49. // encoder A & B setup
  50. setPinInputLow(B12);
  51. setPinInputLow(B13);
  52. #ifndef PLANCK_WATCHDOG_DISABLE
  53. wdgInit();
  54. static WDGConfig wdgcfg;
  55. wdgcfg.pr = STM32_IWDG_PR_S(PLANCK_WATCHDOG_TIMEOUT);
  56. wdgcfg.rlr = STM32_IWDG_RL_S(PLANCK_WATCHDOG_TIMEOUT);
  57. wdgcfg.winr = STM32_IWDG_WIN_DISABLED;
  58. wdgStart(&WDGD1, &wdgcfg);
  59. #endif
  60. }
  61. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  62. #ifndef PLANCK_WATCHDOG_DISABLE
  63. // reset watchdog
  64. wdgReset(&WDGD1);
  65. #endif
  66. bool changed = false;
  67. // actual matrix
  68. for (int col = 0; col < MATRIX_COLS; col++) {
  69. matrix_row_t data = 0;
  70. // strobe col
  71. writePinHigh(matrix_col_pins[col]);
  72. // need wait to settle pin state
  73. wait_us(20);
  74. // read row data
  75. for (int row = 0; row < MATRIX_ROWS; row++) {
  76. data |= (readPin(matrix_row_pins[row]) << row);
  77. }
  78. // unstrobe col
  79. writePinLow(matrix_col_pins[col]);
  80. if (matrix_inverted[col] != data) {
  81. matrix_inverted[col] = data;
  82. }
  83. }
  84. for (int row = 0; row < MATRIX_ROWS; row++) {
  85. matrix_row_t old = current_matrix[row];
  86. current_matrix[row] = 0;
  87. for (int col = 0; col < MATRIX_COLS; col++) {
  88. current_matrix[row] |= ((matrix_inverted[col] & (1 << row) ? 1 : 0) << col);
  89. }
  90. changed |= old != current_matrix[row];
  91. }
  92. return changed;
  93. }
  94. uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
  95. pin_t pin = pad_b ? B13: B12;
  96. setPinInputHigh(pin);
  97. writePinLow(matrix_row_pins[index]);
  98. wait_us(10);
  99. uint8_t ret = readPin(pin) ? 1 : 0;
  100. setPinInputLow(matrix_row_pins[index]);
  101. setPinInputLow(pin);
  102. return ret;
  103. }