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-14
- Revision:
- 5:fef1667b832e
- Parent:
- 4:0719b77970da
- Child:
- 9:289de1b6d378
File content as of revision 5:fef1667b832e:
#include "mbed.h"
#include "SDFileSystemDMA.h"
#define UART_KEY_PRESS 1
#if defined(TARGET_NUCLEO_F411RE)|| defined(TARGET_F401RE)
#define MOSI D11
#define MISO D12
#define SCLK D13
#define CS D10
#elif defined(TARGET_STM32F3)
#define MOSI PC_12
#define MISO PC_11
#define SCLK PC_10
#define CS PC_9
#else
#define MOSI D4
#define MISO D5
#define SCLK D3
#define CS D10
#endif
//#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;
Serial pc(USBTX,USBRX);
SDFileSystem sd( MOSI, MISO, SCLK, CS, "sd", SPI_CLOCK_HZ);
//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
char buffer[512];
#define X_MEGA_BYTE ((1)*1024*1024) /* 1Mbyte */
#define FILE_NAME "GPATH"
void wait_key_press(){
#if UART_KEY_PRESS
printf("\n-- Press any key --\n");
fflush(stdout);
while(!pc.readable());
volatile int res = pc.getc();
#else
#endif
}
void writeTest()
{
//Test write performance by creating a 1MB file
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();
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("[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))){
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!");
printf("\nWriting is very slow, wait several minuts.");
wait_key_press();
writeTest();
readTest();
}
}