main
Default mainpagebasic-fatfs-sdcard-projectmain
Description Source Call Graph
Start Line: 209
int main(void)
{
    unsigned int i;
    unsigned int ByteToRead;
    unsigned int ByteRead;
    unsigned int ByteWritten;
    char key;

    FRESULT res;
    DIR dirs;
    FATFS fs;             // File system object
    FIL FileObject;

    TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- Basic FatFS Full Version with SDCard Project %s --\n\r", SOFTPACK_VERSION);
    printf("-- %s\n\r", BOARD_NAME);
    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    // Init Disk
    printf("-I- Init media Sdcard\n\r");
    MEDSdcard_Initialize(&medias[ID_DRV], MCI_ID);

    // Mount disk
    printf("-I- Mount disk %d\n\r", ID_DRV);
    memset(&fs, 0, sizeof(FATFS));      // Clear file system object
    res = f_mount(ID_DRV, &fs);
    if( res != FR_OK ) {
        printf("-E- f_mount pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }

    // Test if the disk is formated
    res = f_opendir (&dirs,STR_ROOT_DIRECTORY);
    if(res == FR_OK ){

        // erase sdcard to re-format it ?
        printf("-I- The disk is already formated.\n\r");

        // Display the file tree
        printf("-I- Display files contained on the SDcard :\n\r");
        FF_ScanDir(STR_ROOT_DIRECTORY);

        printf("-I- Do you want to erase the sdcard to re-format disk ? (y/n)!\n\r");

        key = DBGU_GetChar();
        if( (key == 'y') ||  (key == 'Y'))
        {
          for(i=0;i<100;i++) {
              MEDSdcard_EraseBlock(&medias[ID_DRV], i);
          }
          printf("-I- Erase the first 100 blocks complete !\n\r");
          res = FR_NO_FILESYSTEM;
        }
    }

    if( res == FR_NO_FILESYSTEM ) {

        // Format disk
        printf("-I- Format disk %d\n\r", ID_DRV);
        printf("-I- Please wait a moment during formating...\n\r");
        res = f_mkfs(ID_DRV,    // Drv
                        0,    // FDISK partition
                        512); // AllocSize
        printf("-I- Format disk finished !\n\r");
        if( res != FR_OK ) {
            printf("-E- f_mkfs pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
            return 0;
        }
    }

    // Create a new file
    printf("-I- Create a file : \"%s\"\n\r", FileName);
    res = f_open(&FileObject, FileName, FA_CREATE_ALWAYS|FA_WRITE);
    if( res != FR_OK ) {
        printf("-E- f_open create pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }

    // Write a checkerboard pattern in the buffer
    for (i=0; i < sizeof(data); i++) {
        if ((i & 1) == 0) {
            data[i] = (i & 0x55);
        }
        else {
            data[i] = (i & 0xAA);
        }
    }
    printf("-I- Write file\n\r");
    res = f_write(&FileObject, data, DATA_SIZE, &ByteWritten);
    printf("-I- ByteWritten=%d\n\r", (int)ByteWritten);
    if( res != FR_OK ) {
        printf("-E- f_write pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }
    else {
        printf("-I- f_write ok: ByteWritten=%d\n\r", (int)ByteWritten);
    }

    // Close the file
    printf("-I- Close file\n\r");
    res = f_close(&FileObject);
    if( res != FR_OK ) {
        printf("-E- f_close pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }

    // Open the file
    printf("-I- Open the same file : \"%s\"\n\r", FileName);
    res = f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ);
    if( res != FR_OK ) {
        printf("-E- f_open read pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }
    ASSERT( FileObject.fsize == DATA_SIZE,  "File size value not expected!\n\r");

    // Read file
    printf("-I- Read file\n\r");
    memset(data, 0, DATA_SIZE);
    ByteToRead = FileObject.fsize;
    res = f_read(&FileObject, data, ByteToRead, &ByteRead);
    if(res != FR_OK) {
        printf("-E- f_read pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }

    // Close the file
    printf("-I- Close file\n\r");
    res = f_close(&FileObject);
    if( res != FR_OK ) {
        printf("-E- f_close pb: 0x%X (%s)\n\r", res, FF_GetStrResult(res));
        return 0;
    }

    // compare read data with the expected data
    for (i=0; i < sizeof(data); i++) {
        ASSERT((((i & 1) == 0) && (data[i] == (i & 0x55)))
               || (data[i] == (i & 0xAA)),
               "Invalid data at data[%u] (expected 0x%02X, read 0x%02X)\n\r",
               i, ((i & 1) == 0) ? (i & 0x55) : (i & 0xAA), data[i]);
    }
    printf("-I- File data Ok !\n\r");

    printf("-I- Test passed !\n\r");

    return 0;
}