fome-fw/firmware/controllers/algo/wave_chart.cpp

245 lines
6.2 KiB
C++
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
2014-09-11 07:04:31 -07:00
* @file wave_chart.cpp
2014-08-29 07:52:33 -07:00
* @brief Dev console wave sniffer logic
*
* Here we have our own build-in logic analyzer. The data we aggregate here is sent to the
* java UI Dev Console so that it can be displayed nicely in the Sniffer tab.
*
* Both external events (see wave_analyzer.c) and internal (see signal executors) are supported
*
* @date Jun 23, 2013
* @author Andrey Belomutskiy, (c) 2012-2014
*
* 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"
2014-08-30 17:05:51 -07:00
#include "wave_chart.h"
2014-08-29 07:52:33 -07:00
#if EFI_WAVE_CHART
2014-08-30 17:05:51 -07:00
#include "engine_configuration.h"
2014-08-29 07:52:33 -07:00
#include "eficonsole.h"
#include "status_loop.h"
2014-11-18 18:05:41 -08:00
#define CHART_DELIMETER '!'
2014-08-29 07:52:33 -07:00
2014-11-07 06:03:03 -08:00
#if EFI_HISTOGRAMS || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
#include "rfiutil.h"
#include "histogram.h"
static histogram_s waveChartHisto;
#endif
2014-11-07 20:03:53 -08:00
EXTERN_ENGINE;
2014-11-21 15:03:02 -08:00
extern uint32_t maxLockTime;
2014-08-30 17:05:51 -07:00
2014-08-29 07:52:33 -07:00
/**
* This is the number of events in the digital chart which would be displayed
* on the 'digital sniffer' pane
*/
#if EFI_PROD_CODE
#define WAVE_LOGGING_SIZE 5000
#else
#define WAVE_LOGGING_SIZE 35000
#endif
int waveChartUsedSize;
//#define DEBUG_WAVE 1
#if DEBUG_WAVE
static Logging debugLogging;
2014-09-11 08:03:38 -07:00
#endif /* DEBUG_WAVE */
2014-08-29 07:52:33 -07:00
static Logging logger;
2014-09-11 08:03:38 -07:00
void WaveChart::resetWaveChart() {
2014-08-29 07:52:33 -07:00
#if DEBUG_WAVE
2014-09-11 08:03:38 -07:00
scheduleSimpleMsg(&debugLogging, "reset while at ", counter);
#endif /* DEBUG_WAVE */
resetLogging(&logging);
counter = 0;
2014-11-06 18:05:30 -08:00
startTimeNt = 0;
2014-09-11 08:03:38 -07:00
appendPrintf(&logging, "wave_chart%s", DELIMETER);
2014-08-29 07:52:33 -07:00
}
static char WAVE_LOGGING_BUFFER[WAVE_LOGGING_SIZE] CCM_OPTIONAL
;
2014-11-11 08:04:06 -08:00
bool_t WaveChart::isStartedTooLongAgo() {
2014-09-15 19:03:09 -07:00
/**
* Say at 300rpm we should get at least four events per revolution.
* That's 300/60*4=20 events per second
* digitalChartSize/20 is the longest meaningful chart.
*
*/
2014-11-06 18:05:30 -08:00
uint64_t chartDurationNt = getTimeNowNt() - startTimeNt;
2014-11-11 08:04:06 -08:00
return startTimeNt!= 0 && NT2US(chartDurationNt) > engineConfiguration->digitalChartSize * 1000000 / 20;
}
bool_t WaveChart::isWaveChartFull() {
return counter >= engineConfiguration->digitalChartSize;
2014-08-29 07:52:33 -07:00
}
static void printStatus(void) {
2014-11-21 15:03:02 -08:00
scheduleMsg(&logger, "digital chart: %s", boolToString(engineConfiguration->isDigitalChartEnabled));
scheduleMsg(&logger, "chartsize=%d", engineConfiguration->digitalChartSize);
2014-08-29 07:52:33 -07:00
}
static void setChartActive(int value) {
2014-11-21 15:03:02 -08:00
engineConfiguration->isDigitalChartEnabled = value;
2014-08-29 07:52:33 -07:00
printStatus();
2014-11-21 15:03:02 -08:00
maxLockTime = 0;
2014-08-29 07:52:33 -07:00
}
void setChartSize(int newSize) {
2014-08-30 17:05:51 -07:00
if (newSize < 5) {
2014-08-29 07:52:33 -07:00
return;
2014-08-30 17:05:51 -07:00
}
engineConfiguration->digitalChartSize = newSize;
2014-08-29 07:52:33 -07:00
printStatus();
}
2014-09-11 08:03:38 -07:00
void WaveChart::publishChartIfFull() {
2014-11-11 08:04:06 -08:00
if (isWaveChartFull() || isStartedTooLongAgo()) {
2014-09-11 08:03:38 -07:00
publishChart();
resetWaveChart();
2014-08-29 07:52:33 -07:00
}
}
2014-09-11 16:02:59 -07:00
void WaveChart::init() {
2014-09-11 08:03:38 -07:00
initLoggingExt(&logging, "wave chart", WAVE_LOGGING_BUFFER, sizeof(WAVE_LOGGING_BUFFER));
isInitialized = TRUE;
resetWaveChart();
}
void WaveChart::publishChart() {
appendPrintf(&logging, DELIMETER);
waveChartUsedSize = loggingSize(&logging);
2014-08-29 07:52:33 -07:00
#if DEBUG_WAVE
Logging *l = &chart->logging;
scheduleSimpleMsg(&debugLogging, "IT'S TIME", strlen(l->buffer));
#endif
2014-08-30 17:05:51 -07:00
bool isFullLog = getFullLog();
2014-11-21 15:03:02 -08:00
if (engineConfiguration->isDigitalChartEnabled && isFullLog) {
2014-09-11 08:03:38 -07:00
scheduleLogging(&logging);
2014-08-30 17:05:51 -07:00
}
2014-08-29 07:52:33 -07:00
}
static char timeBuffer[10];
/**
* @brief Register an event for digital sniffer
*/
2014-11-18 06:03:12 -08:00
void WaveChart::addWaveChartEvent3(const char *name, const char * msg) {
2014-11-21 15:03:02 -08:00
if(!engineConfiguration->isDigitalChartEnabled) {
2014-10-02 20:03:25 -07:00
return;
}
2014-09-11 08:03:38 -07:00
efiAssertVoid(isInitialized, "chart not initialized");
2014-08-29 07:52:33 -07:00
#if DEBUG_WAVE
scheduleSimpleMsg(&debugLogging, "current", chart->counter);
#endif
2014-09-11 08:03:38 -07:00
if (isWaveChartFull()) {
2014-08-29 07:52:33 -07:00
return;
2014-08-30 17:05:51 -07:00
}
2014-08-29 07:52:33 -07:00
#if EFI_HISTOGRAMS && EFI_PROD_CODE
int beforeCallback = hal_lld_get_counter_value();
#endif
2014-11-06 19:03:12 -08:00
uint64_t nowNt = getTimeNowNt();
2014-08-29 07:52:33 -07:00
bool alreadyLocked = lockOutputBuffer(); // we have multiple threads writing to the same output buffer
2014-09-11 08:03:38 -07:00
if (counter == 0) {
2014-11-06 19:03:12 -08:00
startTimeNt = nowNt;
2014-08-30 17:05:51 -07:00
}
2014-09-11 08:03:38 -07:00
counter++;
2014-11-06 18:05:30 -08:00
/**
* We want smaller times within a chart in order to reduce packet size.
*/
/**
* todo: migrate to binary fractions in order to eliminate
* this division? I do not like division
2014-11-06 19:03:12 -08:00
*
* at least that's 32 bit division now
2014-11-06 18:05:30 -08:00
*/
2014-11-06 19:03:12 -08:00
uint32_t diffNt = nowNt - startTimeNt;
2014-11-07 17:04:58 -08:00
uint32_t time100 = NT2US(diffNt / 10);
2014-11-06 18:05:30 -08:00
2014-11-16 15:03:17 -08:00
if (remainingSize(&logging) > 35) {
2014-08-29 07:52:33 -07:00
/**
* printf is a heavy method, append is used here as a performance optimization
*/
2014-09-11 08:03:38 -07:00
appendFast(&logging, name);
2014-11-18 18:05:41 -08:00
appendChar(&logging, CHART_DELIMETER);
2014-09-11 08:03:38 -07:00
appendFast(&logging, msg);
2014-11-18 18:05:41 -08:00
appendChar(&logging, CHART_DELIMETER);
2014-11-06 18:05:30 -08:00
// time100 -= startTime100;
2014-08-29 07:52:33 -07:00
itoa10(timeBuffer, time100);
2014-09-11 08:03:38 -07:00
appendFast(&logging, timeBuffer);
2014-11-18 18:05:41 -08:00
appendChar(&logging, CHART_DELIMETER);
logging.linePointer[0] = 0;
2014-08-29 07:52:33 -07:00
}
if (!alreadyLocked) {
unlockOutputBuffer();
2014-08-30 17:05:51 -07:00
}
2014-08-29 07:52:33 -07:00
#if EFI_HISTOGRAMS && EFI_PROD_CODE
int64_t diff = hal_lld_get_counter_value() - beforeCallback;
if (diff > 0) {
2014-08-30 17:05:51 -07:00
hsAdd(&waveChartHisto, diff);
}
2014-08-29 07:52:33 -07:00
#endif /* EFI_HISTOGRAMS */
}
void showWaveChartHistogram(void) {
2014-11-07 06:03:03 -08:00
#if (EFI_HISTOGRAMS && EFI_PROD_CODE) || defined(__DOXYGEN__)
2014-08-29 07:52:33 -07:00
printHistogram(&logger, &waveChartHisto);
#endif
}
void initWaveChart(WaveChart *chart) {
initLogging(&logger, "wave info");
2014-11-21 15:03:02 -08:00
if (!engineConfiguration->isDigitalChartEnabled) {
2014-08-29 07:52:33 -07:00
printMsg(&logger, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! chart disabled");
2014-08-30 17:05:51 -07:00
}
2014-08-29 07:52:33 -07:00
2014-09-11 16:02:59 -07:00
/**
* constructor does not work because we need specific initialization order
*/
chart->init();
2014-08-29 07:52:33 -07:00
printStatus();
#if DEBUG_WAVE
initLoggingExt(&debugLogging, "wave chart debug", &debugLogging.DEFAULT_BUFFER, sizeof(debugLogging.DEFAULT_BUFFER));
#endif
#if EFI_HISTOGRAMS
initHistogram(&waveChartHisto, "wave chart");
#endif /* EFI_HISTOGRAMS */
addConsoleActionI("chartsize", setChartSize);
addConsoleActionI("chart", setChartActive);
}
#endif /* EFI_WAVE_CHART */