Add runtime VCC measurement as register

- extract logic of undervoltage handling into main.cpp
- fix unit tests
- bump version to 2.1.9 (new register)
pull/269/head
D.R.racer 2023-03-08 11:24:44 +01:00 committed by DRracer
parent 23a9978980
commit 314233b37f
20 changed files with 100 additions and 56 deletions

View File

@ -24,7 +24,7 @@
#include "modules/timebase.h"
#include "modules/motion.h"
#include "modules/usb_cdc.h"
#include "modules/undervoltage_check.h"
#include "modules/voltage.h"
#include "application.h"
@ -140,6 +140,20 @@ void Panic(ErrorCode ec) {
application.Panic(ec);
}
void RuntimeHWChecks() {
mv::vcc.Step();
if (mv::vcc.CurrentVCC() > config::VCCADCThreshold) {
// stop all motors at once
mm::motion.AbortPlannedMoves();
// kill all TMC
mm::motion.Disable(mm::Idler);
mm::motion.Disable(mm::Selector);
mm::motion.Disable(mm::Pulley);
// call for help
Panic(ErrorCode::MCU_UNDERVOLTAGE_VCC);
}
}
/// Main loop of the firmware
/// Proposed architecture
/// checkMsgs();
@ -168,7 +182,8 @@ void loop() {
}
hal::cpu::Step();
mu::cdc.Step();
muv::uv_vcc.Step();
RuntimeHWChecks();
application.Step();

View File

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

View File

@ -1,26 +0,0 @@
/// @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

@ -1,25 +0,0 @@
/// @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;

22
src/modules/voltage.cpp Normal file
View File

@ -0,0 +1,22 @@
/// @file
#include "voltage.h"
#include "../hal/adc.h"
#include "../logic/error_codes.h"
#include "../panic.h"
namespace modules {
namespace voltage {
VCC vcc;
void VCC::Step() {
uint16_t tmp;
// dummy reads are so that the final measurement is valid
for (uint8_t i = 0; i < config::VCCADCReadCnt; i++) {
tmp = hal::adc::ReadADC(config::VCCADCIndex);
}
vcc_val = tmp;
}
} // namespace voltage
} // namespace modules

40
src/modules/voltage.h Normal file
View File

@ -0,0 +1,40 @@
/// @file
#pragma once
#include <stdint.h>
#include "../config/config.h"
/// The modules namespace contains models of MMU's components
namespace modules {
namespace voltage {
/// We are measuring the bandgap voltage, Vb=1.1V
/// To compute the threshold value: `VAL = 1125.3 / AVCC`
/// So for:
/// - AVCC=5V, you get VAL=225.06
/// - AVCC=4.1V, you get VAL=274.46
/// - AVCC=4V, you get VAL=281.35
/// - any lower and the board will probably die sooner than being able to report anything
class VCC {
public:
inline constexpr VCC()
: vcc_val(0) {}
/// Reads the ADC, checks the value
void Step();
/// @returns the current VCC voltage level, platform dependent.
/// @note see VCC measurement setup in config.h
uint16_t CurrentVCC() const { return vcc_val; }
private:
uint16_t vcc_val;
};
/// The one and only instance of Undervoltage_check in the FW
extern VCC vcc;
} // namespace voltage
} // namespace modules
namespace mv = modules::voltage;

View File

@ -16,6 +16,7 @@
#include "modules/pulley.h"
#include "modules/selector.h"
#include "modules/permanent_storage.h"
#include "modules/voltage.h"
/** @defgroup register_table Register Table
*
@ -163,6 +164,7 @@
| 0x1eh 30 | uint16 | Set/Get Pulley iRun current| 0-31 | 14h 20 | 20->350mA: see TMC2130 current conversion| Read / Write | M707 A0x1e | M708 A0x1e Xn
| 0x1fh 31 | uint16 |Set/Get Selector iRun current| 0-31 | 1fh 31 | 31->530mA: see TMC2130 current conversion| Read / Write | M707 A0x1f | M708 A0x1f Xn
| 0x20h 32 | uint16 | Set/Get Idler iRun current | 0-31 | 1fh 31 | 31->530mA: see TMC2130 current conversion| Read / Write | M707 A0x20 | M708 A0x20 Xn
| 0x21h 33 | uint16 | Current VCC voltage level | 225-281 | | 225->5V, 281->4V, higher values mean lower voltage and the board would probably die sooner than reporting anything | Read only | M707 A0x21 | N/A
*/
struct RegisterFlags {
@ -394,6 +396,10 @@ static const RegisterRec registers[] /*PROGMEM*/ = {
[]() -> uint16_t { return mm::motion.CurrentsForAxis(config::Idler).iRun; },
[](uint16_t d) { mm::motion.SetIRunForAxis(config::Idler, d); },
1),
// 0x21 Current VCC voltage level R
RegisterRec(
[]() -> uint16_t { return mv::vcc.CurrentVCC(); },
2),
};
static constexpr uint8_t registersSize = sizeof(registers) / sizeof(RegisterRec);

View File

@ -17,6 +17,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -33,6 +33,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -33,6 +33,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -32,6 +32,7 @@ add_executable(
${CMAKE_SOURCE_DIR}/src/modules/selector.cpp
${CMAKE_SOURCE_DIR}/src/modules/user_input.cpp
${CMAKE_SOURCE_DIR}/src/modules/pulse_gen.cpp
${CMAKE_SOURCE_DIR}/src/modules/voltage.cpp
${MODULES_STUBS_DIR}/stub_adc.cpp
${MODULES_STUBS_DIR}/stub_cpu.cpp
${MODULES_STUBS_DIR}/stub_eeprom.cpp

View File

@ -10,6 +10,6 @@ set(PROJECT_VERSION_MINOR
CACHE STRING "Project minor version" FORCE
)
set(PROJECT_VERSION_REV
8
9
CACHE STRING "Project revision" FORCE
)