first

Dependencies:   SDFileSystemDMA mbed

Refer to:
https://developer.mbed.org/users/mimi3/code/SDFileSystemDMA

Caution

If your board has SRAM less than or equal to 8KB, the 'buffer' size must be set to 512 Bytes.

NUCLEO-F411RE
About 2.5MBytes/sec
/media/uploads/mimi3/sdfilesystemdma-speed-test-teraterm-output-f411re.png
NUCLEO-L152RE
About 1MBytes/sec
/media/uploads/mimi3/sdfilesystemdma-l152re-cui.png

main.cpp

Committer:
mimi3
Date:
2016-02-12
Revision:
0:8f297fe0c66a
Child:
1:c39cfc31349d

File content as of revision 0:8f297fe0c66a:

#include "mbed.h"
#include "SDFileSystemDMA.h"
#define MOSI        D4
#define MISO        D5
#define SCLK        D3
#define CS          D10
//#define CD          D9      // detect SD card insertion. Do open always. 
//#define CD_TYPE     SDFileSystem::SWITCH_POS_NC     // always instertion state
//#define SPI_CLOCK_HZ    ( 12 * 1000000 ) 
Timer timer;

SDFileSystem sd( MOSI, MISO, SCLK, CS, "sd");  
//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
 DigitalIn button(USER_BUTTON, PullUp);

char buffer[512];
#define X_MEGA_BYTE     ((1)*1024*1024) /* 1Mbyte */
#define FILE_NAME   "GPATH"

int base_test() {
    
    printf("Hello World!\n");   
    while(button);

    mkdir("/sd/mydirx", 0777);
    
    FILE *fp = fopen("/sd/mydirx/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    fprintf(fp, "Hello fun SD Card World!");
    fclose(fp); 
 
    printf("Goodbye World!\n");
        return 0;
}

void writeTest()
{
    //Test write performance by creating a 1MB file
    printf("Testing %dByte buffer: write performance...", sizeof(buffer));
    FileHandle* file = sd.open(FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC);
    if (file != NULL) {
        timer.start();
        for (uint32_t i = 0; i < (X_MEGA_BYTE / sizeof(buffer)); i++) {
            if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
                timer.stop();
                printf("write error!\n");
                timer.reset();
                return;
            }
        }
        timer.stop();
        if (file->close())
            printf("failed to close file!\n");
        else{
            printf("done!\n\tResult: %d KB/s\n", (  1024 * 1000000 ) / timer.read_us() );
        }
        timer.reset();
    } else {
        printf("failed to create file!\n");
    }
}


void readTest()
{
    //Test read performance by reading the 1MB file created by writeTest()
    printf("Testing %dByte buffer: read performance...", sizeof(buffer));
    FileHandle* file = sd.open(FILE_NAME, O_RDONLY);
    if (file != NULL) {
        timer.start();
        int iterations = 0;
        while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
            iterations++;
        timer.stop();
        if (iterations != (X_MEGA_BYTE / sizeof(buffer))){
            printf("read error!\n");
                }
        else if (file->close()){
            printf("failed to close file!\n");
                }
       // else if (sd.remove(FILE_NAME))
       //     printf("failed to delete file!\n");
        else{
            printf("done!\n\tResult: %d KB/s\n",  (  1024 * 1000000 ) / timer.read_us());
        }
        timer.reset();
    } else {
        printf("failed to open file!\n");
    }
}

int main(){
    /* UART Baudrate is 9600bps*/
    while(1){
        printf("\nStart SD card accsess test! -- Press button");
        printf("\nWriting is very slow, wait several minuts.\n");   
        while(button);
        writeTest();
        readTest();
    }
}