cancel
Showing results for 
Search instead for 
Did you mean: 

Random Glitches in CSV Output Using SDIO, DMA, and FreeRTOS on STM32F405

AhaUhux
Visitor

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.

AhaUhux_4-1735174329940.png

 

AhaUhux_3-1735174298038.png

 

AhaUhux_2-1735174282724.png

 

AhaUhux_1-1735174256045.png

 

AhaUhux_0-1735174215338.png

 

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 */
}
  • The BME280 sensor is functioning properly.
  • The issue arises when writing to the .csv file, with glitches, character repetition, and other random problems.
  • I have monitored the task stack, which is using about 500 words of the 9000 available.
  • I suspected that the problem could be related to the use of snprintf, but I did not find any issues like .. or .xx. in the buffer.
  • I tried increasing the clockdivider to 4, but the problem persists.
  • I believe the issue may be related to FreeRTOS preemption, but I have enabled NewLibReentrant and FS_REENTRANT, and no other task uses the SDIO or FATFS.
1 REPLY 1

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..