LPC1768 device mbed-os 6.16 fclose and fopen function time problem

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)

I found that the clib functions are much slower than the Mbed file objects. The cpp classes are calling the filesystem functions directly, the clib is using additional buffers.

Just to double check, have you made sure that your firmware on the Mbed interface chip is up to date? If not, this can cause all sorts of weird effects. See here.

Thank you for the suggestion, I’ve applied your advice, then I have a lot of new problems.

1- When I upload 141212.if file to my LPC1768, I cannot get info from Serial Port, also sometimes windows get errors “Your USB device is faulty” and reset button is not working.

2- But the code is working, biggest problem is Serial port is not working.

What should I do? Thank You!

Huh, I can’t say I have seen this before. Sounds like maybe the interface firmware isn’t flashing correctly? Are you sure the file downloaded OK and isn’t corrupt?

Yes I’m sure because blinking led application is working. This code is:
#include “mbed.h”
DigitalOut led1(LED1);
DigitalOut led2(LED2);
int main()
{
while(true)
{
led1 = 1;
led2 = 0;
thread_sleep_for(100);
led1 = 0;
led2 = 1;
thread_sleep_for(100);
printf(“Hello World!\n”);
}

}

But I’m not see Hello World anywhere. Because Serial Port is not working

I’m really not sure, to be honest. It sort of feels like your board might be damaged, because this should work (assuming you have the correct baud rate). Maybe try creating a new BufferedSerial on some of the IO pins, attach a USB serial adapter, and see if anything is getting printed?