diff options
Diffstat (limited to 'software/mpu/usb-cdc-shell.c')
-rw-r--r-- | software/mpu/usb-cdc-shell.c | 113 |
1 files 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 <stdio.h> #include <string.h> +#include <stdlib.h> #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<bi.blk_size; i++) { + chprintf(chp, "%02X ", buf[i]); + if(((i+1)%16) == 0) chprintf(chp, "\r\n"); + else if(((i+1)%8) == 0) chprintf(chp, " "); + } + chprintf(chp, "\r\n\r\n"); + + + // Disconnect + if(blkDisconnect(&SDCD1) == CH_FAILED) { + chprintf(chp, "ERROR: blkDisconnect returned whith error\r\n"); + return; + } + chprintf(chp, "disconnecting ..."); + while(blkGetDriverState(&SDCD1) == BLK_DISCONNECTING) { + chThdSleepMilliseconds(100); + chprintf(chp, "."); + } + if(blkGetDriverState(&SDCD1) != BLK_ACTIVE) { + chprintf(chp, "error.\r\nERROR driver not ready (state now: %s)\r\n", blkstate_to_string((blkGetDriverState(&SDCD1)))); + return; + } + chprintf(chp, "done.\r\n"); + } + else { + chprintf(chp, "ERROR: blkConnect returned with error\r\n"); + } return; } chprintf(chp, "."); @@ -208,6 +289,19 @@ static void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) chprintf(chp, " timeout\r\n"); } +static void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) +{ + (void)argv; + if (argc != 1) { + chprintf(chp, "Usage: sdc <sector>\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); |