diff --git a/firmware/console/binary/tunerstudio_io.cpp b/firmware/console/binary/tunerstudio_io.cpp index 0c271788fa..dc075ec6ae 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -155,7 +155,7 @@ bool stopTsPort(ts_channel_s *tsChannel) { } void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { - efiAssertVoid(CUSTOM_ERR_6570, getRemainingStack(chThdGetSelfX()) > 64, "tunerStudioWriteData"); + efiAssertVoid(CUSTOM_ERR_6570, getCurrentRemainingStack() > 64, "tunerStudioWriteData"); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("chSequentialStreamWrite [%d]\r\n", size); #endif diff --git a/firmware/console/console_io.cpp b/firmware/console/console_io.cpp index 749cbdc074..95db7cf7e0 100644 --- a/firmware/console/console_io.cpp +++ b/firmware/console/console_io.cpp @@ -157,7 +157,7 @@ void runConsoleLoop(ts_channel_s *console) { } while (true) { - efiAssertVoid(CUSTOM_ERR_6571, getRemainingStack(chThdGetSelfX()) > 256, "lowstck#9e"); + efiAssertVoid(CUSTOM_ERR_6571, getCurrentRemainingStack() > 256, "lowstck#9e"); bool end = getConsoleLine((BaseSequentialStream*) console->channel, console->crcReadBuffer, sizeof(console->crcReadBuffer) - 3); if (end) { // firmware simulator is the only case when this happens diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index b051a3293a..27d16c5dec 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -1343,7 +1343,7 @@ void validateConfiguration(DECLARE_ENGINE_PARAMETER_SIGNATURE) { void applyNonPersistentConfiguration(Logging * logger DECLARE_ENGINE_PARAMETER_SUFFIX) { #if EFI_PROD_CODE || defined(__DOXYGEN__) - efiAssertVoid(CUSTOM_APPLY_STACK, getRemainingStack(chThdGetSelfX()) > 256, "apply c"); + efiAssertVoid(CUSTOM_APPLY_STACK, getCurrentRemainingStack() > 256, "apply c"); scheduleMsg(logger, "applyNonPersistentConfiguration()"); #endif diff --git a/firmware/controllers/core/fsio_core.cpp b/firmware/controllers/core/fsio_core.cpp index 0324945998..e07a32607f 100644 --- a/firmware/controllers/core/fsio_core.cpp +++ b/firmware/controllers/core/fsio_core.cpp @@ -139,7 +139,7 @@ void LECalculator::push(le_action_e action, float value) { */ bool LECalculator::processElement(LEElement *element DECLARE_ENGINE_PARAMETER_SUFFIX) { #if EFI_PROD_CODE || defined(__DOXYGEN__) - efiAssert(CUSTOM_ERR_ASSERT, getRemainingStack(chThdGetSelfX()) > 64, "FSIO logic", false); + efiAssert(CUSTOM_ERR_ASSERT, getCurrentRemainingStack() > 64, "FSIO logic", false); #endif switch (element->action) { diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index d9c642ab04..3aec70a519 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -278,7 +278,7 @@ static void invokePerSecond(void) { static void periodicSlowCallback(Engine *engine) { #if (EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT) || defined(__DOXYGEN__) - efiAssertVoid(CUSTOM_ERR_6661, getRemainingStack(chThdGetSelfX()) > 64, "lowStckOnEv"); + efiAssertVoid(CUSTOM_ERR_6661, getCurrentRemainingStack() > 64, "lowStckOnEv"); #if EFI_PROD_CODE /** * We need to push current value into the 64 bit counter often enough so that we do not miss an overflow diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 8a980223d7..a49090393b 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -130,7 +130,7 @@ static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, * connectivity so no console output here */ persisted_configuration_state_e readConfiguration(Logging * logger) { - efiAssert(CUSTOM_ERR_ASSERT, getRemainingStack(chThdGetSelfX()) > 256, "read f", PC_ERROR); + efiAssert(CUSTOM_ERR_ASSERT, getCurrentRemainingStack() > 256, "read f", PC_ERROR); persisted_configuration_state_e result = doReadConfiguration(FLASH_ADDR, logger); if (result != PC_OK) { printMsg(logger, "Reading second configuration copy"); diff --git a/firmware/controllers/map_averaging.cpp b/firmware/controllers/map_averaging.cpp index 963517f598..5dc09d6a4b 100644 --- a/firmware/controllers/map_averaging.cpp +++ b/firmware/controllers/map_averaging.cpp @@ -109,7 +109,7 @@ static bool isAveraging = false; static void startAveraging(void *arg) { (void) arg; - efiAssertVoid(CUSTOM_ERR_6649, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#9"); + efiAssertVoid(CUSTOM_ERR_6649, getCurrentRemainingStack() > 128, "lowstck#9"); bool wasLocked = lockAnyContext(); ; // with locking we would have a consistent state @@ -135,7 +135,7 @@ void mapAveragingAdcCallback(adcsample_t adcValue) { /* Calculates the average values from the ADC samples.*/ measurementsPerRevolutionCounter++; - efiAssertVoid(CUSTOM_ERR_6650, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#9a"); + efiAssertVoid(CUSTOM_ERR_6650, getCurrentRemainingStack() > 128, "lowstck#9a"); #if (EFI_SENSOR_CHART && EFI_ANALOG_SENSORS) || defined(__DOXYGEN__) if (ENGINE(sensorChartMode) == SC_MAP) { diff --git a/firmware/controllers/system/SingleTimerExecutor.cpp b/firmware/controllers/system/SingleTimerExecutor.cpp index faca3c5350..c93586c9e1 100644 --- a/firmware/controllers/system/SingleTimerExecutor.cpp +++ b/firmware/controllers/system/SingleTimerExecutor.cpp @@ -43,7 +43,7 @@ uint32_t lastExecutionCount; static void executorCallback(void *arg) { (void)arg; - efiAssertVoid(CUSTOM_ERR_6624, getRemainingStack(chThdGetSelfX()) > 256, "lowstck#2y"); + efiAssertVoid(CUSTOM_ERR_6624, getCurrentRemainingStack() > 256, "lowstck#2y"); // callbackTime = getTimeNowNt(); // if((callbackTime > nextEventTimeNt) && (callbackTime - nextEventTimeNt > US2NT(5000))) { diff --git a/firmware/controllers/trigger/decoders/trigger_structure.cpp b/firmware/controllers/trigger/decoders/trigger_structure.cpp index e48a1e5412..6fda0b7663 100644 --- a/firmware/controllers/trigger/decoders/trigger_structure.cpp +++ b/firmware/controllers/trigger/decoders/trigger_structure.cpp @@ -404,7 +404,7 @@ void TriggerShape::setSecondTriggerSynchronizationGap(float syncRatio) { void TriggerShape::initializeTriggerShape(Logging *logger, operation_mode_e operationMode, bool useOnlyRisingEdgeForTrigger, const trigger_config_s *triggerConfig) { #if EFI_PROD_CODE || defined(__DOXYGEN__) - efiAssertVoid(CUSTOM_ERR_6641, getRemainingStack(chThdGetSelfX()) > 256, "init t"); + efiAssertVoid(CUSTOM_ERR_6641, getCurrentRemainingStack() > 256, "init t"); scheduleMsg(logger, "initializeTriggerShape(%s/%d)", getTrigger_type_e(triggerConfig->type), (int) triggerConfig->type); #endif diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index ad3c20a524..7252ae2a8e 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -26,13 +26,6 @@ #include #endif -#if (!EFI_PROD_CODE && !EFI_SIMULATOR) || defined(__DOXYGEN__) - -#define chThdGetSelfX() 0 -#define getRemainingStack(x) (999999) - -#endif - #if (EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT) || defined(__DOXYGEN__) #include "main_trigger_callback.h" @@ -373,7 +366,7 @@ static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { - efiAssertVoid(CUSTOM_ERR_6627, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#3"); + efiAssertVoid(CUSTOM_ERR_6627, getCurrentRemainingStack() > 128, "lowstck#3"); efiAssertVoid(CUSTOM_ERR_6628, trgEventIndex < engine->engineCycleEventCount, "handleFuel/event index"); if (!isInjectionEnabled(PASS_ENGINE_PARAMETER_SIGNATURE) || limitedFuel) { @@ -446,7 +439,7 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex D */ return; } - efiAssertVoid(CUSTOM_ERR_6629, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#2"); + efiAssertVoid(CUSTOM_ERR_6629, getCurrentRemainingStack() > 128, "lowstck#2"); if (trgEventIndex >= ENGINE(engineCycleEventCount)) { /** diff --git a/firmware/controllers/trigger/rpm_calculator.cpp b/firmware/controllers/trigger/rpm_calculator.cpp index b902a8c6c4..e8d87eba5d 100644 --- a/firmware/controllers/trigger/rpm_calculator.cpp +++ b/firmware/controllers/trigger/rpm_calculator.cpp @@ -228,7 +228,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index DECLARE_ENGINE_PARAMETER_SUFFIX) { efitick_t nowNt = getTimeNowNt(); #if EFI_PROD_CODE - efiAssertVoid(CUSTOM_ERR_6632, getRemainingStack(chThdGetSelfX()) > 256, "lowstckRCL"); + efiAssertVoid(CUSTOM_ERR_6632, getCurrentRemainingStack() > 256, "lowstckRCL"); #endif RpmCalculator *rpmState = &engine->rpmCalculator; diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index dd1dfdb4df..34746ca37c 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -198,7 +198,7 @@ void hwHandleShaftSignal(trigger_event_e signal) { if (triggerReentraint > maxTriggerReentraint) maxTriggerReentraint = triggerReentraint; triggerReentraint++; - efiAssertVoid(CUSTOM_ERR_6636, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#8"); + efiAssertVoid(CUSTOM_ERR_6636, getCurrentRemainingStack() > 128, "lowstck#8"); engine->triggerCentral.handleShaftSignal(signal PASS_ENGINE_PARAMETER_SUFFIX); triggerReentraint--; triggerDuration = GET_TIMESTAMP() - triggerHandlerEntryTime; diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index d11f117066..74fb8a8188 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -107,7 +107,7 @@ bool isTriggerDecoderError(void) { void calculateTriggerSynchPoint(TriggerShape *shape, TriggerState *state DECLARE_ENGINE_PARAMETER_SUFFIX) { #if EFI_PROD_CODE || defined(__DOXYGEN__) - efiAssertVoid(CUSTOM_ERR_6642, getRemainingStack(chThdGetSelfX()) > 256, "calc s"); + efiAssertVoid(CUSTOM_ERR_6642, getCurrentRemainingStack() > 256, "calc s"); #endif trigger_config_s const*triggerConfig = &engineConfiguration->trigger; @@ -634,7 +634,7 @@ uint32_t findTriggerZeroEventIndex(TriggerState *state, TriggerShape * shape, trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_SUFFIX) { UNUSED(triggerConfig); #if EFI_PROD_CODE || defined(__DOXYGEN__) - efiAssert(CUSTOM_ERR_ASSERT, getRemainingStack(chThdGetSelfX()) > 128, "findPos", -1); + efiAssert(CUSTOM_ERR_ASSERT, getCurrentRemainingStack() > 128, "findPos", -1); #endif errorDetection.clear(); efiAssert(CUSTOM_ERR_ASSERT, state != NULL, "NULL state", -1); diff --git a/firmware/development/engine_sniffer.cpp b/firmware/development/engine_sniffer.cpp index 8b370b2c27..c1a5f5de86 100644 --- a/firmware/development/engine_sniffer.cpp +++ b/firmware/development/engine_sniffer.cpp @@ -172,7 +172,7 @@ void WaveChart::addEvent3(const char *name, const char * msg) { efiAssertVoid(CUSTOM_ERR_6651, name!=NULL, "WC: NULL name"); #if EFI_PROD_CODE - efiAssertVoid(CUSTOM_ERR_6652, getRemainingStack(chThdGetSelfX()) > 32, "lowstck#2c"); + efiAssertVoid(CUSTOM_ERR_6652, getCurrentRemainingStack() > 32, "lowstck#2c"); #endif efiAssertVoid(CUSTOM_ERR_6653, isInitialized, "chart not initialized"); diff --git a/firmware/global.h b/firmware/global.h index d69685f165..42b1fcff21 100644 --- a/firmware/global.h +++ b/firmware/global.h @@ -103,6 +103,8 @@ typedef unsigned int time_t; turnAllPinsOff(); \ enginePins.communicationLedPin.setValue(1); +#define getCurrentRemainingStack() getRemainingStack(chThdGetSelfX()) + /* * Stack debugging * See also getMaxUsedStack() diff --git a/firmware/hw_layer/adc_inputs.cpp b/firmware/hw_layer/adc_inputs.cpp index 4fc42565ed..55f9c40d2e 100644 --- a/firmware/hw_layer/adc_inputs.cpp +++ b/firmware/hw_layer/adc_inputs.cpp @@ -174,7 +174,7 @@ AdcDevice fastAdc(&adcgrpcfg_fast); void doSlowAdc(void) { - efiAssertVoid(CUSTOM_ERR_6658, getRemainingStack(chThdGetSelfX())> 32, "lwStAdcSlow"); + efiAssertVoid(CUSTOM_ERR_6658, getCurrentRemainingStack()> 32, "lwStAdcSlow"); #if EFI_INTERNAL_ADC @@ -206,7 +206,7 @@ static void pwmpcb_slow(PWMDriver *pwmp) { } static void pwmpcb_fast(PWMDriver *pwmp) { - efiAssertVoid(CUSTOM_ERR_6659, getRemainingStack(chThdGetSelfX())> 32, "lwStAdcFast"); + efiAssertVoid(CUSTOM_ERR_6659, getCurrentRemainingStack()> 32, "lwStAdcFast"); #if EFI_INTERNAL_ADC (void) pwmp; @@ -573,7 +573,7 @@ int getSlowAdcCounter() { static void adc_callback_slow(ADCDriver *adcp, adcsample_t *buffer, size_t n) { (void) buffer; (void) n; - efiAssertVoid(CUSTOM_ERR_6671, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#9c"); + efiAssertVoid(CUSTOM_ERR_6671, getCurrentRemainingStack() > 128, "lowstck#9c"); /* Note, only in the ADC_COMPLETE state because the ADC driver fires * an intermediate callback when the buffer is half full. */ if (adcp->state == ADC_COMPLETE) { diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index adbe271fac..03bdcb31bf 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -87,7 +87,7 @@ bool rtcWorks = true; * Only one consumer can use SPI bus at a given time */ void lockSpi(spi_device_e device) { - efiAssertVoid(CUSTOM_ERR_6674, getRemainingStack(chThdGetSelfX()) > 128, "lockSpi"); + efiAssertVoid(CUSTOM_ERR_6674, getCurrentRemainingStack() > 128, "lockSpi"); // todo: different locks for different SPI devices! chMtxLock(&spiMtx); } @@ -191,7 +191,7 @@ void adc_callback_fast(ADCDriver *adcp, adcsample_t *buffer, size_t n) { /** * this callback is executed 10 000 times a second, it needs to be as fast as possible */ - efiAssertVoid(CUSTOM_ERR_6676, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#9b"); + efiAssertVoid(CUSTOM_ERR_6676, getCurrentRemainingStack() > 128, "lowstck#9b"); #if EFI_MAP_AVERAGING mapAveragingAdcCallback(fastAdc.samples[fastMapSampleIndex]); @@ -325,7 +325,7 @@ void showBor(void) { } void initHardware(Logging *l) { - efiAssertVoid(CUSTOM_IH_STACK, getRemainingStack(chThdGetSelfX()) > 256, "init h"); + efiAssertVoid(CUSTOM_IH_STACK, getCurrentRemainingStack() > 256, "init h"); sharedLogger = l; engine_configuration_s *engineConfiguration = engine->engineConfigurationPtr; efiAssertVoid(CUSTOM_EC_NULL, engineConfiguration!=NULL, "engineConfiguration"); diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index 29dda6e3f1..f62c1a369c 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -161,7 +161,7 @@ static void scheduleReboot(void) { } void runRusEfi(void) { - efiAssertVoid(CUSTOM_RM_STACK_1, getRemainingStack(chThdGetSelfX()) > 512, "init s"); + efiAssertVoid(CUSTOM_RM_STACK_1, getCurrentRemainingStack() > 512, "init s"); assertEngineReference(); engine->setConfig(config); initIntermediateLoggingBuffer(); @@ -233,7 +233,7 @@ void runRusEfi(void) { * control is around main_trigger_callback */ while (true) { - efiAssertVoid(CUSTOM_RM_STACK, getRemainingStack(chThdGetSelfX()) > 128, "stack#1"); + efiAssertVoid(CUSTOM_RM_STACK, getCurrentRemainingStack() > 128, "stack#1"); #if (EFI_CLI_SUPPORT && !EFI_UART_ECHO_TEST_MODE) || defined(__DOXYGEN__) // sensor state + all pending messages for our own dev console diff --git a/firmware/util/datalogging.cpp b/firmware/util/datalogging.cpp index 8b313a8d9d..63e253eebb 100644 --- a/firmware/util/datalogging.cpp +++ b/firmware/util/datalogging.cpp @@ -100,7 +100,7 @@ static void vappendPrintfI(Logging *logging, const char *fmt, va_list arg) { return; } intermediateLoggingBuffer.eos = 0; // reset - efiAssertVoid(CUSTOM_ERR_6603, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#1b"); + efiAssertVoid(CUSTOM_ERR_6603, getCurrentRemainingStack() > 128, "lowstck#1b"); chvprintf((BaseSequentialStream *) &intermediateLoggingBuffer, fmt, arg); intermediateLoggingBuffer.buffer[intermediateLoggingBuffer.eos] = 0; // need to terminate explicitly logging->append((char *)intermediateLoggingBuffer.buffer); @@ -110,7 +110,7 @@ static void vappendPrintfI(Logging *logging, const char *fmt, va_list arg) { * this method acquires system lock to guard the shared intermediateLoggingBuffer memory stream */ void Logging::vappendPrintf(const char *fmt, va_list arg) { - efiAssertVoid(CUSTOM_ERR_6604, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#5b"); + efiAssertVoid(CUSTOM_ERR_6604, getCurrentRemainingStack() > 128, "lowstck#5b"); int wasLocked = lockAnyContext(); vappendPrintfI(this, fmt, arg); if (!wasLocked) { @@ -120,7 +120,7 @@ void Logging::vappendPrintf(const char *fmt, va_list arg) { // todo: replace with logging->appendPrintf void appendPrintf(Logging *logging, const char *fmt, ...) { - efiAssertVoid(CUSTOM_APPEND_STACK, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#4"); + efiAssertVoid(CUSTOM_APPEND_STACK, getCurrentRemainingStack() > 128, "lowstck#4"); va_list ap; va_start(ap, fmt); logging->vappendPrintf(fmt, ap); @@ -128,7 +128,7 @@ void appendPrintf(Logging *logging, const char *fmt, ...) { } void Logging::appendPrintf(const char *fmt, ...) { - efiAssertVoid(CUSTOM_APPEND_STACK, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#4"); + efiAssertVoid(CUSTOM_APPEND_STACK, getCurrentRemainingStack() > 128, "lowstck#4"); va_list ap; va_start(ap, fmt); vappendPrintf(fmt, ap); @@ -250,7 +250,7 @@ void resetLogging(Logging *logging) { * This method should only be invoked on main thread because only the main thread can write to the console */ void printMsg(Logging *logger, const char *fmt, ...) { - efiAssertVoid(CUSTOM_ERR_6605, getRemainingStack(chThdGetSelfX()) > 128, "lowstck#5o"); + efiAssertVoid(CUSTOM_ERR_6605, getCurrentRemainingStack() > 128, "lowstck#5o"); // resetLogging(logging); // I guess 'reset' is not needed here? appendMsgPrefix(logger); diff --git a/simulator/simulator/global.h b/simulator/simulator/global.h index 4bc8cd7148..ca6f12ef9f 100644 --- a/simulator/simulator/global.h +++ b/simulator/simulator/global.h @@ -64,6 +64,8 @@ extern "C" void printToConsole(char *p); +#define getCurrentRemainingStack() getRemainingStack(chThdGetSelfX()) + int getRemainingStack(thread_t *otp); // todo: move somewhere else? diff --git a/unit_tests/global.h b/unit_tests/global.h index bbb0912674..3b0516e55d 100644 --- a/unit_tests/global.h +++ b/unit_tests/global.h @@ -17,6 +17,10 @@ #include "boards.h" +//#define chThdGetSelfX() 0 +//#define getRemainingStack(x) (999999) +#define getCurrentRemainingStack() (999999) + // this is needed by all DECLARE_ENGINE_PARAMETER_* usages #include "engine_configuration_generated_structures.h"