From e979363730a520687fd901be09827c2bd696b0fb Mon Sep 17 00:00:00 2001 From: Jason <83615043+JJassonn69@users.noreply.github.com> Date: Tue, 21 Apr 2026 03:30:02 +0545 Subject: [PATCH] fix(mcu): volatile emergency state + AGC holdoff zero-guard (closes #83) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug 1 (main.cpp:630): system_emergency_state lacked volatile. Under -O1+ the compiler is permitted to hoist the read outside the blink loop, making while (system_emergency_state) unconditionally infinite. Once entered, the only escape was the 4 s IWDG timeout — which resets the MCU and re-energizes the PA rails that Emergency_Stop() explicitly cut. Marking the variable volatile forces a memory read on every iteration so an external clear (ISR, USB command, manual reset) can break the loop correctly. Bug 2 (ADAR1000_AGC.cpp:59): holdoff_frames is a public uint8_t; if a caller sets it to 0, the condition holdoff_counter >= holdoff_frames is always true (any uint8_t >= 0), causing the AGC outer loop to increase gain on every non-saturated frame with no holdoff delay. With alternating sat/no-sat frames this produces a ±step oscillation that prevents the receiver from settling. Fix: clamp holdoff_frames to a minimum of 1 in the constructor, preserving all existing test assertions (none use 0; default remains 4). Reported-by: shaun0927 (Junghwan) --- .../9_1_Microcontroller/9_1_1_C_Cpp_Libraries/ADAR1000_AGC.cpp | 1 + 9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/ADAR1000_AGC.cpp b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/ADAR1000_AGC.cpp index 068b80b..3828db2 100644 --- a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/ADAR1000_AGC.cpp +++ b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/ADAR1000_AGC.cpp @@ -24,6 +24,7 @@ ADAR1000_AGC::ADAR1000_AGC() , saturation_event_count(0) { memset(cal_offset, 0, sizeof(cal_offset)); + if (holdoff_frames == 0) holdoff_frames = 1; } // --------------------------------------------------------------------------- diff --git a/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp b/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp index b7ef46f..01876a6 100644 --- a/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp +++ b/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp @@ -627,7 +627,7 @@ typedef enum { static SystemError_t last_error = ERROR_NONE; static uint32_t error_count = 0; -static bool system_emergency_state = false; +static volatile bool system_emergency_state = false; // Error handler function SystemError_t checkSystemHealth(void) {