From b22c53e89422ec70e340fbc1f2c8a66e99c3ef12 Mon Sep 17 00:00:00 2001 From: Roy_01 Date: Thu, 17 Jul 2025 16:18:21 +0800 Subject: [PATCH] feat: new hot_swap_monitor --- app/inc/pel_v3_0.h | 2 +- app/src/pel_v3_0.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/app/inc/pel_v3_0.h b/app/inc/pel_v3_0.h index 468bdd3..773f9ec 100644 --- a/app/inc/pel_v3_0.h +++ b/app/inc/pel_v3_0.h @@ -14,7 +14,7 @@ extern "C" #define VERSION_DATE_MONTH 6 #define VERSION_DATE_DAY 27 #define VERSION_DATE_HOUR 17 -#define VERSION_DATE_MINUTE 13 +#define VERSION_DATE_MINUTE 15 #define PEL_0P5R_MASK (0x01 << 0) #define PEL_1P0R_MASK (0x01 << 1) diff --git a/app/src/pel_v3_0.c b/app/src/pel_v3_0.c index cb47bd6..6c5a945 100644 --- a/app/src/pel_v3_0.c +++ b/app/src/pel_v3_0.c @@ -22,6 +22,73 @@ #if (DEF_ELITE_MODEL == DEF_ELITE_PEL_V3_0) +#include "FreeRTOS.h" +#include "nrfx_gpiote.h" +#include "timers.h" +#define DEBOUNCE_DELAY_MS 30 +#define HOT_SWAP_IS_CONNECTED 0 +#define VCC_DUT1_output_en() nrf_gpio_pin_set(HOT_SWAP_1_PIN) +#define VCC_DUT1_output_dis() nrf_gpio_pin_clear(HOT_SWAP_1_PIN) +static TimerHandle_t debounce_timer = NULL; + +static void debounce_timer_callback(TimerHandle_t xTimer) +{ + bool state = nrf_gpio_pin_read(HOT_SWAP_SIGNAL_PIN); + + if (state == HOT_SWAP_IS_CONNECTED) + { + VCC_DUT1_output_en(); + } + else + { + VCC_DUT1_output_dis(); + } +} + +static void hot_swap_gpiote_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) +{ + // only handle timer, not handle GPIO + if (debounce_timer != NULL) + { + // reset and start debounce timer + xTimerStopFromISR(debounce_timer, NULL); + xTimerStartFromISR(debounce_timer, NULL); + } +} + +void hot_swap_monitor_init(void) +{ + nrfx_gpiote_in_config_t config = { + .hi_accuracy = 0, + .is_watcher = 0, + .pull = NRF_GPIO_PIN_PULLUP, + .sense = NRF_GPIOTE_POLARITY_TOGGLE, + .skip_gpio_setup = 0 + }; + + nrfx_gpiote_init(); + nrfx_gpiote_in_init(HOT_SWAP_SIGNAL_PIN, &config, hot_swap_gpiote_handler); + nrfx_gpiote_in_event_enable(HOT_SWAP_SIGNAL_PIN, true); + + debounce_timer = xTimerCreate("debounce", pdMS_TO_TICKS(DEBOUNCE_DELAY_MS), pdFALSE, NULL, debounce_timer_callback); + if (debounce_timer == NULL) + { + NRF_LOG_ERROR("Failed to create debounce timer"); + return; + } + + // initial HOT_SWAP_1_PIN + bool state = nrf_gpio_pin_read(HOT_SWAP_SIGNAL_PIN); + if (state == HOT_SWAP_IS_CONNECTED) + { + VCC_DUT1_output_en(); + } + else + { + VCC_DUT1_output_dis(); + } +} + typedef struct { uint32_t pattern_index; @@ -887,6 +954,8 @@ const elite_instance_t *pel_init(void) ASSERT(xTaskCreate(pel_scan_mode_task, "pel_scan_mode", 512, NULL, 4, NULL)); + hot_swap_monitor_init(); + return &pel_elite_instance; }