diff --git a/src/main.cpp b/src/main.cpp index cc2cf57..09b765e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 8fd33fc..f3555b0 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -18,7 +18,7 @@ target_sources( serial.cpp speed_table.cpp timebase.cpp - undervoltage_check.cpp usb_cdc.cpp user_input.cpp + voltage.cpp ) diff --git a/src/modules/undervoltage_check.cpp b/src/modules/undervoltage_check.cpp deleted file mode 100644 index db063b2..0000000 --- a/src/modules/undervoltage_check.cpp +++ /dev/null @@ -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 diff --git a/src/modules/undervoltage_check.h b/src/modules/undervoltage_check.h deleted file mode 100644 index cc9f7f6..0000000 --- a/src/modules/undervoltage_check.h +++ /dev/null @@ -1,25 +0,0 @@ -/// @file undervoltage_check.h -#pragma once - -#include -#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; diff --git a/src/modules/voltage.cpp b/src/modules/voltage.cpp new file mode 100644 index 0000000..56b623e --- /dev/null +++ b/src/modules/voltage.cpp @@ -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 diff --git a/src/modules/voltage.h b/src/modules/voltage.h new file mode 100644 index 0000000..c3cc065 --- /dev/null +++ b/src/modules/voltage.h @@ -0,0 +1,40 @@ +/// @file +#pragma once + +#include +#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; diff --git a/src/registers.cpp b/src/registers.cpp index 012ae92..f46d39d 100644 --- a/src/registers.cpp +++ b/src/registers.cpp @@ -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 * @@ -159,10 +160,11 @@ | 0x1ah 26 | uint16 | Get Pulley position | 0000h 0 | ffffh 65535 | unit mm | Read only | M707 A0x1a | N/A | 0x1bh 27 | uint16 | Set/Get_Selector_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=park pos | Read / Write | M707 A0x1b | M708 A0x1b Xn | 0x1ch 28 | uint16 | Set/Get_Idler_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=disengaged | Read / Write | M707 A0x1c | M708 A0x1c Xn -| 0x1dh 29 | uint8 | Set/Get Selector cut iRun current | 0 to 63 (aka 0-1024mA)| 31 (530mA) | | Read / Write | M707 A0x1d | M708 A0x1d Xn +| 0x1dh 29 | uint8 | Set/Get Selector cut iRun current | 0 to 63 (aka 0-1024mA)| 31 (530mA) | | Read / Write | M707 A0x1d | M708 A0x1d Xn | 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); diff --git a/tests/unit/application/CMakeLists.txt b/tests/unit/application/CMakeLists.txt index a2f0fc0..dcbb541 100644 --- a/tests/unit/application/CMakeLists.txt +++ b/tests/unit/application/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/cut_filament/CMakeLists.txt b/tests/unit/logic/cut_filament/CMakeLists.txt index be071c0..60eee84 100644 --- a/tests/unit/logic/cut_filament/CMakeLists.txt +++ b/tests/unit/logic/cut_filament/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/eject_filament/CMakeLists.txt b/tests/unit/logic/eject_filament/CMakeLists.txt index 3055830..07ec52f 100644 --- a/tests/unit/logic/eject_filament/CMakeLists.txt +++ b/tests/unit/logic/eject_filament/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/failing_tmc/CMakeLists.txt b/tests/unit/logic/failing_tmc/CMakeLists.txt index 887799c..cda5a28 100644 --- a/tests/unit/logic/failing_tmc/CMakeLists.txt +++ b/tests/unit/logic/failing_tmc/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/feed_to_bondtech/CMakeLists.txt b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt index 4a19992..4e60828 100644 --- a/tests/unit/logic/feed_to_bondtech/CMakeLists.txt +++ b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/feed_to_finda/CMakeLists.txt b/tests/unit/logic/feed_to_finda/CMakeLists.txt index f555a9f..99c39bf 100644 --- a/tests/unit/logic/feed_to_finda/CMakeLists.txt +++ b/tests/unit/logic/feed_to_finda/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/homing/CMakeLists.txt b/tests/unit/logic/homing/CMakeLists.txt index 454f398..4626575 100644 --- a/tests/unit/logic/homing/CMakeLists.txt +++ b/tests/unit/logic/homing/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/load_filament/CMakeLists.txt b/tests/unit/logic/load_filament/CMakeLists.txt index e353305..1c1650b 100644 --- a/tests/unit/logic/load_filament/CMakeLists.txt +++ b/tests/unit/logic/load_filament/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/start_up/CMakeLists.txt b/tests/unit/logic/start_up/CMakeLists.txt index b963db7..ff3f666 100644 --- a/tests/unit/logic/start_up/CMakeLists.txt +++ b/tests/unit/logic/start_up/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/tool_change/CMakeLists.txt b/tests/unit/logic/tool_change/CMakeLists.txt index 2bb1d14..ea8efb1 100644 --- a/tests/unit/logic/tool_change/CMakeLists.txt +++ b/tests/unit/logic/tool_change/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/unload_filament/CMakeLists.txt b/tests/unit/logic/unload_filament/CMakeLists.txt index 04b0bf5..1fdfd79 100644 --- a/tests/unit/logic/unload_filament/CMakeLists.txt +++ b/tests/unit/logic/unload_filament/CMakeLists.txt @@ -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 diff --git a/tests/unit/logic/unload_to_finda/CMakeLists.txt b/tests/unit/logic/unload_to_finda/CMakeLists.txt index 781b02c..02e377c 100644 --- a/tests/unit/logic/unload_to_finda/CMakeLists.txt +++ b/tests/unit/logic/unload_to_finda/CMakeLists.txt @@ -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 diff --git a/version.cmake b/version.cmake index 50cfeb8..db930a3 100644 --- a/version.cmake +++ b/version.cmake @@ -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 )