Tool/software: TI-RTOS
Hi,
i wrote 1 timer with 200 msec its called to swi from their its calling task(through semaphore ) --->task consist SD_SPI operation its WRITING DATA EVERT 200ms ..5 mins once i like to close the file and i want create new file ...................i wrote some routine its creating files its writing data to first file only remaining file are creating but data not writing.
/*
* Copyright (c) 2015, 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.
*/
/*
* ======== fatsd.c ========
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Swi.h>
#include <ti/sysbios/knl/Semaphore.h>
/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDSPI.h>
#include <ti/drivers/SPI.h>
#include <ti/sysbios/hal/Seconds.h>
#include <time.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
#define SPI_MSG_LENGTH 16
#define TASKSTACKSIZE 768
char inputfile[40] = "fat:"STR(DRIVE_NUM)":input.txt";
char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";
const char textarray[] = "@Ch0:0,Ch1:1,Ch2:2,Ch3:3,Ch4:4,Ch5:5,Ch6:6,Ch7:7#\n";
int init_spi = 0;
UInt32 sleepTickCount;
unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
Task_Struct task0Struct;
Char task0Stack[TASKSTACKSIZE];
Char task1Stack[TASKSTACKSIZE];
Swi_Struct swi0Struct, swi1Struct;
Swi_Handle swi0Handle, swi1Handle;
Semaphore_Struct semStruct;
Semaphore_Handle semHandle;
char strTime[40];
char filename[20];
int count = 0;
void ReadChnData()
{
count++;
Swi_post(swi0Handle);
}
/*
* ======== swi0Fxn =======
*/
Void swi0Fxn(UArg arg0, UArg arg1)
{
System_printf("count : %d\n", count);
System_flush();
Semaphore_post(semHandle);
}
/*
* ======== swi1Fxn =======
*/
Void swi1Fxn(UArg arg0, UArg arg1)
{
System_printf("Enter swi1Fxn, a0 = %d, a1 = %d\n", (Int) arg0, (Int) arg1);
System_printf("swi1 trigger = %d\n", Swi_getTrigger());
System_printf("swi1 pri = %d\n", Swi_getPri(swi1Handle));
System_printf("Exit swi1Fxn\n");
System_flush();
// Semaphore_post(sem0Handle);
}
void GetStrTime(char *buff1)
{
time_t now;
struct tm ts;
char buff[40];
/* Set the current time in seconds since epoch*/
Seconds_set(1500654672);
time(&now);
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&now);
strftime(buff, 30, "%m%d%H%M", &ts);
strncpy(buff1, buff, sizeof(buff));
}
void SDcardoperation()
{
SDSPI_Handle sdspiHandle;
SDSPI_Params sdspiParams;
int numberofbytes;
GetStrTime(strTime);
strcpy(tempstr, inputfile);
sprintf(filename, "%s.txt", strTime);
strcat(tempstr, filename);
printf("%s\n", tempstr);
SDSPI_Params_init(&sdspiParams);
sdspiHandle = SDSPI_open(Board_SDSPI0, DRIVE_NUM, &sdspiParams);
if (sdspiHandle == NULL)
{
System_abort("Error starting the SD card\n");
}
else
{
System_printf("Drive %u is mounted\n", DRIVE_NUM);
}
src = fopen(tempstr, "a+");
if (src != NULL)
{
for (;;)
{
Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
numberofbytes = fwrite(textarray, 1, strlen(textarray), src);
fflush(src);
// Semaphore_post(semHandle);
Task_sleep(sleepTickCount);
System_printf("done\n");
System_flush();
count1++;
if (numberofbytes == strlen(textarray))
{
System_printf("numberofbytes : %d \n", numberofbytes);
System_flush();
}
if (count1 >= 300)
{
count2++;
fclose(src);
src =NULL;
SDSPI_close(sdspiHandle);
System_printf("Drive %u unmounted\n", DRIVE_NUM);
System_flush();
GPIO_write(Board_LED0, Board_LED_OFF);
count1 = 0;
memset(filename, 0, 20);
//BIOS_exit(0);
GetStrTime(strTime);
memset(tempstr, 0, sizeof(tempstr));
strcpy(tempstr, inputfile);
sprintf(filename, "%s.txt", strTime);
strcat(tempstr, filename);
printf("%s\n", tempstr);
System_printf("Filename :%s \n", tempstr);
SDSPI_Params_init(&sdspiParams);
sdspiHandle = SDSPI_open(Board_SDSPI0, DRIVE_NUM, &sdspiParams);
if (sdspiHandle == NULL)
{
System_abort("Error starting the SD card\n");
}
else
{
System_printf("Drive %u is mounted\n", DRIVE_NUM);
}
src = fopen(tempstr, "a+");
if (src != NULL)
{
System_printf("Opening \n");
}
else
{
System_printf("Opening error \n");
}
if(count2 >= 2){
BIOS_exit(0);
}
}
}
}
}
/*
* ======== main ========
*/
int main(void)
{
Task_Params taskParams;
Semaphore_Params semParams;
Swi_Params swiParams;
/* Call board init functions */
Board_initGeneral();
Board_initGPIO();
Board_initSDSPI();
//Fat_sd_init();
/* Construct file copy Task thread */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task0Stack;
Task_construct(&task0Struct, (Task_FuncPtr) SDcardoperation, &taskParams, NULL);
Swi_Params_init(&swiParams);
swiParams.arg0 = 1;
swiParams.arg1 = 0;
swiParams.priority = 2;
swiParams.trigger = 2;
Swi_construct(&swi0Struct, (Swi_FuncPtr) swi0Fxn, &swiParams, NULL);
swi0Handle = Swi_handle(&swi0Struct);
swiParams.arg0 = 2;
swiParams.arg1 = 0;
swiParams.priority = 1;
swiParams.trigger = 3;
Swi_construct(&swi1Struct, (Swi_FuncPtr) swi1Fxn, &swiParams, NULL);
swi1Handle = Swi_handle(&swi1Struct);
/* Construct a Semaphore object to be use as a resource lock, inital count 1 */
Semaphore_Params_init(&semParams);
Semaphore_construct(&semStruct, 1, &semParams);
/* Obtain instance handle */
semHandle = Semaphore_handle(&semStruct);
/* We want to sleep for 10000 microseconds */
sleepTickCount = 10000 / Clock_tickPeriod;
/* Turn on user LED */
GPIO_write(Board_LED0, Board_LED_ON);
System_printf("Starting the FatSD example\n");
/* Start BIOS */
BIOS_start();
return (0);
}