From 3f670bfde52d8be95af27533b84f226151ae959d Mon Sep 17 00:00:00 2001 From: vegano1 Date: Fri, 14 Jun 2024 16:29:49 -0400 Subject: [PATCH] - software debounce the irq lines - change safety relay pin PB6 -> PB5 - add c1 revision --- bootloader/firmware/stm32G4/CMakeLists.txt | 4 +- hepa-uv/firmware/CMakeLists.txt | 7 ++- hepa-uv/firmware/main_rev1.cpp | 31 +++++++----- hepa-uv/firmware/utility_gpio.c | 4 +- include/common/core/wtf.hpp | 6 --- include/hepa-uv/core/uv_task.hpp | 47 ++++++++++++------- .../hepa-uv/firmware/gpio_drive_hardware.hpp | 4 +- include/hepa-uv/firmware/utility_gpio.h | 4 +- 8 files changed, 62 insertions(+), 45 deletions(-) delete mode 100644 include/common/core/wtf.hpp diff --git a/bootloader/firmware/stm32G4/CMakeLists.txt b/bootloader/firmware/stm32G4/CMakeLists.txt index fc7625e56..7fc4988d5 100644 --- a/bootloader/firmware/stm32G4/CMakeLists.txt +++ b/bootloader/firmware/stm32G4/CMakeLists.txt @@ -170,8 +170,8 @@ endmacro() foreach_revision( PROJECT_NAME bootloader-hepa-uv CALL_FOREACH_REV hepauv_bootloader_loop - REVISIONS a1 b1 - SOURCES hepauv_sources hepauv_sources + REVISIONS a1 b1 c1 + SOURCES hepauv_sources hepauv_sources hepauv_sources NO_CREATE_IMAGE_HEX NO_CREATE_INSTALL_RULES ) diff --git a/hepa-uv/firmware/CMakeLists.txt b/hepa-uv/firmware/CMakeLists.txt index 509def804..f6f1d6eaa 100644 --- a/hepa-uv/firmware/CMakeLists.txt +++ b/hepa-uv/firmware/CMakeLists.txt @@ -44,7 +44,6 @@ set(HEPAUV_SRCS_A1 ) set(HEPAUV_SRCS_B1 ${HEPAUV_SRCS_A1}) set(HEPAUV_SRCS_C1 ${HEPAUV_SRCS_B1}) -set(HEPAUV_SRCS_C2 ${HEPAUV_SRCS_C1}) macro(hepa_uv_loop) set(_driver_suffix ${PROJECT_NAME}_${REVISION}) @@ -84,12 +83,12 @@ endmacro() foreach_revision( PROJECT_NAME hepa-uv - REVISIONS a1 b1 - SOURCES HEPAUV_SRCS_A1 HEPAUV_SRCS_B1 + REVISIONS a1 b1 c1 + SOURCES HEPAUV_SRCS_A1 HEPAUV_SRCS_B1 HEPAUV_SRCS_C1 CALL_FOREACH_REV hepa_uv_loop) alias_for_revision(PROJECT_NAME hepa-uv REVISION a1 REVISION_ALIAS proto) -alias_for_revision(PROJECT_NAME hepa-uv REVISION b1 REVISION_ALIAS rev1) +alias_for_revision(PROJECT_NAME hepa-uv REVISION c1 REVISION_ALIAS rev1) add_clang_tidy_target( TARGET_NAME hepa-uv-lint diff --git a/hepa-uv/firmware/main_rev1.cpp b/hepa-uv/firmware/main_rev1.cpp index a3cc0f502..a724e3fc7 100644 --- a/hepa-uv/firmware/main_rev1.cpp +++ b/hepa-uv/firmware/main_rev1.cpp @@ -1,4 +1,5 @@ #include +#include // clang-format off #include "FreeRTOS.h" @@ -92,6 +93,17 @@ class EEPromHardwareInterface }; static auto eeprom_hw_iface = EEPromHardwareInterface(); +#if PCB_PRIMARY_REVISION == 'a' || PCB_PRIMARY_REVISION == 'b' +static constexpr std::optional safety_relay_active = + std::nullopt; +#else +static std::optional safety_relay_active = + gpio::PinConfig{// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) + .port = nSAFETY_ACTIVE_MCU_PORT, + .pin = nSAFETY_ACTIVE_MCU_PIN, + .active_setting = nSAFETY_ACTIVE_AS}; +#endif + static auto gpio_drive_pins = gpio_drive_hardware::GpioDrivePins{ .door_open = gpio::PinConfig{ @@ -123,17 +135,14 @@ static auto gpio_drive_pins = gpio_drive_hardware::GpioDrivePins{ .port = HEPA_ON_OFF_PORT, .pin = HEPA_ON_OFF_PIN, .active_setting = HEPA_ON_OFF_AS}, - .uv_on_off = gpio::PinConfig{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) - .port = UV_ON_OFF_MCU_PORT, - .pin = UV_ON_OFF_MCU_PIN, - .active_setting = UV_ON_OFF_AS}, - .safety_relay_active = gpio::PinConfig{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) - .port = nSAFETY_ACTIVE_MCU_PORT, - .pin = nSAFETY_ACTIVE_MCU_PIN, - .active_setting = nSAFETY_ACTIVE_AS}, - }; + .uv_on_off = + gpio::PinConfig{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) + .port = UV_ON_OFF_MCU_PORT, + .pin = UV_ON_OFF_MCU_PIN, + .active_setting = UV_ON_OFF_AS}, + .safety_relay_active = safety_relay_active, +}; static auto& hepauv_queues = hepauv_tasks::get_main_queues(); diff --git a/hepa-uv/firmware/utility_gpio.c b/hepa-uv/firmware/utility_gpio.c index 35d3e2485..3ab288d69 100644 --- a/hepa-uv/firmware/utility_gpio.c +++ b/hepa-uv/firmware/utility_gpio.c @@ -142,10 +142,10 @@ void uv_on_off_output_init() { void safety_relay_active_input_init() { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); - /*Configure GPIO pin nSAFETY_ACTIVE_MCU: PB6 */ + /*Configure GPIO pin nSAFETY_ACTIVE_MCU: PB5 */ GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = nSAFETY_ACTIVE_MCU_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(nSAFETY_ACTIVE_MCU_PORT, &GPIO_InitStruct); } diff --git a/include/common/core/wtf.hpp b/include/common/core/wtf.hpp deleted file mode 100644 index 4a539f642..000000000 --- a/include/common/core/wtf.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include - - -void something(uint32_t time_ms) { printf("something %lu", time_ms); } diff --git a/include/hepa-uv/core/uv_task.hpp b/include/hepa-uv/core/uv_task.hpp index 586dd2002..cc3fb1658 100644 --- a/include/hepa-uv/core/uv_task.hpp +++ b/include/hepa-uv/core/uv_task.hpp @@ -9,14 +9,15 @@ #include "hepa-uv/core/messages.hpp" #include "hepa-uv/firmware/gpio_drive_hardware.hpp" #include "hepa-uv/firmware/uv_control_hardware.hpp" -#include "ot_utils/freertos/freertos_timer.hpp" #include "ot_utils/freertos/freertos_sleep.hpp" +#include "ot_utils/freertos/freertos_timer.hpp" namespace uv_task { // How long to keep the UV light on in seconds. static constexpr uint32_t DELAY_S = 60 * 15; // 15 minutes static constexpr uint32_t MAX_DELAY_S = 60 * 60; // 1hr max timeout +static constexpr uint32_t DEBOUNCE_MS = 250; // button debounce using TaskMessage = uv_task_messages::TaskMessage; @@ -34,12 +35,17 @@ class UVMessageHandler { can_client{can_client}, _timer( "UVTask", [ThisPtr = this] { ThisPtr->timer_callback(); }, - DELAY_S * 1000) { + DELAY_S * 1000), + debounce_timer( + "UVTaskDebounce", [ThisPtr = this] { ThisPtr->debounce_cb(); }, + DEBOUNCE_MS) { // get current state uv_push_button = gpio::is_set(drive_pins.uv_push_button); door_closed = gpio::is_set(drive_pins.door_open); reed_switch_set = gpio::is_set(drive_pins.reed_switch); - safety_relay_active = gpio::is_set(drive_pins.safety_relay_active); + if (drive_pins.safety_relay_active.has_value()) + safety_relay_active = + gpio::is_set(drive_pins.safety_relay_active.value()); // turn off UV Ballast gpio::reset(drive_pins.uv_on_off); } @@ -60,6 +66,12 @@ class UVMessageHandler { uv_push_button = false; } + // callback to debounce the irq signals + void debounce_cb() { + debounce_timer.stop(); + set_uv_light_state(uv_push_button, uv_off_timeout_s); + } + void visit(const std::monostate &) {} // Handle GPIO EXTI Interrupts here @@ -69,20 +81,18 @@ class UVMessageHandler { return; } + // debounce + if (debounce_timer.is_running()) return; + debounce_timer.start(); + // update states - if (m.pin == drive_pins.uv_push_button.pin) { + door_closed = gpio::is_set(drive_pins.door_open); + reed_switch_set = gpio::is_set(drive_pins.reed_switch); + if (drive_pins.safety_relay_active.has_value()) + safety_relay_active = + gpio::is_set(drive_pins.safety_relay_active.value()); + if (m.pin == drive_pins.uv_push_button.pin) uv_push_button = !uv_push_button; - } else if (m.pin == drive_pins.door_open.pin) { - door_closed = gpio::is_set(drive_pins.door_open); - } else if (m.pin == drive_pins.reed_switch.pin) { - reed_switch_set = gpio::is_set(drive_pins.reed_switch); - } - - // Always update safety relay state - safety_relay_active = gpio::is_set(drive_pins.safety_relay_active); - - // Drive the UV light - set_uv_light_state(uv_push_button, uv_off_timeout_s); } void visit(const can::messages::SetHepaUVStateRequest &m) { @@ -160,9 +170,11 @@ class UVMessageHandler { } // wait 10ms for safety relay, then update the states - ot_utils::freertos_sleep::sleep(10); - safety_relay_active = gpio::is_set(drive_pins.safety_relay_active); + ot_utils::freertos_sleep::sleep(100); uv_current_ma = uv_hardware.get_uv_light_current(); + if (drive_pins.safety_relay_active.has_value()) + safety_relay_active = + gpio::is_set(drive_pins.safety_relay_active.value()); if (!safety_relay_active) { // we tried to set the uv light, but the relay is not active if (_timer.is_running()) { @@ -203,6 +215,7 @@ class UVMessageHandler { LEDControlClient &led_control_client; CanClient &can_client; ot_utils::freertos_timer::FreeRTOSTimer _timer; + ot_utils::freertos_timer::FreeRTOSTimer debounce_timer; }; /** diff --git a/include/hepa-uv/firmware/gpio_drive_hardware.hpp b/include/hepa-uv/firmware/gpio_drive_hardware.hpp index 1c622ccc0..040d94eca 100644 --- a/include/hepa-uv/firmware/gpio_drive_hardware.hpp +++ b/include/hepa-uv/firmware/gpio_drive_hardware.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "common/firmware/gpio.hpp" namespace gpio_drive_hardware { @@ -11,7 +13,7 @@ struct GpioDrivePins { gpio::PinConfig uv_push_button; gpio::PinConfig hepa_on_off; gpio::PinConfig uv_on_off; - gpio::PinConfig safety_relay_active; + std::optional safety_relay_active = std::nullopt; }; } // namespace gpio_drive_hardware \ No newline at end of file diff --git a/include/hepa-uv/firmware/utility_gpio.h b/include/hepa-uv/firmware/utility_gpio.h index 400f96e47..90a8cbfc4 100644 --- a/include/hepa-uv/firmware/utility_gpio.h +++ b/include/hepa-uv/firmware/utility_gpio.h @@ -103,7 +103,7 @@ void utility_gpio_init(); // UV_W_CTRL PB2 #define UV_W_CTRL_PORT GPIOB #define UV_W_CTRL_PIN GPIO_PIN_2 -// nSAFETY_ACTIVE_MCU PB6 +// nSAFETY_ACTIVE_MCU PB5 #define nSAFETY_ACTIVE_MCU_PORT GPIOB -#define nSAFETY_ACTIVE_MCU_PIN GPIO_PIN_6 +#define nSAFETY_ACTIVE_MCU_PIN GPIO_PIN_5 #define nSAFETY_ACTIVE_AS GPIO_PIN_RESET \ No newline at end of file