Hello everyone
I need help understanding the sendDataSDCard() function method from the out-of-box example code for the MSP430FR5994 launchpad. The code is located in the SDCardLogMode translation unit and is copied below. This function reads a log file from the SD Card and sends it to a PC GUI app. My big two questions are
- How did sendDataSDCad() find an existing log file (it appears to create a new log) and
- Why are the first 5 strings discarded when the file is opened (assuming they are discarded) ?
My detailed walk through and understanding (immediately below) may help locate were I am confused (confusion is underneath in red).
- Assign HAL_SDCard callbacks to sdCardLib
- Check for sdcard
- Construct a logfile name by incrementing numLogFiles (a persistent variable)
- Here is my first tripping point. Wouldn’t this be a new file with empty data?
- If numLogFiles was reset somewhere before sendDataSDCard() then it could point to a real log file but (thankfully) doesn’t appear so.
- If my understanding is wrong and this does point to an existing log file why are the first 5 strings discarded (assuming that they are)? Is it possible that this is a FatFs requirement when accessing a file? If not, the header below doesn’t look like it is 5 strings (but it’s possible that some unprintable characters (/r, /t, /n) were discarded).
“ SDCard Logging Start Time: “ + Timestamp “temperature and Voltage (12-bit ADC raw data):”
- Open the file. If successful, set numData = 0. If not successful, then skip the next five f_gets() strings (up to 32 bytes) and then starts counting the number of remaining strings. Store count in numDat.
-
- Is there anything to count?
2 Close the file
3 Condition GPIO pins for UART operation.
-
- Is the 900000 cycle delay to allow the UART to finish transmitting? Why not poll the Tx Flag instead to allow for different Baud rates?
4 Send the numDat count to the PC GUI App.
-
- The comment above says “send FRAM index”. No FRAM involved here. Maybe a residual from a copy n’ paste?
5 Open the file again and toss the next 5 buffers (strings)
-
- The text files (read using a PC) don’t appear to have 5 strings before temp/voltage data.
6 Then for each string in numDat: Break into substrings using the “ “ delimiter. First substring holds both the timestamp and Temp reading. Second string holds only the battery voltage.
-
- Is the comment “// Read TimeStamp from SDCard Log file” another stale copy n’ paste residual? It doesn’t make sense here.
thank you for your help
jim
void sendDataSDCard()
{
//Plugin SDcard interface to SDCard lib
SDCardLib_init(&sdCardLib, &sdIntf_MSP430FR5994LP);
//Detect SD card
SDCardLib_Status st = SDCardLib_detectCard(&sdCardLib);
if (st == SDCARDLIB_STATUS_NOT_PRESENT) {
SDCardLib_unInit(&sdCardLib);
mode = '0';
noSDCard = 1;
return;
}
// Construct log file's name
if (numLogFiles == 0) numLogFiles++;
strcpy(filename, "data_log/log_");
char num[5];
itoa(numLogFiles, num, 10);
strcat(filename, num);
strcat(filename, ".txt");
uint16_t numData = 0;
rc = f_open(&fil, filename, FA_READ | FA_OPEN_EXISTING);
if (rc) {
numData = 0;
}
else {
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
while (f_gets(buffer, MAX_BUF_SIZE, &fil) != NULL)
{
numData++;
}
}
rc = f_close(&fil);
// Select UART TXD on P2.0
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN0, GPIO_SECONDARY_MODULE_FUNCTION);
// Send FRAM Index
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)((numData)>>8));
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)(numData));
__delay_cycles(900000);
rc = f_open(&fil, filename, FA_READ | FA_OPEN_EXISTING);
if (rc) {
f_close(&fil);
SDCardLib_unInit(&sdCardLib);
return;
}
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
f_gets(buffer, MAX_BUF_SIZE, &fil);
uint16_t i;
char *dataStr;
int data;
for (i=0;i<numData;i++)
{
// Reads TimeStamp from SDCard log file
f_gets(buffer, MAX_BUF_SIZE, &fil);
dataStr = strtok(buffer, " ");
data = atoi(dataStr);
// Send logged Temperature Sensor ata
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)(data>>8));
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)(data));
dataStr = strtok(NULL, " ");
data = atoi(dataStr);
// Send logged Battery Monitor data
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)(data>>8));
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)(data));
}
rc = f_close(&fil);
SDCardLib_unInit(&sdCardLib);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
mode = '0';
}