VCC undervoltage check module

pull/265/head
Alex Voinea 2023-03-06 08:58:05 +01:00
parent 260c933c36
commit ba86dbd806
6 changed files with 64 additions and 0 deletions

View File

@ -42,6 +42,14 @@ static constexpr const uint16_t buttonADCLimits[buttonCount][2] = { { 0, 50 }, {
static constexpr const uint16_t buttonADCMaxValue = 1023; ///< used in unit tests
static constexpr const uint8_t buttonsADCIndex = 5; ///< ADC index of buttons input
// VCC measurement setup
static constexpr const uint8_t VCCADCIndex = 30; ///< ADC index of scaled VCC input
static constexpr const uint16_t VCCADCThreshold = 274; ///< ADC value for triggering the UV_VCC error
/// We are measuring the bandgap voltage, Vb=1.1V.
/// To compute the threshold value: `VAL = 1125.3 / AVCC`
/// So for AVCC=4.1V, you get VAL=274.46
static constexpr const uint8_t VCCADCReadCnt = 10; ///< Number of ADC reads to perform, only the last one being used
// Motion and planning
/// Do not plan moves equal or shorter than the requested steps

View File

@ -118,4 +118,6 @@ enum class ErrorCode : uint_fast16_t {
/// Unfixable possible cause: bad or cracked solder joints on the PCB, failed shift register, failed driver.
MMU_SOLDERING_NEEDS_ATTENTION = 0xC200,
/// MCU VCC rail undervoltage.
MCU_UNDERVOLTAGE_VCC = 0xC400,
};

View File

@ -24,6 +24,7 @@
#include "modules/timebase.h"
#include "modules/motion.h"
#include "modules/usb_cdc.h"
#include "modules/undervoltage_check.h"
#include "application.h"
@ -167,6 +168,7 @@ void loop() {
}
hal::cpu::Step();
mu::cdc.Step();
muv::uv_vcc.Step();
application.Step();

View File

@ -18,6 +18,7 @@ target_sources(
serial.cpp
speed_table.cpp
timebase.cpp
undervoltage_check.cpp
usb_cdc.cpp
user_input.cpp
)

View File

@ -0,0 +1,26 @@
/// @file undervoltage_check.cpp
#include "undervoltage_check.h"
#include "../hal/adc.h"
#include "../logic/error_codes.h"
#include "../panic.h"
namespace modules {
namespace undervoltage_check {
Undervoltage_check uv_vcc;
void Undervoltage_check::Step() {
uint16_t vcc_val;
// dummy reads are so that the final measurement is valid
for (uint8_t i = 0; i < config::VCCADCReadCnt; i++) {
vcc_val = hal::adc::ReadADC(config::VCCADCIndex);
}
if (vcc_val > config::VCCADCThreshold) {
Panic(ErrorCode::MCU_UNDERVOLTAGE_VCC);
}
}
} // namespace undervoltage_check
} // namespace modules

View File

@ -0,0 +1,25 @@
/// @file undervoltage_check.h
#pragma once
#include <stdint.h>
#include "../config/config.h"
/// The modules namespace contains models of MMU's components
namespace modules {
namespace undervoltage_check {
class Undervoltage_check {
public:
inline constexpr Undervoltage_check() = default;
/// Reads the ADC, checks the value
void Step();
};
/// The one and only instance of Undervoltage_check in the FW
extern Undervoltage_check uv_vcc;
} // namespace undervoltage_check
} // namespace modules
namespace muv = modules::undervoltage_check;