DRESULT disk_read (
  BYTE pdrv,    /* Physical drive nmuber to identify the drive */
  BYTE *buff,   /* Data buffer to store read data */
  DWORD sector, /* Sector address in LBA */
  UINT count    /* Number of sectors to read */
)
{
  switch (pdrv) {
#if HAL_USE_MMC_SPI
  case MMC:
    if (blkGetDriverState(&MMCD1) != BLK_READY)
      return RES_NOTRDY;
    if (mmcStartSequentialRead(&MMCD1, sector))
      return RES_ERROR;
    while (count > 0) {
      if (mmcSequentialRead(&MMCD1, buff))
        return RES_ERROR;
      buff += MMCSD_BLOCK_SIZE;
      count--;
    }
    if (mmcStopSequentialRead(&MMCD1))
        return RES_ERROR;
    return RES_OK;
#else
  case SDC:
    if (blkGetDriverState(&SDCD1) != BLK_READY)
      return RES_NOTRDY;
    if (sdcRead(&SDCD1, sector, buff, count))
      return RES_ERROR;
    return RES_OK;
#endif
  }
  return RES_PARERR;
}
DRESULT disk_readp (
		BYTE* buff,    /* [OUT] Pointer to the read buffer */
		DWORD sector,  /* [IN]  Sector number */
		UINT offset,   /* [IN]  Byte offset in the sector to start to read */
		UINT count     /* [IN]  Number of bytes to read */
		) {

	if (sector != sectpos) {
		#if HAL_USE_MMC_SPI
			if (blkGetDriverState(&MMCD1) != BLK_READY)
				return RES_NOTRDY;
			if (mmcStartSequentialRead(&MMCD1, sector))
				return RES_ERROR;
			if (mmcSequentialRead(&MMCD1, sectBuf))
				return RES_ERROR;
			if (mmcStopSequentialRead(&MMCD1))
				return RES_ERROR;
		#else
			if (blkGetDriverState(&SDCD1) != BLK_READY)
				return RES_NOTRDY;
			if (sdcRead(&SDCD1, sector, sectBuf, 1))
				return RES_ERROR;
		#endif
			sectpos = sector;
	}
	memcpy(buff, sectBuf + offset, count);
	return RES_OK;
}
Example #3
0
/**
 * @brief   Parody of UNIX badblocks program.
 *
 * @param[in] start       first block to check
 * @param[in] end         last block to check
 * @param[in] blockatonce number of blocks to check at once
 * @param[in] pattern     check pattern
 *
 * @return              The operation status.
 * @retval SDC_SUCCESS  operation succeeded, the requested blocks have been
 *                      read.
 * @retval SDC_FAILED   operation failed, the state of the buffer is uncertain.
 */
bool_t badblocks(uint32_t start, uint32_t end, uint32_t blockatonce, uint8_t pattern){
  uint32_t position = 0;
  uint32_t i = 0;

  chDbgCheck(blockatonce <= SDC_BURST_SIZE, "badblocks");

  /* fill control buffer */
  for (i=0; i < MMCSD_BLOCK_SIZE * blockatonce; i++)
    outbuf[i] = pattern;

  /* fill SD card with pattern. */
  position = start;
  while (position < end){
    if (sdcWrite(&SDCD1, position, outbuf, blockatonce))
      goto ERROR;
    position += blockatonce;
  }

  /* read and compare. */
  position = start;
  while (position < end){
    if (sdcRead(&SDCD1, position, inbuf, blockatonce))
      goto ERROR;
    if (memcmp(inbuf, outbuf, blockatonce * MMCSD_BLOCK_SIZE) != 0)
      goto ERROR;
    position += blockatonce;
  }
  return FALSE;

ERROR:
  return TRUE;
}
Example #4
0
DRESULT disk_read (
    BYTE drv,        /* Physical drive nmuber (0..) */
    BYTE *buff,        /* Data buffer to store read data */
    DWORD sector,    /* Sector address (LBA) */
    BYTE count        /* Number of sectors to read (1..255) */
)
{
  switch (drv) {
#if HAL_USE_MMC_SPI
  case MMC:
    if (mmcGetDriverState(&MMCD1) != MMC_READY)
      return RES_NOTRDY;
    if (mmcStartSequentialRead(&MMCD1, sector))
      return RES_ERROR;
    while (count > 0) {
      if (mmcSequentialRead(&MMCD1, buff))
        return RES_ERROR;
      buff += MMC_SECTOR_SIZE;
      count--;
    }
    if (mmcStopSequentialRead(&MMCD1))
        return RES_ERROR;
    return RES_OK;
#else
  case SDC:
    if (sdcGetDriverState(&SDCD1) != SDC_ACTIVE)
      return RES_NOTRDY;
    if (sdcRead(&SDCD1, sector, buff, count))
      return RES_ERROR;
    return RES_OK;
#endif
  }
  return RES_PARERR;
}
Example #5
0
void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) {
  static const char *mode[] = {"SDV11", "SDV20", "MMC", NULL};
  systime_t start, end;
  uint32_t n, startblk;

  if (argc != 1) {
    chprintf(chp, "Usage: sdiotest read|write|erase|all\r\n");
    return;
  }

  /* Card presence check.*/
  if (!blkIsInserted(&SDCD1)) {
    chprintf(chp, "Card not inserted, aborting.\r\n");
    return;
  }

  /* Connection to the card.*/
  chprintf(chp, "Connecting... ");
  if (sdcConnect(&SDCD1)) {
    chprintf(chp, "failed\r\n");
    return;
  }

  chprintf(chp, "OK\r\n\r\nCard Info\r\n");
  chprintf(chp, "CSD      : %08X %8X %08X %08X \r\n",
           SDCD1.csd[3], SDCD1.csd[2], SDCD1.csd[1], SDCD1.csd[0]);
  chprintf(chp, "CID      : %08X %8X %08X %08X \r\n",
           SDCD1.cid[3], SDCD1.cid[2], SDCD1.cid[1], SDCD1.cid[0]);
  chprintf(chp, "Mode     : %s\r\n", mode[SDCD1.cardmode & 3U]);
  chprintf(chp, "Capacity : %DMB\r\n", SDCD1.capacity / 2048);

  /* The test is performed in the middle of the flash area.*/
  startblk = (SDCD1.capacity / MMCSD_BLOCK_SIZE) / 2;

  if ((strcmp(argv[0], "read") == 0) ||
      (strcmp(argv[0], "all") == 0)) {

    /* Single block read performance, aligned.*/
    chprintf(chp, "Single block aligned read performance:           ");
    start = chVTGetSystemTime();
    end = start + MS2ST(1000);
    n = 0;
    do {
      if (blkRead(&SDCD1, startblk, buf, 1)) {
        chprintf(chp, "failed\r\n");
        goto exittest;
      }
      n++;
    } while (chVTIsSystemTimeWithin(start, end));
    chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);

    /* Multiple sequential blocks read performance, aligned.*/
    chprintf(chp, "16 sequential blocks aligned read performance:   ");
    start = chVTGetSystemTime();
    end = start + MS2ST(1000);
    n = 0;
    do {
      if (blkRead(&SDCD1, startblk, buf, SDC_BURST_SIZE)) {
        chprintf(chp, "failed\r\n");
        goto exittest;
      }
      n += SDC_BURST_SIZE;
    } while (chVTIsSystemTimeWithin(start, end));
    chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);

#if STM32_SDC_SDIO_UNALIGNED_SUPPORT
    /* Single block read performance, unaligned.*/
    chprintf(chp, "Single block unaligned read performance:         ");
    start = chVTGetSystemTime();
    end = start + MS2ST(1000);
    n = 0;
    do {
      if (blkRead(&SDCD1, startblk, buf + 1, 1)) {
        chprintf(chp, "failed\r\n");
        goto exittest;
      }
      n++;
    } while (chVTIsSystemTimeWithin(start, end));
    chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);

    /* Multiple sequential blocks read performance, unaligned.*/
    chprintf(chp, "16 sequential blocks unaligned read performance: ");
    start = chVTGetSystemTime();
    end = start + MS2ST(1000);
    n = 0;
    do {
      if (blkRead(&SDCD1, startblk, buf + 1, SDC_BURST_SIZE)) {
        chprintf(chp, "failed\r\n");
        goto exittest;
      }
      n += SDC_BURST_SIZE;
    } while (chVTIsSystemTimeWithin(start, end));
    chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);
#endif /* STM32_SDC_SDIO_UNALIGNED_SUPPORT */
  }

  if ((strcmp(argv[0], "write") == 0) ||
      (strcmp(argv[0], "all") == 0)) {
    unsigned i;

    memset(buf, 0xAA, MMCSD_BLOCK_SIZE * 2);
    chprintf(chp, "Writing...");
    if(sdcWrite(&SDCD1, startblk, buf, 2)) {
      chprintf(chp, "failed\r\n");
      goto exittest;
    }
    chprintf(chp, "OK\r\n");

    memset(buf, 0x55, MMCSD_BLOCK_SIZE * 2);
    chprintf(chp, "Reading...");
    if (blkRead(&SDCD1, startblk, buf, 1)) {
      chprintf(chp, "failed\r\n");
      goto exittest;
    }
    chprintf(chp, "OK\r\n");

    for (i = 0; i < MMCSD_BLOCK_SIZE; i++)
      buf[i] = i + 8;
    chprintf(chp, "Writing...");
    if(sdcWrite(&SDCD1, startblk, buf, 2)) {
      chprintf(chp, "failed\r\n");
      goto exittest;
    }
    chprintf(chp, "OK\r\n");

    memset(buf, 0, MMCSD_BLOCK_SIZE * 2);
    chprintf(chp, "Reading...");
    if (blkRead(&SDCD1, startblk, buf, 1)) {
      chprintf(chp, "failed\r\n");
      goto exittest;
    }
    chprintf(chp, "OK\r\n");
  }

  if ((strcmp(argv[0], "erase") == 0) ||
      (strcmp(argv[0], "all") == 0)) {
    /**
     * Test sdcErase()
     * Strategy:
     *   1. Fill two blocks with non-constant data
     *   2. Write two blocks starting at startblk
     *   3. Erase the second of the two blocks
     *      3.1. First block should be equal to the data written
     *      3.2. Second block should NOT be equal too the data written (i.e. erased).
     *   4. Erase both first and second block
     *      4.1 Both blocks should not be equal to the data initially written
     * Precondition: SDC_BURST_SIZE >= 2
     */
    memset(buf, 0, MMCSD_BLOCK_SIZE * 2);
    memset(buf2, 0, MMCSD_BLOCK_SIZE * 2);
    /* 1. */
    unsigned int i = 0;
    for (; i < MMCSD_BLOCK_SIZE * 2; ++i) {
      buf[i] = (i + 7) % 'T'; //Ensure block 1/2 are not equal
    }
    /* 2. */
    if(sdcWrite(&SDCD1, startblk, buf, 2)) {
      chprintf(chp, "sdcErase() test write failed\r\n");
      goto exittest;
    }
    /* 3. (erase) */
    if(sdcErase(&SDCD1, startblk + 1, startblk + 2)) {
      chprintf(chp, "sdcErase() failed\r\n");
      goto exittest;
    }
    sdcflags_t errflags = sdcGetAndClearErrors(&SDCD1);
    if(errflags) {
      chprintf(chp, "sdcErase() yielded error flags: %d\r\n", errflags);
      goto exittest;
    }
    if(sdcRead(&SDCD1, startblk, buf2, 2)) {
      chprintf(chp, "single-block sdcErase() failed\r\n");
      goto exittest;
    }
    /* 3.1. */
    if(memcmp(buf, buf2, MMCSD_BLOCK_SIZE) != 0) {
      chprintf(chp, "sdcErase() non-erased block compare failed\r\n");
      goto exittest;
    }
    /* 3.2. */
    if(memcmp(buf + MMCSD_BLOCK_SIZE,
              buf2 + MMCSD_BLOCK_SIZE, MMCSD_BLOCK_SIZE) == 0) {
      chprintf(chp, "sdcErase() erased block compare failed\r\n");
      goto exittest;
    }
    /* 4. */
    if(sdcErase(&SDCD1, startblk, startblk + 2)) {
      chprintf(chp, "multi-block sdcErase() failed\r\n");
      goto exittest;
    }
    if(sdcRead(&SDCD1, startblk, buf2, 2)) {
      chprintf(chp, "single-block sdcErase() failed\r\n");
      goto exittest;
    }
    /* 4.1 */
    if(memcmp(buf, buf2, MMCSD_BLOCK_SIZE) == 0) {
      chprintf(chp, "multi-block sdcErase() erased block compare failed\r\n");
      goto exittest;
    }
    if(memcmp(buf + MMCSD_BLOCK_SIZE,
              buf2 + MMCSD_BLOCK_SIZE, MMCSD_BLOCK_SIZE) == 0) {
      chprintf(chp, "multi-block sdcErase() erased block compare failed\r\n");
      goto exittest;
    }
    /* END of sdcErase() test */
  }
  
  /* Card disconnect and command end.*/
exittest:
  sdcDisconnect(&SDCD1);
}
Example #6
0
void cmd_sdiotest(BaseSequentialStream *chp, int argc, char *argv[]){
  (void)argc;
  (void)argv;
  uint32_t i = 0;

  chprintf(chp, "Trying to connect SDIO... ");
  chThdSleepMilliseconds(100);

  if (!sdcConnect(&SDCD1)) {

    chprintf(chp, "OK\r\n");
    chprintf(chp, "*** Card CSD content is: ");
    chprintf(chp, "%X %X %X %X \r\n", (&SDCD1)->csd[3], (&SDCD1)->csd[2],
                                      (&SDCD1)->csd[1], (&SDCD1)->csd[0]);

    chprintf(chp, "Single aligned read...");
    chThdSleepMilliseconds(100);
    if (sdcRead(&SDCD1, 0, inbuf, 1))
      chSysHalt();
    chprintf(chp, " OK\r\n");
    chThdSleepMilliseconds(100);


    chprintf(chp, "Single unaligned read...");
    chThdSleepMilliseconds(100);
    if (sdcRead(&SDCD1, 0, inbuf + 1, 1))
      chSysHalt();
    if (sdcRead(&SDCD1, 0, inbuf + 2, 1))
      chSysHalt();
    if (sdcRead(&SDCD1, 0, inbuf + 3, 1))
      chSysHalt();
    chprintf(chp, " OK\r\n");
    chThdSleepMilliseconds(100);


    chprintf(chp, "Multiple aligned reads...");
    chThdSleepMilliseconds(100);
    fillbuffers(0x55);
    /* fill reference buffer from SD card */
    if (sdcRead(&SDCD1, 0, inbuf, SDC_BURST_SIZE))
      chSysHalt();
    for (i=0; i<1000; i++){
      if (sdcRead(&SDCD1, 0, outbuf, SDC_BURST_SIZE))
        chSysHalt();
      if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
        chSysHalt();
    }
    chprintf(chp, " OK\r\n");
    chThdSleepMilliseconds(100);


    chprintf(chp, "Multiple unaligned reads...");
    chThdSleepMilliseconds(100);
    fillbuffers(0x55);
    /* fill reference buffer from SD card */
    if (sdcRead(&SDCD1, 0, inbuf + 1, SDC_BURST_SIZE))
      chSysHalt();
    for (i=0; i<1000; i++){
      if (sdcRead(&SDCD1, 0, outbuf + 1, SDC_BURST_SIZE))
        chSysHalt();
      if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
        chSysHalt();
    }
    chprintf(chp, " OK\r\n");
    chThdSleepMilliseconds(100);

#if SDC_DATA_DESTRUCTIVE_TEST

    chprintf(chp, "Single aligned write...");
    chThdSleepMilliseconds(100);
    fillbuffer(0xAA, inbuf);
    if (sdcWrite(&SDCD1, 0, inbuf, 1))
      chSysHalt();
    fillbuffer(0, outbuf);
    if (sdcRead(&SDCD1, 0, outbuf, 1))
      chSysHalt();
    if (memcmp(inbuf, outbuf, MMCSD_BLOCK_SIZE) != 0)
      chSysHalt();
    chprintf(chp, " OK\r\n");

    chprintf(chp, "Single unaligned write...");
    chThdSleepMilliseconds(100);
    fillbuffer(0xFF, inbuf);
    if (sdcWrite(&SDCD1, 0, inbuf+1, 1))
      chSysHalt();
    fillbuffer(0, outbuf);
    if (sdcRead(&SDCD1, 0, outbuf+1, 1))
      chSysHalt();
    if (memcmp(inbuf+1, outbuf+1, MMCSD_BLOCK_SIZE) != 0)
      chSysHalt();
    chprintf(chp, " OK\r\n");

    chprintf(chp, "Running badblocks at 0x10000 offset...");
    chThdSleepMilliseconds(100);
    if(badblocks(0x10000, 0x11000, SDC_BURST_SIZE, 0xAA))
      chSysHalt();
    chprintf(chp, " OK\r\n");
#endif /* !SDC_DATA_DESTRUCTIVE_TEST */


    /**
     * Now perform some FS tests.
     */

    FRESULT err;
    uint32_t clusters;
    FATFS *fsp;
    FIL FileObject;
    uint32_t bytes_written;
    uint32_t bytes_read;
    FILINFO filinfo;
    uint8_t teststring[] = {"This is test file\r\n"};

    chprintf(chp, "Register working area for filesystem... ");
    chThdSleepMilliseconds(100);
    err = f_mount(0, &SDC_FS);
    if (err != FR_OK){
      chSysHalt();
    }
    else{
      fs_ready = TRUE;
      chprintf(chp, "OK\r\n");
    }


#if SDC_DATA_DESTRUCTIVE_TEST
    chprintf(chp, "Formatting... ");
    chThdSleepMilliseconds(100);
    err = f_mkfs (0,0,0);
    if (err != FR_OK){
      chSysHalt();
    }
    else{
      chprintf(chp, "OK\r\n");
    }
#endif /* SDC_DATA_DESTRUCTIVE_TEST */


    chprintf(chp, "Mount filesystem... ");
    chThdSleepMilliseconds(100);
    err = f_getfree("/", &clusters, &fsp);
    if (err != FR_OK) {
      chSysHalt();
    }
    chprintf(chp, "OK\r\n");
    chprintf(chp,
             "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free\r\n",
             clusters, (uint32_t)SDC_FS.csize,
             clusters * (uint32_t)SDC_FS.csize * (uint32_t)MMCSD_BLOCK_SIZE);


    chprintf(chp, "Create file \"chtest.txt\"... ");
    chThdSleepMilliseconds(100);
    err = f_open(&FileObject, "0:chtest.txt", FA_WRITE | FA_OPEN_ALWAYS);
    if (err != FR_OK) {
      chSysHalt();
    }
    chprintf(chp, "OK\r\n");
    chprintf(chp, "Write some data in it... ");
    chThdSleepMilliseconds(100);
    err = f_write(&FileObject, teststring, sizeof(teststring), (void *)&bytes_written);
    if (err != FR_OK) {
      chSysHalt();
    }
    else
      chprintf(chp, "OK\r\n");

    chprintf(chp, "Close file \"chtest.txt\"... ");
    err = f_close(&FileObject);
    if (err != FR_OK) {
      chSysHalt();
    }
    else
      chprintf(chp, "OK\r\n");

    chprintf(chp, "Check file size \"chtest.txt\"... ");
    err = f_stat("0:chtest.txt", &filinfo);
    chThdSleepMilliseconds(100);
    if (err != FR_OK) {
      chSysHalt();
    }
    else{
      if (filinfo.fsize == sizeof(teststring))
        chprintf(chp, "OK\r\n");
      else
        chSysHalt();
    }

    chprintf(chp, "Check file content \"chtest.txt\"... ");
    err = f_open(&FileObject, "0:chtest.txt", FA_READ | FA_OPEN_EXISTING);
    chThdSleepMilliseconds(100);
    if (err != FR_OK) {
      chSysHalt();
    }
    uint8_t buf[sizeof(teststring)];
    err = f_read(&FileObject, buf, sizeof(teststring), (void *)&bytes_read);
    if (err != FR_OK) {
      chSysHalt();
    }
    else{
      if (memcmp(teststring, buf, sizeof(teststring)) != 0){
        chSysHalt();
      }
      else{
        chprintf(chp, "OK\r\n");
      }
    }

    chprintf(chp, "Umount filesystem... ");
    f_mount(0, NULL);
    chprintf(chp, "OK\r\n");

    chprintf(chp, "Disconnecting from SDIO...");
    chThdSleepMilliseconds(100);
    if (sdcDisconnect(&SDCD1))
      chSysHalt();
    chprintf(chp, " OK\r\n");
    chprintf(chp, "------------------------------------------------------\r\n");
    chprintf(chp, "All tests passed successfully.\r\n");
    chThdSleepMilliseconds(100);
  }
  else{
    chSysHalt();
  }
}
Example #7
0
int
casio_qv_read(sdcInfo info, unsigned char *buf, int len) {
    return(sdcRead(info, buf, len));
}
Example #8
0
void cmd_sdiotest(void) {
  uint32_t i = 0;
  FRESULT err = 0;
  bool format = false;


  chThdSleepMilliseconds(100);

  if (!sdioConnect ()) {
    sdioDebug ("   FAIL\r\n ");
    return;
  }

  if (TRUE) {
    sdioDebug ("OK\r\n");
    sdioDebug ("*** Card CSD content is: ");
    sdioDebug ("%X %X %X %X \r\n", (&SDCD1)->csd[3], (&SDCD1)->csd[2],
        (&SDCD1)->csd[1], (&SDCD1)->csd[0]);


    sdioDebug ("capacity = %d sectors of 512 bytes = %d Mo\r\n",
        SDCD1.capacity, SDCD1.capacity / 2048);

    sdioDebug ("Single aligned read...");
    chThdSleepMilliseconds(100);
    if (sdcRead(&SDCD1, 0, inbuf, 1))
      goto error;
    sdioDebug (" OK\r\n");
    chThdSleepMilliseconds(100);


    sdioDebug ("Single unaligned read...");
    chThdSleepMilliseconds(100);
    if (sdcRead(&SDCD1, 0, inbuf + 1, 1))
      goto error;
    if (sdcRead(&SDCD1, 0, inbuf + 2, 1))
      goto error;
    if (sdcRead(&SDCD1, 0, inbuf + 3, 1))
      goto error;
    sdioDebug (" OK\r\n");
    chThdSleepMilliseconds(100);


    sdioDebug ("Multiple aligned reads...");
    chThdSleepMilliseconds(100);
    fillbuffers(0x55);
    /* fill reference buffer from SD card */
    if (sdcRead(&SDCD1, 0, inbuf, SDC_BURST_SIZE))
      goto error;
    for (i=0; i<1000; i++){
      if (sdcRead(&SDCD1, 0, outbuf, SDC_BURST_SIZE))
        goto error;
      if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
        goto error;
    }
    sdioDebug (" OK\r\n");
    chThdSleepMilliseconds(100);


    sdioDebug ("Multiple unaligned reads...");
    chThdSleepMilliseconds(100);
    fillbuffers(0x55);
    /* fill reference buffer from SD card */
    if (sdcRead(&SDCD1, 0, inbuf + 1, SDC_BURST_SIZE))
      goto error;
    for (i=0; i<1000; i++){
      if (sdcRead(&SDCD1, 0, outbuf + 1, SDC_BURST_SIZE))
        goto error;
      if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
        goto error;
    }
    sdioDebug (" OK\r\n");
    chThdSleepMilliseconds(100);

#if SDC_DATA_DESTRUCTIVE_TEST
    if (format) {

      sdioDebug ("Single aligned write...");
      chThdSleepMilliseconds(100);
      fillbuffer(0xAA, inbuf);
      if (sdcWrite(&SDCD1, 0, inbuf, 1))
        goto error;
      fillbuffer(0, outbuf);
      if (sdcRead(&SDCD1, 0, outbuf, 1))
        goto error;
      if (memcmp(inbuf, outbuf, MMCSD_BLOCK_SIZE) != 0)
        goto error;
      sdioDebug (" OK\r\n");

      sdioDebug ("Running badblocks at 0x10000 offset...");
      chThdSleepMilliseconds(100);
      if(badblocks(0x10000, 0x11000, SDC_BURST_SIZE, 0xAA))
        goto error;
      sdioDebug (" OK\r\n");
    } else {

    }
#endif /* !SDC_DATA_DESTRUCTIVE_TEST */


    /**
     * Now perform some FS tests.
     */

    DWORD clusters=0;
    FATFS *fsp=NULL;
    FIL FileObject;
    uint32_t bytes_written=0;
    uint32_t bytes_read=0;
    FILINFO fno ;
#if _USE_LFN
    char lfn[_MAX_LFN + 1];
    fno.lfname = lfn;
    fno.lfsize = sizeof lfn;
#endif

    const uint8_t teststring[] = {"This is test file\r\n"} ;
    /* FS object.*/
    static FATFS SDC_FS;




#if SDC_DATA_DESTRUCTIVE_TEST
    if (format) {
      sdioDebug ("Formatting... ");
      chThdSleepMilliseconds(100);

      sdioDebug ("Register working area for filesystem... ");
      chThdSleepMilliseconds(100);
      err = f_mount(0, &SDC_FS);
      if (err != FR_OK){
        goto error;
      }
      else{
        sdioDebug ("OK\r\n");
      }
    }

    if (format) {
      sdioDebug ("f_mkfs starting ... ");
      chThdSleepMilliseconds(100);
      err = f_mkfs (0,0,0);
      if (err != FR_OK){
        goto error;
      }  else {
        sdioDebug ("OK\r\n");
      }
    }
#endif /* SDC_DATA_DESTRUCTIVE_TEST */


    sdioDebug ("get free space on filesystem... ");
    chThdSleepMilliseconds(100);
    err =  f_getfree(NULL, &clusters, &fsp);

    if (err != FR_OK)
      goto error;

    sdioDebug ("OK\r\n");
    sdioDebug ("FS: %lu free clusters, %lu sectors per cluster, %lu bytes free\r\n",
        clusters, (uint32_t)SDC_FS.csize,
        clusters * (uint32_t)SDC_FS.csize * (uint32_t)MMCSD_BLOCK_SIZE);


    sdioDebug ("Create file \"chtest.txt\"... ");
    chThdSleepMilliseconds(100);
    err = f_open(&FileObject, "chtest.txt", FA_WRITE | FA_OPEN_ALWAYS);
    if (err != FR_OK) {
      goto error;
    }
    sdioDebug ("OK\r\n");
    sdioDebug ("Write some data in it... ");
    chThdSleepMilliseconds(100);
    err = f_write(&FileObject, teststring, sizeof(teststring), (void *)&bytes_written);
    if (err != FR_OK) {
      goto error;
    }
    else
      sdioDebug ("OK\r\n");

    sdioDebug ("Close file \"chtest.txt\"... ");
    err = f_close(&FileObject);
    if (err != FR_OK) {
      goto error;
    }
    else
      sdioDebug ("OK\r\n");


    sdioDebug ("Check file size \"chtest.txt\"... ");
    chThdSleepMilliseconds(10);
    err = f_stat("chtest.txt", &fno);
    chThdSleepMilliseconds(100);
    if (err != FR_OK) {
      goto error;
    } else {
      if (fno.fsize == sizeof(teststring)) {
        sdioDebug ("OK\r\n");
      }
      else
        goto error;
    }


    sdioDebug ("Check file content \"chtest.txt\"... ");
    err = f_open(&FileObject, "chtest.txt", FA_READ | FA_OPEN_EXISTING);
    chThdSleepMilliseconds(100);
    if (err != FR_OK) {
      goto error;
    }
    uint8_t buf[sizeof(teststring)];
    err = f_read(&FileObject, buf, sizeof(teststring), (void *)&bytes_read);
    if (err != FR_OK) {
      goto error;
    } else {
      if (memcmp(teststring, buf, sizeof(teststring)) != 0){
        goto error;
      } else {
        sdioDebug ("OK\r\n");
      }
    }

    {
      FILINFO fno;
      DIR dir;
      //    char *fn;   /* This function is assuming non-Unicode cfg. */
#if _USE_LFN
      char lfn[_MAX_LFN + 1];
      fno.lfname = lfn;
      fno.lfsize = sizeof lfn;
#endif
      const char *path = "";
      FRESULT res =0;

      res = f_opendir(&dir, path);                       /* Open the directory */
      if (res == FR_OK) {
        for (;;) {
          res = f_readdir(&dir, &fno);                   /* Read a directory item */
          if (res != FR_OK || fno.fname[0] == 0) break;  /* Break on error or end of dir */
          if (fno.fname[0] == '.') continue;             /* Ignore dot entry */
#if _USE_LFN
          // fn = fno.lfname;
#else
          // fn = fno.fname;
#endif
          /* It is a file. */
          //chprintf(chp, "readdir %s/%s\r\n", path, fn);
        }
      }
    }

    sdioDebug ("Umount filesystem... ");
    f_mount(0, NULL);
    sdioDebug ("OK\r\n");

    sdioDebug ("Disconnecting from SDIO...");
    chThdSleepMilliseconds(100);
    if (!sdioDisconnect())
      goto error;
    sdioDebug (" OK\r\n");
    sdioDebug ("------------------------------------------------------\r\n");
    sdioDebug ("All tests passed successfully.\r\n");
    chThdSleepMilliseconds(100);


    sdioDisconnect();
    return;
  }
error:
  sdioDebug ("SDC error [%d] occurs\r\n", err);
  sdioDisconnect();
}
Example #9
0
/*
 * Application entry point.
 */
int main(void) {

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

    /*
     * Initializes the SDIO drivers.
     */
    sdcStart(&SDCD1, &sdccfg);
    if (!sdcConnect(&SDCD1)) {
        int i;

        /* Single aligned read.*/
        if (sdcRead(&SDCD1, 0, blkbuf, 1))
            chSysHalt();

        /* Single unaligned read.*/
        if (sdcRead(&SDCD1, 0, blkbuf + 1, 1))
            chSysHalt();

        /* Multiple aligned read.*/
        if (sdcRead(&SDCD1, 0, blkbuf, 4))
            chSysHalt();

        /* Multiple unaligned read.*/
        if (sdcRead(&SDCD1, 0, blkbuf + 1, 4))
            chSysHalt();

        /* Repeated multiple aligned reads.*/
        for (i = 0; i < 1000; i++) {
            if (sdcRead(&SDCD1, 0, blkbuf, 4))
                chSysHalt();
        }

        /* Repeated multiple unaligned reads.*/
        for (i = 0; i < 1000; i++) {
            if (sdcRead(&SDCD1, 0, blkbuf + 1, 4))
                chSysHalt();
        }

        /* Repeated multiple aligned writes.*/
        for (i = 0; i < 100; i++) {
            if (sdcRead(&SDCD1, 0x10000, blkbuf, 4))
                chSysHalt();
            if (sdcWrite(&SDCD1, 0x10000, blkbuf, 4))
                chSysHalt();
            if (sdcWrite(&SDCD1, 0x10000, blkbuf, 4))
                chSysHalt();
        }

        /* Repeated multiple unaligned writes.*/
        for (i = 0; i < 100; i++) {
            if (sdcRead(&SDCD1, 0x10000, blkbuf + 1, 4))
                chSysHalt();
            if (sdcWrite(&SDCD1, 0x10000, blkbuf + 1, 4))
                chSysHalt();
            if (sdcWrite(&SDCD1, 0x10000, blkbuf + 1, 4))
                chSysHalt();
        }

        if (sdcDisconnect(&SDCD1))
            chSysHalt();
    }

    /*
     * Normal main() thread activity.
     */
    while (TRUE) {
        chThdSleepMilliseconds(500);
    }
}