Skip to content

Commit

Permalink
- renamed UV_NO_MCU_PIN to nUV_PRESSED_PIN for accuracy
Browse files Browse the repository at this point in the history
- added nSAFETY_ACTIVE_MCU input pin to know the state of the safety relay
- added internal safety_relay_active flag in uv_task to keep track of safety relay state
- update safety_relay_active state from the nSAFETY_ACTIVE_MCU gpis whenever uv_task handles irq.
- added safety_relay_active flag to HepaUVStateResponse
- check the safety_relay_active state after turning on the uv light
- send safety_relay_inactive 0x11 error code if the relay is not active
- added ot_utils::freertos_sleep::sleep to cpp-utils to sleep the task for n milliseconds
  • Loading branch information
vegano1 committed May 31, 2024
1 parent fb2308b commit e48b8b5
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 15 deletions.
12 changes: 12 additions & 0 deletions cpp-utils/include/ot_utils/freertos/freertos_sleep.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "FreeRTOS.h"
#include "task.h"

namespace ot_utils {
namespace freertos_sleep {

static void sleep(uint32_t time_ms) { if (time_ms > 0) vTaskDelay(pdMS_TO_TICKS(time_ms)); }

} // namespace freertos_sleep
} // namespace ot_utils
14 changes: 10 additions & 4 deletions hepa-uv/firmware/main_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static auto gpio_drive_pins = gpio_drive_hardware::GpioDrivePins{
.uv_push_button =
gpio::PinConfig{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = UV_NO_MCU_PORT,
.pin = UV_NO_MCU_PIN,
.port = nUV_PRESSED_PORT,
.pin = nUV_PRESSED_PIN,
},
.hepa_on_off =
gpio::PinConfig{
Expand All @@ -127,7 +127,13 @@ static auto gpio_drive_pins = gpio_drive_hardware::GpioDrivePins{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = UV_ON_OFF_MCU_PORT,
.pin = UV_ON_OFF_MCU_PIN,
.active_setting = UV_ON_OFF_AS}};
.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},
};

static auto& hepauv_queues = hepauv_tasks::get_main_queues();

Expand All @@ -140,7 +146,7 @@ extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
case DOOR_OPEN_MCU_PIN:
case REED_SW_MCU_PIN:
case HEPA_NO_MCU_PIN:
case UV_NO_MCU_PIN:
case nUV_PRESSED_PIN:
if (hepauv_queues.hepa_queue != nullptr) {
static_cast<void>(hepauv_queues.hepa_queue->try_write_isr(
GPIOInterruptChanged{.pin = GPIO_Pin}));
Expand Down
21 changes: 19 additions & 2 deletions hepa-uv/firmware/utility_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ void uv_push_button_input_gpio_init(void) {
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pin UV_NO_MCU : PC2 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = UV_NO_MCU_PIN;
GPIO_InitStruct.Pin = nUV_PRESSED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(UV_NO_MCU_PORT, &GPIO_InitStruct);
HAL_GPIO_Init(nUV_PRESSED_PORT, &GPIO_InitStruct);
}

/**
Expand Down Expand Up @@ -134,6 +134,22 @@ void uv_on_off_output_init() {
HAL_GPIO_Init(UV_ON_OFF_MCU_PORT, &GPIO_InitStruct);
}

/**
* @brief nSAFETY_ACTIVE_MCU GPIO Initialization Function
* @param None
* @retval None
*/
void safety_relay_active_input_init() {
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin nSAFETY_ACTIVE_MCU: PB6 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = nSAFETY_ACTIVE_MCU_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(nSAFETY_ACTIVE_MCU_PORT, &GPIO_InitStruct);
}

/**
* @brief NVIC EXTI interrupt priority Initialization
* @param None
Expand Down Expand Up @@ -168,4 +184,5 @@ void utility_gpio_init(void) {
hepa_on_off_output_init();
uv_push_button_input_gpio_init();
uv_on_off_output_init();
safety_relay_active_input_init();
}
1 change: 1 addition & 0 deletions include/bootloader/core/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ typedef enum {
can_errorcode_over_pressure = 0xd,
can_errorcode_door_open = 0xe,
can_errorcode_reed_open = 0xf,
can_errorcode_safety_relay_inactive = 0x11,
} CANErrorCode;

/** Tool types detected on Head. */
Expand Down
1 change: 1 addition & 0 deletions include/can/core/ids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ enum class ErrorCode {
over_pressure = 0xd,
door_open = 0xe,
reed_open = 0xf,
safety_relay_inactive = 0x11,
};

/** Error Severity levels. */
Expand Down
2 changes: 2 additions & 0 deletions include/can/core/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ struct GetHepaUVStateResponse
uint8_t uv_light_on;
uint32_t remaining_time_s;
uint16_t uv_current_ma;
uint8_t safety_relay_active;

template <bit_utils::ByteIterator Output, typename Limit>
auto serialize(Output body, Limit limit) const -> uint8_t {
Expand All @@ -1720,6 +1721,7 @@ struct GetHepaUVStateResponse
iter = bit_utils::int_to_bytes(uv_light_on, iter, limit);
iter = bit_utils::int_to_bytes(remaining_time_s, iter, limit);
iter = bit_utils::int_to_bytes(uv_current_ma, iter, limit);
iter = bit_utils::int_to_bytes(safety_relay_active, iter, limit);
return iter - body;
}

Expand Down
6 changes: 6 additions & 0 deletions include/common/core/wtf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <stdio.h>


void something(uint32_t time_ms) { printf("something %lu", time_ms); }
35 changes: 33 additions & 2 deletions include/hepa-uv/core/uv_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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"

namespace uv_task {

Expand Down Expand Up @@ -38,6 +39,7 @@ class UVMessageHandler {
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);
// turn off UV Ballast
gpio::reset(drive_pins.uv_on_off);
}
Expand Down Expand Up @@ -76,6 +78,9 @@ class UVMessageHandler {
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);
}
Expand All @@ -95,7 +100,8 @@ class UVMessageHandler {
.timeout_s = uv_off_timeout_s,
.uv_light_on = uv_light_on,
.remaining_time_s = (_timer.get_remaining_time() / 1000),
.uv_current_ma = uv_current_ma};
.uv_current_ma = uv_current_ma,
.safety_relay_active = safety_relay_active};
can_client.send_can_message(can::ids::NodeId::host, resp);
}

Expand Down Expand Up @@ -153,15 +159,40 @@ class UVMessageHandler {
if (_timer.is_running()) _timer.stop();
}

// Update the voltage usage of the uv light
// 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);
uv_current_ma = uv_hardware.get_uv_light_current();
if (!safety_relay_active) {
// we tried to set the uv light, but the relay is not active
if (_timer.is_running()) {
gpio::reset(drive_pins.uv_on_off);
_timer.stop();
led_control_client.send_led_control_message(
led_control_task_messages::PushButtonLED(UV_BUTTON, 0, 0,
50, 0));
}
// send error
auto msg = can::messages::ErrorMessage{
.message_index = 0,
.severity = can::ids::ErrorSeverity::warning,
.error_code = can::ids::ErrorCode::safety_relay_inactive,
};
can_client.send_can_message(can::ids::NodeId::host, msg);

uv_push_button = false;
uv_light_on = false;
uv_current_ma = 0;
return;
}

// TODO: send state change CAN message to host
}

// state tracking variables
bool door_closed = false;
bool reed_switch_set = false;
bool safety_relay_active = false;
bool uv_push_button = false;
bool uv_light_on = false;
uint32_t uv_off_timeout_s = DELAY_S;
Expand Down
1 change: 1 addition & 0 deletions include/hepa-uv/firmware/gpio_drive_hardware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct GpioDrivePins {
gpio::PinConfig uv_push_button;
gpio::PinConfig hepa_on_off;
gpio::PinConfig uv_on_off;
gpio::PinConfig safety_relay_active;
};

} // namespace gpio_drive_hardware
15 changes: 8 additions & 7 deletions include/hepa-uv/firmware/utility_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ void utility_gpio_init();
#define UV_ON_OFF_MCU_PORT GPIOA
#define UV_ON_OFF_MCU_PIN GPIO_PIN_4
#define UV_ON_OFF_AS GPIO_PIN_RESET
// UV_NO_MCU PC2
#define UV_NO_MCU_PORT GPIOC
#define UV_NO_MCU_PIN GPIO_PIN_2
// UV_ADC PA3
#define UV_ADC_PORT GPIOC
#define UV_ADC_PIN GPIO_PIN_3
// nUV_PRESSED PC2
#define nUV_PRESSED_PORT GPIOC
#define nUV_PRESSED_PIN GPIO_PIN_2
// UV_B_CTRL PC5
#define UV_B_CTRL_PORT GPIOC
#define UV_B_CTRL_PIN GPIO_PIN_5
Expand All @@ -105,4 +102,8 @@ void utility_gpio_init();
#define UV_R_CTRL_PIN GPIO_PIN_1
// UV_W_CTRL PB2
#define UV_W_CTRL_PORT GPIOB
#define UV_W_CTRL_PIN GPIO_PIN_2
#define UV_W_CTRL_PIN GPIO_PIN_2
// nSAFETY_ACTIVE_MCU PB6
#define nSAFETY_ACTIVE_MCU_PORT GPIOB
#define nSAFETY_ACTIVE_MCU_PIN GPIO_PIN_6
#define nSAFETY_ACTIVE_AS GPIO_PIN_RESET

0 comments on commit e48b8b5

Please sign in to comment.