SensorTile SD Card Data Logger filename improvement

Blog Post created by beals.william on Mar 29, 2018

I have been coming up to speed with the sensors on the SensorTile mainly using the SD card data logger example code.  One confusing part for data gathering is after each power cycle, the logger starts over with "SensorTile_Log_N000.csv", erasing and over-writing any previous log.  Since time stamps are not possible, this means any power cycle (intentional or not) means you start overwriting previous logs and have no way of knowing other than missing files and no way to know which files may have gone missing.  Below is a modification of the DATALOG_SD_Log_Enable() routine in datalog_application.c that instead skips over any already existing log files and opens up a file with the first unused serial number it finds.  No data lost!  I'm new to fatfs, so it may not be bullet-proof code, but at least works.

(wmb's are just my tag to know changes I've made to the original distributions)


// wmb: Updated version of the log file creation routine.  This routine finds the first unused log file
// wmb: number and opens up a log file with that number.  That way previous log files are preserved and
// wmb: new logfiles get later numbers.
uint8_t DATALOG_SD_Log_Enable(void)
  uint16_t sdcard_file_counter = 0;
  char header[] = "T [ms],AccX [mg],AccY [mg],AccZ [mg],GyroX [mdps],GyroY [mdps],GyroZ [mdps],MagX [mgauss],MagY [mgauss],MagZ [mgauss],P [mB],T [°C],H [%]\r\n";
  uint32_t byteswritten; /* written byte count */
  char file_name[30] = {0};
  /* SD SPI CS Config */


  // Loop with incrementing log file numbers until file name not found (means available)
    sprintf(file_name, "%s%.3d%s", "SensorTile_Log_N", sdcard_file_counter, ".csv");
  } while (f_stat(file_name,NULL) != FR_NO_FILE) ;


  if(f_open(&MyFile, (char const*)file_name, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
    return 0;


  if(f_write(&MyFile, (const void*)&header, sizeof(header)-1, (void *)&byteswritten) != FR_OK)
    return 0;
  return 1;

Hope you find it useful!  Any comments about making it better appreciated too.