123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- #include <string.h>
- #include "ch.h"
- #include "hal.h"
- #include "chprintf.h"
- #include "shell.h"
- static uint8_t sd_scratchpad[512];
- static const SDCConfig sdccfg = {
- sd_scratchpad,
- SDC_MODE_4BIT
- };
- static THD_WORKING_AREA(waThread1, 128);
- static THD_FUNCTION(Thread1, arg) {
- (void)arg;
- chRegSetThreadName("blinker");
- while (true) {
- palSetPad(GPIOC, GPIOC_LED);
- chThdSleepMilliseconds(500);
- palClearPad(GPIOC, GPIOC_LED);
- chThdSleepMilliseconds(500);
- }
- }
- #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
- #define SDC_BURST_SIZE 16
- static uint8_t buf[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE + 4];
- static uint8_t buf2[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE ];
- 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;
- }
-
- if (!blkIsInserted(&SDCD1)) {
- chprintf(chp, "Card not inserted, aborting.\r\n");
- return;
- }
-
- 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);
-
- startblk = (SDCD1.capacity / MMCSD_BLOCK_SIZE) / 2;
- if ((strcmp(argv[0], "read") == 0) ||
- (strcmp(argv[0], "all") == 0)) {
-
- chprintf(chp, "Single block aligned read performance: ");
- start = chVTGetSystemTime();
- end = chTimeAddX(start, TIME_MS2I(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);
-
- chprintf(chp, "16 sequential blocks aligned read performance: ");
- start = chVTGetSystemTime();
- end = chTimeAddX(start, TIME_MS2I(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
-
- chprintf(chp, "Single block unaligned read performance: ");
- start = chVTGetSystemTime();
- end = chTimeAddX(start, TIME_MS2I(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);
-
- chprintf(chp, "16 sequential blocks unaligned read performance: ");
- start = chVTGetSystemTime();
- end = chTimeAddX(start, TIME_MS2I(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
- }
- 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)) {
-
- memset(buf, 0, MMCSD_BLOCK_SIZE * 2);
- memset(buf2, 0, MMCSD_BLOCK_SIZE * 2);
-
- unsigned int i = 0;
- for (; i < MMCSD_BLOCK_SIZE * 2; ++i) {
- buf[i] = (i + 7) % 'T';
- }
-
- if(sdcWrite(&SDCD1, startblk, buf, 2)) {
- chprintf(chp, "sdcErase() test write failed\r\n");
- goto exittest;
- }
-
- 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;
- }
-
- if(memcmp(buf, buf2, MMCSD_BLOCK_SIZE) != 0) {
- chprintf(chp, "sdcErase() non-erased block compare failed\r\n");
- goto exittest;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- }
-
-
- exittest:
- sdcDisconnect(&SDCD1);
- }
- static const ShellCommand commands[] = {
- {"sdc", cmd_sdc},
- {NULL, NULL}
- };
- static const ShellConfig shell_cfg1 = {
- (BaseSequentialStream *)&SD6,
- commands
- };
- int main(void) {
-
- halInit();
- chSysInit();
-
- shellInit();
-
- sdStart(&SD6, NULL);
-
- sdcStart(&SDCD1, &sdccfg);
-
- chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
-
- while (true) {
- thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
- "shell", NORMALPRIO + 1,
- shellThread, (void *)&shell_cfg1);
- chThdWait(shelltp);
- chThdSleepMilliseconds(1000);
- }
- }
|