diff --git a/firmware/config/stm32f4ems/chconf.h b/firmware/config/stm32f4ems/chconf.h index 533c440fb1..343e747798 100644 --- a/firmware/config/stm32f4ems/chconf.h +++ b/firmware/config/stm32f4ems/chconf.h @@ -35,10 +35,28 @@ #define CHPRINTF_USE_FLOAT TRUE #if !defined(EFI_CLOCK_LOCKS) || defined(__DOXYGEN__) +// looks like this value could not be defined in efifeatures.h - please define either externally or just change the value here #define EFI_CLOCK_LOCKS FALSE #endif /* EFI_CLOCK_LOCKS */ +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + #ifndef __ASSEMBLER__ + #if EFI_CLOCK_LOCKS + void irqEnterHook(void); + void irqExitHook(void); + #else /* EFI_CLOCK_LOCKS */ + #define irqEnterHook() {} + #define irqExitHook() {} + #endif /*EFI_CLOCK_LOCKS */ + #endif /* __ASSEMBLER__ */ +#ifdef __cplusplus +} +#endif /* __cplusplus */ + #if EFI_CLOCK_LOCKS #ifdef __cplusplus extern "C" @@ -511,6 +529,7 @@ extern "C" */ #define CH_CFG_IRQ_PROLOGUE_HOOK() { \ /* IRQ prologue code here.*/ \ + irqEnterHook(); \ } /** @@ -518,6 +537,7 @@ extern "C" */ #define CH_CFG_IRQ_EPILOGUE_HOOK() { \ /* IRQ epilogue code here.*/ \ + irqExitHook(); \ } /** diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index a14bb871ad..cd1b7a7670 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -246,6 +246,43 @@ static void resetAccel(void) { engine->wallFuel.reset(); } + +#if EFI_CLOCK_LOCKS +static int previousSecond; + +typedef FLStack irq_enter_timestamps_t; + +static irq_enter_timestamps_t irqEnterTimestamps; + +void irqEnterHook(void) { + irqEnterTimestamps.push(GET_TIMESTAMP()); +} + +static int currentIrqDurationAccumulator = 0; +static int currentIrqCounter = 0; +/** + * See also maxLockedDuration + */ +int perSecondIrqDuration = 0; +int perSecondIrqCounter = 0; +void irqExitHook(void) { + int enterTime = irqEnterTimestamps.pop(); + currentIrqDurationAccumulator += (GET_TIMESTAMP() - enterTime); + currentIrqCounter++; +} +#endif /* EFI_CLOCK_LOCKS */ + +static void invokePerSecond(void) { +#if EFI_CLOCK_LOCKS + // this data transfer is not atomic but should be totally good enough + perSecondIrqDuration = currentIrqDurationAccumulator; + perSecondIrqCounter = currentIrqCounter; + currentIrqDurationAccumulator = currentIrqCounter = 0; +#endif /* EFI_CLOCK_LOCKS */ + +} + + static void periodicSlowCallback(Engine *engine) { efiAssertVoid(CUSTOM_ERR_6661, getRemainingStack(chThdGetSelfX()) > 64, "lowStckOnEv"); #if EFI_PROD_CODE @@ -253,11 +290,16 @@ static void periodicSlowCallback(Engine *engine) { * We need to push current value into the 64 bit counter often enough so that we do not miss an overflow */ bool alreadyLocked = lockAnyContext(); - updateAndSet(&halTime.state, port_rt_get_counter_value()); + updateAndSet(&halTime.state, GET_TIMESTAMP()); if (!alreadyLocked) { unlockAnyContext(); } -#endif + int timeSeconds = getTimeNowSeconds(); + if (previousSecond != timeSeconds) { + previousSecond = timeSeconds; + invokePerSecond(); + } +#endif /* EFI_PROD_CODE */ /** * Update engine RPM state if needed (check timeouts). @@ -265,9 +307,9 @@ static void periodicSlowCallback(Engine *engine) { engine->rpmCalculator.checkIfSpinning(PASS_ENGINE_PARAMETER_SIGNATURE); if (engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) { -#if (EFI_PROD_CODE && EFI_ENGINE_CONTROL && EFI_INTERNAL_FLASH) || defined(__DOXYGEN__) +#if EFI_INTERNAL_FLASH || defined(__DOXYGEN__) writeToFlashIfPending(); -#endif +#endif /* EFI_INTERNAL_FLASH */ resetAccel(); } else { updatePrimeInjectionPulseState(PASS_ENGINE_PARAMETER_SIGNATURE); @@ -282,7 +324,7 @@ static void periodicSlowCallback(Engine *engine) { engine->updateSlowSensors(); engine->checkShutdown(); -#if (EFI_PROD_CODE && EFI_FSIO) || defined(__DOXYGEN__) +#if EFI_FSIO || defined(__DOXYGEN__) runFsio(PASS_ENGINE_PARAMETER_SIGNATURE); #endif /* EFI_PROD_CODE && EFI_FSIO */ @@ -736,5 +778,5 @@ int getRusEfiVersion(void) { if (initBootloader() != 0) return 123; #endif /* EFI_BOOTLOADER_INCLUDE_CODE */ - return 20181209; + return 20181223; } diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 0e97e53713..bf3318e479 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -505,6 +505,9 @@ extern uint32_t hwSetTimerDuration; extern uint32_t maxLockedDuration; extern uint32_t maxEventCallbackDuration; +extern int perSecondIrqDuration; +extern int perSecondIrqCounter; + #if (EFI_PROD_CODE) || defined(__DOXYGEN__) extern uint32_t maxPrecisionCallbackDuration; #endif /* EFI_PROD_CODE */ @@ -622,6 +625,10 @@ void triggerInfo(void) { #if EFI_CLOCK_LOCKS || defined(__DOXYGEN__) scheduleMsg(logger, "maxLockedDuration=%d / maxTriggerReentraint=%d", maxLockedDuration, maxTriggerReentraint); + + scheduleMsg(logger, "perSecondIrqDuration=%d ticks / perSecondIrqCounter=%d", perSecondIrqDuration, perSecondIrqCounter); + scheduleMsg(logger, "IRQ CPU utilization %f%%", perSecondIrqDuration / 168000000.0 * 100); + #endif /* EFI_CLOCK_LOCKS */ scheduleMsg(logger, "maxEventCallbackDuration=%d", maxEventCallbackDuration);