Skip to content

Commit

Permalink
- software debounce the irq lines
Browse files Browse the repository at this point in the history
- change safety relay pin PB6 -> PB5
- add c1 revision
  • Loading branch information
vegano1 committed Jun 14, 2024
1 parent e48b8b5 commit 3f670bf
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 45 deletions.
4 changes: 2 additions & 2 deletions bootloader/firmware/stm32G4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
7 changes: 3 additions & 4 deletions hepa-uv/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
Expand Down
31 changes: 20 additions & 11 deletions hepa-uv/firmware/main_rev1.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <array>
#include <optional>

// clang-format off
#include "FreeRTOS.h"
Expand Down Expand Up @@ -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<gpio::PinConfig> safety_relay_active =
std::nullopt;
#else
static std::optional<gpio::PinConfig> 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{
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions hepa-uv/firmware/utility_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 0 additions & 6 deletions include/common/core/wtf.hpp

This file was deleted.

47 changes: 30 additions & 17 deletions include/hepa-uv/core/uv_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion include/hepa-uv/firmware/gpio_drive_hardware.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <optional>

#include "common/firmware/gpio.hpp"

namespace gpio_drive_hardware {
Expand All @@ -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<gpio::PinConfig> safety_relay_active = std::nullopt;
};

} // namespace gpio_drive_hardware
4 changes: 2 additions & 2 deletions include/hepa-uv/firmware/utility_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f670bf

Please sign in to comment.