2019-07-07 12:22:46 -07:00
|
|
|
/*
|
|
|
|
* @file tooth_logger.cpp
|
|
|
|
*
|
|
|
|
* @date Jul 7, 2019
|
|
|
|
* @author Matthew Kennedy
|
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2019-07-06 17:48:58 -07:00
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "tooth_logger.h"
|
2019-07-07 12:22:46 -07:00
|
|
|
|
|
|
|
#if EFI_TOOTH_LOGGER
|
|
|
|
|
2020-04-13 17:47:54 -07:00
|
|
|
typedef struct __attribute__ ((packed)) {
|
|
|
|
uint16_t timestamp;
|
|
|
|
} tooth_logger_s;
|
|
|
|
|
|
|
|
typedef struct __attribute__ ((packed)) {
|
|
|
|
// the whole order of all packet bytes is reversed, not just the 'endian-swap' integers
|
|
|
|
uint32_t timestamp;
|
|
|
|
// unfortunately all these fields are required by TS...
|
|
|
|
bool priLevel : 1;
|
|
|
|
bool secLevel : 1;
|
2020-10-25 19:26:20 -07:00
|
|
|
bool trigger : 1;
|
2020-04-13 17:47:54 -07:00
|
|
|
bool sync : 1;
|
2020-05-25 21:07:18 -07:00
|
|
|
bool coil : 1;
|
|
|
|
bool injector : 1;
|
2020-04-13 17:47:54 -07:00
|
|
|
} composite_logger_s;
|
|
|
|
|
2020-05-25 19:38:57 -07:00
|
|
|
/**
|
|
|
|
* Engine idles around 20Hz and revs up to 140Hz, at 60/2 and 8 cylinders we have about 20Khz events
|
|
|
|
* If we can read buffer at 50Hz we want buffer to be about 400 elements.
|
|
|
|
*/
|
2020-04-13 17:47:54 -07:00
|
|
|
static composite_logger_s buffer[COMPOSITE_PACKET_COUNT] CCM_OPTIONAL;
|
2020-10-25 19:26:20 -07:00
|
|
|
static composite_logger_s *ptr_buffer_first = &buffer[0];
|
|
|
|
static composite_logger_s *ptr_buffer_second = &buffer[(COMPOSITE_PACKET_COUNT/2)-1];
|
2019-07-06 17:48:58 -07:00
|
|
|
static size_t NextIdx = 0;
|
|
|
|
static volatile bool ToothLoggerEnabled = false;
|
2020-10-25 19:26:20 -07:00
|
|
|
static volatile bool firstBuffer = true;
|
2019-07-06 17:48:58 -07:00
|
|
|
static uint32_t lastEdgeTimestamp = 0;
|
|
|
|
|
2021-07-02 16:28:09 -07:00
|
|
|
static bool currentTrigger1 = false;
|
|
|
|
static bool currentTrigger2 = false;
|
|
|
|
static bool currentTdc = false;
|
2020-05-25 21:07:18 -07:00
|
|
|
// any coil, all coils thrown together
|
2021-07-02 16:28:09 -07:00
|
|
|
static bool currentCoilState = false;
|
2020-05-25 21:07:18 -07:00
|
|
|
// same about injectors
|
2021-07-02 16:28:09 -07:00
|
|
|
static bool currentInjectorState = false;
|
2020-04-13 17:47:54 -07:00
|
|
|
|
2020-05-26 19:30:53 -07:00
|
|
|
int getCompositeRecordCount() {
|
|
|
|
return NextIdx;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:36:10 -07:00
|
|
|
|
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
#include "logicdata.h"
|
|
|
|
int copyCompositeEvents(CompositeEvent *events) {
|
2020-12-08 10:59:40 -08:00
|
|
|
for (size_t i = 0; i < NextIdx; i++) {
|
2020-07-19 21:36:10 -07:00
|
|
|
CompositeEvent *event = &events[i];
|
2020-07-20 06:04:29 -07:00
|
|
|
event->timestamp = SWAP_UINT32(buffer[i].timestamp);
|
2020-07-19 21:36:10 -07:00
|
|
|
event->primaryTrigger = buffer[i].priLevel;
|
|
|
|
event->secondaryTrigger = buffer[i].secLevel;
|
2020-10-25 19:26:20 -07:00
|
|
|
event->isTDC = buffer[i].trigger;
|
2020-07-19 21:36:10 -07:00
|
|
|
event->sync = buffer[i].sync;
|
|
|
|
event->coil = buffer[i].coil;
|
|
|
|
event->injector = buffer[i].injector;
|
|
|
|
}
|
|
|
|
return NextIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // EFI_UNIT_TEST
|
|
|
|
|
2021-08-15 10:55:28 -07:00
|
|
|
static void setToothLogReady(bool value) {
|
|
|
|
#if EFI_TUNER_STUDIO
|
2021-12-07 17:18:47 -08:00
|
|
|
engine->outputChannels.toothLogReady = value;
|
2021-08-15 10:55:28 -07:00
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
static void SetNextCompositeEntry(efitick_t timestamp) {
|
2020-04-19 17:46:29 -07:00
|
|
|
uint32_t nowUs = NT2US(timestamp);
|
2020-10-25 19:26:20 -07:00
|
|
|
|
2019-07-06 17:48:58 -07:00
|
|
|
// TS uses big endian, grumble
|
2020-04-13 17:47:54 -07:00
|
|
|
buffer[NextIdx].timestamp = SWAP_UINT32(nowUs);
|
2021-07-02 16:28:09 -07:00
|
|
|
buffer[NextIdx].priLevel = currentTrigger1;
|
|
|
|
buffer[NextIdx].secLevel = currentTrigger2;
|
|
|
|
buffer[NextIdx].trigger = currentTdc;
|
2021-07-03 07:37:03 -07:00
|
|
|
buffer[NextIdx].sync = engine->triggerCentral.triggerState.getShaftSynchronized();
|
2021-07-02 16:28:09 -07:00
|
|
|
buffer[NextIdx].coil = currentCoilState;
|
|
|
|
buffer[NextIdx].injector = currentInjectorState;
|
2020-04-13 17:47:54 -07:00
|
|
|
|
2019-07-06 17:48:58 -07:00
|
|
|
NextIdx++;
|
|
|
|
|
2020-04-13 17:47:54 -07:00
|
|
|
static_assert(sizeof(composite_logger_s) == COMPOSITE_PACKET_SIZE, "composite packet size");
|
|
|
|
|
2020-10-25 19:26:20 -07:00
|
|
|
//If we hit the end, loop
|
|
|
|
if ((firstBuffer) && (NextIdx >= (COMPOSITE_PACKET_COUNT/2))) {
|
|
|
|
/* first half is full */
|
2021-08-15 10:55:28 -07:00
|
|
|
setToothLogReady(true);
|
2020-10-25 19:26:20 -07:00
|
|
|
firstBuffer = false;
|
|
|
|
}
|
|
|
|
if ((!firstBuffer) && (NextIdx >= sizeof(buffer) / sizeof(buffer[0]))) {
|
2021-08-15 10:55:28 -07:00
|
|
|
setToothLogReady(true);
|
2019-07-06 17:48:58 -07:00
|
|
|
NextIdx = 0;
|
2020-10-25 19:26:20 -07:00
|
|
|
firstBuffer = true;
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
2020-10-25 19:26:20 -07:00
|
|
|
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void LogTriggerTooth(trigger_event_e tooth, efitick_t timestamp) {
|
2019-07-06 17:48:58 -07:00
|
|
|
// bail if we aren't enabled
|
2019-09-03 17:35:52 -07:00
|
|
|
if (!ToothLoggerEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-06 17:48:58 -07:00
|
|
|
|
2020-10-16 08:04:27 -07:00
|
|
|
// Don't log at significant engine speed
|
2022-01-20 19:06:20 -08:00
|
|
|
if (Sensor::getOrZero(SensorType::Rpm) > engineConfiguration->engineSnifferRpmThreshold) {
|
2020-10-16 08:04:27 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopePerf perf(PE::LogTriggerTooth);
|
|
|
|
|
2020-04-13 17:47:54 -07:00
|
|
|
/*
|
|
|
|
// We currently only support the primary trigger falling edge
|
|
|
|
// (this is the edge that VR sensors are accurate on)
|
|
|
|
// Since VR sensors are the most useful case here, this is okay for now.
|
|
|
|
if (tooth != SHAFT_PRIMARY_FALLING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t nowUs = NT2US(timestamp);
|
|
|
|
// 10us per LSB - this gives plenty of accuracy, yet fits 655.35 ms in to a uint16
|
|
|
|
uint16_t delta = static_cast<uint16_t>((nowUs - lastEdgeTimestamp) / 10);
|
|
|
|
lastEdgeTimestamp = nowUs;
|
|
|
|
|
|
|
|
SetNextEntry(delta);
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (tooth) {
|
|
|
|
case SHAFT_PRIMARY_FALLING:
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTrigger1 = false;
|
2020-04-13 17:47:54 -07:00
|
|
|
break;
|
|
|
|
case SHAFT_PRIMARY_RISING:
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTrigger1 = true;
|
2020-04-13 17:47:54 -07:00
|
|
|
break;
|
|
|
|
case SHAFT_SECONDARY_FALLING:
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTrigger2 = false;
|
2020-04-13 17:47:54 -07:00
|
|
|
break;
|
|
|
|
case SHAFT_SECONDARY_RISING:
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTrigger2 = true;
|
2020-04-13 17:47:54 -07:00
|
|
|
break;
|
2021-07-02 17:12:31 -07:00
|
|
|
// major hack to get most value of limited logic data write
|
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
case SHAFT_3RD_FALLING:
|
|
|
|
currentCoilState = false;
|
|
|
|
break;
|
|
|
|
case SHAFT_3RD_RISING:
|
|
|
|
currentCoilState = true;
|
|
|
|
break;
|
|
|
|
#endif
|
2020-04-13 17:47:54 -07:00
|
|
|
default:
|
|
|
|
break;
|
2019-09-03 17:35:52 -07:00
|
|
|
}
|
2019-07-06 17:48:58 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
SetNextCompositeEntry(timestamp);
|
2020-04-19 17:46:29 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void LogTriggerTopDeadCenter(efitick_t timestamp) {
|
2020-04-19 17:46:29 -07:00
|
|
|
// bail if we aren't enabled
|
|
|
|
if (!ToothLoggerEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTdc = true;
|
2021-11-16 01:15:29 -08:00
|
|
|
SetNextCompositeEntry(timestamp);
|
2021-07-02 16:28:09 -07:00
|
|
|
currentTdc = false;
|
2021-11-16 01:15:29 -08:00
|
|
|
SetNextCompositeEntry(timestamp + 10);
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void LogTriggerCoilState(efitick_t timestamp, bool state) {
|
2020-05-25 21:07:18 -07:00
|
|
|
if (!ToothLoggerEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-02 16:28:09 -07:00
|
|
|
currentCoilState = state;
|
2020-10-25 19:26:20 -07:00
|
|
|
UNUSED(timestamp);
|
2021-11-16 01:15:29 -08:00
|
|
|
//SetNextCompositeEntry(timestamp, trigger1, trigger2, trigger);
|
2020-05-25 21:07:18 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void LogTriggerInjectorState(efitick_t timestamp, bool state) {
|
2020-05-25 21:07:18 -07:00
|
|
|
if (!ToothLoggerEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-02 16:28:09 -07:00
|
|
|
currentInjectorState = state;
|
2020-10-25 19:26:20 -07:00
|
|
|
UNUSED(timestamp);
|
2021-11-16 01:15:29 -08:00
|
|
|
//SetNextCompositeEntry(timestamp, trigger1, trigger2, trigger);
|
2020-05-25 21:07:18 -07:00
|
|
|
}
|
|
|
|
|
2019-07-06 17:48:58 -07:00
|
|
|
void EnableToothLogger() {
|
|
|
|
// Clear the buffer
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
|
|
|
// Reset the last edge to now - this prevents the first edge logged from being bogus
|
2020-01-19 19:23:41 -08:00
|
|
|
lastEdgeTimestamp = getTimeNowUs();
|
2019-07-06 17:48:58 -07:00
|
|
|
|
|
|
|
// Reset write index
|
|
|
|
NextIdx = 0;
|
|
|
|
|
|
|
|
// Enable logging of edges as they come
|
|
|
|
ToothLoggerEnabled = true;
|
|
|
|
|
2021-08-15 10:55:28 -07:00
|
|
|
|
|
|
|
setToothLogReady(false);
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
|
|
|
|
2020-05-26 19:30:53 -07:00
|
|
|
void EnableToothLoggerIfNotEnabled() {
|
|
|
|
if (!ToothLoggerEnabled) {
|
|
|
|
EnableToothLogger();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 17:48:58 -07:00
|
|
|
void DisableToothLogger() {
|
|
|
|
ToothLoggerEnabled = false;
|
2021-08-15 10:55:28 -07:00
|
|
|
setToothLogReady(false);
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ToothLoggerBuffer GetToothLoggerBuffer() {
|
2021-08-15 11:14:13 -07:00
|
|
|
// tell TS that we do not have data until we have data again
|
|
|
|
setToothLogReady(false);
|
2020-10-25 19:26:20 -07:00
|
|
|
if (firstBuffer) {
|
|
|
|
return { reinterpret_cast<uint8_t*>(ptr_buffer_second), (sizeof(buffer)/2) };
|
|
|
|
} else {
|
|
|
|
return { reinterpret_cast<uint8_t*>(ptr_buffer_first), (sizeof(buffer)/2) };
|
|
|
|
}
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
2019-07-07 12:22:46 -07:00
|
|
|
|
2020-10-25 19:26:20 -07:00
|
|
|
|
2019-07-07 12:22:46 -07:00
|
|
|
#endif /* EFI_TOOTH_LOGGER */
|