/*********************************************************************
*
*       _USBTask
*/
static void _USBTask(void) {
    _AddMSD();
    USB_Start();
    while(1) {
        //
        // Wait for configuration
        //
        while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) {
            BSP_ToggleLED(0);
            USB_OS_Delay(50);
        }
        BSP_SetLED(0);
        FS_Unmount("");
        USB_MSD_Task();    // Task, does only return when device is non-configured mode
        FS_Mount("");
    }
}
void MainTask(void) {
  unsigned i;
  U32 Space;
  U32 NumLoops;
  U32 NumBytes;
  U32 NumBytesAtOnce;
  FS_FILE * pFile;
  I32 t;

  FS_Init();
  _TestNo = -1;
  //
  // Check if we need to low-level format the volume
  //
  if (FS_IsLLFormatted(VOLUME_NAME) == 0) {
    FS_X_Log("Low level formatting\n");
    FS_FormatLow(VOLUME_NAME);  /* Erase & Low-level  format the flash */
  }
  //
  // Volume is always high level formatted
  // before doing any performance tests.
  //
  FS_X_Log("High level formatting\n");
#if FS_SUPPORT_FAT
  if (FS_FormatSD(VOLUME_NAME) == 0) {
#else
  if (FS_Format(VOLUME_NAME, NULL) == 0) {
#endif
    //
    // Disable that the directory entry is every time
    // updated after a write operation
    //
    FS_ConfigUpdateDirOnWrite(0);
    //
    // Fill the buffer with data
    //
    FS_MEMSET((void*)_aBuffer, 'a', sizeof(_aBuffer));
    //
    // Get some general info
    //
    Space          = FS_GetVolumeFreeSpace(VOLUME_NAME);
    Space          = MIN(Space, FILE_SIZE);
    NumBytes       = BLOCK_SIZE * NUM_BLOCKS_MEASURE;
    NumBytesAtOnce = BLOCK_SIZE;
    NumLoops       = Space / NumBytes;

    //
    // Create file of full size
    //
    _StartTest("W", NumBytes);
    pFile = FS_FOpen(FILE_NAME, "w");
    //
    // Preallocate the file, setting the file pointer to the highest position
    // and declare it as the end of the file.
    //
    FS_SetFilePos(pFile, Space, FS_FILE_BEGIN);
    FS_SetEndOfFile(pFile);
    //
    // Set file position to the beginning
    //
    FS_SetFilePos(pFile, 0, FS_FILE_BEGIN);
    //
    // Check write performance with clusters/file size preallocated
    //
    sprintf(_ac, "Writing %lu chunks of %lu Bytes: ", NumLoops, NumBytes);
    FS_X_Log(_ac);
    for (i = 0; i < NumLoops ; i++) {
      t = _WriteFile(pFile, &_aBuffer[0], NumBytesAtOnce);
      _StoreResult(t);
      FS_X_Log(".");
    }
    FS_X_Log("OK\n");
    FS_FClose(pFile);
    //
    // Check read performance
    //
    _StartTest("R", NumBytes);
    sprintf(_ac, "Reading %lu chunks of %lu Bytes: " , NumLoops, NumBytes);
    FS_X_Log(_ac);
    pFile = FS_FOpen(FILE_NAME, "r");
    for (i = 0; i < NumLoops; i++) {
      t = _ReadFile(pFile, _aBuffer, NumBytesAtOnce);
      _StoreResult(t);
      FS_X_Log(".");
    }
    FS_X_Log("OK\n\n");
    FS_FClose(pFile);
    //
    // Show results for performance list
    //
    for (i = 0; i <= (unsigned)_TestNo; i++) {
      sprintf(_ac, "%s Speed: %f kByte/s\n", _aResult[i].sName, _GetAverage(i));
      FS_X_Log(_ac);
    }
    FS_X_Log("Finished\n");
    FS_Unmount(VOLUME_NAME);
  } else {
    FS_X_Log("Volume could not be formatted!\n");
  }
  while (1) {
    ;
  }
}