2012-03-06 18:43:12 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2013-03-30 10:32:37 +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
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
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.
|
2012-03-06 18:43:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#include "chprintf.h"
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
#include "ff.h"
|
|
|
|
|
2012-06-01 19:46:22 +00:00
|
|
|
#define SDC_DATA_DESTRUCTIVE_TEST FALSE
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-03-06 21:27:13 +00:00
|
|
|
#define SDC_BURST_SIZE 8 /* how many sectors reads at once */
|
2012-05-23 21:08:51 +00:00
|
|
|
static uint8_t outbuf[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE + 1];
|
|
|
|
static uint8_t inbuf[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE + 1];
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
/* FS object.*/
|
|
|
|
static FATFS SDC_FS;
|
|
|
|
|
|
|
|
/* FS mounted and ready.*/
|
|
|
|
static bool_t fs_ready = FALSE;
|
2012-03-06 18:43:12 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-06 21:27:13 +00:00
|
|
|
* @brief Parody of UNIX badblocks program.
|
2012-03-06 18:43:12 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2012-03-06 21:27:13 +00:00
|
|
|
* @retval SDC_SUCCESS operation succeeded, the requested blocks have been
|
2012-03-06 18:43:12 +00:00
|
|
|
* read.
|
2012-03-06 21:27:13 +00:00
|
|
|
* @retval SDC_FAILED operation failed, the state of the buffer is uncertain.
|
2012-03-06 18:43:12 +00:00
|
|
|
*/
|
|
|
|
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 */
|
2012-05-23 21:08:51 +00:00
|
|
|
for (i=0; i < MMCSD_BLOCK_SIZE * blockatonce; i++)
|
2012-03-06 18:43:12 +00:00
|
|
|
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;
|
2012-05-23 21:08:51 +00:00
|
|
|
if (memcmp(inbuf, outbuf, blockatonce * MMCSD_BLOCK_SIZE) != 0)
|
2012-03-06 18:43:12 +00:00
|
|
|
goto ERROR;
|
|
|
|
position += blockatonce;
|
|
|
|
}
|
2012-04-16 20:12:16 +00:00
|
|
|
return FALSE;
|
2012-03-06 18:43:12 +00:00
|
|
|
|
|
|
|
ERROR:
|
2012-04-16 20:12:16 +00:00
|
|
|
return TRUE;
|
2012-03-06 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 21:27:13 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void fillbuffer(uint8_t pattern, uint8_t *b){
|
2012-03-06 18:43:12 +00:00
|
|
|
uint32_t i = 0;
|
2012-05-23 21:08:51 +00:00
|
|
|
for (i=0; i < MMCSD_BLOCK_SIZE * SDC_BURST_SIZE; i++)
|
2012-03-06 21:27:13 +00:00
|
|
|
b[i] = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void fillbuffers(uint8_t pattern){
|
|
|
|
fillbuffer(pattern, inbuf);
|
|
|
|
fillbuffer(pattern, outbuf);
|
2012-03-06 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2012-03-06 18:43:12 +00:00
|
|
|
*/
|
2012-03-08 19:52:44 +00:00
|
|
|
bool_t sdc_lld_is_write_protected(SDCDriver *sdcp) {
|
|
|
|
(void)sdcp;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-05-23 21:08:51 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool_t sdc_lld_is_card_inserted(SDCDriver *sdcp) {
|
|
|
|
(void)sdcp;
|
|
|
|
return !palReadPad(GPIOE, GPIOE_SDIO_DETECT);
|
|
|
|
}
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2012-03-06 18:43:12 +00:00
|
|
|
*/
|
2012-05-08 17:09:20 +00:00
|
|
|
void cmd_sdiotest(BaseSequentialStream *chp, int argc, char *argv[]){
|
2012-03-08 19:52:44 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2012-03-06 18:43:12 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Trying to connect SDIO... ");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 18:43:12 +00:00
|
|
|
|
|
|
|
if (!sdcConnect(&SDCD1)) {
|
|
|
|
|
2012-06-01 19:46:22 +00:00
|
|
|
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...");
|
2012-03-08 19:52:44 +00:00
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
if (sdcRead(&SDCD1, 0, inbuf, 1))
|
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
|
|
|
|
chprintf(chp, "Single unaligned read...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
if (sdcRead(&SDCD1, 0, inbuf + 1, 1))
|
|
|
|
chSysHalt();
|
|
|
|
if (sdcRead(&SDCD1, 0, inbuf + 2, 1))
|
|
|
|
chSysHalt();
|
|
|
|
if (sdcRead(&SDCD1, 0, inbuf + 3, 1))
|
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Multiple aligned reads...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
fillbuffers(0x55);
|
|
|
|
/* fill reference buffer from SD card */
|
2012-03-06 18:43:12 +00:00
|
|
|
if (sdcRead(&SDCD1, 0, inbuf, SDC_BURST_SIZE))
|
|
|
|
chSysHalt();
|
|
|
|
for (i=0; i<1000; i++){
|
|
|
|
if (sdcRead(&SDCD1, 0, outbuf, SDC_BURST_SIZE))
|
|
|
|
chSysHalt();
|
2012-05-23 21:08:51 +00:00
|
|
|
if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
|
2012-03-06 18:43:12 +00:00
|
|
|
chSysHalt();
|
|
|
|
}
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
|
2012-03-06 18:43:12 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Multiple unaligned reads...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
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();
|
2012-05-23 21:08:51 +00:00
|
|
|
if (memcmp(inbuf, outbuf, SDC_BURST_SIZE * MMCSD_BLOCK_SIZE) != 0)
|
2012-03-06 21:27:13 +00:00
|
|
|
chSysHalt();
|
|
|
|
}
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 18:43:12 +00:00
|
|
|
|
|
|
|
#if SDC_DATA_DESTRUCTIVE_TEST
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Single aligned write...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
fillbuffer(0xAA, inbuf);
|
|
|
|
if (sdcWrite(&SDCD1, 0, inbuf, 1))
|
|
|
|
chSysHalt();
|
|
|
|
fillbuffer(0, outbuf);
|
|
|
|
if (sdcRead(&SDCD1, 0, outbuf, 1))
|
|
|
|
chSysHalt();
|
2012-05-23 21:08:51 +00:00
|
|
|
if (memcmp(inbuf, outbuf, MMCSD_BLOCK_SIZE) != 0)
|
2012-03-06 21:27:13 +00:00
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Single unaligned write...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
fillbuffer(0xFF, inbuf);
|
|
|
|
if (sdcWrite(&SDCD1, 0, inbuf+1, 1))
|
2012-03-06 18:43:12 +00:00
|
|
|
chSysHalt();
|
2012-03-06 21:27:13 +00:00
|
|
|
fillbuffer(0, outbuf);
|
|
|
|
if (sdcRead(&SDCD1, 0, outbuf+1, 1))
|
|
|
|
chSysHalt();
|
2012-05-23 21:08:51 +00:00
|
|
|
if (memcmp(inbuf+1, outbuf+1, MMCSD_BLOCK_SIZE) != 0)
|
2012-03-06 21:27:13 +00:00
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Running badblocks at 0x10000 offset...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 21:27:13 +00:00
|
|
|
if(badblocks(0x10000, 0x11000, SDC_BURST_SIZE, 0xAA))
|
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
2012-03-06 18:43:12 +00:00
|
|
|
#endif /* !SDC_DATA_DESTRUCTIVE_TEST */
|
|
|
|
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
/**
|
2012-09-19 11:53:11 +00:00
|
|
|
* Now perform some FS tests.
|
2012-03-08 19:52:44 +00:00
|
|
|
*/
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
FRESULT err;
|
|
|
|
uint32_t clusters;
|
|
|
|
FATFS *fsp;
|
|
|
|
FIL FileObject;
|
|
|
|
uint32_t bytes_written;
|
2012-04-22 19:36:46 +00:00
|
|
|
uint32_t bytes_read;
|
|
|
|
FILINFO filinfo;
|
|
|
|
uint8_t teststring[] = {"This is test file\r\n"};
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
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 */
|
2012-03-06 21:27:13 +00:00
|
|
|
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
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,
|
2012-05-23 21:08:51 +00:00
|
|
|
clusters * (uint32_t)SDC_FS.csize * (uint32_t)MMCSD_BLOCK_SIZE);
|
2012-03-08 19:52:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
2012-04-22 19:36:46 +00:00
|
|
|
err = f_write(&FileObject, teststring, sizeof(teststring), (void *)&bytes_written);
|
2012-03-08 19:52:44 +00:00
|
|
|
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");
|
|
|
|
|
2012-04-22 19:36:46 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, "Umount filesystem... ");
|
|
|
|
f_mount(0, NULL);
|
|
|
|
chprintf(chp, "OK\r\n");
|
|
|
|
|
|
|
|
chprintf(chp, "Disconnecting from SDIO...");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 18:43:12 +00:00
|
|
|
if (sdcDisconnect(&SDCD1))
|
|
|
|
chSysHalt();
|
2012-03-08 19:52:44 +00:00
|
|
|
chprintf(chp, " OK\r\n");
|
|
|
|
chprintf(chp, "------------------------------------------------------\r\n");
|
|
|
|
chprintf(chp, "All tests passed successfully.\r\n");
|
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 18:43:12 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
chSysHalt();
|
|
|
|
}
|
2012-03-08 19:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SDIO configuration.
|
|
|
|
*/
|
|
|
|
static const SDCConfig sdccfg = {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static SerialConfig ser_cfg = {
|
|
|
|
115200,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
static const ShellCommand commands[] = {
|
|
|
|
{"sdiotest", cmd_sdiotest},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
static const ShellConfig shell_cfg1 = {
|
2012-05-08 17:09:20 +00:00
|
|
|
(BaseSequentialStream *)&SD2,
|
2012-03-08 19:52:44 +00:00
|
|
|
commands
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
|
|
|
/* start debugging serial link */
|
|
|
|
sdStart(&SD2, &ser_cfg);
|
|
|
|
shellInit();
|
|
|
|
static WORKING_AREA(waShell, 2048);
|
|
|
|
shellCreateStatic(&shell_cfg1, waShell, sizeof(waShell), NORMALPRIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes the SDIO drivers.
|
|
|
|
*/
|
|
|
|
sdcStart(&SDCD1, &sdccfg);
|
2012-03-06 21:27:13 +00:00
|
|
|
|
2012-03-06 18:43:12 +00:00
|
|
|
/*
|
|
|
|
* Normal main() thread activity.
|
2012-03-06 21:27:13 +00:00
|
|
|
* Blinking signaling about successful passing.
|
2012-03-06 18:43:12 +00:00
|
|
|
*/
|
|
|
|
while (TRUE) {
|
|
|
|
palTogglePad(GPIOB, GPIOB_LED_R);
|
2012-03-08 19:52:44 +00:00
|
|
|
chThdSleepMilliseconds(100);
|
2012-03-06 18:43:12 +00:00
|
|
|
}
|
|
|
|
}
|