SD logging: cutting out intermediate buffer since we already have nicely buffered writer

This commit is contained in:
rusefillc 2022-10-11 23:41:18 -04:00
parent f0c81cc658
commit 64e0f660bc
2 changed files with 13 additions and 23 deletions

View File

@ -15,8 +15,6 @@
// floating number of seconds with millisecond precision // floating number of seconds with millisecond precision
static scaled_channel<uint32_t, TIME_PRECISION> packedTime; static scaled_channel<uint32_t, TIME_PRECISION> packedTime;
// todo: we are at the edge of sdLogBuffer size and at the moment we have no code to make sure buffer does not overflow
// todo: make this logic smarter
// The list of logged fields lives in a separate file so it can eventually be tool-generated // The list of logged fields lives in a separate file so it can eventually be tool-generated
#include "log_fields_generated.h" #include "log_fields_generated.h"
@ -30,8 +28,6 @@ static constexpr uint16_t computeFieldsRecordLength() {
} }
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
// this one needs to be in main ram so that SD card SPI DMA works fine
static NO_CACHE char sdLogBuffer[250];
static uint64_t binaryLogCount = 0; static uint64_t binaryLogCount = 0;
extern bool main_loop_started; extern bool main_loop_started;
@ -44,9 +40,7 @@ void writeSdLogLine(Writer& bufferedWriter) {
writeFileHeader(bufferedWriter); writeFileHeader(bufferedWriter);
} else { } else {
updateTunerStudioState(); updateTunerStudioState();
size_t length = writeBlock(sdLogBuffer); writeBlock(bufferedWriter);
efiAssertVoid(OBD_PCM_Processor_Fault, length <= efi::size(sdLogBuffer), "SD log buffer overflow");
bufferedWriter.write(sdLogBuffer, length);
} }
binaryLogCount++; binaryLogCount++;
@ -104,7 +98,9 @@ static uint8_t blockRollCounter = 0;
//static efitimeus_t prevSdCardLineTime = 0; //static efitimeus_t prevSdCardLineTime = 0;
size_t writeBlock(char* buffer) { void writeBlock(Writer& outBuffer) {
static char buffer[16];
// Offset 0 = Block type, standard data block in this case // Offset 0 = Block type, standard data block in this case
buffer[0] = 0; buffer[0] = 0;
@ -117,31 +113,25 @@ size_t writeBlock(char* buffer) {
buffer[2] = timestamp >> 8; buffer[2] = timestamp >> 8;
buffer[3] = timestamp & 0xFF; buffer[3] = timestamp & 0xFF;
outBuffer.write(buffer, 4);
// todo: add a log field for SD card period // todo: add a log field for SD card period
// prevSdCardLineTime = nowUs; // prevSdCardLineTime = nowUs;
packedTime = getTimeNowMs() * 1.0 / TIME_PRECISION; packedTime = getTimeNowMs() * 1.0 / TIME_PRECISION;
// Offset 4 = field data
const char* dataBlockStart = buffer + 4;
char* dataBlock = buffer + 4;
uint8_t sum = 0; uint8_t sum = 0;
for (size_t fieldIndex = 0; fieldIndex < efi::size(fields); fieldIndex++) { for (size_t fieldIndex = 0; fieldIndex < efi::size(fields); fieldIndex++) {
size_t entrySize = fields[fieldIndex].writeData(dataBlock); size_t entrySize = fields[fieldIndex].writeData(buffer);
for (size_t byteIndex = 0; byteIndex < entrySize; byteIndex++) { for (size_t byteIndex = 0; byteIndex < entrySize; byteIndex++) {
// "CRC" at the end is just the sum of all bytes // "CRC" at the end is just the sum of all bytes
sum += dataBlock[byteIndex]; sum += buffer[byteIndex];
} }
outBuffer.write(buffer, entrySize);
// Increment pointer to next entry
dataBlock += entrySize;
} }
size_t dataBlockSize = dataBlock - dataBlockStart; buffer[0] = sum;
// 1 byte checksum footer
*dataBlock = sum; outBuffer.write(buffer, 1);
// Total size has 4 byte header + 1 byte checksum
return dataBlockSize + 5;
} }

View File

@ -7,4 +7,4 @@
struct Writer; struct Writer;
void writeFileHeader(Writer& buffer); void writeFileHeader(Writer& buffer);
void writeSdLogLine(Writer& buffer); void writeSdLogLine(Writer& buffer);
size_t writeBlock(char* buffer); void writeBlock(Writer& outBuffer);