ec_switch_matrix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright 2023 Cipulot
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "ec_switch_matrix.h"
  17. #include "analog.h"
  18. #include "atomic_util.h"
  19. #include "print.h"
  20. #include "wait.h"
  21. /* Pin and port array */
  22. const uint32_t row_pins[] = MATRIX_ROW_PINS;
  23. const uint8_t col_channels[] = MATRIX_COL_CHANNELS;
  24. const uint32_t mux_sel_pins[] = MUX_SEL_PINS;
  25. static ecsm_config_t config;
  26. static uint16_t ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];
  27. static adc_mux adcMux;
  28. static inline void discharge_capacitor(void) {
  29. writePinLow(DISCHARGE_PIN);
  30. }
  31. static inline void charge_capacitor(uint8_t row) {
  32. writePinHigh(DISCHARGE_PIN);
  33. writePinHigh(row_pins[row]);
  34. }
  35. static inline void init_mux_sel(void) {
  36. for (int idx = 0; idx < 3; idx++) {
  37. setPinOutput(mux_sel_pins[idx]);
  38. }
  39. }
  40. static inline void select_mux(uint8_t col) {
  41. uint8_t ch = col_channels[col];
  42. writePin(mux_sel_pins[0], ch & 1);
  43. writePin(mux_sel_pins[1], ch & 2);
  44. writePin(mux_sel_pins[2], ch & 4);
  45. }
  46. static inline void init_row(void) {
  47. for (int idx = 0; idx < MATRIX_ROWS; idx++) {
  48. setPinOutput(row_pins[idx]);
  49. writePinLow(row_pins[idx]);
  50. }
  51. }
  52. /* Initialize the peripherals pins */
  53. int ecsm_init(ecsm_config_t const* const ecsm_config) {
  54. // Initialize config
  55. config = *ecsm_config;
  56. palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
  57. adcMux = pinToMux(ANALOG_PORT);
  58. //Dummy call to make sure that adcStart() has been called in the appropriate state
  59. adc_read(adcMux);
  60. // Initialize discharge pin as discharge mode
  61. writePinLow(DISCHARGE_PIN);
  62. setPinOutputOpenDrain(DISCHARGE_PIN);
  63. // Initialize drive lines
  64. init_row();
  65. // Initialize multiplexer select pin
  66. init_mux_sel();
  67. // Enable AMUX
  68. setPinOutput(APLEX_EN_PIN);
  69. writePinLow(APLEX_EN_PIN);
  70. return 0;
  71. }
  72. int ecsm_update(ecsm_config_t const* const ecsm_config) {
  73. // Save config
  74. config = *ecsm_config;
  75. return 0;
  76. }
  77. // Read the capacitive sensor value
  78. uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
  79. uint16_t sw_value = 0;
  80. // Select the multiplexer
  81. writePinHigh(APLEX_EN_PIN);
  82. select_mux(col);
  83. writePinLow(APLEX_EN_PIN);
  84. // Set strobe pins to low state
  85. writePinLow(row_pins[row]);
  86. ATOMIC_BLOCK_FORCEON {
  87. // Set the row pin to high state and have capacitor charge
  88. charge_capacitor(row);
  89. // Read the ADC value
  90. sw_value = adc_read(adcMux);
  91. }
  92. // Discharge peak hold capacitor
  93. discharge_capacitor();
  94. // Waiting for the ghost capacitor to discharge fully
  95. wait_us(DISCHARGE_TIME);
  96. return sw_value;
  97. }
  98. // Update press/release state of key
  99. bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
  100. bool current_state = (*current_row >> col) & 1;
  101. // Press to release
  102. if (current_state && sw_value < config.ecsm_actuation_threshold) {
  103. *current_row &= ~(1 << col);
  104. return true;
  105. }
  106. // Release to press
  107. if ((!current_state) && sw_value > config.ecsm_release_threshold) {
  108. *current_row |= (1 << col);
  109. return true;
  110. }
  111. return false;
  112. }
  113. // Scan key values and update matrix state
  114. bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
  115. bool updated = false;
  116. for (int col = 0; col < sizeof(col_channels); col++) {
  117. for (int row = 0; row < MATRIX_ROWS; row++) {
  118. ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
  119. updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
  120. }
  121. }
  122. return updated;
  123. }
  124. // Debug print key values
  125. void ecsm_print_matrix(void) {
  126. for (int row = 0; row < MATRIX_ROWS; row++) {
  127. for (int col = 0; col < MATRIX_COLS; col++) {
  128. uprintf("%4d", ecsm_sw_value[row][col]);
  129. if (col < (MATRIX_COLS - 1)) {
  130. print(",");
  131. }
  132. }
  133. print("\n");
  134. }
  135. print("\n");
  136. }