I'm trying to write to an SD card using an STM32F303 controller using STM32 HAL and the FATFS libraries. I'm able to mount the card and successfully write the file to the SD card and write values to it for some time. However after some time, the controller haults and the watchdog timer resets the controller. The project contains multiple peripherals running(SPI, CAN, UART, ADCs). I've tried debugging this issue with the STlink and OpenOCD gdb debugger on vscode, and I've narrowed down the issue to this part of the code (SDcard_logtoCSV function) :
/*File system declerations */
FATFS fs; // file system
FIL log_file; // File
FILINFO fno;
FRESULT fresult; // result
UINT br, bw; // File read/write count
#define BUFFER_SIZE 1024
char stringBuffer[BUFFER_SIZE];
int bufsize (char *buf)
{
int i=0;
while (*buf++ != '\0') i++;
return i;
};
void clear_buffer (void)
{
for (int i=0; i<BUFFER_SIZE; i++) stringBuffer[i] = '\0';
};
void SDcard_logtoCSV(void)
{
if(HAL_GetTick() - lastTick >= LOGGING_INTERVAL)
{
lastTick = HAL_GetTick();
fresult = f_open(&log_file, "Log_File.csv", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
fresult = f_lseek(&log_file, f_size(&log_file));
sprintf(stringBuffer, "%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%d,%d,%d,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f \r\n",
(HAL_GetTick()/1000.0),
packVoltage,packCurrent,packPower,SoC,SoCCapacityAh,operationalState,
faultState,cycleCount,cellVoltageHigh,cellVoltageLow,cellVoltageMisMatch,
tempBatteryHigh,tempBatteryLow,tempBatteryAverage,
cellVoltagesIndividual[0].cellVoltage,
cellVoltagesIndividual[1].cellVoltage,
cellVoltagesIndividual[2].cellVoltage,
cellVoltagesIndividual[3].cellVoltage,
cellVoltagesIndividual[4].cellVoltage,
cellVoltagesIndividual[5].cellVoltage,
cellVoltagesIndividual[6].cellVoltage,
cellVoltagesIndividual[7].cellVoltage,
cellVoltagesIndividual[8].cellVoltage,
cellVoltagesIndividual[9].cellVoltage,
cellVoltagesIndividual[10].cellVoltage,
cellVoltagesIndividual[11].cellVoltage,
cellVoltagesIndividual[12].cellVoltage,
cellVoltagesIndividual[13].cellVoltage,
cellVoltagesIndividual[14].cellVoltage,
cellVoltagesIndividual[15].cellVoltage,
cellVoltagesIndividual[16].cellVoltage,
cellVoltagesIndividual[17].cellVoltage,
auxVoltagesIndividual[2].auxVoltage,
auxVoltagesIndividual[3].auxVoltage,
auxVoltagesIndividual[4].auxVoltage,
auxVoltagesIndividual[5].auxVoltage,
auxVoltagesIndividual[6].auxVoltage,
auxVoltagesIndividual[7].auxVoltage,
auxVoltagesIndividual[8].auxVoltage);
fresult = f_write(&log_file,stringBuffer, bufsize(stringBuffer), &bw);
f_close(&log_file);
clear_buffer();
}
};
Is there some memory leak or memory allocation issue occuring with this implementation, as multiple other processes are also running on the controller?
My heap and stack allocation is :
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
The RAM is 40k and flash memory is 256k. Please let me know what the issue could be and possible solutions, thanks!