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.

RTOS/SIMPLELINK-MSP432-SDK: RTOS/MSP432P401R: MCU does not get into deepsleep mode

Part Number: SIMPLELINK-MSP432-SDK

Tool/software: TI-RTOS

Hi Team,

This query was raised around 9 months ago but i did not get a confirm answer for that. 

So I am posting again with reference to the same query.

I am using the SimpleLink MSP432 SDK -V 1.40.00.28 version.

Initially I tried to run the "powerdeepsleep" example and found the when the msp432 goes into deepsleep mode by pressing the SW1 on the launchpad the value of "sleepstate" variable in the function "int_fast16_t Power_sleep(uint_fast16_t sleepState)" found in the PowerMSP432.c file changes to 4 (which is the macro value of PowerMSP432_DEEPSLEEP_1 as defined in the PowerMSP432.h file.

The current still is in milliamps (over 2 mA) as compare to 1.6uA that was mentioned in the post for which I have attached the link above.I have also attached screen shot for the current drawn measured and also when the state is changed.

Now I tried to execute the same concept in my firmware written which also has a watchdog timer based wakeup of unit every 6 hours.

The concept is that every 6 hours the mcu wakes up and does some work and once that task is done I am calling the "Power_setPolicy((Power_PolicyFxn)PowerMSP432_deepSleepPolicy);" which is the same function called in the powerdeepsleep example when the switch SW1 is pressed.

But in my case, the value of the sleepState variable never changes to 4 and always stays 1.

As a result the MCU neither goes into LPM3 nor in LPM4 mode.

Due to this the idle mode current drawn in my application shows 20 mA which is too much since ours is battery operated application.

I am attaching the code for reference.

Please if someone can help me out to get this confusion cleared would be great.

Thank you

Vikram   Demo code for Production board getting into Deepsleep.zip

Many thanks in advance.

  • Hello Vikram,

    The firmware I attached was tested with an SD Card. The only difference I see from your code is the fact that there is a power up sequence for the LDO to the SD Card which I do not have (and I had clearly highlighted that in my post). As I mentioned in my response with the code; I have tested this locally with an always powered SD Card and I do not see a file error.
  • Thank you Amit for the response.

    I understand that you won't have the same setup as mine but i am trying to understand why would it fail if I am doing a powering sequence.

    Will the different GPIO used to enable SD card will make difference?

    In your firmware you are using P4_6 and in my firmware i am using P8_0.

    Secondly, in your case you are using LCD part I am not using the same and lastly I see that in the MSP432.c file you have 2 seperate CS lines for SPI1 and SPI2 but i don't see in your fatsd.c  they been used?

    Lastly, will an RTC impact powering up the LDOs or the SD card enable signals.

    Vikram

  • Sorry one more question using this new library and CCS v8, i am getting compilation error for "f_printf" function used.
    I have commented out for now to compile but i need to use this function.

    I had this query posted earlier and that time one of the TI forum specialist had asked me to use "fatfs.am4fg" and "fatfs.aem4f" files built at his end which i had saved in the folder under the "C:\ti\simplelink_msp432p4_sdk_1_60_00_12\source\third_party\fatfs\lib" but now in the new SDK version istalled I don't see this files in the folder to replace. C
    Can you guide in this case?

    Vikram
  • Hello Vikram,

    I am not using an LCD part. My setup is with the MSP432P401R device. I am using one of the existing examples for SD Card from the SDK repository. The Board files are generally a superset board file for multiple examples and hence may have defines like that for LCD, etc which does not mean that LCD is being used.

    Since you are powering down the SD Card, are you providing enough delay for the power to the SD Card to stabilize before attempting an access?
    RTC is a timer which does not have bearing on the GPIO control for LCD.
  • Ok thank you much appreciated.

    And can you throw some light on the issue related to f_printf as i have posted earlier?

    Vikram
  • Hello Vikram,

    fwrite is now the nee API for file writes as I do not see any example referring to the f_printf.
  • But fwrite is cio library and i am using ff library.
    Also, i am trying to write some thing as "f_printf(&dst, "Version: \"%s\";\n",PROBE_VERSION);" how would i do this with fwrite?

    Above is only one line and I was able to do this in the earlier version of SDK 1.60 because it has library files as "fatfs.am4fg" and "fatfs.aem4f"
    so what happened to those files in the new SDK 2.10.

    Please find the post below that was in regards to the f_printf issue that i had raised for 1.60 version
    e2e.ti.com/.../621786

    Thank you
    Vikram
  • Hello Vikram,

    The APi structure for fwrite is

    fwrite(STRING, 1, LENGTH_OF_STRING, FILE HANDLER); The string will be constructed with string operations
  • I think what you are referring to is the one found in "#include <third_party/fatfs/ffcio.h>"
    I am already using equivalent of that which is f_write found in "#include <third_party/fatfs/ff.h>"

    As i mentioned I am using the sample example fatsdraw to build my application which is based on ff.h

    So not sure how would I write the stuff?
    Also on the point of the SD card error that I get after waking up from sleep mode, I also tried keeping the LDO on even when the unit is in deep sleep so this will match the same scenario as you had which is keeping the SD card always powered up.
    Still I get the FR_DSK_ERR and that too in the debug mode as well.

    Vikram
  • Hello Vikram,

    And I have used the same example from SDK 2.10 to put the device in deep sleep and wakeup and I do not get any FR_DSK_ERR with the SD Card always powered. I am not sure at this point how I can help you resolve the issue when it cannot be replicated.
  • Can you please do me one favor and try to write the same stuff but using the ff.h library instead of ffcio.h so that we are on same page and if you find any issue in that case?

    So basically using the fatfsraw example as base and building on top of it.

    Vikram
  • Hello Vikram,

    Attached is the fatsdraw example with LPM3 transition and using f_write. It works and I have checked LPM3 transition as well. Make sure enableParking in the board file is set as false and the defines for the project specified earlier are also used.

    /*
     * Copyright (c) 2015-2018, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== fatsdraw.c ========
     */
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    
    #if defined(__IAR_SYSTEMS_ICC__)
    /*
     *  Prevent time() macro in time.h from being used instead of our
     *  generated time() function.
     */
    #define _NO_DEFINITIONS_IN_HEADER_FILES 1
    #endif
    
    #include <time.h>
    
    #if defined(__IAR_SYSTEMS_ICC__)
    #undef _NO_DEFINITIONS_IN_HEADER_FILES
    #endif
    
    /* POSIX Header files */
    #include <pthread.h>
    
    #include <third_party/fatfs/ff.h>
    
    #include <ti/display/Display.h>
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/SDFatFS.h>
    
    #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
    #include <ti/drivers/dpl/HwiP.h>
    #include <semaphore.h>
    #include <ti/drivers/Power.h>
    #include <ti/drivers/power/PowerMSP432.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    /* Buffer size used for the file copy process */
    #ifndef CPY_BUFF_SIZE
    #define CPY_BUFF_SIZE       2048
    #endif
    
    /* String conversion macro */
    #define STR_(n)             #n
    #define STR(n)              STR_(n)
    
    /* Drive number used for FatFs */
    #define DRIVE_NUM           0
    
    const char inputfile[] = STR(DRIVE_NUM)":input.txt";
    const char outputfile[] = STR(DRIVE_NUM)":output2.txt";
    
    const char textarray[] = \
    "***********************************************************************\n"
    "0         1         2         3         4         5         6         7\n"
    "01234567890123456789012345678901234567890123456789012345678901234567890\n"
    "This is some text to be inserted into the inputfile if there isn't\n"
    "already an existing file located on the media.\n"
    "If an inputfile already exists, or if the file was already once\n"
    "generated, then the inputfile will NOT be modified.\n"
    "***********************************************************************\n";
    
    static Display_Handle display;
    
    unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
    
    FIL src;
    FIL dst;
    
    /* Set this to the current UNIX time in seconds */
    const struct timespec ts = {
        .tv_sec = 1469647026,
        .tv_nsec = 0
    };
    
    /* Mutex for indicating wakeup of device */
    sem_t devWakeMutex;
    
    /* Power Policy Object*/
    Power_NotifyObj notifyObj;
    
    void RTC_C_IRQHandler(void);
    
    /* -------------------------------------------------------------------
     * Set up of the current RTC time when the system is first powered up.
     * This is set for April 28th 2017 11:30:00 PM
     * ------------------------------------------------------------------- */
    const RTC_C_Calendar currentTime =
    {
            00,
            30,
            23,
            06,
            28,
            04,
            2017
    };
    
    /* Callback function for Power Policy Manager to perform specific tasks on
     * LMP3 Entry and Wake. During LPM3 Entry, the GPIO for AFE Power Enable and
     * Sensor Enable are made low to reduce current consumption. Also since
     * enableparking is disabled, the unused IO's are configured to output low
     * state so that current consumption is reduced. During LPM3 exit the GPIO for
     * AFE Power Enable and Sensor Enable are made High.
     * */
    uint8_t notifyDeepSleepFxn(uint8_t eventType, uint8_t eventArg, uint8_t clientArg)
    {
        if(PowerMSP432_ENTERING_DEEPSLEEP == eventType)
        {
            /* Set the unused IOs to low state */
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PA, (PIN_ALL16 & ~(GPIO_PIN5|GPIO_PIN6|GPIO_PIN7)));
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PB, (PIN_ALL16 & ~(GPIO_PIN6)));
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PC, PIN_ALL16);
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PD, PIN_ALL16);
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PE, PIN_ALL16);
            MAP_GPIO_setOutputLowOnPin(GPIO_PORT_PJ, PIN_ALL16);
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PA, (PIN_ALL16 & ~(GPIO_PIN5|GPIO_PIN6|GPIO_PIN7)));
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PB, (PIN_ALL16 & ~(GPIO_PIN6)));
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PC, PIN_ALL16);
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PD, PIN_ALL16);
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PE, PIN_ALL16);
            MAP_GPIO_setAsOutputPin(GPIO_PORT_PJ, PIN_ALL16);
    
    
            /* Turn off PSS high-side supervisors to consume less power in shutdown */
            MAP_PSS_disableHighSide();
        }
        else
        {
            MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
                    GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
    
        }
    
        return 0;
    }
    void RTC_C_IRQHandler(void)
    {
        /* Clear the interrupt status for alarm and timer-0 interrupt */
        MAP_RTC_C_clearInterruptFlag(RTC_C_CLOCK_ALARM_INTERRUPT);
    
        /* Post the semaphore to process the ADCBuf Task and FFT */
        sem_post(&devWakeMutex);
    }
    
    /* Function to set the next wakeup time after N Hours and M minutes */
    void predictive_maintenance_setWakeTime(uint8_t Hours, uint8_t Minutes)
    {
        RTC_C_Calendar newTime;
        uint_fast8_t excessHours = 0;
    
        /* Get the current time */
        newTime = MAP_RTC_C_getCalendarTime();
    
        /* Check if the wakeup period parameters are valid
         * Hour interval cannot be more than 24
         * Minute interval cannot be more than 60
         * Hour and Minute both cannot be 0 */
        if((Hours > 24) || (Minutes > 60) || ((Hours == 0) && (Minutes == 0)))
        {
            while(1);
        }
    
        /* Get the current time and check if there is a roll over for minute or
         * hour. If there is a hour roll over, then change the next match time
         * for 0 hrs. If there is a minute roll over, then increment the hour
         * and then set the minute match time. If there is no roll over for
         * either minute or hour then program the next match time with hour
         * unchanged and minute added. */
    
        newTime.minutes = newTime.minutes + Minutes;
    
        if(newTime.minutes > 59)
        {
            newTime.minutes = newTime.minutes - 60;
            excessHours = 1;
        }
    
        newTime.hours = newTime.hours + Hours + excessHours;
    
        if(newTime.hours > 23)
        {
            newTime.hours = newTime.hours - 24;
        }
    
        MAP_RTC_C_setCalendarAlarm(newTime.minutes,
                                   newTime.hours,
                                   RTC_C_ALARMCONDITION_OFF,
                                   RTC_C_ALARMCONDITION_OFF);
    }
    
    /*
     *  ======== printDrive ========
     *  Function to print drive information such as the total disk space
     *  This function was created by referencing FatFs's API documentation
     *  http://elm-chan.org/fsw/ff/en/getfree.html
     *
     *  This function call may take a while to process, depending on the size of
     *  SD Card used.
     */
    void printDrive(const char *driveNumber, FATFS **fatfs)
    {
        FRESULT fresult;
        DWORD   freeClusterCount;
        DWORD   totalSectorCount;
        DWORD   freeSectorCount;
    
        Display_printf(display, 0, 0, "Reading disk information...");
    
        fresult = f_getfree(driveNumber, &freeClusterCount, fatfs);
        if (fresult) {
            Display_printf(display, 0, 0,
                "Error getting the free cluster count from the FatFs object");
            while (1);
        }
        else {
            Display_printf(display, 0, 0, "done\n");
    
            /* Get total sectors and free sectors */
            totalSectorCount = ((*fatfs)->n_fatent - 2) * (*fatfs)->csize;
            freeSectorCount  = freeClusterCount * (*fatfs)->csize;
    
            /* Print the free space (assuming 512 bytes/sector) */
            Display_printf(display, 0, 0,
                "Total Disk size: %10lu KiB\n Free Disk space: %10lu KiB\n",
                totalSectorCount / 2, freeSectorCount  / 2);
        }
    }
    
    /*
     *  ======== mainThread ========
     *  Thread to perform a file copy
     *
     *  Thread tries to open an existing file inputfile[]. If the file doesn't
     *  exist, create one and write some known content into it.
     *  The contents of the inputfile[] are then copied to an output file
     *  outputfile[]. Once completed, the contents of the output file are
     *  printed onto the system console (stdout).
     */
    void *mainThread(void *arg0)
    {
        FRESULT fresult;
    
        /* Hwi variables */
        HwiP_Handle hwiPRTC = NULL;
        HwiP_Params hwiPRTCParams;
    
        /* Variables to keep track of the file copy progress */
        unsigned int bytesRead = 0;
        unsigned int bytesWritten = 0;
        unsigned int filesize;
        unsigned int totalBytesCopied = 0;
    
        SDFatFS_Handle sdfatfsHandle;
    
        /* Call driver init functions */
        GPIO_init();
        Display_init();
        SDFatFS_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Open the display for output */
        display = Display_open(Display_Type_UART, NULL);
        if (display == NULL) {
            /* Failed to open display driver */
            while (1);
        }
    
        /* Initialize real-time clock */
        clock_settime(CLOCK_REALTIME, &ts);
    
        Display_printf(display, 0, 0, "Starting the FatSD Raw example\n");
        Display_printf(display, 0, 0,
            "This example requires a FAT filesystem on the SD card.\n");
        Display_printf(display, 0, 0,
            "You will get errors if your SD card is not formatted with a filesystem.\n");
    
        MAP_RTC_C_initCalendar(&currentTime, RTC_C_FORMAT_BINARY);
        MAP_RTC_C_setPrescaleValue(RTC_C_PRESCALE_0, 0);
        MAP_RTC_C_definePrescaleEvent(RTC_C_PRESCALE_0, RTC_C_PSEVENTDIVIDER_64);
        MAP_RTC_C_clearInterruptFlag(RTC_C_CLOCK_ALARM_INTERRUPT);
        MAP_RTC_C_enableInterrupt(RTC_C_CLOCK_ALARM_INTERRUPT);
        MAP_RTC_C_startClock();
    
        Display_close(display);
    
        /* Enable interrupts from RTC Module. */
        HwiP_Params_init(&hwiPRTCParams);
        hwiPRTCParams.arg = 0;
        hwiPRTCParams.priority = 0x20;
        hwiPRTC = HwiP_create(INT_RTC_C, (HwiP_Fxn)RTC_C_IRQHandler, &hwiPRTCParams);
    
        if (hwiPRTC == NULL)
        {
            while(1);
        }
    
        /* Create Mutex for Device wakeup due to GPIO switch release or RTC
         * wake up */
        sem_init(&devWakeMutex, 1, 0);
    
        Power_setPolicy((Power_PolicyFxn)PowerMSP432_deepSleepPolicy);
    
        Power_setConstraint(PowerMSP432_DISALLOW_DEEPSLEEP_1);
    
        /* Register for entering shutdown notifications */
        Power_registerNotify(&notifyObj, PowerMSP432_ENTERING_DEEPSLEEP | PowerMSP432_AWAKE_DEEPSLEEP,
            (Power_NotifyFxn) notifyDeepSleepFxn, 0);
    
        for(;;)
        {
            /* Turn on user LED */
            GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
            /* Open the display for output */
            display = Display_open(Display_Type_UART, NULL);
            if (display == NULL) {
                /* Failed to open display driver */
                while (1);
            }
    
            /* Mount and register the SD Card */
            sdfatfsHandle = SDFatFS_open(Board_SDFatFS0, DRIVE_NUM);
            if (sdfatfsHandle == NULL) {
                Display_printf(display, 0, 0, "Error starting the SD card\n");
                while (1);
            }
            else {
                Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
            }
    
            printDrive(STR(DRIVE_NUM), &(dst.obj.fs));
    
            /* Try to open the source file */
            fresult = f_open(&src, inputfile, FA_READ);
            if (fresult != FR_OK) {
                Display_printf(display, 0, 0, "Creating a new file \"%s\"...",
                    inputfile);
    
                /* Open file for both reading and writing */
                fresult = f_open(&src, inputfile, FA_CREATE_NEW|FA_READ|FA_WRITE);
                if (fresult != FR_OK) {
                    Display_printf(display, 0, 0,
                        "Error: \"%s\" could not be created.\nPlease check the "
                        "Getting Started Guide if additional jumpers are necessary.\n",
                        inputfile);
                    Display_printf(display, 0, 0, "Aborting...\n");
                    while (1);
                }
    
                f_write(&src, textarray, strlen(textarray), &bytesWritten);
                f_sync(&src);
    
                /* Reset the internal file pointer */
                f_lseek(&src, 0);
    
                Display_printf(display, 0, 0, "done\n");
            }
            else {
                Display_printf(display, 0, 0, "Using existing copy of \"%s\"\n",
                    inputfile);
            }
    
            /* Create a new file object for the file copy */
            fresult = f_open(&dst, outputfile, FA_CREATE_NEW|FA_WRITE);
            if (fresult != FR_OK) {
                Display_printf(display, 0, 0, "Error creating \"%s\"\n", outputfile);
                fresult = f_open(&dst, outputfile, FA_OPEN_EXISTING|FA_WRITE);
                if (fresult != FR_OK) {
                    Display_printf(display, 0, 0, "Could not open existing file...\n");
                    Display_printf(display, 0, 0, "Aborting...\n");
                    while (1);
                }
                else
                {
                    Display_printf(display, 0, 0, "Starting file copy\n");
                }
            }
            else {
                Display_printf(display, 0, 0, "Starting file copy\n");
            }
    
            /*  Copy the contents from the src to the dst */
            while (true) {
                /*  Read from source file */
                fresult = f_read(&src, cpy_buff, CPY_BUFF_SIZE, &bytesRead);
                if (fresult || bytesRead == 0) {
                    break; /* Error or EOF */
                }
    
                /*  Write to dst file */
                fresult = f_write(&dst, cpy_buff, bytesRead, &bytesWritten);
                if (fresult || bytesWritten < bytesRead) {
                    Display_printf(display, 0, 0, "Disk Full\n");
                    break; /* Error or Disk Full */
                }
    
                /*  Update the total number of bytes copied */
                totalBytesCopied += bytesWritten;
            }
    
            f_sync(&dst);
    
            /* Get the filesize of the source file */
            filesize = f_size(&src);
    
            /* Close both inputfile[] and outputfile[] */
            f_close(&src);
            f_close(&dst);
    
            Display_printf(display, 0, 0,
                "File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n", inputfile,
                 filesize, outputfile, totalBytesCopied);
    
            /* Now output the outputfile[] contents onto the console */
            fresult = f_open(&dst, outputfile, FA_READ);
            if (fresult != FR_OK) {
                Display_printf(display, 0, 0, "Error opening \"%s\"\n", outputfile);
                Display_printf(display, 0, 0, "Aborting...\n");
                while (1);
            }
    
            /* Print file contents */
            while (true) {
                /* Read from output file */
                fresult = f_read(&dst, cpy_buff, CPY_BUFF_SIZE, &bytesRead);
                if (fresult || bytesRead == 0) {
                    break; /* Error or EOF */
                }
                cpy_buff[bytesRead] = '\0';
                /* Write output */
                Display_printf(display, 0, 0, "%s", cpy_buff);
            }
    
            /* Close the file */
            f_close(&dst);
    
            printDrive(STR(DRIVE_NUM), &(dst.obj.fs));
    
            /* Stopping the SDCard */
            SDFatFS_close(sdfatfsHandle);
    
            Display_printf(display, 0, 0, "Drive %u unmounted\n", DRIVE_NUM);
    
            /* Turn off user LED */
            GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
    
            Display_close(display);
    
            predictive_maintenance_setWakeTime(0, 1);
            sem_wait(&devWakeMutex);
        }
    
    }
    
    /*
     *  ======== fatfs_getFatTime ========
     */
    int32_t fatfs_getFatTime(void)
    {
        time_t seconds;
        uint32_t fatTime;
        struct tm *pTime;
    
        /*
         *  TI time() returns seconds elapsed since 1900, while other tools
         *  return seconds from 1970.  However, both TI and GNU localtime()
         *  sets tm tm_year to number of years since 1900.
         */
        seconds = time(NULL);
    
        pTime = localtime(&seconds);
    
        /*
         *  localtime() sets pTime->tm_year to number of years
         *  since 1900, so subtract 80 from tm_year to get FAT time
         *  offset from 1980.
         */
        fatTime = ((uint32_t)(pTime->tm_year - 80) << 25) |
            ((uint32_t)(pTime->tm_mon) << 21) |
            ((uint32_t)(pTime->tm_mday) << 16) |
            ((uint32_t)(pTime->tm_hour) << 11) |
            ((uint32_t)(pTime->tm_min) << 5) |
            ((uint32_t)(pTime->tm_sec) >> 1);
    
        return ((int32_t)fatTime);
    }
    

    I have modified the source code such that if the output file does not exist, it shall create the same. If it exists then create will fail and it will open the file.

**Attention** This is a public forum