AnsweredAssumed Answered

SDCARD task question

Question asked by David XY Zhou on Jun 13, 2016
Latest reply on Jun 16, 2016 by David XY Zhou

Hi,

I have a K60 tower board: TWRK60F120MUM.

I have observed an interesting MQX programming topic related to the SDCard task. I hope some expert can point me where is the issue.

I worked out SD card init, open file, and write file based on demo.c of the SDCard task. Summaried here:

I moved FILE pointers as global to retain the handle valves:

The SDCard_Init runs as a task. (as demo.c)

 

volatile MQX_FILE_PTRghComHandle=NULL;
volatile MQX_FILE_PTRghFileSystem=NULL;
volatile MQX_FILE_PTRgphSDCard=NULL, gphPartition=NULL;

void SDCard_Init(uint32_t temp)

{

bool        inserted = TRUE, readonly = FALSE, last = FALSE;
ghComHandle = fopen(BSP_SDCARD_ESDHC_CHANNEL, (void *)(SPI_FLAG_FULL_DUPLEX));

_io_sdcard_install("sdcard:", (void *)&_bsp_sdcard0_init, ghComHandle); 

inserted =1;//!lwgpio_get_value(&sd_detect);  always inserted

for (;;) {

    if (last != inserted) {

    last = inserted;
    if (inserted) {
        gphSDCard = fopen("sdcard:", 0);
        _io_part_mgr_install(gphSDCard, partman_name, 0);
        gphPartition = fopen(partition_name, NULL);
        _io_ioctl(gphPartition, IO_IOCTL_VAL_PART, NULL);
            // Install MFS over partition
        _io_mfs_install(gphPartition, filesystem_name, 0);
        ghFileSystem = fopen(filesystem_name, NULL);
        //LogFileOpen();
        giSdCardReady=1;
    }
    else { //never closed
    }
}

}

}

 

In the Main Task, wait until giSdCardReady=1. Then call LogFileOpen().

uint8_t LogFileOpen() {

    int error_code=0;

    MQX_FILE_PTR    fd_ptrWrite ;

    fd_ptrWrite = fopen("a:myfile2.txt", "w+");

}

fopen() opens successfully.  It works.

 

Because SDCard_Init runs at lower priority and the SDCard is always inserted. So the task really runs only once. for initialization of SD card. So I call the SDCard_Init() as a function in the Main task, instead of running as a task.

Then call to LogFileOpen() fails to open the file.

Main_Task{

     SDCard_init(); //when called as function, the for (;;) is commented out, so it only runs once.

     LogFileOpen(); //fails

}

 

That is the first question: why SDCard_Init has to be run as a task. Main() is a task, too. Why can not call the SDCard_init()?

 

Then I copy all the code of the SDCard_init() into Main task as follows:

Main_Task{

//   SDCard_init();

bool        inserted = TRUE, readonly = FALSE, last = FALSE;
ghComHandle = fopen(BSP_SDCARD_ESDHC_CHANNEL, (void *)(SPI_FLAG_FULL_DUPLEX));

_io_sdcard_install("sdcard:", (void *)&_bsp_sdcard0_init, ghComHandle); 

inserted =1;//!lwgpio_get_value(&sd_detect);  always inserted

if (1) {

    if (last != inserted) {

    last = inserted;
    if (inserted) {
        gphSDCard = fopen("sdcard:", 0);
        _io_part_mgr_install(gphSDCard, partman_name, 0);
        gphPartition = fopen(partition_name, NULL);
        _io_ioctl(gphPartition, IO_IOCTL_VAL_PART, NULL);
            // Install MFS over partition
        _io_mfs_install(gphPartition, filesystem_name, 0);
        ghFileSystem = fopen(filesystem_name, NULL);
        //LogFileOpen();
        giSdCardReady=1;
    }
    else { //never closed
    }
}

}

     LogFileOpen(); //it works

}

 

I don't understand that why in the Main task, SDCard_init() (as a funtion) can not be called, and LogFileOpen() fails. and if the exact same code of SDCard_init() has to be embedded into the Main Task in order to make LogFileOpen work?

 

What I have missed?

 

Thank you,

David Zhou

Outcomes