PSoC 4 BLE_emFile Component

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

   

I'm working on PSoC 4BLE . I created a program using emFile(SPI) component . I created a folder & file & able to store the data. I want to do some time slotting there. I want that file will automatically create in every 1 hour & directory will automatically create in every day so i'll have 24 files in a day & they'll store in a directory. Directory will have date stamping & file will have time stamping so the folder for today have date as folder name like - 02/20/2016 & it has 24 files according to time basis . Can anyone suggest me to do with emFile component.

   

 

   

/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/

   

#include <project.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include "adc.h"
#include "rtc.c"
#include <FS.h>
#include <time.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <Global.h>

   

   
    uint32 dataReady ;
    int16 result[ADC_SAR_SEQ_TOTAL_CHANNELS_NUM];
    uint32 timer_delay ;
        
    int length;
 
    int  main()
    {
    int16 res = 0;
    int32 temperature;
    //char  uartLine[250];
    int16 ADCCountsCorrected;
    char tempBuffer[16u];
    char dateBuffer[16u];
    char mixBuffer[32u];
       // int i=1;
    //char* extension = ".txt";
     //char file_name[30] = "File";
  //  char sdFile[strlen(file_name)+strlen(extension)+1];
    char sdFile[30] = "File.txt";
    char str_date[30];
    char time_str[30];
    char dir_name[30];

   

    //char temp[30]="\\";
    //char temp1[30]="\\";
    uint32 time1;
    uint32 date;
    char sdVolName[32];     /* Buffer that will hold SD card Volume name */
    FS_FILE * pFile;
    //time_t current_time;
    // struct tm *tm;
    //current_time = time( NULL);
    //tm = localtime(&current_time);
    
   U32 TimeStamp;

   

    
    CyGlobalIntEnable;
    
    /* Initialize the UART */
    UART_Start();
    UART_UartPutCRLF(32u);
    UART_UartPutCRLF(32u);
    UART_UartPutString("Starting readings...");
    UART_UartPutCRLF(32u);
    
    PWM_Start();
    PWM_TriggerCommand(PWM_MASK, PWM_CMD_START);

   

    /* Init and start sequencing SAR ADC */
    ADC_SAR_SEQ_Start();
    ADC_SAR_SEQ_StartConvert();
    /* Enable interrupt and set interrupt handler to local routine */
    ADC_SAR_SEQ_IRQ_StartEx(ADC_SAR_SEQ_ISR_LOC);
    
    /* Init interrupt from timer to measure the temperature rarely */
    ISR_TIMER_StartEx(ISR_TIMER_LOC);

   

/* Enable WCO */
    CySysClkWcoStart();

   

    {
        
/* Alarm structure initialization */
    alarmTimeDate.time = ALARM_TIME_HR_MIN_SEC;
    alarmTimeDate.date = ALARM_DATE_MONTH_DAY_YEAR;
    
    /* Prepare COUNTER0 to use it by CySysTimerDelay function in
     * "INTERRUPT" mode: disable "clear on match" functionality, configure
     * COUNTER0 to generate interrupts on match.
     */
    CySysWdtSetClearOnMatch(CY_SYS_WDT_COUNTER0, 0u);
    CySysWdtSetMode(CY_SYS_WDT_COUNTER0, CY_SYS_WDT_MODE_INT);
    
    /* Enable WDT COUNTER0 */
    CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK);
    
    /* Disable servicing interrupts from WDT_COUNTER0 to prevent
       trigger callback before the CySysTimerDelay() function. */
    CySysWdtDisableCounterIsr(CY_SYS_WDT_COUNTER0);
    
    /* Register EnableRtcOperation() by the COUNTER0. */
    CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER0, EnableRtcOperation);
    
    /* Initiate run the EnableRtcOperation() callback in WCO_STARTUP_DELAY_CYCLES interval. */
    CySysTimerDelay(CY_SYS_WDT_COUNTER0, CY_SYS_TIMER_INTERRUPT, WCO_STARTUP_DELAY_CYCLES);
    
    /* Start RTC component */
    RTC_Start();
    
    /* Set Date and Time */
    RTC_SetDateAndTime(TIME_HR_MIN_SEC,DATE_MONTH_DAY_YEAR);

   

    /* Ignore second mask to call alarm */
    RTC_SetAlarmMask((uint32)~RTC_ALARM_SEC_MASK);
    
    /* Set Alarm Date and Time */
    RTC_SetAlarmDateAndTime(&alarmTimeDate);

   

    /* Set RTC time update period */
    RTC_SetPeriod(1u, TICK_EACH_1_HZ);

   

    /* Set function AlarmIsrHandler to be called when alarm triggers */
    RTC_SetAlarmHandler(AlarmIsrHandler);
       /* Initialize file system */
    FS_Init();
    
     /* Get volume name of SD card #0 */
    if(0 != FS_GetVolumeName(0u, &sdVolName[0], 9u))
    {
        /* Getting volume name succeeded */
        UART_UartPutString(" mixBuffer ");
        UART_UartPutString(sdVolName);
    }
    else
    {
        /* Operation Failed - indicate this */
        UART_UartPutString(" Failed to get mixBuffer");
    }
   
     /* This will create directory "Dir" on SD card */
    
    if(0 == FS_MkDir("DIR"))
    {
            
        /* Display successful directory creation message */
       UART_UartPutString(" Directory created");
   }
    else
   {
        /* Display failure message */
       UART_UartPutString(" Failed to create directory");
    }

   

    while(1)
    {    
        /* Get Date and Time from RTC */
        time1 = RTC_GetTime();
        date = RTC_GetDate();
        
   length = sprintf(mixBuffer, "\r\n%02lu:%02lu:%02lu ,%02lu/%02lu/%02lu,Temperature Value: %dC,ADC Voltage Measurement: %dmV ",RTC_GetHours(time1), RTC_GetMinutes(time1), RTC_GetSecond(time1),RTC_GetMonth(date), RTC_GetDay(date), RTC_GetYear(date),(int16) temperature,(uint16) res);
        UART_UartPutString(mixBuffer);
           
    /* When conversion of sequencing channels has completed */
        if((dataReady & ADC_SAR_SEQ_EOS_MASK) != 0u)
        {
            /* Get voltage, measured by ADC */
            dataReady &= ~ADC_SAR_SEQ_EOS_MASK;
            res = ADC_SAR_SEQ_CountsTo_mVolts(CH0_N, result[CH0_N]);
        }    
        
        /* When conversion of the injection channel has completed */
        if((dataReady & ADC_SAR_SEQ_INJ_EOC_MASK) != 0u)
        {
            dataReady &= ~ADC_SAR_SEQ_INJ_EOC_MASK;

   

            /******************************************************************************
            * Adjust data from ADC with respect to Vref value.
            * This adjustment is to be done if Vref is set to any other than
            * internal 1.024V.
            * For more detailed description see Functional Description section
            * of DieTemp P4 datasheet.
            *******************************************************************************/
            ADCCountsCorrected = result[TEMP_CH]*((int16)((float)ADC_VREF_VALUE_V/1.024));
            
            temperature = DieTemp_CountsTo_Celsius(ADCCountsCorrected);
                
        if (alarmFlag == 1u)
        {
            UART_UartPutString("  Alarm ");
            alarmFlag  = 0u;
        }
        else
        {
            UART_UartPutString(" No alarm");
        }
    
        //UART_UartPutString("\r")     
   }
    //sprintf(str_date, "%02lu-%02lu-%02lu", RTC_GetMonth(date),RTC_GetDay(date),RTC_GetYear(date));
 
    //strftime(str_date, sizeof(str_date), "%m %d %Y", tm);
    //sprintf(file_name, "%s",myint);
       // snprintf( sdFile, sizeof( sdFile ), "%s%c%s", file_name,i, extension );
    // UART_UartPutString(str_date);
       // i++;
    
       // strcpy(file_name,"%02lu-%02lu-%02lu");
    
//strcpy(sdFile,date_str);

   

     pFile = FS_FOpen(sdFile, "a");
    
    /* Check if file was created */

   

    if(pFile)
    {
        /* Indicate successful file creation message */
        UART_UartPutString("File ");
        UART_UartPutString(sdFile);
        UART_UartPutString(" was opened");
        
         if(0 != FS_FWrite(mixBuffer,1,length , pFile))
        {
            /* Inditate that data was written to a file */
           UART_UartPutString("mixBuffer ");
           UART_UartPutString(sdFile);
           UART_UartPutString(" written to file");
        }
        else
        {
            UART_UartPutString(" Failed to write data to file");
        }           
         if(0 == FS_FClose(pFile))
        {
            UART_UartPutString(" File was closed");
        }
        else
        {
            UART_UartPutString(" Failed to close file");
        }
    }
    else
    {
        UART_UartPutString(" Failed to create file");
    }
    if(0 == FS_Move(sdFile, "\\DIR\\Time.txt"))
    {
        UART_UartPutString(" File was moved to DIR");
    }
    else
    {
        UART_UartPutString(" Failed to move file");
    }
       sprintf(dir_name, "Date-%02lu-%02lu-%02lu", RTC_GetMonth(date),RTC_GetDay(date),RTC_GetYear(date));
    
    // strcat( temp, dir_name);
    //strcat(temp,temp1);
    //UART_UartPutString(temp);

   

    
     if(0 == FS_Rename("\\Dir", dir_name))
    {
        UART_UartPutString(" Rename Successful");
    }
    else
    {
        UART_UartPutString(" Rename Unsuccessful");
    }
     UART_UartPutString(" mixBuffer completed");
    }
    }
     return length;
    }
    
/* [] END OF FILE */

   

 

   

 

   

Thanks,

0 Likes
1 Solution
himc_284346
Level 3
Level 3
Welcome! 10 replies posted 5 replies posted

This might not be possible, beacuse FS_FOpen() takes const char* as the argument. Can you try type casting.

View solution in original post

0 Likes
2 Replies
himc_284346
Level 3
Level 3
Welcome! 10 replies posted 5 replies posted

This might not be possible, beacuse FS_FOpen() takes const char* as the argument. Can you try type casting.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Use 'sprintf' to create char buffer that has the name as you need id. The you can use that buffer as argument to FW_FOpen().