Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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
NUCLEO-L152RE
About 1MBytes/sec

main.cpp
- Committer:
- mimi3
- Date:
- 2016-02-20
- Revision:
- 10:5f6fc7dc119b
- Parent:
- 9:289de1b6d378
- Child:
- 11:d5fc11ac948a
File content as of revision 10:5f6fc7dc119b:
#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(USBTX,USBRX);
SDFileSystem sd( MOSI, MISO, SCLK, CS, "sd", SPI_CLOCK_HZ);
char buffer[512];
#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 %dByte buffer: write performance...", sizeof(buffer));
fflush(stdout);
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 %dByte 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");
}
}
int main(){
/* UART Baudrate is 9600bps*/
while(1){
pc.printf("\nSysClock= %d Hz", SystemCoreClock);
pc.printf(" mbed rev.%d", MBED_LIBRARY_VERSION);
pc.printf("\nStart SD card accsess test!");
pc.printf("\n SPI(%d) device",SPI_DEV);
pc.printf("\n[r]: Start Read Test. At least execute [w] onece");
pc.printf("\n[w]: Start Write and Read Test");
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();
}
}
}