2024-12-25 01:53 PM - last edited on 2024-12-26 12:32 AM by SofLit
I am working on a data logger project using SDIO with DMA and FreeRTOS. The only sensor currently in use is the BME280, which is working fine. However, I am encountering issues when writing the CSV file, such as glitches, character repetition, and other seemingly random problems.
Here is the relevant code:
/* USER CODE BEGIN Header_StartSDTask */
#define BUFFER_SIZE 4096
#define BUFFER_THRESHOLD BUFFER_SIZE/4*3
#define HEADER "Time;BaroTemperature;BaroPressure;BaroHumidity;PitotTemperature;PitotDiffPressure;Latitude;Longitude;IMUax;IMUay;IMUaz;IMUgx;IMUgy;IMUgz;IMUmx;IMUmy;IMUmz\n"
char buffer[BUFFER_SIZE] = HEADER;
uint8_t sensorsResponse;
uint16_t adress = sizeof(HEADER) - 1;
uint32_t maxAdress;
uint32_t lastTime;
uint16_t count = 0;
uint16_t count2 = 0;
char adressCarac;
void bufferize() {
adress += snprintf(&buffer[adress], (BUFFER_SIZE - adress), "%lu;", lastTime);
// Additional sensor data processing...
}
/**
* @brief Function implementing the SDTask thread.
* @PAram argument: Not used
* @retval None
*/
/* USER CODE END Header_StartSDTask */
void StartSDTask(void *argument) {
/* USER CODE BEGIN StartSDTask */
uint8_t sensorID;
uint8_t writeCount;
logger.initialize();
logger.createFile("log.csv");
/* Infinite loop */
for (;;) {
while (osMessageQueueGet(SensorsQueueHandle, &sensorID, NULL, osWaitForever) == osOK) {
// Sensor data processing...
if (adress >= BUFFER_THRESHOLD) {
// Clearing the buffer after the threshold
logger.write(buffer, adress + 1);
writeCount++;
memset(buffer, 0, BUFFER_SIZE);
maxAdress = maxAdress > adress ? maxAdress : adress;
adress = 0;
if (writeCount >= 10) {
logger.flush();
writeCount = 0;
}
}
}
}
/* USER CODE END StartSDTask */
}
2024-12-25 06:01 PM
No value to using uint16_t, the MCU is 32-bit
You'd need to ensure the serialization of the buffer, so there isn't concurrent interaction of the buffer vs the writing. The writing (read/write) can take 100's of milli-seconds
I'd probably do writing in multiples of the sectors or clusters, and pull-down the overspill.
Make sure the glitches here aren't related to your write points.