Hello
i have tried the following code but when the debugger reaches at ptr = fopen (fname, "r");
it goes to run time error.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <XMC4500.h> /* SFR declarations of the selected device */
#include <DAVE3.h> /* Declarations from DAVE3 Code Generation */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define block_size 512
/* helper macro */
#define CHECK_STATUS(stat, err)\
if (stat != DAVEApp_SUCCESS)\
return err
/* globals here be tracked by debugger */
volatile _Bool Test_Successfull=false;
/** File system object structure */
FATFS myfsObject;
char Buffer0[];
/* Function to demonstrate the basic operations on a physical drive by 4 tasks */
void SLTHA002_BasicDemo(void const *Argument);
/* Task function */
void SDMMC_lTaskFunction(void const *Arg);
/* read from a file */
int file_read(const char *fname);
int SDMMC_StatusMultApp[5] = {DAVEApp_SUCCESS, SLTHA002_ERROR};
volatile uint32_t ThreadCount ;
/** Stores the parameters associated with each task */
typedef struct TaskParamsType
{
uint8_t ThreadNumber;
//char *Buffer;
} TaskParamsType;
int main(void)
{
/* DAVE Initialization */
DAVE_Init();
osThreadId ThreadId;
/* Thread definition */
osThreadDef(SLTHA002_BasicDemo, osPriorityBelowNormal, 1, 0);
/* Thread creation */
ThreadId = osThreadCreate(osThread(SLTHA002_BasicDemo),NULL);
if (ThreadId != NULL )
{
/* Start the OS kernel explicitly.*/
osKernelStart();
}
return 0;
}
/* read from a file */
int file_read(const char *fname)
{
FILE *ptr;
status_t status;
uint32_t result;
uint32_t num_blocks;
char temp_buffer[1024];
do {
ptr = fopen(fname, "r");
fseek(ptr, 0, SEEK_SET);
if (ptr == NULL)
break ;
// block_size = sizeof(temp_buffer);
num_blocks = sizeof(char);
result = fread(&temp_buffer, num_blocks, block_size, ptr);
status = fclose(ptr);
if (result == 0)
break;
} while (0);
return 1;
}
/**
* Task Function used by 4 threads to perform basic operations on the card. */
void SDMMC_lTaskFunction(void const *Arg)
{
char Buffer[10];
FILE *ptr;
int BytesWritten = 0;
int BytesRead = 0;
#if defined (__GNUC__) || defined ( __TASKING__)
struct stat FileStat = {0};
#endif
/* Buffer to store the data read from the card. */
char ReadBuffer[30];
/* Stores the renamed directory string */
char RenameDirName[6];
char temp_buffer[512];
int result;
status_t status;
TaskParamsType *TaskParamPtr = (TaskParamsType *)Arg;
if (TaskParamPtr->ThreadNumber == 1)
{
file_read("file1.TXT");
/* read file and verify contents */
// if (file_read("file1.TXT") < 0)
// return -1;
}
/* Increment the thread count */
ThreadCount++;
/* Write the Test Status of the tasks into array */
//SDMMC_StatusMultApp[TaskParamPtr->ThreadNumber] = Result ;
/* Terminate the thread. */
osThreadTerminate(osThreadGetId());
}
/**
* Main thread Task function
*/
void SLTHA002_BasicDemo(void const *Argument)
{
/* Thread ID'S of Task */
osThreadId ThreadId[1];
uint32_t Count =0;
TaskParamsType TaskParams[1];
/* Thread function Definition */
osThreadDef(SDMMC_lTaskFunction, osPriorityNormal, 1, 0);
int Result ;
do
{
/* Mount the drive */
Result = mount(0, &myfsObject);
if(Result != DAVEApp_SUCCESS)
{
break;
}
/* Thread numbered*/
for (Count=0; Count<1; Count++)
{
TaskParams[Count].ThreadNumber = Count + 1;
}
/* Creation of task. */
for (Count=0; Count<1; Count++)
{
ThreadId[Count] = osThreadCreate(osThread(SDMMC_lTaskFunction),\
(void *)&(TaskParams[Count]));
if (ThreadId[Count] == NULL)
{
break;
}
}/* End of " if ( Status1[i]!= DAVEApp_SUCCESS)" */
/* Main thread will wait till all the thread completes their operation */
while(1)
{
if (ThreadCount == 1)
{
break;
}
}
} while(0);
while(1);
}
Originally Posted by
Salem
> Status = mount(0, &myfsObject);
Why are you trying to do this every time around the loop?
> if (file_read("FILE1.TXT") < 0)
> Status = stat("FILE1.txt", &fstat);
Is your file system case sensitive?
From the look of things, you're programming some target device, so you should at least post some kind of attempt at threads, as well as citing any reference material you have on your particular thread library.
Maybe your thread library is POSIX compliant, but it's not a given.