FAT directory management

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to manage directories on SD memory card using the provided FAT library
Features shown are:

  • get current directory
  • change directory
  • create a subdirectory
  • delete a subdirectory
  • rename a subdirectory


FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the code since the pinout is slightly different.

Introduction to FAT Library

FAT file system library can be downloaded here


How to use it

Create a new project. Using the "External Lib" tool of IDE import all the files of the FAT file system.


NOTE: If you would like to use RTCC to provide timestamp on created files, enable RTCC library from Wizard tool of IDE (required only for Flyport Wi-Fi and Ethernet).


Compile and download code to the device.
Open the Serial Monitor utility on the IDE to send commands to Flyport. Serial monitor also provides feedback information about operation in progress.

Type the character reported above to start related operation:

Character Operation
0 Print current directory
1 Create a subdirectory named "openpicus"
2 Delete the subdirectory named "openpicus"
3 Change Directory to "openpicus"
4 Move to parent directory
5 Rename the subdirectory "openpicus" in "Flyport"

Bill of Materials

  • 1x Flyport Wifi/Ethernet/GPRS Module
  • 1x miniUSB programmer
  • 1x Power Supply
  • 1x Grove Board
  • 1x microSD card, formatted with FAT32 file system


Schematic

Please, refer to GROVE Board schematic for microSD card connections

Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
#include "sd_manager.h"
 
// Functions to be called to check SDCard insertion status 
void SDCheck(); //     call inside while(1) loop if you plan to 
                //    remove and insert SD memory card at runtime
 
char mybuff[110];
char cmd;
BOOL repeatInit = TRUE;
BOOL initRes = FALSE;
BOOL resultSD = FALSE;
int error = 0;
int len;
 
void FlyportTask()
{  
    vTaskDelay(100);
    UARTWrite(1,"Flyport SD-Card FAT32 library test!\r\n");
 
#if defined USE_RTCC_LIB
    struct tm rtccValue;
    //initialization of RTCC Value to a defined date
    rtccValue.tm_hour = 20;
    rtccValue.tm_min = 1;
    rtccValue.tm_sec = 2;
    rtccValue.tm_mday = 6;
    rtccValue.tm_mon = 3;
    rtccValue.tm_year = 113;
    rtccValue.tm_wday = SATURDAY;
    RTCCSet(&rtccValue);
#endif
 
    while(1)
    {
        SDCheck();
 
        // Code to manage files
        if(UARTBufferSize(1) > 0)
        {
            UARTRead(1, &cmd, 1);
            UARTFlush(1);
 
            switch(cmd)
            {
                case '0':
                    if(SDDirGet(mybuff, 110))
                    {
                        UARTWrite(1, "Current directory: ");    
                        UARTWrite(1, mybuff);
                        UARTWrite(1, "\r\n");
                    }
                    else
                    {
                        UARTWrite(1, "Error on SDDirGet\r\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);
                    }
                    break;
 
                case '1':
                    if(SDDirCreate("openpicus"))
                    {
                        UARTWrite(1, "Directory \"openpicus\" created!\r\n");
                    }
                    else
                    {
                        UARTWrite(1, "Error on SDDirCreate\r\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);
                    }
                    break;
 
                case '2':
                    if (SDDirDelete("openpicus"))
                    {
                        UARTWrite(1, "Directory \"openpicus\" deleted!\r\n");
                    }
                    else
                    {
                        UARTWrite(1, "Error on SDDirDelete\r\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);
                    }
                    break;
 
                case '3':
                    if(SDDirChange("openpicus"))
                    {
                        UARTWrite(1, "Directory changed to \"openpicus\"\r\n");
                    }
                    else
                    {
                        UARTWrite(1, "Error on SDDirChange\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);
                    }
                    break;
 
                case '4':
                    if(SDDirBack())
                    {
                        UARTWrite(1, "Moved to parent directory\r\n");
                    }
                    else
                    {
                        UARTWrite(1, "Error on SDDirBack\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);
                        break;
                    }
                    break;
 
                case '5':
                    if(SDDirRename("openpicus", "Flyport"))
                    {
                        UARTWrite(1, "Directory \"openpicus\" changed to \"Flyport\"\r\n");
                    }    
                    else
                    {
                        UARTWrite(1, "Error on SDDirRename\n");
                        sprintf(mybuff, "Err:%d\n", SDGetErr());
                        UARTWrite(1, mybuff);                        
                    }
                    break;
 
            }
        }
    }
}
 
void SDCheck()
{
    // Check for SD-Card detect signal
    if(IOGet(p19) == TRUE)
    {
        if(repeatInit == FALSE)
        {
            UARTWrite(1, "SDUnMount()...\n");
            resultSD = SDUnMount();
            repeatInit = TRUE;
        }   
    }
    else
    {
        if(repeatInit == TRUE)
        {
            UARTWrite(1, "SDInit()...");
 
            // Please, choose your pinout with 
            //  SDInit(...);
            // or use instead
            //  SDOnNest(SD_GROVE, 15);
            //  SDOnNest(SD_MUSIC, 15);
            // if you are using a Grove Nest or Music Nest Carrier Board
            initRes = SDInit(p18, p7, p17, p9, p19, 15); 
 
            if (initRes)
            {
                repeatInit = FALSE;
                UARTWrite(1,"Initialized\n");
            }
            else
            {
                error = SDGetErr();
                if (error == SD_NOT_PRESENT)
                {
                    UARTWrite(1, "SD Card Not Present\n");
                }   
                else if (error == SD_FS_NOT_INIT)
                {
                    UARTWrite(1, "Generic error\n");
                    while (1); // Lock here for generic error!
                }   
            }   
        }
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox