ec_switch_matrix.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_0);
  69. writePinLow(APLEX_EN_PIN_0);
  70. setPinOutput(APLEX_EN_PIN_1);
  71. writePinLow(APLEX_EN_PIN_1);
  72. return 0;
  73. }
  74. int ecsm_update(ecsm_config_t const* const ecsm_config) {
  75. // Save config
  76. config = *ecsm_config;
  77. return 0;
  78. }
  79. // Read the capacitive sensor value
  80. uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
  81. uint16_t sw_value = 0;
  82. // Select the multiplexer
  83. if (channel == 0) {
  84. writePinHigh(APLEX_EN_PIN_0);
  85. select_mux(col);
  86. writePinLow(APLEX_EN_PIN_0);
  87. } else {
  88. writePinHigh(APLEX_EN_PIN_1);
  89. select_mux(col);
  90. writePinLow(APLEX_EN_PIN_1);
  91. }
  92. // Set strobe pins to low state
  93. writePinLow(row_pins[row]);
  94. ATOMIC_BLOCK_FORCEON {
  95. // Set the row pin to high state and have capacitor charge
  96. charge_capacitor(row);
  97. // Read the ADC value
  98. sw_value = adc_read(adcMux);
  99. }
  100. // Discharge peak hold capacitor
  101. discharge_capacitor();
  102. // Waiting for the ghost capacitor to discharge fully
  103. wait_us(DISCHARGE_TIME);
  104. return sw_value;
  105. }
  106. // Update press/release state of key
  107. bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
  108. bool current_state = (*current_row >> col) & 1;
  109. // Press to release
  110. if (current_state && sw_value < config.ecsm_actuation_threshold) {
  111. *current_row &= ~(1 << col);
  112. return true;
  113. }
  114. // Release to press
  115. if ((!current_state) && sw_value > config.ecsm_release_threshold) {
  116. *current_row |= (1 << col);
  117. return true;
  118. }
  119. return false;
  120. }
  121. // Scan key values and update matrix state
  122. bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
  123. bool updated = false;
  124. // Disable AMUX of channel 1
  125. writePinHigh(APLEX_EN_PIN_1);
  126. for (int col = 0; col < sizeof(col_channels); col++) {
  127. for (int row = 0; row < MATRIX_ROWS; row++) {
  128. ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
  129. updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
  130. }
  131. }
  132. // Disable AMUX of channel 1
  133. writePinHigh(APLEX_EN_PIN_0);
  134. for (int col = 0; col < (sizeof(col_channels) - 1); col++) {
  135. for (int row = 0; row < MATRIX_ROWS; row++) {
  136. ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
  137. updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
  138. }
  139. }
  140. return updated;
  141. }
  142. // Debug print key values
  143. void ecsm_print_matrix(void) {
  144. for (int row = 0; row < MATRIX_ROWS; row++) {
  145. for (int col = 0; col < MATRIX_COLS; col++) {
  146. uprintf("%4d", ecsm_sw_value[row][col]);
  147. if (col < (MATRIX_COLS - 1)) {
  148. print(",");
  149. }
  150. }
  151. print("\n");
  152. }
  153. print("\n");
  154. }