This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28379D: Incorrect numerical conversion with "strtod" function

Part Number: TMS320F28379D
Other Parts Discussed in Thread: LAUNCHXL-F28379D, , C2000WARE

Tool/software:

Hello, 

I am currently working with the LAUNCHXL-F28379D, specifically on reading numerical data from an SD card. I am encountering an issue with the use of the strtod function.
Below, I have included the code being used and the content of the text file in the SD card.
During debugging, I have observed that the data in string form is read correctly and stored in the variable 'tok'. However, after using the strtod function, the numerical values stored in Array are always 10, regardless of the input string.

What could be the problem?

Thank you.


Code:

/*
* SD_Card.c
*/

#include "driverlib.h"
#include "device.h"
#include "board.h"
#include <string.h>
#include <stdlib.h>

#include <sdspi/sdspi.h>
#include <sdspi/SDFatFS.h>

double var1;
double var2;
double var3;
double var4;
double var5;


uint16_t SDFatFS_config_count = 1;
SDFatFS_Object sdfatfsObject;

SDSPI_Object sdspiObject = {
.spiHandle = mySDCardSPI_BASE,
.spiCsGpioIndex = mySDCardCS
};

SDFatFS_Object* SDFatFS_config [] = {&sdfatfsObject};

SDSPI_Handle sdspiHandle = &sdspiObject;

/* String conversion macro */
#define STR_(n) #n
#define STR(n) STR_(n)

/* Drive number used for FatFs */
#define DRIVE_NUM 0

FRESULT fresult;
FIL src;

#define ARRAY_SIZE 400

char lettura [ARRAY_SIZE];
double Array [20];

void Lettura_SD (void)
{
Board_init();

SDFatFS_init();
SDFatFS_Handle sdFatFs_handle = SDFatFS_open(sdspiHandle, DRIVE_NUM);
if (sdFatFs_handle == NULL)
{
while(1);
}

// Lettura file con parametri

char inputfile[] = STR(DRIVE_NUM)":input.txt";

fresult = f_open(&src, inputfile, FA_READ);

if (fresult != FR_OK) {
while(1);
}

uint16_t bw;

f_read(&src, lettura, 40, &bw);

char* tok;
char* stopstring;

tok = strtok(lettura, ":");
tok = strtok(NULL, ":");

int N_variables = atoi(tok);

int i;

for (i = 0; i<N_variables; i++)
{
tok = strtok(NULL, ":");
Array[i] = strtod(tok, &stopstring);
}

fresult = f_close(&src);
if (fresult != FR_OK)
{
while(1);
}

var1 = Array[0];
var2 = Array[1];
var3 = Array[2];
var4 = Array[3];
var5 = Array[4];

}

int32_t fatfs_getFatTime(void)
{
return 0;
}



Text file:

N.variables:5
var1:17
var2:53.4
var3:0.003
var4:101232
var5:1.30467




  • Hi, 

    We are looking into it and will provide a response by Friday, 28th June. 

    Thanks. 

  • I have brought this thread to the attention of the compiler experts.

    Please note that due the holiday in the US, responses will be delayed. 

    Thank you for your patience.

  • To summarize, you are saying that for this code ...

    tok = "17";
    val = strtod(tok, &stopstring);

    The value returned by strtod is 10.0.  Is that correct?

    Thanks and regards,

    -George

  • Hi,

    Yes, the value returned by strtod is 10.0. Additionally, despite the subsequent iterations where the value of tok gets updated with the other values in the text file, the value returned by strtod remains 10.0.

  • I am unable to reproduce that result.  What version of the compiler (not CCS) do you use?  Please show the compiler build options.  Copy and paste the text of the options, and do not use a screenshot.

    Thanks and regards,

    -George

  • I apologize for the delayed response. The compiler version we are using is TI v22.6.1.LTS. Regarding the compiler build options, I am attaching a screenshot of the project properties. I'm sorry, but I'm not sure which specific options you need. Could you please guide me on how to locate the information you require?
    Thank you. 

  • Here is another way to get me the build options.  Please rebuild the entire project.  One way to do that is to right-click on the name of the project and select Rebuild Project.  Then save the contents of the Console (not Problems) view to a text file.  Use the icon named Copy Build Log (older versions of CCS) or Save build log to file (newer versions of CCS).  When you name the log file, be sure to use the file extension .txt.  Please attach that text file to your next post.

    Another experiment you can try ... Increase the stack as much as you can.  Does that help?

    Thanks and regards,

    -George

  • I tried to increase the stack but the issue persists. 
    Please find the requested text file attached below.

    **** Build of configuration CPU1_FLASH for project Test_SD2_strtod03 ****
    
    "C:\\ti\\ccs1250\\ccs\\utils\\bin\\gmake" -k -j 8 all -O 
     
    Building file: "../device/F2837xD_CodeStartBranch.asm"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="device/F2837xD_CodeStartBranch.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg" --obj_directory="device"  "../device/F2837xD_CodeStartBranch.asm"
    Finished building: "../device/F2837xD_CodeStartBranch.asm"
     
    Building file: "../GlobalVariables.c"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="GlobalVariables.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg"  "../GlobalVariables.c"
    Finished building: "../GlobalVariables.c"
     
    Building file: "../SD_Card.c"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="SD_Card.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg"  "../SD_Card.c"
    Finished building: "../SD_Card.c"
     
    Building file: "../empty_driverlib_main.c"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="empty_driverlib_main.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg"  "../empty_driverlib_main.c"
    Finished building: "../empty_driverlib_main.c"
     
    Building file: "../board.c"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="board.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg"  "../board.c"
    Finished building: "../board.c"
     
    Building file: "../device/device.c"
    Invoking: C2000 Compiler
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/libraries/fatfs" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/common/include" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/device_support/f2837xd/headers/include" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/device" --include_path="C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib" --include_path="C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi --preproc_with_compile --preproc_dependency="device/device.d_raw" --include_path="C:/Users/Fust/workspace_convertitoreDCDC/Test_SD2_strtod03/CPU1_FLASH/syscfg" --obj_directory="device"  "../device/device.c"
    Finished building: "../device/device.c"
     
    Building target: "Test_SD2_strtod03.out"
    Invoking: C2000 Linker
    "C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000" -v28 -ml -mt --cla_support=cla1 --float_support=fpu64 --tmu_support=tmu0 --vcu_support=vcu2 -Ooff --advice:performance=all --define=_LAUNCHXL_F28379D --define=_DUAL_HEADERS --define=DEBUG --define=_FLASH --define=CPU1 --diag_suppress=10063 --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi -z -m"Test_SD2_strtod03.map" --stack_size=0x3F8 --warn_sections -i"C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/lib" -i"C:/ti/ccs1250/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include" --reread_libs --diag_wrap=off --display_error_number --xml_link_info="Test_SD2_strtod03_linkInfo.xml" --entry_point=code_start --rom_model -o "Test_SD2_strtod03.out" "./GlobalVariables.obj" "./SD_Card.obj" "./board.obj" "./empty_driverlib_main.obj" "./device/F2837xD_CodeStartBranch.obj" "./device/device.obj" "../2837xD_FLASH_lnk_cpu1.cmd" "C:/ti/c2000/C2000Ware_5_01_00_00/driverlib/f2837xd/driverlib/ccs/Debug/driverlib.lib" "C:/Users/Fust/Downloads/C2000_5/C2000Ware_5_01_00_00/libraries/fatfs/ccs/f2837xd/Debug/fatfs.lib"  -llibc.a 
    <Linking>
    Finished building target: "Test_SD2_strtod03.out"
     
    
    **** Build Finished ****
    


  • Thank you for submitting the build options.  Unfortunately, I remain unable to reproduce the issue.

    At this point, I am doubtful the compiler or the RTS routine strtod are the cause of the problem.  Something else must be wrong.  I have changed responsibility for this thread to the team that can help you debug the problem.

    Thanks and regards,

    -George

  • Hi Giampiero,

    I apologize for the delay. Can you give an example of what the lettura array would contain so I can understand the expected vs. actual behavior going on?

    Best Regards,

    Delaney

  • Hi, 
    The 'lettura' array should contain the individual characters from the text file (as shown in the previous message where I attached the code, and also included below for reference). Specifically, the first element in the array 'lettura' is 'N', the second is '.', the third is 'v', and so on. I have verified using breakpoints, that the 'lettura' array is correctly filled with the characters from the text file. Additionally, I checked the use of 'strtok' with breakpoints and the array 'lettura' is correctly segmented in this case as well.

    The issue arises when attempting to convert the 'tok' variable to a number using 'strtod'. For example, in the case of the first value to be read -17- despite 'tok' contains a series of characters where the first ones corresponds to "1" and "7", the value saved in the array after converting in double is 10.0. The same happens with other values: no matter what numbers are present in 'tok', the value stored in the array 'Array' via 'strtod' is always 10.0.
    This problem does not occur if, instead of converting the string in a double, I use 'strtof' to convert to a float. 
    I hope this clarifies the issue and provides a satisfactory explanation. 
    Thank you in advance for your response. 



    Text file:

    N.variables:5
    var1:17
    var2:53.4
    var3:0.003
    var4:101232
    var5:1.30467


  • HI Giampiero,

    I apologize for the late reply. Thank you for the explanation of the problem. I am unfortunately also unable to reproduce the issue by itself. I am going to loop in the SPI SD Card expert to see if they can help with reproducing the issue using the full code you have provided since I am not familiar with this library.

    Best Regards,

    Delaney

  • Hello, 
    I have conducted further tests and it seems that the issue may not be directly related to the strtod function itself.
    Since there are no specific examples for reading from an SD card for the microcontroller I am using (tms320f28379d), I previously asked this forum for guidance. I was advised to follow these steps:

    • Add the fatfs.lib library related to my microcontroller in Library Resources;
    • Include the path in C2000 Compiler-> Include Options-> .../C2000Ware_5_01_00_00/libraries/fatfs
    • After doing this, I look at the example file from the following path C2000Ware_5_01_00_00\driverlib\f2838x\examples\c28x\sdspi as suggested in the forum, which is for a different board than the one I am working on. So, I added the board.h and board.c files from that example to my project.
    • Finally, I created a .h and .c file where I followed the example for another type of board to read data from my SD card.

    Here's what I observed: as long as I use strtod to convert a string defined directly in the code and execute all steps above except adding the SD_card.h and SD_card.c (that handles SD card reading), conversion works correctly. 
    However, since I add the SD_card.h and SD_card.c files, the code stop functioning correctly. Specifically, if I set C2000 Compiler -> Processor Options-> float_support to fpu32, the values stored in num remains 0.00. If I set it to fpu64, it always stores 10.00.

    I attached below the code used for my tests.

    Here the version of the file that works:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "driverlib.h"
    #include "device.h"
    
    double num;
    char* stringa = "12.54fd";
    
    //
    // Main
    //
    void main(void)
    {
        Device_init();
        Device_initGPIO();
    
        while(1)
        {
            char* stopstring;
            num = strtod(stringa, &stopstring);
        }
    }



    While below there is the version that doesn't work:

    empty_driverlib_main.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "driverlib.h"
    #include "device.h"
    
    #include "SD_card.h"
    
    double num;
    char* stringa = "12.54fd";
    
    //
    // Main
    //
    void main(void)
    {
        Device_init();
        Device_initGPIO();
    
        Lettura_SD();
    
        while(1)
        {
            char* stopstring;
            num = strtod(stringa, &stopstring);
        }
    }


    SD_card.c:

    #include "driverlib.h"
    #include "device.h"
    #include "board.h"
    
    #include <sdspi/sdspi.h>
    #include <sdspi/SDFatFS.h>
    
    uint16_t SDFatFS_config_count = 1;
    SDFatFS_Object sdfatfsObject;
    
    SDSPI_Object sdspiObject = {
            .spiHandle = mySDCardSPI_BASE,
            .spiCsGpioIndex = mySDCardCS
    };
    
    SDFatFS_Object* SDFatFS_config [] = {&sdfatfsObject};
    
    SDSPI_Handle sdspiHandle = &sdspiObject;
    
    /* String conversion macro */
    #define STR_(n)             #n
    #define STR(n)              STR_(n)
    
    /* Drive number used for FatFs */
    #define DRIVE_NUM           0
    
    FRESULT fresult;
    FIL src;
    
    #define ARRAY_SIZE 400
    
    void Lettura_SD ()
    {
        Board_init();
    
        SDFatFS_init();
        SDFatFS_Handle sdFatFs_handle = SDFatFS_open(sdspiHandle, DRIVE_NUM);
        if (sdFatFs_handle == NULL)
        {
            while(1);
        }
    
        // Lettura file con parametri
    
        char inputfile[] = STR(DRIVE_NUM)":input.txt";
    
        fresult = f_open(&src, inputfile, FA_READ);
    
        if (fresult != FR_OK) {
            while(1);
        }
    
        fresult = f_close(&src);
        if (fresult != FR_OK)
        {
            while(1);
        }
    }
    
    int32_t fatfs_getFatTime(void)
    {
        return 0;
    }



    I hope this provide additional insight for resolving the issue.

    Thank you in advance for your help.

  • Hi Giampiero,

    Thank you for the detailed follow-up and debugging efforts. We will recreate the issue and get back to you with a response within a few days. Once again, apologies for the delay, and thanks for the reply.

    Regards,

    Arnav