AnsweredAssumed Answered

Multithreaded FatFS

Question asked by Sam Tyler on Aug 4, 2015
Latest reply on Aug 17, 2015 by Norm Davies

Hi

I'm having more problems using SD card on FRDM-K64. Using KSDK 1.3.0.

 

I'm trying to record in a single thread while other applications are running in separate threads. I have limited the problem down to the following. The SD card write will not fail if OSA_TimeDelay is commented, otherwise will stop executing before 1000 iterations.

 

Source Code:

/*
 * SD card writes will randomly fail with action from other thread.
 * */

#if 1

// SDK Included Files
#include "fsl_os_abstraction.h"
#include "fsl_debug_console.h"

// Project specific files
#include "fsl_mpu_driver.h"
#include "board.h"

#include "ff.h" //remove after test
#include "diskio.h" //remove after test

FATFS fatfs;

static void primary_thread(void *params)
{
    FIL file;
    uint8_t status;
    uint32_t frame_num = 0;
    uint8_t frame_buffer[5000];
    uint8_t frame_len = 5;
    uint32_t size;
    char filename[40];
    uint8_t i;

    status = f_mount(SD, &fatfs);
    if (status)
    {
        PRINTF("f_mount error %i\r\n", status);
        while(1);
    }

    // disable Memory Protection Unit
    for(i = 0; i < MPU_INSTANCE_COUNT; i++)
    {
        MPU_HAL_Disable(g_mpuBase[i]);
    }

    status = disk_initialize(SD);
    if (status != 0)
    {
        PRINTF("Failed to initialise SD card\r\n");
        while(1);
    }

    while (1)
    {
        sprintf(filename, "%i:fl%i.txt", SD, frame_num++);
        PRINTF("FILENAME =%s\r\n ", filename);
        status = f_open(&file, filename, FA_WRITE | FA_CREATE_ALWAYS);
        if (status)
        {
            PRINTF("f_open error %i\r\n", status);
            while(1);
        }

        status = f_write(&file, frame_buffer, frame_len, &size);
        if (status || size != frame_len)
        {
            PRINTF("f_write error %i\r\n", status);
            while(1);
        }

        status += f_close(&file);
        if (status)
        {
            PRINTF("f_close error %i\r\n", status);
            while(1);
        }

        GPIO1_TOGGLE;
    }
}

static void secondary_thread(void *params)
{
    while (1)
    {
        GPIO2_TOGGLE;
        OSA_TaskYield();

           OSA_TimeDelay(25); //commenting out this line will stop the issue
    }
}

int main(void)
{
    hardware_init();
    OSA_Init();

    uint32_t status;

    status = OSA_TaskCreate(primary_thread, (uint8_t*)"primary_thread", 10000L, NULL, 4L, NULL, false, NULL);
    if (kStatus_OSA_Success != status)
    {
        PRINTF("Error Creating thread \"output_thread\" \r\n");
        while(1)
            ;
    }

    status = OSA_TaskCreate(secondary_thread, (uint8_t*)"secondary_thread", 10000L, NULL, 4L, NULL, false, NULL);
    if (kStatus_OSA_Success != status)
    {
        PRINTF("Error Creating thread \"output_thread\" \r\n");
        while(1)
            ;
    }

    OSA_Start();
    return 1;
}

#endif



























 

 

Stack:

1gmqlfz.png

5J5eOyw.png

 

Any suggestions as to what is going wrong here?

 

Cheers,

 

Sam

Outcomes