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-23
Revision:
12:dfa39c797789
Parent:
11:d5fc11ac948a
Child:
18:5fb42182d85d

File content as of revision 12:dfa39c797789:

#include "mbed.h"
#include "SDFileSystemDMA.h"

/* SD card Interface connection
 *           :  aitendo   :  General
 *  MCU sig. : IFB-254-SD :  PIN name
     ---     :    1       :   9 dat2
       CS    :    2       :   1 cs/dat3
      MOSI   :    3       :   2 di/cmd
       GND   :    4       :   3 vss1
       VCC   :    5       :   4 vdd
       CLK   :    6       :   5 clk
       GND   :    7       :   6 vss2
      MISO   :    8       :   7 do/dat0
       ---   :    9       :   8 dat1
       ---   :   10       :  11 wp
       ---   :   11       :  10 cd1
       ---   :   12       :  - case GND
*/

/* You should confirm SPI_DEV macro in "spi_device.h" to specify SPI device number.
 * (default is 1 (SPI1))
 */
#include "spi_device.h"

#define UART_KEY_PRESS  1

#define SPI_CLOCK_HZ    ( 12 * 1000000 ) 
Timer timer;
//Serial pc(PA_9,PA_10);
Serial pc(USBTX,USBRX);

SDFileSystem sd( MOSI, MISO, SCLK, CS, "sd", SPI_CLOCK_HZ);  

//char buffer[512*1]  __attribute__ ((aligned (4))) ;    /*   512 byte */
//char buffer[512*2]  __attribute__ ((aligned (4))) ;    /*  1024 byte */
char buffer[512*4]  __attribute__ ((aligned (4))) ;    /*  2028 byte */
//char buffer[512*8]  __attribute__ ((aligned (4))) ;    /*  4096 byte */
//char buffer[512*16] __attribute__ ((aligned (4))) ;    /*  8192 byte */
//char buffer[512*32] __attribute__ ((aligned (4))) ;    /* 16384 byte */

#define X_MEGA_BYTE     ((1)*1024*1024) /* 1Mbyte */
#define FILE_NAME   "GPATH"
void wait_key_press(){
#if UART_KEY_PRESS
	pc.printf("\n-- Select 'r' or 'w' --\n");  
	fflush(stdout); 
	while(!pc.readable());
	volatile int res = pc.getc();
#else

#endif
}

void writeTest()
{
	//Test write performance by creating a 1MB file
	pc.printf("[Write]: Testing %d byte buffer: write performance...", sizeof(buffer));
	fflush(stdout); 
	for(uint32_t k = 0; k < sizeof(buffer); k++){
		buffer[k] = k & 0xff;
	}

	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();
				pc.printf("write error!\n");
				timer.reset();
				return;
			}
		}
		timer.stop();
		if (file->close())
			pc.printf("failed to close file!\n");
		else{
			pc.printf("done!\n\tResult: %d KB/s\n", (  1024 * 1000000 ) / timer.read_us() );
		}
		timer.reset();
	} else {
		pc.printf("failed to create file!\n");
	}
}

void readTest()
{
	//Test read performance by reading the 1MB file created by writeTest()
	pc.printf("[Read]: Testing %d byte buffer: read performance...", sizeof(buffer));
	fflush(stdout); 
	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))){
			pc.printf("read error!\n");
		}
		else if (file->close()){
			pc.printf("failed to close file!\n");
		}
		// else if (sd.remove(FILE_NAME))
		//     pc.printf("failed to delete file!\n");
		else{
			pc.printf("done!\n\tResult: %d KB/s\n",  (  1024 * 1000000 ) / timer.read_us());
		}
		timer.reset();
	} else {
		pc.printf("failed to open file!\n");
	}
}

void verifyTest()
{
	int errorCount = 0;
	//Test read performance by reading the 1MB file created by writeTest()
	pc.printf("[Read]: Verifying %d byte buffer ...", sizeof(buffer));
	fflush(stdout); 
	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++;
			for(uint32_t k = 0; k < sizeof(buffer); k++){
				if ( buffer[k] != (k & 0xff) ){
					errorCount++;    
				}
			}
		}
		timer.stop();
		if (iterations != (X_MEGA_BYTE / sizeof(buffer))){
			pc.printf("read error!\n");
		}
		else if (file->close()){
			pc.printf("failed to close file!\n");
		}
		// else if (sd.remove(FILE_NAME))
		//     pc.printf("failed to delete file!\n");
		else{
			//    pc.printf("done!\n\tResult: %d KB/s\n",  (  1024 * 1000000 ) / timer.read_us());

		}
		timer.reset();
	} else {
		pc.printf("failed to open file!\n");
	}
	pc.printf("\n     Error Count : %d", errorCount);
}

int main(){
	/* UART Baudrate is 9600bps*/
	while(1){
		pc.printf("\n---------------------------------------");
		pc.printf("\nSPI(%d), SysClock= %d Hz",SPI_DEV, SystemCoreClock);
		pc.printf("   mbed rev.%d", MBED_LIBRARY_VERSION);
		pc.printf("\nStart SD card accsess test!");
		pc.printf("\n[r]: Start Read Test. At least execute [w] onece");
		pc.printf("\n[w]: Start Write and Read Test");
		pc.printf("\n[v]: Start Read Verify Test. At least execute [w] onece");
		pc.printf("\nWriting is very slow, wait tens of seconds."); 
		pc.printf("\n-- Select [r] or [w] --\n");  
		fflush(stdout); 
		while(!pc.readable());
		char ch = pc.getc();
		if( ch =='r' ){
			readTest();
		} else if( ch =='w' ){
			writeTest();
			readTest();
		} else if( ch =='v' ){
			verifyTest();
		}
	}
}