Hello everyone! I have many problems with fopen() and fclose() functions.
Firstly, I have a program that reads 3 data every 4ms and prints these data to the SD Card every 1-second pass (250x3 750 data). The print process is on a thread, the reading process is on the main function.
My first problem is about fopen() and fclose() functions. When these functions have done, the total spend is 100ms around. It is so huge for me. I have to read data every 4 ms.
My second problem is about fwrite() function inside of the thread. When I write fwrite() function inside of the thread, then I am able to print maximum of 128 data until fopen() and fclose() work.
My code is :
#include “mbed.h”
#include “ARMDAQ.h”
#include “SDCard.h”
#include
#include
#include “rtos.h”
ARMDAQ ARMDAQ;
SDCard SDCard;
Timer t;
int timer_time1,timer_time2;
using namespace chrono;
float MyBuffer[1500];
EventFlags eventflag;
#define SAMPLE_FLAG1 (1UL << 0) // Define a flag for event flag
void WriteToSD()
{
while(true)
{
eventflag.wait_any(SAMPLE_FLAG1);
fwrite(MyBuffer,sizeof(float), 128, SDCard.f);
}
}
int main()
{
Thread thread;
int BufferIndexCounter = 0;
printf(" ---------- TDG ---------- \n");
int ExceedCount = 0;
float PositiveThreshold = 0.5,NegativeThreshold = -0.5;
SDCard.MountSd();
SDCard.FileActive();
thread.start(mbed::callback(WriteToSD));
while(true)
{
if(ARMDAQ.DRDYInt->read())
{
ARMDAQ.ARMDAQ_ADXL355_ReadAccData();
////////////////////////////////////////////////////////////////////////////////////////////
///////////////////// Data Reading and determination trigger mode //////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
MyBuffer[BufferIndexCounter] = ARMDAQ.adxl355DataTemp[0];
if(MyBuffer[BufferIndexCounter]>PositiveThreshold || MyBuffer[BufferIndexCounter]< NegativeThreshold)
{
ExceedCount++;
}
BufferIndexCounter++;
////////////////////////////////////////////////////////////////////////////////////////////
MyBuffer[BufferIndexCounter] = ARMDAQ.adxl355DataTemp[1];
if(MyBuffer[BufferIndexCounter]>PositiveThreshold || MyBuffer[BufferIndexCounter]< NegativeThreshold)
{
ExceedCount++;
}
BufferIndexCounter++;
////////////////////////////////////////////////////////////////////////////////////////////
MyBuffer[BufferIndexCounter] = ARMDAQ.adxl355DataTemp[2];
BufferIndexCounter++;
////////////////////////////////////////////////////////////////////////////////////////////
if(BufferIndexCounter %750 == 0)
{
t.start();
fclose(SDCard.f);
//timer_time1 = duration_cast<chrono::microseconds>(t.elapsed_time()).count();
timer_time1 = t.read_us();
SDCard.f = fopen(SDCard.FileName,"a+");
//timer_time2 = duration_cast<chrono::microseconds>(t.elapsed_time()).count();
timer_time2 = t.read_us();
printf("%d %d\n",timer_time1,timer_time2);
t.stop();
t.reset();
eventflag.set(SAMPLE_FLAG1);
}
if(BufferIndexCounter == 1500)
{
BufferIndexCounter = 0;
}
}
}
printf("\n ---------- // TDG \\ ---------- \n");
}
output is :
6095 ------------------------------------------------ 54406
40215 ------------------------------------------------ 88591
12032 ------------------------------------------------ 60459
12039 ------------------------------------------------ 60474
12108 ------------------------------------------------ 60537
12106 ------------------------------------------------ 60536
12111 ------------------------------------------------ 60539
12105 ------------------------------------------------ 60538
timer_time1(after fclose) ------------------ timer_time2(after fopen)