mi mi / Mbed 2 deprecated SDFileSystemDMA-test Featured

Dependencies:   SDFileSystemDMA mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystemDMA.h"
00003 
00004 /* SD card Interface connection
00005  *           :  aitendo   :  General
00006  *  MCU sig. : IFB-254-SD :  PIN name
00007      ---     :    1       :   9 dat2
00008        CS    :    2       :   1 cs/dat3
00009       MOSI   :    3       :   2 di/cmd
00010        GND   :    4       :   3 vss1
00011        VCC   :    5       :   4 vdd
00012        CLK   :    6       :   5 clk
00013        GND   :    7       :   6 vss2
00014       MISO   :    8       :   7 do/dat0
00015        ---   :    9       :   8 dat1
00016        ---   :   10       :  11 wp
00017        ---   :   11       :  10 cd1
00018        ---   :   12       :  - case GND
00019 */
00020 /* SD card pin
00021 Pin side
00022 --------------\
00023         9     = \    DAT2/NC
00024             1 ===|   CS/DAT3    [CS]
00025             2 ===|   CMD/DI     [DI]
00026             3 ===|   VSS1
00027 Bottom      4 ===|   VDD
00028 View        5 ===|   CLK        [CLK]
00029             6 ===|   VSS2
00030             7 ===|   DO/DAT0    [DO]
00031             8   =|   DAT1/IRQ
00032 -----------------
00033 
00034                                         Arduino      NUCLEO-F411       NUCLEO-F030R8
00035 Logo side
00036 -----------------
00037             8   =|   DAT1/IRQ
00038             7 ===|   DO/DAT0    [DO]     D12           D12/PA_6           D5/PB_4
00039             6 ===|   VSS2
00040 Top         5 ===|   CLK        [CLK]    D13           D13/PA_5           D3/PB_3
00041 View        4 ===|   VDD
00042             3 ===|   VSS1
00043             2 ===|   CMD/DI     [DI]     D11           D11/PA_7           D4/PB_5
00044             1 ===|   CS/DAT3    [CS]     D8            D10/PB_6           D10/PB_6
00045         9     = /    DAT2/NC
00046 --------------/
00047 */
00048 
00049 
00050 /* You should confirm SPI_DEV macro in "spi_device.h" to specify SPI device number.
00051  * (default is 1 (SPI1))
00052  */
00053 #include "spi_device.h"
00054 #define UART_KEY_PRESS  1
00055 
00056 #define SPI_CLOCK_HZ    ( 12 * 1000000 )
00057 
00058 Timer timer;
00059 //Serial pc(PA_9,PA_10);
00060 Serial pc(USBTX,USBRX); /* Baudrate is 9600bps*/
00061 
00062 SDFileSystem sd( MOSI, MISO, SCLK, CS, "sd", SPI_CLOCK_HZ);
00063 
00064 /* Set buffer size */
00065 char buffer[512*1]  __attribute__ ((aligned (4))) ;      /*   512 bytes */
00066 //char buffer[512*2]  __attribute__ ((aligned (4))) ;    /*  1024 bytes */
00067 //char buffer[512*4]  __attribute__ ((aligned (4))) ;    /*  2048 bytes */
00068 //char buffer[512*8]  __attribute__ ((aligned (4))) ;    /*  4096 bytes */
00069 //char buffer[512*16] __attribute__ ((aligned (4))) ;    /*  8192 bytes */
00070 //char buffer[512*32] __attribute__ ((aligned (4))) ;    /* 16384 bytes */
00071 
00072 #define X_MEGA_BYTE     ((1)*1024*1024) /* 1Mbyte */
00073 #define FILE_NAME   "GPATH"
00074 
00075 #if defined(__CC_ARM)     /* ARMCC      */
00076 #define TOOLCHAIN   "ARM/uARM"
00077 #define C_VERSION   __ARMCC_VERSION
00078 #elif defined(__GNUC__)       /* GCC        */
00079 #define TOOLCHAIN   "GCC_ARM"
00080 #elif defined(__ICCARM__)   /* IAR        */
00081 #define TOOLCHAIN   "IAR"
00082 #else
00083 #define TOOLCHAIN   "Unknown"
00084 #warning "This compiler is not yet supported."
00085 #endif
00086 
00087 int wait_key_press(){
00088 #if UART_KEY_PRESS
00089     while(!pc.readable());
00090     return  pc.getc();
00091 #endif
00092 }
00093 
00094 void writeTest()
00095 {
00096 #if !_FS_READONLY
00097     //Test write performance by creating a 1MB file
00098     pc.printf("[Write]: Testing %d bytes buffer: write performance...", sizeof(buffer));
00099     fflush(stdout);
00100     for(uint32_t k = 0; k < sizeof(buffer); k++){
00101         buffer[k] = k & 0xff;
00102     }
00103 
00104     FileHandle* file = sd.open(FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC);
00105     if (file != NULL) {
00106         timer.start();
00107         for (uint32_t i = 0; i < (X_MEGA_BYTE / sizeof(buffer)); i++) {
00108             if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
00109                 timer.stop();
00110                 pc.printf("write error!\n");
00111                 timer.reset();
00112                 return;
00113             }
00114         }
00115         timer.stop();
00116         if (file->close())
00117             pc.printf("failed to close file!\n");
00118         else{
00119             pc.printf("done!\n\tResult: %d KB/s\n", (  1024 * 1000000 ) / timer.read_us() );
00120         }
00121         timer.reset();
00122     } else {
00123         pc.printf("failed to create file!\n");
00124     }
00125 #endif
00126 }
00127 
00128 void readTest()
00129 {
00130     //Test read performance by reading the 1MB file created by writeTest()
00131     pc.printf("[Read]: Testing %d bytes buffer: read performance...", sizeof(buffer));
00132     fflush(stdout);
00133     FileHandle* file = sd.open(FILE_NAME, O_RDONLY);
00134     if (file != NULL) {
00135         timer.start();
00136         int iterations = 0;
00137         while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)){
00138             iterations++;
00139         }
00140         timer.stop();
00141         if (iterations != (X_MEGA_BYTE / sizeof(buffer))){
00142             pc.printf("read error!\n");
00143         }
00144         else if (file->close()){
00145             pc.printf("failed to close file!\n");
00146         }
00147         else{
00148             pc.printf("done!\n\tResult: %d KB/s\n",  (  1024 * 1000000 ) / timer.read_us());
00149         }
00150         timer.reset();
00151     } else {
00152         pc.printf("failed to open file!\n");
00153     }
00154 }
00155 
00156 void verifyTest()
00157 {
00158     int errorCount = 0;
00159     //Test read performance by reading the 1MB file created by writeTest()
00160     pc.printf("[Read]: Verifying %d bytes buffer ...", sizeof(buffer));
00161     fflush(stdout);
00162     FileHandle* file = sd.open(FILE_NAME, O_RDONLY);
00163     if (file != NULL) {
00164         timer.start();
00165         int iterations = 0;
00166         while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)){
00167             iterations++;
00168             for(uint32_t k = 0; k < sizeof(buffer); k++){
00169                 if ( buffer[k] != (k & 0xff) ){
00170                     errorCount++;
00171                 }
00172             }
00173         }
00174         timer.stop();
00175         if (iterations != (X_MEGA_BYTE / sizeof(buffer))){
00176             pc.printf("read error!\n");
00177         }
00178         else if (file->close()){
00179             pc.printf("failed to close file!\n");
00180         }
00181         else{
00182             //    pc.printf("done!\n\tResult: %d KB/s\n",  (  1024 * 1000000 ) / timer.read_us());
00183 
00184         }
00185         timer.reset();
00186     } else {
00187         pc.printf("failed to open file!\n");
00188     }
00189     pc.printf("\n     Error Count : %d", errorCount);
00190 }
00191 
00192 void warning(){
00193     FileHandle* file = sd.open(FILE_NAME, O_RDONLY);
00194     if (file == NULL) {
00195         pc.printf("\n***************************************");
00196         pc.printf("\n*** First, you must execute [w] command");
00197         pc.printf("\n*** to generate test file ( %s ).",FILE_NAME);
00198         pc.printf("\n***************************************");
00199     } else {
00200         file->close();
00201     }
00202 }
00203 
00204 time_t read_rtc(void) {
00205     return 0;
00206 }
00207 
00208 int main(){
00209     int i = 1;
00210     /* setbuf( stdout, NULL ); */
00211     /*
00212      * Refer to https://developer.mbed.org/questions/69644/RTC-error-LSE-clock-initialization-faile/
00213      */
00214     attach_rtc(&read_rtc, NULL, NULL, NULL);
00215     /**/
00216     while(1){
00217         pc.printf("\n(%2d) ------ SDFileSystemDMA Test ----------------------------",i++);
00218         pc.printf("\nR/W Buffer size         = %8d Bytes", sizeof(buffer) );
00219         pc.printf("\nSystemClock             = %d Hz",SystemCoreClock);
00220         pc.printf("\nSPI(%d),  Max SPI Clock  = %d Hz (Probably)",SPI_DEV,SystemCoreClock>>1);
00221         pc.printf("\nCompiler                = %s", TOOLCHAIN);
00222         pc.printf("\nmbed-lib                = Rev.%d", MBED_LIBRARY_VERSION);
00223         pc.printf("\n\nStart SD card accsess test !");
00224         pc.printf("\n[r]: Start Read Test. At least execute [w] once");
00225         pc.printf("\n[w]: Start Write and Read Test");
00226         pc.printf("\n[v]: Start Read Verify Test. At least execute [w] once");
00227         pc.printf("\nNote: Writing is very slow, wait tens of seconds.");
00228         warning();
00229         pc.printf("\n--- Select [r] or [w] or [v]---\n");
00230         fflush(stdout);
00231         char ch = wait_key_press();
00232         if( ch =='r' ){
00233             readTest();
00234         } else if( ch =='w' ){
00235             writeTest();
00236             readTest();
00237         } else if( ch =='v' ){
00238             verifyTest();
00239         }
00240         else{
00241             continue;
00242         }
00243         pc.printf("\n--- Press any key ---\n");
00244         fflush(stdout);
00245         wait_key_press();
00246     }
00247 }
00248