main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <string.h>
  14. #include "ch.h"
  15. #include "hal.h"
  16. #include "chprintf.h"
  17. #include "shell.h"
  18. /*
  19. * Working area for driver.
  20. */
  21. static uint8_t sd_scratchpad[512];
  22. /*
  23. * SDIO configuration.
  24. */
  25. static const SDCConfig sdccfg = {
  26. sd_scratchpad,
  27. SDC_MODE_4BIT
  28. };
  29. /*
  30. * LED blinker thread, times are in milliseconds.
  31. */
  32. static THD_WORKING_AREA(waThread1, 128);
  33. static THD_FUNCTION(Thread1, arg) {
  34. (void)arg;
  35. chRegSetThreadName("blinker");
  36. while (true) {
  37. palSetPad(GPIOC, GPIOC_LED);
  38. chThdSleepMilliseconds(500);
  39. palClearPad(GPIOC, GPIOC_LED);
  40. chThdSleepMilliseconds(500);
  41. }
  42. }
  43. /*===========================================================================*/
  44. /* Command line related. */
  45. /*===========================================================================*/
  46. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  47. #define SDC_BURST_SIZE 16
  48. /* Buffer for block read/write operations, note that extra bytes are
  49. allocated in order to support unaligned operations.*/
  50. static uint8_t buf[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE + 4];
  51. /* Additional buffer for sdcErase() test */
  52. static uint8_t buf2[MMCSD_BLOCK_SIZE * SDC_BURST_SIZE ];
  53. void cmd_sdc(BaseSequentialStream *chp, int argc, char *argv[]) {
  54. static const char *mode[] = {"SDV11", "SDV20", "MMC", NULL};
  55. systime_t start, end;
  56. uint32_t n, startblk;
  57. if (argc != 1) {
  58. chprintf(chp, "Usage: sdiotest read|write|erase|all\r\n");
  59. return;
  60. }
  61. /* Card presence check.*/
  62. if (!blkIsInserted(&SDCD1)) {
  63. chprintf(chp, "Card not inserted, aborting.\r\n");
  64. return;
  65. }
  66. /* Connection to the card.*/
  67. chprintf(chp, "Connecting... ");
  68. if (sdcConnect(&SDCD1)) {
  69. chprintf(chp, "failed\r\n");
  70. return;
  71. }
  72. chprintf(chp, "OK\r\n\r\nCard Info\r\n");
  73. chprintf(chp, "CSD : %08X %8X %08X %08X \r\n",
  74. SDCD1.csd[3], SDCD1.csd[2], SDCD1.csd[1], SDCD1.csd[0]);
  75. chprintf(chp, "CID : %08X %8X %08X %08X \r\n",
  76. SDCD1.cid[3], SDCD1.cid[2], SDCD1.cid[1], SDCD1.cid[0]);
  77. chprintf(chp, "Mode : %s\r\n", mode[SDCD1.cardmode & 3U]);
  78. chprintf(chp, "Capacity : %DMB\r\n", SDCD1.capacity / 2048);
  79. /* The test is performed in the middle of the flash area.*/
  80. startblk = (SDCD1.capacity / MMCSD_BLOCK_SIZE) / 2;
  81. if ((strcmp(argv[0], "read") == 0) ||
  82. (strcmp(argv[0], "all") == 0)) {
  83. /* Single block read performance, aligned.*/
  84. chprintf(chp, "Single block aligned read performance: ");
  85. start = chVTGetSystemTime();
  86. end = chTimeAddX(start, TIME_MS2I(1000));
  87. n = 0;
  88. do {
  89. if (blkRead(&SDCD1, startblk, buf, 1)) {
  90. chprintf(chp, "failed\r\n");
  91. goto exittest;
  92. }
  93. n++;
  94. } while (chVTIsSystemTimeWithin(start, end));
  95. chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);
  96. /* Multiple sequential blocks read performance, aligned.*/
  97. chprintf(chp, "16 sequential blocks aligned read performance: ");
  98. start = chVTGetSystemTime();
  99. end = chTimeAddX(start, TIME_MS2I(1000));
  100. n = 0;
  101. do {
  102. if (blkRead(&SDCD1, startblk, buf, SDC_BURST_SIZE)) {
  103. chprintf(chp, "failed\r\n");
  104. goto exittest;
  105. }
  106. n += SDC_BURST_SIZE;
  107. } while (chVTIsSystemTimeWithin(start, end));
  108. chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);
  109. #if STM32_SDC_SDIO_UNALIGNED_SUPPORT
  110. /* Single block read performance, unaligned.*/
  111. chprintf(chp, "Single block unaligned read performance: ");
  112. start = chVTGetSystemTime();
  113. end = chTimeAddX(start, TIME_MS2I(1000));
  114. n = 0;
  115. do {
  116. if (blkRead(&SDCD1, startblk, buf + 1, 1)) {
  117. chprintf(chp, "failed\r\n");
  118. goto exittest;
  119. }
  120. n++;
  121. } while (chVTIsSystemTimeWithin(start, end));
  122. chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);
  123. /* Multiple sequential blocks read performance, unaligned.*/
  124. chprintf(chp, "16 sequential blocks unaligned read performance: ");
  125. start = chVTGetSystemTime();
  126. end = chTimeAddX(start, TIME_MS2I(1000));
  127. n = 0;
  128. do {
  129. if (blkRead(&SDCD1, startblk, buf + 1, SDC_BURST_SIZE)) {
  130. chprintf(chp, "failed\r\n");
  131. goto exittest;
  132. }
  133. n += SDC_BURST_SIZE;
  134. } while (chVTIsSystemTimeWithin(start, end));
  135. chprintf(chp, "%D blocks/S, %D bytes/S\r\n", n, n * MMCSD_BLOCK_SIZE);
  136. #endif /* STM32_SDC_SDIO_UNALIGNED_SUPPORT */
  137. }
  138. if ((strcmp(argv[0], "write") == 0) ||
  139. (strcmp(argv[0], "all") == 0)) {
  140. unsigned i;
  141. memset(buf, 0xAA, MMCSD_BLOCK_SIZE * 2);
  142. chprintf(chp, "Writing...");
  143. if(sdcWrite(&SDCD1, startblk, buf, 2)) {
  144. chprintf(chp, "failed\r\n");
  145. goto exittest;
  146. }
  147. chprintf(chp, "OK\r\n");
  148. memset(buf, 0x55, MMCSD_BLOCK_SIZE * 2);
  149. chprintf(chp, "Reading...");
  150. if (blkRead(&SDCD1, startblk, buf, 1)) {
  151. chprintf(chp, "failed\r\n");
  152. goto exittest;
  153. }
  154. chprintf(chp, "OK\r\n");
  155. for (i = 0; i < MMCSD_BLOCK_SIZE; i++)
  156. buf[i] = i + 8;
  157. chprintf(chp, "Writing...");
  158. if(sdcWrite(&SDCD1, startblk, buf, 2)) {
  159. chprintf(chp, "failed\r\n");
  160. goto exittest;
  161. }
  162. chprintf(chp, "OK\r\n");
  163. memset(buf, 0, MMCSD_BLOCK_SIZE * 2);
  164. chprintf(chp, "Reading...");
  165. if (blkRead(&SDCD1, startblk, buf, 1)) {
  166. chprintf(chp, "failed\r\n");
  167. goto exittest;
  168. }
  169. chprintf(chp, "OK\r\n");
  170. }
  171. if ((strcmp(argv[0], "erase") == 0) ||
  172. (strcmp(argv[0], "all") == 0)) {
  173. /**
  174. * Test sdcErase()
  175. * Strategy:
  176. * 1. Fill two blocks with non-constant data
  177. * 2. Write two blocks starting at startblk
  178. * 3. Erase the second of the two blocks
  179. * 3.1. First block should be equal to the data written
  180. * 3.2. Second block should NOT be equal too the data written (i.e. erased).
  181. * 4. Erase both first and second block
  182. * 4.1 Both blocks should not be equal to the data initially written
  183. * Precondition: SDC_BURST_SIZE >= 2
  184. */
  185. memset(buf, 0, MMCSD_BLOCK_SIZE * 2);
  186. memset(buf2, 0, MMCSD_BLOCK_SIZE * 2);
  187. /* 1. */
  188. unsigned int i = 0;
  189. for (; i < MMCSD_BLOCK_SIZE * 2; ++i) {
  190. buf[i] = (i + 7) % 'T'; //Ensure block 1/2 are not equal
  191. }
  192. /* 2. */
  193. if(sdcWrite(&SDCD1, startblk, buf, 2)) {
  194. chprintf(chp, "sdcErase() test write failed\r\n");
  195. goto exittest;
  196. }
  197. /* 3. (erase) */
  198. if(sdcErase(&SDCD1, startblk + 1, startblk + 2)) {
  199. chprintf(chp, "sdcErase() failed\r\n");
  200. goto exittest;
  201. }
  202. sdcflags_t errflags = sdcGetAndClearErrors(&SDCD1);
  203. if(errflags) {
  204. chprintf(chp, "sdcErase() yielded error flags: %d\r\n", errflags);
  205. goto exittest;
  206. }
  207. if(sdcRead(&SDCD1, startblk, buf2, 2)) {
  208. chprintf(chp, "single-block sdcErase() failed\r\n");
  209. goto exittest;
  210. }
  211. /* 3.1. */
  212. if(memcmp(buf, buf2, MMCSD_BLOCK_SIZE) != 0) {
  213. chprintf(chp, "sdcErase() non-erased block compare failed\r\n");
  214. goto exittest;
  215. }
  216. /* 3.2. */
  217. if(memcmp(buf + MMCSD_BLOCK_SIZE,
  218. buf2 + MMCSD_BLOCK_SIZE, MMCSD_BLOCK_SIZE) == 0) {
  219. chprintf(chp, "sdcErase() erased block compare failed\r\n");
  220. goto exittest;
  221. }
  222. /* 4. */
  223. if(sdcErase(&SDCD1, startblk, startblk + 2)) {
  224. chprintf(chp, "multi-block sdcErase() failed\r\n");
  225. goto exittest;
  226. }
  227. if(sdcRead(&SDCD1, startblk, buf2, 2)) {
  228. chprintf(chp, "single-block sdcErase() failed\r\n");
  229. goto exittest;
  230. }
  231. /* 4.1 */
  232. if(memcmp(buf, buf2, MMCSD_BLOCK_SIZE) == 0) {
  233. chprintf(chp, "multi-block sdcErase() erased block compare failed\r\n");
  234. goto exittest;
  235. }
  236. if(memcmp(buf + MMCSD_BLOCK_SIZE,
  237. buf2 + MMCSD_BLOCK_SIZE, MMCSD_BLOCK_SIZE) == 0) {
  238. chprintf(chp, "multi-block sdcErase() erased block compare failed\r\n");
  239. goto exittest;
  240. }
  241. /* END of sdcErase() test */
  242. }
  243. /* Card disconnect and command end.*/
  244. exittest:
  245. sdcDisconnect(&SDCD1);
  246. }
  247. static const ShellCommand commands[] = {
  248. {"sdc", cmd_sdc},
  249. {NULL, NULL}
  250. };
  251. static const ShellConfig shell_cfg1 = {
  252. (BaseSequentialStream *)&SD6,
  253. commands
  254. };
  255. /*
  256. * Application entry point.
  257. */
  258. int main(void) {
  259. /*
  260. * System initializations.
  261. * - HAL initialization, this also initializes the configured device drivers
  262. * and performs the board-specific initializations.
  263. * - Kernel initialization, the main() function becomes a thread and the
  264. * RTOS is active.
  265. */
  266. halInit();
  267. chSysInit();
  268. /*
  269. * Shell manager initialization.
  270. */
  271. shellInit();
  272. /*
  273. * Activates the serial driver 6 using the driver default configuration.
  274. */
  275. sdStart(&SD6, NULL);
  276. /*
  277. * Initializes the SDIO drivers.
  278. */
  279. sdcStart(&SDCD1, &sdccfg);
  280. /*
  281. * Creates the blinker thread.
  282. */
  283. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  284. /*
  285. * Normal main() thread activity, spawning shells.
  286. */
  287. while (true) {
  288. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  289. "shell", NORMALPRIO + 1,
  290. shellThread, (void *)&shell_cfg1);
  291. chThdWait(shelltp); /* Waiting termination. */
  292. chThdSleepMilliseconds(1000);
  293. }
  294. }