fome-fw/firmware/console/status_loop.cpp

638 lines
20 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file status_loop.cpp
* @brief Human-readable protocol status messages
*
* http://rusefi.com/forum/viewtopic.php?t=263 Dev console overview
* http://rusefi.com/forum/viewtopic.php?t=210 Commands overview
*
*
* @date Mar 15, 2013
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-08-29 07:52:33 -07:00
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "main.h"
#include "status_loop.h"
#include "adc_inputs.h"
2014-12-28 09:03:29 -08:00
#if EFI_WAVE_ANALYZER || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
#include "wave_analyzer.h"
#endif
#include "trigger_central.h"
#include "engine_state.h"
#include "io_pins.h"
2014-09-27 12:03:45 -07:00
#include "efiGpio.h"
2014-08-29 07:52:33 -07:00
#include "mmc_card.h"
#include "console_io.h"
#include "malfunction_central.h"
#include "speed_density.h"
#include "advance_map.h"
#include "tunerstudio.h"
#include "fuel_math.h"
#include "main_trigger_callback.h"
#include "engine_math.h"
#include "idle_thread.h"
#include "engine_configuration.h"
#include "rfiutil.h"
#include "svnversion.h"
#include "engine.h"
#include "lcd_controller.h"
2015-01-13 06:03:51 -08:00
#include "settings.h"
2015-01-17 21:04:10 -08:00
#include "rusefi_outputs.h"
2014-08-29 07:52:33 -07:00
2015-01-24 13:04:21 -08:00
extern bool_t main_loop_started;
2014-12-28 09:03:29 -08:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
// todo: move this logic to algo folder!
#include "rtc_helper.h"
#include "lcd_HD44780.h"
#include "rusefi.h"
#include "pin_repository.h"
#include "flash_main.h"
2014-09-20 18:02:53 -07:00
#include "max31855.h"
2014-12-28 09:03:29 -08:00
#include "vehicle_speed.h"
2014-08-29 07:52:33 -07:00
#endif
2015-01-12 20:03:39 -08:00
extern engine_pins_s enginePins;
2015-01-17 21:04:10 -08:00
extern TriggerCentral triggerCentral;
static bool_t subscription[(int) RO_LAST_ELEMENT];
2015-01-07 15:04:40 -08:00
2014-08-29 07:52:33 -07:00
// this 'true' value is needed for simulator
static volatile bool fullLog = true;
2015-01-01 16:03:31 -08:00
int warningEnabled = true;
2014-08-29 07:52:33 -07:00
//int warningEnabled = FALSE;
extern board_configuration_s *boardConfiguration;
2014-11-24 09:03:09 -08:00
extern bool hasFirmwareErrorFlag;
2014-08-29 07:52:33 -07:00
#define FULL_LOGGING_KEY "fl"
static Logging logger;
static void setWarningEnabled(int value) {
warningEnabled = value;
}
2014-12-28 09:03:29 -08:00
#if EFI_FILE_LOGGING || defined(__DOXYGEN__)
2015-01-24 13:04:21 -08:00
static LoggingWithStorage fileLogger("file logger");
2014-08-29 07:52:33 -07:00
#endif /* EFI_FILE_LOGGING */
2015-01-09 20:06:13 -08:00
static int logFileLineIndex = 0;
#define TAB "\t"
static void reportSensorF(Logging *log, bool fileFormat, const char *caption, const char *units, float value,
int precision) {
if (!fileFormat) {
2014-12-28 09:03:29 -08:00
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
debugFloat(log, caption, value, precision);
2014-08-29 07:52:33 -07:00
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
2015-01-09 20:06:13 -08:00
} else {
2014-08-29 07:52:33 -07:00
2014-12-28 09:03:29 -08:00
#if EFI_FILE_LOGGING || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
if (logFileLineIndex == 0) {
append(log, caption);
append(log, TAB);
} else if (logFileLineIndex == 1) {
append(log, units);
append(log, TAB);
} else {
appendFloat(log, value, precision);
append(log, TAB);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_FILE_LOGGING */
2015-01-09 20:06:13 -08:00
}
2014-08-29 07:52:33 -07:00
}
2015-01-09 20:06:13 -08:00
static void reportSensorI(Logging *log, bool fileFormat, const char *caption, const char *units, int value) {
if (!fileFormat) {
2014-12-28 09:03:29 -08:00
#if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
debugInt(log, caption, value);
2014-08-29 07:52:33 -07:00
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
2015-01-09 20:06:13 -08:00
} else {
2014-12-28 09:03:29 -08:00
#if EFI_FILE_LOGGING || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
if (logFileLineIndex == 0) {
append(log, caption);
append(log, TAB);
} else if (logFileLineIndex == 1) {
append(log, units);
append(log, TAB);
} else {
appendPrintf(log, "%d%s", value, TAB);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_FILE_LOGGING */
2015-01-09 20:06:13 -08:00
}
2014-08-29 07:52:33 -07:00
}
static const char* boolean2string(int value) {
return value ? "YES" : "NO";
}
2015-01-09 20:06:13 -08:00
void printSensors(Logging *log, bool fileFormat, Engine *engine) {
2014-08-29 07:52:33 -07:00
// current time, in milliseconds
int nowMs = currentTimeMillis();
float sec = ((float) nowMs) / 1000;
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "time", "", sec, 3);
2014-08-29 07:52:33 -07:00
2015-01-20 15:04:01 -08:00
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
reportSensorI(log, fileFormat, "rpm", "RPM", getRpmE(engine));
2015-01-20 15:04:01 -08:00
reportSensorF(log, fileFormat, "TRG_0_DUTY", "%", getTriggerDutyCycle(0), 2);
reportSensorF(log, fileFormat, "TRG_1_DUTY", "%", getTriggerDutyCycle(1), 2);
#endif
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "maf", "V", getMaf(), 2);
2014-08-29 07:52:33 -07:00
2014-11-03 18:03:13 -08:00
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2015-01-20 15:04:01 -08:00
#if EFI_ANALOG_SENSORS || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
if (engineConfiguration->hasMapSensor) {
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "MAP", "kPa", getMap(), 2);
reportSensorF(log, fileFormat, "map_r", "V", getRawMap(), 2);
2014-08-29 07:52:33 -07:00
}
2014-12-10 09:03:39 -08:00
if (engineConfiguration->hasBaroSensor) {
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "baro", "kPa", getBaroPressure(), 2);
2014-12-10 09:03:39 -08:00
}
if (engineConfiguration->hasAfrSensor) {
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "afr", "AFR", getAfr(), 2);
2014-12-10 09:03:39 -08:00
}
2015-01-20 15:04:01 -08:00
#endif
#if EFI_VEHICLE_SPEED || defined(__DOXYGEN__)
2014-12-28 09:03:29 -08:00
if (engineConfiguration->hasVehicleSpeedSensor) {
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "vss", "kph", getVehicleSpeed(), 2);
2014-12-28 09:03:29 -08:00
}
#endif /* EFI_PROD_CODE */
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "vref", "V", getVRef(engineConfiguration), 2);
reportSensorF(log, fileFormat, "vbatt", "V", getVBatt(engineConfiguration), 2);
2014-08-29 07:52:33 -07:00
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "TP", "%", getTPS(PASS_ENGINE_PARAMETER_F), 2);
2014-08-29 07:52:33 -07:00
if (engineConfiguration->hasCltSensor) {
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "CLT", "C", getCoolantTemperature(engine), 2);
2014-08-29 07:52:33 -07:00
}
2015-01-09 20:06:13 -08:00
reportSensorF(log, fileFormat, "MAT", "C", getIntakeAirTemperature(engine), 2);
2014-08-29 07:52:33 -07:00
// debugFloat(&logger, "tch", getTCharge1(tps), 2);
2015-01-09 20:06:13 -08:00
}
2015-01-17 21:04:10 -08:00
EXTERN_ENGINE
;
2015-01-09 20:06:13 -08:00
void writeLogLine(void) {
2015-01-24 13:04:21 -08:00
if (!main_loop_started)
return;
2014-12-28 09:03:29 -08:00
#if EFI_FILE_LOGGING || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
resetLogging(&fileLogger);
printSensors(&fileLogger, true, engine);
if (isSdCardAlive()) {
appendPrintf(&fileLogger, "\r\n");
appendToLog(fileLogger.buffer);
logFileLineIndex++;
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_FILE_LOGGING */
}
2015-01-17 21:04:10 -08:00
static void printState(Engine *engine) {
2014-12-24 10:05:36 -08:00
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
2015-01-09 20:06:13 -08:00
printSensors(&logger, false, engine);
2014-08-29 07:52:33 -07:00
2014-11-03 18:03:13 -08:00
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2014-10-13 09:03:10 -07:00
int rpm = getRpmE(engine);
2015-01-17 21:04:10 -08:00
if (subscription[(int) RO_TOTAL_REVOLUTION_COUNTER])
debugInt(&logger, "ckp_c", getCrankEventCounter());
if (subscription[(int) RO_RUNNING_REVOLUTION_COUNTER])
debugInt(&logger, "ckp_r", triggerCentral.triggerState.runningRevolutionCounter);
2014-08-29 07:52:33 -07:00
2015-01-18 07:06:08 -08:00
if (subscription[(int) RO_RUNNING_TRIGGER_ERROR])
debugInt(&logger, "trg_r_errors", triggerCentral.triggerState.runningTriggerErrorCounter);
if (subscription[(int) RO_RUNNING_ORDERING_TRIGGER_ERROR])
debugInt(&logger, "trg_r_order_errors", triggerCentral.triggerState.runningOrderingErrorCounter);
if (subscription[(int) RO_WAVE_CHART_CURRENT_SIZE])
debugInt(&logger, "wave_chart_current", 0);
2014-08-29 07:52:33 -07:00
// debugInt(&logger, "idl", getIdleSwitch());
// debugFloat(&logger, "table_spark", getAdvance(rpm, getMaf()), 2);
2014-11-24 18:03:34 -08:00
float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER);
float baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER);
2014-08-29 07:52:33 -07:00
debugFloat(&logger, "fuel_base", baseFuel, 2);
// debugFloat(&logger, "fuel_iat", getIatCorrection(getIntakeAirTemperature()), 2);
// debugFloat(&logger, "fuel_clt", getCltCorrection(getCoolantTemperature()), 2);
2014-11-07 21:07:22 -08:00
debugFloat(&logger, "fuel_lag", getInjectorLag(getVBatt(engineConfiguration) PASS_ENGINE_PARAMETER), 2);
2014-11-07 20:03:53 -08:00
debugFloat(&logger, "fuel", getFuelMs(rpm PASS_ENGINE_PARAMETER), 2);
2014-08-29 07:52:33 -07:00
2014-11-07 21:07:22 -08:00
debugFloat(&logger, "timing", getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER), 2);
2014-08-29 07:52:33 -07:00
// float map = getMap();
#endif /* EFI_SHAFT_POSITION_INPUT */
}
#define INITIAL_FULL_LOG TRUE
//#define INITIAL_FULL_LOG FALSE
static char LOGGING_BUFFER[700];
volatile int needToReportStatus = FALSE;
static int prevCkpEventCounter = -1;
2015-01-13 19:04:02 -08:00
static LoggingWithStorage logger2;
2014-08-29 07:52:33 -07:00
static void printStatus(void) {
needToReportStatus = TRUE;
}
/**
* Time when the firmware version was reported last time, in seconds
* TODO: implement a request/response instead of just constantly sending this out
*/
static systime_t timeOfPreviousPrintVersion = (systime_t) -1;
2014-12-28 09:03:29 -08:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
static void printOutPin(const char *pinName, brain_pin_e hwPin) {
2014-09-05 12:02:55 -07:00
appendPrintf(&logger, "outpin%s%s@%s%s", DELIMETER, pinName, hwPortname(hwPin), DELIMETER);
2014-08-29 07:52:33 -07:00
}
#endif /* EFI_PROD_CODE */
2014-11-03 18:03:13 -08:00
static void printInfo(Engine *engine, systime_t nowSeconds) {
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2014-08-29 07:52:33 -07:00
/**
* we report the version every 4 seconds - this way the console does not need to
* request it and we will display it pretty soon
*/
if (overflowDiff(nowSeconds, timeOfPreviousPrintVersion) < 4) {
return;
}
timeOfPreviousPrintVersion = nowSeconds;
appendPrintf(&logger, "rusEfiVersion%s%d@%s %s%s", DELIMETER, getRusEfiVersion(), VCS_VERSION,
2014-09-04 13:02:47 -07:00
getConfigurationName(engineConfiguration->engineType),
2014-08-29 07:52:33 -07:00
DELIMETER);
2014-12-28 09:03:29 -08:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
printOutPin(WC_CRANK1, boardConfiguration->triggerInputPins[0]);
printOutPin(WC_CRANK2, boardConfiguration->triggerInputPins[1]);
2014-12-23 20:03:31 -08:00
#if EFI_WAVE_ANALYZER || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
printOutPin(WA_CHANNEL_1, boardConfiguration->logicAnalyzerPins[0]);
printOutPin(WA_CHANNEL_2, boardConfiguration->logicAnalyzerPins[1]);
2014-12-23 20:03:31 -08:00
#endif
2014-08-29 07:52:33 -07:00
for (int i = 0; i < engineConfiguration->cylindersCount; i++) {
2015-01-13 12:03:49 -08:00
printOutPin(enginePins.coils[i].name, boardConfiguration->ignitionPins[i]);
2014-08-29 07:52:33 -07:00
2015-01-13 12:03:49 -08:00
printOutPin(enginePins.injectors[i].name, boardConfiguration->injectionPins[i]);
2014-08-29 07:52:33 -07:00
}
#endif
}
static systime_t timeOfPreviousReport = (systime_t) -1;
extern char errorMessageBuffer[200];
/**
* @brief Sends all pending data to dev console
*/
2014-10-13 09:03:10 -07:00
void updateDevConsoleState(Engine *engine) {
2014-08-29 07:52:33 -07:00
if (!isConsoleReady()) {
return;
}
// looks like this is not needed anymore
// checkIfShouldHalt();
printPending();
2014-12-28 09:03:29 -08:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
// todo: unify with simulator!
if (hasFirmwareError()) {
printMsg(&logger, "firmware error: %s", errorMessageBuffer);
warningEnabled = FALSE;
chThdSleepMilliseconds(200);
return;
}
#endif
2014-12-23 20:03:31 -08:00
#if (EFI_PROD_CODE && HAL_USE_ADC) || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
pokeAdcInputs();
#endif
if (!fullLog) {
return;
}
systime_t nowSeconds = getTimeNowSeconds();
2014-11-03 18:03:13 -08:00
printInfo(engine, nowSeconds);
2014-08-29 07:52:33 -07:00
2015-01-20 15:04:01 -08:00
#if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
int currentCkpEventCounter = getCrankEventCounter();
if (prevCkpEventCounter == currentCkpEventCounter && timeOfPreviousReport == nowSeconds) {
return;
}
timeOfPreviousReport = nowSeconds;
prevCkpEventCounter = currentCkpEventCounter;
2015-01-20 15:04:01 -08:00
#else
chThdSleepMilliseconds(200);
#endif
2015-01-17 21:04:10 -08:00
printState(engine);
2014-08-29 07:52:33 -07:00
#if EFI_WAVE_ANALYZER
printWave(&logger);
#endif
printLine(&logger);
}
#if EFI_PROD_CODE
/*
* command example:
* sfm 3500 400
* that would be 'show fuel for rpm 3500 maf 4.0'
*/
2014-10-21 10:03:09 -07:00
static void showFuelInfo2(float rpm, float engineLoad, Engine *engine) {
2014-11-03 18:03:13 -08:00
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2014-10-13 13:02:58 -07:00
float baseFuelMs = getBaseTableFuel(engineConfiguration, (int) rpm, engineLoad);
2014-08-29 07:52:33 -07:00
2014-11-03 18:03:13 -08:00
scheduleMsg(&logger, "SD magic fuel %f", sdMath(engineConfiguration, 100, 100, 14.7, convertCelsiusToKelvin(20)));
scheduleMsg(&logger2, "algo=%s/pump=%s", getEngine_load_mode_e(engineConfiguration->algorithm),
2015-01-12 20:03:39 -08:00
boolToString(enginePins.fuelPumpRelay.getLogicValue()));
2014-08-29 07:52:33 -07:00
2015-01-20 15:04:01 -08:00
#if EFI_ENGINE_CONTROL
2014-10-21 10:03:09 -07:00
scheduleMsg(&logger2, "cranking fuel: %f", getCrankingFuel(engine));
2014-09-11 13:03:00 -07:00
2014-11-11 13:05:09 -08:00
if (engine->rpmCalculator.isRunning()) {
2014-11-07 21:07:22 -08:00
float iatCorrection = getIatCorrection(getIntakeAirTemperature(engine) PASS_ENGINE_PARAMETER);
float cltCorrection = getCltCorrection(getCoolantTemperature(engine) PASS_ENGINE_PARAMETER);
float injectorLag = getInjectorLag(getVBatt(engineConfiguration) PASS_ENGINE_PARAMETER);
2014-09-05 12:02:55 -07:00
scheduleMsg(&logger2, "rpm=%f engineLoad=%f", rpm, engineLoad);
2014-10-13 13:02:58 -07:00
scheduleMsg(&logger2, "baseFuel=%f", baseFuelMs);
2014-08-29 07:52:33 -07:00
2014-09-05 12:02:55 -07:00
scheduleMsg(&logger2, "iatCorrection=%f cltCorrection=%f injectorLag=%f", iatCorrection, cltCorrection,
injectorLag);
2014-11-07 21:07:22 -08:00
float value = getRunningFuel(baseFuelMs, (int) rpm PASS_ENGINE_PARAMETER);
2014-09-05 12:02:55 -07:00
scheduleMsg(&logger2, "injection pulse width: %f", value);
}
2015-01-20 15:04:01 -08:00
#endif
2014-08-29 07:52:33 -07:00
}
2015-01-20 15:04:01 -08:00
#if EFI_ENGINE_CONTROL
2014-10-21 10:03:09 -07:00
static void showFuelInfo(Engine *engine) {
2014-11-24 18:03:34 -08:00
showFuelInfo2((float) getRpmE(engine), getEngineLoadT(PASS_ENGINE_PARAMETER), engine);
2014-08-29 07:52:33 -07:00
}
2015-01-20 15:04:01 -08:00
#endif
2014-08-29 07:52:33 -07:00
#endif /* EFI_PROD_CODE */
static THD_WORKING_AREA(lcdThreadStack, UTILITY_THREAD_STACK_SIZE);
2014-10-29 08:03:57 -07:00
/**
* blinking thread to show that we are alive
2015-01-20 20:07:37 -08:00
* that's a trivial task - a smaller stack should work
2014-10-29 08:03:57 -07:00
*/
2015-01-20 20:07:37 -08:00
static THD_WORKING_AREA(blinkingStack, 128);
2014-10-29 08:03:57 -07:00
2015-01-07 16:03:45 -08:00
static OutputPin communicationPin;
OutputPin checkEnginePin;
OutputPin warningPin;
OutputPin runningPin;
2015-01-13 10:06:16 -08:00
extern engine_pins_s enginePins;
2015-01-07 16:03:45 -08:00
2015-01-13 10:06:16 -08:00
static OutputPin *leds[] = { &warningPin, &runningPin, &enginePins.errorLedPin, &communicationPin, &checkEnginePin };
2014-12-07 16:03:14 -08:00
2015-01-26 16:04:19 -08:00
static void initStatisLeds() {
2015-01-07 17:04:09 -08:00
#if EFI_PROD_CODE
2015-01-07 16:03:45 -08:00
outputPinRegister("communication status 1", &communicationPin, LED_COMMUNICATION_PORT, LED_COMMUNICATION_PIN);
2015-01-07 17:04:09 -08:00
#endif
2015-01-07 16:03:45 -08:00
#if EFI_WARNING_LED
outputPinRegister("warning", &warningPin, LED_WARNING_PORT, LED_WARNING_PIN);
outputPinRegister("is running status", &runningPin, LED_RUNNING_STATUS_PORT, LED_RUNNING_STATUS_PIN);
#endif /* EFI_WARNING_LED */
2015-01-26 16:04:19 -08:00
}
2015-01-07 16:03:45 -08:00
2015-01-26 16:04:19 -08:00
/**
* This method would blink all the LEDs just to test them
*/
static void initialLedsBlink(void) {
2014-12-07 16:03:14 -08:00
int size = sizeof(leds) / sizeof(leds[0]);
for (int i = 0; i < size; i++)
2015-01-07 15:04:40 -08:00
leds[i]->setValue(1);
2014-12-07 16:03:14 -08:00
chThdSleepMilliseconds(100);
for (int i = 0; i < size; i++)
2015-01-07 15:04:40 -08:00
leds[i]->setValue(0);
2014-12-07 16:03:14 -08:00
}
2014-10-29 08:03:57 -07:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2015-01-26 16:04:19 -08:00
/**
* this thread has a lower-then-usual stack size so we cannot afford *print* methods here
*/
2015-01-20 20:07:37 -08:00
static void blinkingThread(void *arg) {
2014-10-29 08:03:57 -07:00
(void) arg;
chRegSetThreadName("communication blinking");
2014-12-07 16:03:14 -08:00
initialLedsBlink();
while (true) {
2014-10-29 08:03:57 -07:00
int delay;
if (getNeedToWriteConfiguration()) {
delay = isConsoleReady() ? 200 : 66;
} else {
delay = isConsoleReady() ? 100 : 33;
}
2015-01-07 16:03:45 -08:00
communicationPin.setValue(0);
2015-01-20 20:07:37 -08:00
warningPin.setValue(0);
2014-10-29 08:03:57 -07:00
chThdSleepMilliseconds(delay);
2015-01-07 16:03:45 -08:00
communicationPin.setValue(1);
2014-10-29 08:03:57 -07:00
#if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)
if (isTriggerDecoderError() || isIgnitionTimingError())
2015-01-07 16:03:45 -08:00
warningPin.setValue(1);
2015-01-20 20:07:37 -08:00
#endif
2014-10-29 08:03:57 -07:00
chThdSleepMilliseconds(delay);
2015-01-20 20:07:37 -08:00
2014-10-29 08:03:57 -07:00
}
}
2015-01-20 20:07:37 -08:00
2014-10-29 08:03:57 -07:00
#endif /* EFI_PROD_CODE */
2014-10-21 09:04:13 -07:00
static void lcdThread(Engine *engine) {
2014-08-29 07:52:33 -07:00
chRegSetThreadName("lcd");
while (true) {
#if EFI_HD44780_LCD
2014-10-21 09:04:13 -07:00
updateHD44780lcd(engine);
2014-08-29 07:52:33 -07:00
#endif
2014-10-21 09:04:13 -07:00
chThdSleepMilliseconds(engine->engineConfiguration->bc.lcdThreadPeriod);
2014-08-29 07:52:33 -07:00
}
}
2015-01-09 20:06:13 -08:00
// stack for Tuner Studio thread
2014-08-29 07:52:33 -07:00
static THD_WORKING_AREA(tsThreadStack, UTILITY_THREAD_STACK_SIZE);
#if EFI_TUNER_STUDIO
2014-10-21 09:04:13 -07:00
void updateTunerStudioState(Engine *engine, TunerStudioOutputChannels *tsOutputChannels) {
2014-12-24 10:05:36 -08:00
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
2014-10-21 09:04:13 -07:00
int rpm = getRpmE(engine);
2014-08-29 07:52:33 -07:00
#else
int rpm = 0;
#endif
2014-11-03 18:03:13 -08:00
engine_configuration_s *engineConfiguration = engine->engineConfiguration;
2014-11-24 19:03:19 -08:00
float tps = getTPS(PASS_ENGINE_PARAMETER_F);
2014-10-31 12:03:00 -07:00
float coolant = getCoolantTemperature(engine);
float intake = getIntakeAirTemperature(engine);
2014-08-29 07:52:33 -07:00
2014-11-24 18:03:34 -08:00
float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER);
2014-10-13 13:02:58 -07:00
float baseFuelMs = getBaseTableFuel(engineConfiguration, (int) rpm, engineLoad);
2014-08-29 07:52:33 -07:00
2014-12-09 18:03:04 -08:00
// header
tsOutputChannels->tsConfigVersion = TS_FILE_VERSION;
// engine state
2014-08-29 07:52:33 -07:00
tsOutputChannels->rpm = rpm;
tsOutputChannels->coolant_temperature = coolant;
tsOutputChannels->intake_air_temperature = intake;
tsOutputChannels->throttle_positon = tps;
tsOutputChannels->mass_air_flow = getMaf();
tsOutputChannels->air_fuel_ratio = getAfr();
2014-11-06 10:04:30 -08:00
tsOutputChannels->v_batt = getVBatt(engineConfiguration);
2014-11-29 20:03:22 -08:00
tsOutputChannels->tpsADC = getTPS10bitAdc(PASS_ENGINE_PARAMETER_F);
2014-08-29 07:52:33 -07:00
tsOutputChannels->atmospherePressure = getBaroPressure();
tsOutputChannels->manifold_air_pressure = getMap();
2014-12-09 18:03:04 -08:00
tsOutputChannels->engineLoad = engineLoad;
2015-01-14 20:03:40 -08:00
tsOutputChannels->rpmAcceleration = engine->rpmCalculator.getRpmAcceleration();
2014-12-09 18:03:04 -08:00
2014-08-29 07:52:33 -07:00
tsOutputChannels->checkEngine = hasErrorCodes();
#if EFI_PROD_CODE
2014-12-30 12:03:24 -08:00
tsOutputChannels->egtValues.values[0] = getEgtValue(0);
2014-09-20 18:02:53 -07:00
2014-08-29 07:52:33 -07:00
tsOutputChannels->needBurn = getNeedToWriteConfiguration();
tsOutputChannels->hasSdCard = isSdCardAlive();
2015-01-12 20:03:39 -08:00
tsOutputChannels->isFuelPumpOn = enginePins.fuelPumpRelay.getLogicValue();
tsOutputChannels->isFanOn = enginePins.fanRelay.getLogicValue();
tsOutputChannels->isO2HeaterOn = enginePins.o2heater.getLogicValue();
2014-08-29 07:52:33 -07:00
tsOutputChannels->ignition_enabled = engineConfiguration->isIgnitionEnabled;
tsOutputChannels->injection_enabled = engineConfiguration->isInjectionEnabled;
tsOutputChannels->cylinder_cleanup_enabled = engineConfiguration->isCylinderCleanupEnabled;
2014-10-31 12:03:00 -07:00
tsOutputChannels->cylinder_cleanup_activated = engine->isCylinderCleanupMode;
2014-08-29 07:52:33 -07:00
tsOutputChannels->secondTriggerChannelEnabled = engineConfiguration->secondTriggerChannelEnabled;
2014-12-28 09:03:29 -08:00
tsOutputChannels->vehicleSpeedKph = getVehicleSpeed();
2014-10-31 12:03:00 -07:00
tsOutputChannels->isCltError = !isValidCoolantTemperature(getCoolantTemperature(engine));
tsOutputChannels->isIatError = !isValidIntakeAirTemperature(getIntakeAirTemperature(engine));
2014-08-29 07:52:33 -07:00
#endif
2015-01-03 21:03:23 -08:00
tsOutputChannels->clutchUpState = engine->clutchUpState;
tsOutputChannels->clutchDownState = engine->clutchDownState;
2014-08-29 07:52:33 -07:00
tsOutputChannels->tCharge = getTCharge(rpm, tps, coolant, intake);
2014-12-06 15:03:17 -08:00
float timing = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER);
tsOutputChannels->inj_adv = timing > 360 ? timing - 720 : timing;
2014-11-07 21:07:22 -08:00
tsOutputChannels->sparkDwell = getSparkDwellMsT(rpm PASS_ENGINE_PARAMETER);
2014-12-09 18:03:04 -08:00
tsOutputChannels->baseFuel = baseFuelMs;
2014-11-07 21:07:22 -08:00
tsOutputChannels->pulseWidthMs = getRunningFuel(baseFuelMs, rpm PASS_ENGINE_PARAMETER);
2014-10-21 09:04:13 -07:00
tsOutputChannels->crankingFuelMs = getCrankingFuel(engine);
2014-08-29 07:52:33 -07:00
}
extern TunerStudioOutputChannels tsOutputChannels;
#endif /* EFI_TUNER_STUDIO */
2014-10-21 09:04:13 -07:00
static void tsStatusThread(Engine *engine) {
2014-08-29 07:52:33 -07:00
chRegSetThreadName("tuner s");
while (true) {
#if EFI_TUNER_STUDIO
// sensor state for EFI Analytics Tuner Studio
2014-10-21 09:04:13 -07:00
updateTunerStudioState(engine, &tsOutputChannels);
2014-08-29 07:52:33 -07:00
#endif /* EFI_TUNER_STUDIO */
chThdSleepMilliseconds(boardConfiguration->tunerStudioThreadPeriod);
}
}
2015-01-17 21:04:10 -08:00
static void subscribe(int outputOrdinal) {
subscription[outputOrdinal] = true;
}
static void unsubscribe(int outputOrdinal) {
subscription[outputOrdinal] = false;
}
2014-10-21 10:03:09 -07:00
void initStatusLoop(Engine *engine) {
2014-08-29 07:52:33 -07:00
#if EFI_PROD_CODE || EFI_SIMULATOR
initLoggingExt(&logger, "status loop", LOGGING_BUFFER, sizeof(LOGGING_BUFFER));
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
setFullLog(INITIAL_FULL_LOG);
addConsoleActionI(FULL_LOGGING_KEY, setFullLog);
addConsoleActionI("warn", setWarningEnabled);
#if EFI_PROD_CODE
initLogging(&logger2, "main event handler");
2015-01-20 15:04:01 -08:00
#if EFI_ENGINE_CONTROL
2014-11-03 18:03:13 -08:00
addConsoleActionFFP("fuelinfo2", (VoidFloatFloatVoidPtr) showFuelInfo2, engine);
addConsoleActionP("fuelinfo", (VoidPtr) showFuelInfo, engine);
2015-01-20 15:04:01 -08:00
#endif
2014-08-29 07:52:33 -07:00
2015-01-17 21:04:10 -08:00
subscription[(int) RO_TRG1_DUTY] = true;
subscription[(int) RO_TRG2_DUTY] = true;
subscription[(int) RO_TRG3_DUTY] = false;
subscription[(int) RO_TRG4_DUTY] = false;
subscription[(int) RO_TOTAL_REVOLUTION_COUNTER] = true;
subscription[(int) RO_RUNNING_REVOLUTION_COUNTER] = false;
addConsoleActionI("subscribe", subscribe);
addConsoleActionI("unsubscribe", unsubscribe);
2014-08-29 07:52:33 -07:00
addConsoleAction("status", printStatus);
#endif /* EFI_PROD_CODE */
}
2014-10-21 09:04:13 -07:00
void startStatusThreads(Engine *engine) {
2014-08-29 07:52:33 -07:00
// todo: refactoring needed, this file should probably be split into pieces
2014-10-21 09:04:13 -07:00
chThdCreateStatic(lcdThreadStack, sizeof(lcdThreadStack), NORMALPRIO, (tfunc_t) lcdThread, engine);
chThdCreateStatic(tsThreadStack, sizeof(tsThreadStack), NORMALPRIO, (tfunc_t) tsStatusThread, engine);
2014-10-29 08:03:57 -07:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2015-01-26 16:04:19 -08:00
initStatisLeds();
2015-01-20 20:07:37 -08:00
chThdCreateStatic(blinkingStack, sizeof(blinkingStack), NORMALPRIO, (tfunc_t) blinkingThread, NULL);
2014-10-29 08:03:57 -07:00
#endif /* EFI_PROD_CODE */
2014-08-29 07:52:33 -07:00
}
void setFullLog(int value) {
print("Setting full logging: %s\r\n", boolean2string(value));
printMsg(&logger, "%s%d", FULL_LOGGING_KEY, value);
fullLog = value;
}
bool getFullLog(void) {
return fullLog;
}