logo

qmk_firmware

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

matrix.c (4116B)


  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 "matrix.h"
  18. #include <hal_pal.h>
  19. #include <math.h>
  20. #include "wait.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. #if !defined(PLANCK_WATCHDOG_TIMEOUT)
  32. # define PLANCK_WATCHDOG_TIMEOUT 1.0
  33. #endif
  34. /* matrix state(1:on, 0:off) */
  35. static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  36. static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  37. static matrix_row_t matrix_inverted[MATRIX_COLS];
  38. void matrix_init_custom(void) {
  39. // actual matrix setup - cols
  40. for (int i = 0; i < MATRIX_COLS; i++) {
  41. gpio_set_pin_output(matrix_col_pins[i]);
  42. gpio_write_pin_low(matrix_col_pins[i]);
  43. }
  44. // rows
  45. for (int i = 0; i < MATRIX_ROWS; i++) {
  46. gpio_set_pin_input_low(matrix_row_pins[i]);
  47. }
  48. // encoder A & B setup
  49. gpio_set_pin_input_low(B12);
  50. gpio_set_pin_input_low(B13);
  51. #ifndef PLANCK_WATCHDOG_DISABLE
  52. wdgInit();
  53. static WDGConfig wdgcfg;
  54. wdgcfg.pr = STM32_IWDG_PR_S(PLANCK_WATCHDOG_TIMEOUT);
  55. wdgcfg.rlr = STM32_IWDG_RL_S(PLANCK_WATCHDOG_TIMEOUT);
  56. wdgcfg.winr = STM32_IWDG_WIN_DISABLED;
  57. wdgStart(&WDGD1, &wdgcfg);
  58. #endif
  59. }
  60. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  61. #ifndef PLANCK_WATCHDOG_DISABLE
  62. // reset watchdog
  63. wdgReset(&WDGD1);
  64. #endif
  65. bool changed = false;
  66. // actual matrix
  67. for (int col = 0; col < MATRIX_COLS; col++) {
  68. matrix_row_t data = 0;
  69. // strobe col
  70. gpio_write_pin_high(matrix_col_pins[col]);
  71. // need wait to settle pin state
  72. wait_us(20);
  73. // read row data
  74. for (int row = 0; row < MATRIX_ROWS; row++) {
  75. data |= (gpio_read_pin(matrix_row_pins[row]) << row);
  76. }
  77. // unstrobe col
  78. gpio_write_pin_low(matrix_col_pins[col]);
  79. if (matrix_inverted[col] != data) {
  80. matrix_inverted[col] = data;
  81. }
  82. }
  83. for (int row = 0; row < MATRIX_ROWS; row++) {
  84. matrix_row_t old = current_matrix[row];
  85. current_matrix[row] = 0;
  86. for (int col = 0; col < MATRIX_COLS; col++) {
  87. current_matrix[row] |= ((matrix_inverted[col] & (1 << row) ? 1 : 0) << col);
  88. }
  89. changed |= old != current_matrix[row];
  90. }
  91. return changed;
  92. }
  93. #if defined(ENCODER_ENABLE) || defined(ENCODER_MAP_ENABLE)
  94. #if !defined(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY)
  95. # define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 10
  96. #endif
  97. void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
  98. }
  99. uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
  100. pin_t col_pin = pad_b ? B13 : B12;
  101. gpio_set_pin_output(col_pin);
  102. gpio_write_pin_high(col_pin);
  103. wait_us(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY);
  104. uint8_t ret = gpio_read_pin(matrix_row_pins[index]) ? 0 : 1;
  105. gpio_set_pin_input_low(col_pin);
  106. return ret;
  107. }
  108. #endif // ENCODER_ENABLE || ENCODER_MAP_ENABLE