From 2b7314989972d9ff6a555194f6c037b3304faa24 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 13 Jul 2019 10:24:23 -0400 Subject: [PATCH] having fun with async - reducing threads --- firmware/console/status_loop.cpp | 128 ++++++++++----------- firmware/controllers/engine_controller.cpp | 4 +- 2 files changed, 62 insertions(+), 70 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 70f02107d3..c4327e6c90 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -73,6 +73,7 @@ extern bool main_loop_started; #include "max31855.h" #include "vehicle_speed.h" #include "single_timer_executor.h" +#include "periodic_task.h" #endif /* EFI_PROD_CODE */ #if EFI_CJ125 @@ -343,7 +344,6 @@ static void printSensors(Logging *log) { } - void writeLogLine(void) { #if EFI_FILE_LOGGING if (!main_loop_started) @@ -525,12 +525,6 @@ static void showFuelInfo(void) { } #endif -/** - * blinking thread to show that we are alive - * that's a trivial task - a smaller stack should work - */ -static THD_WORKING_AREA(blinkingStack, 128); - static OutputPin *leds[] = { &enginePins.warningLedPin, &enginePins.runningLedPin, &enginePins.checkEnginePin, &enginePins.errorLedPin, &enginePins.communicationLedPin, &enginePins.checkEnginePin }; @@ -543,26 +537,6 @@ static void initStatusLeds(void) { enginePins.runningLedPin.initPin("led: running status", engineConfiguration->runningLedPin); } -/** - * This method would blink all the LEDs just to test them - */ -static void initialLedsBlink(void) { - if (hasFirmwareError()) { - // make sure we do not turn the fatal LED off if already have - // fatal error by now - return; - } - int size = sizeof(leds) / sizeof(leds[0]); - for (int i = 0; i < size && !hasFirmwareError(); i++) - leds[i]->setValue(1); - - chThdSleepMilliseconds(100); - - // re-checking in case the error has happened while we were sleeping - for (int i = 0; i < size && !hasFirmwareError(); i++) - leds[i]->setValue(0); -} - #define BLINKING_PERIOD_MS 33 #if EFI_PROD_CODE @@ -578,48 +552,65 @@ static bool isTriggerErrorNow() { extern bool consoleByteArrived; -/** - * this thread has a lower-then-usual stack size so we cannot afford *print* methods here - */ -static void blinkingThread(void *arg) { - (void) arg; - chRegSetThreadName("communication blinking"); - - initialLedsBlink(); - - while (true) { - int onTimeMs = is_usb_serial_ready() ? 3 * BLINKING_PERIOD_MS : BLINKING_PERIOD_MS; - -#if EFI_INTERNAL_FLASH - if (getNeedToWriteConfiguration()) { - onTimeMs = 2 * onTimeMs; - } -#endif - int offTimeMs = onTimeMs; - - if (hasFirmwareError()) { - // special behavior in case of fatal error - not equal on/off time - // this special behaviour helps to notice that something is not right, also - // differentiates software firmware error from fatal interrupt error with CPU halt. - offTimeMs = 50; - onTimeMs = 450; - } - - enginePins.communicationLedPin.setValue(0); - enginePins.warningLedPin.setValue(0); - chThdSleepMilliseconds(offTimeMs); - - enginePins.communicationLedPin.setValue(1); -#if EFI_ENGINE_CONTROL - if (isTriggerErrorNow() || isIgnitionTimingError() || consoleByteArrived) { - consoleByteArrived = false; - enginePins.warningLedPin.setValue(1); - } -#endif /* EFI_ENGINE_CONTROL */ - chThdSleepMilliseconds(onTimeMs); +class BlinkingTask : public PeriodicTimerController { + int getPeriodMs() override { + return counter % 2 == 0 ? onTimeMs : offTimeMs; } -} + + void setAllLeds(int value) { + // make sure we do not turn the fatal LED off if already have + // fatal error by now + for (int i = 0; !hasFirmwareError() && i < sizeof(leds) / sizeof(leds[0]); i++) { + leds[i]->setValue(value); + } + } + + void PeriodicTask() override { + counter++; + if (counter == 1) { + // first invocation of BlinkingTask + setAllLeds(1); + } else if (counter == 2) { + // second invocation of BlinkingTask + setAllLeds(0); + } else if (counter % 2 == 0) { + enginePins.communicationLedPin.setValue(0); + enginePins.warningLedPin.setValue(0); + } else { + if (hasFirmwareError()) { + // special behavior in case of fatal error - not equal on/off time + // this special behaviour helps to notice that something is not right, also + // differentiates software firmware error from fatal interrupt error with CPU halt. + offTimeMs = 50; + onTimeMs = 450; + } else { + onTimeMs = is_usb_serial_ready() ? 3 * BLINKING_PERIOD_MS : BLINKING_PERIOD_MS; +#if EFI_INTERNAL_FLASH + if (getNeedToWriteConfiguration()) { + onTimeMs = 2 * onTimeMs; + } +#endif + offTimeMs = onTimeMs; + } + + enginePins.communicationLedPin.setValue(1); + #if EFI_ENGINE_CONTROL + if (isTriggerErrorNow() || isIgnitionTimingError() || consoleByteArrived) { + consoleByteArrived = false; + enginePins.warningLedPin.setValue(1); + } + #endif /* EFI_ENGINE_CONTROL */ + } + } + +private: + int counter = 0; + int onTimeMs = 100; + int offTimeMs = 100; +}; + +static BlinkingTask blinkingTask; #endif /* EFI_PROD_CODE */ @@ -964,8 +955,9 @@ void startStatusThreads(void) { // todo: refactoring needed, this file should probably be split into pieces #if EFI_PROD_CODE initStatusLeds(); - chThdCreateStatic(blinkingStack, sizeof(blinkingStack), NORMALPRIO, (tfunc_t) blinkingThread, NULL); + blinkingTask.Start(); #endif /* EFI_PROD_CODE */ + #if EFI_LCD lcdInstance.Start(); #endif /* EFI_LCD */ diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index eb15477e2c..fc883675d3 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -793,7 +793,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) // help to notice when RAM usage goes up - if a code change adds to RAM usage these variables would fail // linking process which is the way to raise the alarm #ifndef RAM_UNUSED_SIZE -#define RAM_UNUSED_SIZE 13000 +#define RAM_UNUSED_SIZE 14500 #endif #ifndef CCM_UNUSED_SIZE #define CCM_UNUSED_SIZE 4600 @@ -814,6 +814,6 @@ int getRusEfiVersion(void) { if (initBootloader() != 0) return 123; #endif /* EFI_BOOTLOADER_INCLUDE_CODE */ - return 20190712; + return 20190713; } #endif /* EFI_UNIT_TEST */