2013-08-23 08:11:27 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2013-08-23 08:11:27 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-08-23 08:11:27 +00:00
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#include "chprintf.h"
|
2013-09-22 08:44:11 +00:00
|
|
|
#include "shell.h"
|
|
|
|
|
2015-02-19 17:42:12 +00:00
|
|
|
/*
|
|
|
|
* Working area for driver.
|
|
|
|
*/
|
|
|
|
static uint8_t sd_scratchpad[512];
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
/*
|
|
|
|
* SDIO configuration.
|
|
|
|
*/
|
|
|
|
static const SDCConfig sdccfg = {
|
2015-02-23 14:23:15 +00:00
|
|
|
sd_scratchpad,
|
|
|
|
SDC_MODE_4BIT
|
2013-09-22 08:44:11 +00:00
|
|
|
};
|
2013-08-23 08:11:27 +00:00
|
|
|
|
2013-09-21 10:06:54 +00:00
|
|
|
/*
|
|
|
|
* LED blinker thread, times are in milliseconds.
|
2013-08-23 08:11:27 +00:00
|
|
|
*/
|
2013-09-21 10:06:54 +00:00
|
|
|
static THD_WORKING_AREA(waThread1, 128);
|
2015-04-03 12:48:22 +00:00
|
|
|
static THD_FUNCTION(Thread1, arg) {
|
2013-08-23 08:11:27 +00:00
|
|
|
|
2013-09-21 10:06:54 +00:00
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("blinker");
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2013-09-21 10:06:54 +00:00
|
|
|
palSetPad(GPIOC, GPIOC_LED);
|
|
|
|
chThdSleepMilliseconds(500);
|
|
|
|
palClearPad(GPIOC, GPIOC_LED);
|
|
|
|
chThdSleepMilliseconds(500);
|
2013-08-23 08:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Command line related. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-02-19 17:42:12 +00:00
|
|
|
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
|
2013-09-22 08:44:11 +00:00
|
|
|
|
2013-09-22 09:08:10 +00:00
|
|
|
#define SDC_BURST_SIZE 16
|
2013-09-22 08:44:11 +00:00
|
|
|
|
2015-05-11 14:48:35 +00:00
|
|
|
/* Buffer for block read/write operations, note that extra bytes are
|
2013-09-22 09:08:10 +00:00
|
|
|
allocated in order to support unaligned operations.*/
|
2015-05-11 14:48:35 +00:00
|
|
|
static uint8_t buf[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE + 4];
|
|
|
|
|
2015-03-30 17:27:41 +00:00
|
|
|
/* Additional buffer for sdcErase() test */
|
|
|
|
static uint8_t buf2[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE ];
|
2013-09-22 09:08:10 +00:00
|
|
|
|
|
|
|
void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) {
|
|
|
|
static const char *mode[] = {"SDV11", "SDV20", "MMC", NULL};
|
2013-09-23 19:03:43 +00:00
|
|
|
systime_t start, end;
|
|
|
|
uint32_t n, startblk;
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
if (argc != 1) {
|
2015-03-30 17:27:41 +00:00
|
|
|
chprintf(chp, "Usage: sdiotest read|write|erase|all\r\n");
|
2013-09-22 08:44:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Card presence check.*/
|
|
|
|
if (!blkIsInserted(&SDCD1)) {
|
|
|
|
chprintf(chp, "Card not inserted, aborting.\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connection to the card.*/
|
2013-09-22 09:08:10 +00:00
|
|
|
chprintf(chp, "Connecting... ");
|
2013-09-22 08:44:11 +00:00
|
|
|
if (sdcConnect(&SDCD1)) {
|
|
|
|
chprintf(chp, "failed\r\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 17:42:12 +00:00
|
|
|
|
2013-09-22 09:08:10 +00:00
|
|
|
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]);
|
2015-05-11 14:48:35 +00:00
|
|
|
chprintf(chp, "Mode : %s\r\n", mode[SDCD1.cardmode & 3U]);
|
2013-09-22 09:08:10 +00:00
|
|
|
chprintf(chp, "Capacity : %DMB\r\n", SDCD1.capacity / 2048);
|
2013-09-22 08:44:11 +00:00
|
|
|
|
2013-09-23 19:03:43 +00:00
|
|
|
/* The test is performed in the middle of the flash area.*/
|
|
|
|
startblk = (SDCD1.capacity / MMCSD_BLOCK_SIZE) / 2;
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
if ((strcmp(argv[0], "read") == 0) ||
|
|
|
|
(strcmp(argv[0], "all") == 0)) {
|
|
|
|
|
2013-09-26 13:02:22 +00:00
|
|
|
/* Single block read performance, aligned.*/
|
|
|
|
chprintf(chp, "Single block aligned read performance: ");
|
2013-09-23 19:03:43 +00:00
|
|
|
start = chVTGetSystemTime();
|
|
|
|
end = start + MS2ST(1000);
|
|
|
|
n = 0;
|
|
|
|
do {
|
2013-09-26 12:46:43 +00:00
|
|
|
if (blkRead(&SDCD1, startblk, buf, 1)) {
|
2013-09-23 19:03:43 +00:00
|
|
|
chprintf(chp, "failed\r\n");
|
2013-09-26 13:02:22 +00:00
|
|
|
goto exittest;
|
2013-09-23 19:03:43 +00:00
|
|
|
}
|
|
|
|
n++;
|
|
|
|
} while (chVTIsSystemTimeWithin(start, end));
|
2013-09-26 13:02:22 +00:00
|
|
|
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 */
|
2013-09-22 08:44:11 +00:00
|
|
|
}
|
2013-09-26 13:02:22 +00:00
|
|
|
|
2015-05-11 14:48:35 +00:00
|
|
|
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");
|
|
|
|
}
|
2015-03-30 17:27:41 +00:00
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2013-09-22 09:08:10 +00:00
|
|
|
/* Card disconnect and command end.*/
|
2013-09-26 13:02:22 +00:00
|
|
|
exittest:
|
2013-09-22 09:08:10 +00:00
|
|
|
sdcDisconnect(&SDCD1);
|
2013-09-22 08:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const ShellCommand commands[] = {
|
2013-09-22 09:08:10 +00:00
|
|
|
{"sdc", cmd_sdc},
|
2013-09-22 08:44:11 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const ShellConfig shell_cfg1 = {
|
|
|
|
(BaseSequentialStream *)&SD6,
|
|
|
|
commands
|
|
|
|
};
|
|
|
|
|
2013-08-23 08:11:27 +00:00
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
2013-09-21 10:06:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-08-23 08:11:27 +00:00
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
2013-09-22 08:44:11 +00:00
|
|
|
/*
|
|
|
|
* Shell manager initialization.
|
|
|
|
*/
|
|
|
|
shellInit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activates the serial driver 6 using the driver default configuration.
|
|
|
|
*/
|
|
|
|
sdStart(&SD6, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes the SDIO drivers.
|
|
|
|
*/
|
|
|
|
sdcStart(&SDCD1, &sdccfg);
|
|
|
|
|
2013-08-23 08:11:27 +00:00
|
|
|
/*
|
2013-09-21 10:06:54 +00:00
|
|
|
* Creates the blinker thread.
|
2013-08-23 08:11:27 +00:00
|
|
|
*/
|
2013-09-21 10:06:54 +00:00
|
|
|
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
2013-08-23 08:11:27 +00:00
|
|
|
|
|
|
|
/*
|
2016-02-20 21:50:58 +00:00
|
|
|
* Normal main() thread activity, spawning shells.
|
2013-08-23 08:11:27 +00:00
|
|
|
*/
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2016-02-20 21:50:58 +00:00
|
|
|
thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
|
2016-02-21 10:45:42 +00:00
|
|
|
"shell", NORMALPRIO + 1,
|
|
|
|
shellThread, (void *)&shell_cfg1);
|
2016-02-20 21:50:58 +00:00
|
|
|
chThdWait(shelltp); /* Waiting termination. */
|
2013-09-22 08:44:11 +00:00
|
|
|
chThdSleepMilliseconds(1000);
|
2013-08-23 08:11:27 +00:00
|
|
|
}
|
|
|
|
}
|