From cfd126515a458c3e73104ba95466cf5ff220a67a Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 9 Apr 2014 23:39:29 +0200 Subject: first working sdc read test --- software/mpu/usb-cdc-shell.c | 113 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 13 deletions(-) diff --git a/software/mpu/usb-cdc-shell.c b/software/mpu/usb-cdc-shell.c index 145c941..00276aa 100644 --- a/software/mpu/usb-cdc-shell.c +++ b/software/mpu/usb-cdc-shell.c @@ -50,6 +50,7 @@ #include #include +#include #include "ch.h" #include "hal.h" @@ -184,21 +185,101 @@ static const SDCConfig sdccfg = { 0 }; -static void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) +static char* blkstate_to_string(blkstate_t state) { - (void)argv; - if (argc > 0) { - chprintf(chp, "Usage: sdc\r\n"); - return; + switch(state) { + case BLK_UNINIT: return "Not initialized."; + case BLK_STOP: return "Stopped."; + case BLK_ACTIVE: return "Interface active."; + case BLK_CONNECTING: return "Connection in progress."; + case BLK_DISCONNECTING: return "Disconnection in progress."; + case BLK_READY: return "Device ready."; + case BLK_READING: return "Read operation in progress."; + case BLK_WRITING: return "Write operation in progress."; + case BLK_SYNCING: return "Sync. operation in progress. "; } + return "unkown"; +} + +static void sdc_read_test(BaseSequentialStream *chp, const uint32_t sector) +{ chprintf(chp, "Running SDCard Test\r\n"); + chprintf(chp, "-------------------\r\n\r\n"); unsigned int n; chprintf(chp, "waiting for card ."); for(n = 0; n < 20; n++) { if(blkIsInserted(&SDCD1)) { chprintf(chp, " found\r\n"); - chprintf(chp, " write proteciton: %s\r\n", blkIsWriteProtected(&SDCD1) ? "ON" : "OFF"); + chprintf(chp, "write protection: %s\r\n", blkIsWriteProtected(&SDCD1) ? "ON" : "OFF"); + + // Connect + if(blkConnect(&SDCD1) == CH_SUCCESS) { + chprintf(chp, "connecting ..."); + while(blkGetDriverState(&SDCD1) == BLK_CONNECTING) { + chThdSleepMilliseconds(100); + chprintf(chp, "."); + } + if(blkGetDriverState(&SDCD1) != BLK_READY) { + chprintf(chp, "error.\r\nERROR driver not ready (state now: %s)\r\n", blkstate_to_string((blkGetDriverState(&SDCD1)))); + return; + } + chprintf(chp, "done.\r\n"); + + BlockDeviceInfo bi; + if(blkGetInfo(&SDCD1, &bi) == CH_FAILED) { + chprintf(chp, "ERROR: reading device info\r\n"); + return; + } + chprintf(chp, "Device: blocksize=%d, blocknum=%d\r\n", bi.blk_size, bi.blk_num); + + if(bi.blk_num <= sector) { + chprintf(chp, "Requested sector (%d) is after end of device\r\n", sector); + return; + } + + // Read + chprintf(chp, "\r\nReading Block %ld: \r\n", sector); + + uint8_t buf[1024]; + if(sizeof(buf) < bi.blk_size) { + chprintf(chp, "Your read buffer ist too small - aborting...\r\n"); + return; + } + memset(buf, 0, sizeof(buf)); + if(blkRead(&SDCD1, sector, buf, 1) == CH_FAILED) { + chprintf(chp, "ERROR: blkRead returned with error\r\n"); + return; + } + + uint32_t i; + for(i=0; i\r\n"); + return; + } + + sdcStart(&SDCD1, &sdccfg); + sdc_read_test(chp, strtoul(argv[0], NULL, 10)); + sdcStop(&SDCD1); +} + static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) { size_t n, size; @@ -358,10 +452,6 @@ int8_t usbCDCShellRun(void) shellInit(); - - sdcStart(&SDCD1, &sdccfg); - - shell_return_code = MPU_CONTINUE; Thread *shelltp = NULL; while (TRUE) { @@ -375,9 +465,6 @@ int8_t usbCDCShellRun(void) chThdSleepMilliseconds(1000); } - sdcStop(&SDCD1); - - usbDisconnect(serusbcfg.usbp); usbStop(serusbcfg.usbp); sduStop(&SDU1); -- cgit v1.2.3