rusefi-1/firmware/controllers/error_handling.cpp

144 lines
3.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file error_handling.cpp
*
* @date Apr 1, 2014
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
2015-07-10 06:01:56 -07:00
*/
#include "main.h"
#include "error_handling.h"
#include "io_pins.h"
#include "memstreams.h"
#include "efilib2.h"
#if EFI_HD44780_LCD
#include "lcd_HD44780.h"
#endif /* EFI_HD44780_LCD */
static time_t timeOfPreviousWarning = -10;
static LoggingWithStorage logger("error handling");
#define WARNING_PREFIX "WARNING: "
extern int warningEnabled;
2016-01-11 14:01:33 -08:00
extern bool main_loop_started;
2015-07-10 06:01:56 -07:00
const char *dbg_panic_file;
int dbg_panic_line;
void chDbgPanic3(const char *msg, const char * file, int line) {
if (hasFatalError())
return;
dbg_panic_file = file;
dbg_panic_line = line;
#if CH_DBG_SYSTEM_STATE_CHECK
dbg_panic_msg = msg;
#endif /* CH_DBG_SYSTEM_STATE_CHECK */
ON_FATAL_ERROR();
#if EFI_HD44780_LCD
lcdShowFatalMessage((char *) msg);
#endif /* EFI_HD44780_LCD */
if (!main_loop_started) {
print("fatal %s %s:%d\r\n", msg, file, line);
chThdSleepSeconds(1);
chSysHalt();
}
}
#define WARNING_BUFFER_SIZE 80
static char warningBuffer[WARNING_BUFFER_SIZE];
2016-04-03 15:02:39 -07:00
static bool isWarningStreamInitialized = false;
2015-07-10 06:01:56 -07:00
static MemoryStream warningStream;
2016-01-30 19:03:36 -08:00
static int warningCounter = 0;
2015-07-10 06:01:56 -07:00
/**
* OBD_PCM_Processor_Fault is the general error code for now
*
* @returns TRUE in case there are too many warnings
*/
int warning(obd_code_e code, const char *fmt, ...) {
2016-04-03 15:02:39 -07:00
efiAssert(isWarningStreamInitialized, "warn stream", false);
2016-01-30 19:03:36 -08:00
UNUSED(code);
2015-07-10 06:01:56 -07:00
int now = getTimeNowSeconds();
if (absI(now - timeOfPreviousWarning) < 10 || !warningEnabled)
return true; // we just had another warning, let's not spam
timeOfPreviousWarning = now;
2016-01-30 19:03:36 -08:00
warningCounter++;
2015-07-10 06:01:56 -07:00
resetLogging(&logger); // todo: is 'reset' really needed here?
appendMsgPrefix(&logger);
va_list ap;
va_start(ap, fmt);
append(&logger, WARNING_PREFIX);
warningStream.eos = 0; // reset
chvprintf((BaseSequentialStream *) &warningStream, fmt, ap);
warningStream.buffer[warningStream.eos] = 0;
va_end(ap);
append(&logger, warningBuffer);
append(&logger, DELIMETER);
scheduleLogging(&logger);
return FALSE;
}
char *getWarninig(void) {
return warningBuffer;
}
uint32_t lastLockTime;
uint32_t maxLockTime = 0;
bool isInsideTriggerHandler = false;
void onLockHook(void) {
lastLockTime = GET_TIMESTAMP();
}
void onUnlockHook(void) {
uint32_t t = GET_TIMESTAMP() - lastLockTime;
if (t > maxLockTime) {
maxLockTime = t;
}
// if (t > 2800) {
// // un-comment this if you want a nice stop for a breakpoint
// maxLockTime = t + 1;
// }
}
void initErrorHandling(void) {
msObjectInit(&warningStream, (uint8_t *) warningBuffer, WARNING_BUFFER_SIZE, 0);
2016-04-03 15:02:39 -07:00
isWarningStreamInitialized = true;
2015-07-10 06:01:56 -07:00
}
2016-02-04 12:01:45 -08:00
extern virtual_timers_list_t vtlist;
2016-01-11 14:01:33 -08:00
extern bool main_loop_started;
2015-07-10 06:01:56 -07:00
int getVtSizeEstimate(void) {
2016-02-04 18:03:01 -08:00
virtual_timer_t *first = vtlist.vt_next;
virtual_timer_t *cur = first->vt_next;
2015-07-10 06:01:56 -07:00
int c = 0;
while (c++ < 20 && cur != first) {
cur = cur->vt_next;
}
return c;
}
int globalVt;
int allReady = 0;
void assertVtList(void) {
if (!main_loop_started)
return;
globalVt = getVtSizeEstimate();
//efiAssertVoid(globalVt > 3, "VT list?");
if(globalVt <=3 ) {
allReady++;
}
}