test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include "sys/sys_debug.h"
  32. #include "os_time.h"
  33. //#include "FreeRTOS.h"
  34. #include "os_semaphore.h"
  35. #include "hal_def.h"
  36. #include "hal_ccm.h"
  37. #include "hal_sdhost.h"
  38. #include "sdmmc.h"
  39. #include "sdio.h"
  40. #include "_sd_define.h"
  41. #include "_sdhost.h"
  42. #define TEST_SD
  43. #define TEST_SD_WRITE
  44. #ifdef TEST_SD
  45. #define SIZE_1K (1024)
  46. #define SIZE_1M (SIZE_1K*SIZE_1K)
  47. #define READ_WRITE_SINGLE_SIZE (16*SIZE_1K)
  48. #define READ_WRITE_TOTAL_SIZE (8*SIZE_1M)
  49. struct sdmmc_tester {
  50. struct mmc_card *card;
  51. SDCard_InitTypeDef card_param;
  52. SDC_InitTypeDef sdc_param;
  53. uint16_t sdc_id;
  54. uint16_t card_id;
  55. uint8_t wbuf[READ_WRITE_SINGLE_SIZE];
  56. uint8_t rbuf[READ_WRITE_SINGLE_SIZE];
  57. #ifdef CONFIG_DETECT_CARD
  58. OS_Semaphore_t card_present_sem;
  59. #endif
  60. };
  61. static struct sdmmc_tester *sdmmc_test = NULL;
  62. #ifdef CONFIG_DETECT_CARD
  63. static int detect_status = -1;
  64. void card_detect(uint32_t present)
  65. {
  66. if (present) {
  67. detect_status = 0;
  68. rt_kprintf("%s insert\n", __func__);
  69. if (mmc_card_create(sdmmc_test->card_id, &sdmmc_test->card_param) != 0) {
  70. rt_kprintf("mmc create fail\n");
  71. return ;
  72. }
  73. sdmmc_test->card = mmc_card_open(sdmmc_test->card_id);
  74. if (sdmmc_test->card == NULL) {
  75. rt_kprintf("mmc open fail\n");
  76. return ;
  77. }
  78. /* scan card for detect card is exist? */
  79. if (!mmc_card_present(sdmmc_test->card)) {
  80. // if (mmc_rescan(sdmmc_test->card, sdmmc_test->card->id)) {
  81. sdmmc_test->card->type = MMC_TYPE_SD;
  82. if (mmc_rescan(sdmmc_test->card, sdmmc_test->sdc_id)) {
  83. rt_kprintf("Initial card failed!!\n");
  84. mmc_card_close(sdmmc_test->card_id);
  85. detect_status = -1;
  86. return ;
  87. } else {
  88. rt_kprintf("Initial card success. capacity :%dMB\n", sdmmc_test->card->csd.capacity / 1024);
  89. mmc_card_close(sdmmc_test->card_id);
  90. #ifndef CONFIG_KERNEL_FREERTOS
  91. // void mount_sdmmc_filesystem(int card_id);
  92. // mount_sdmmc_filesystem((int)sdmmc_test->card_id);
  93. #endif
  94. SDC_SemPost(&sdmmc_test->card_present_sem);
  95. }
  96. } else {
  97. rt_kprintf("%s not eixst\n", __func__);
  98. mmc_card_close(sdmmc_test->card_id);
  99. return ;
  100. }
  101. } else {
  102. struct mmc_card *card;
  103. rt_kprintf("%s removed\n", __func__);
  104. card = mmc_card_open(sdmmc_test->card_id);
  105. if (card == NULL) {
  106. rt_kprintf("card open fail\n");
  107. } else {
  108. if (mmc_card_present(card)) {
  109. mmc_card_deinit(card);
  110. }
  111. mmc_card_close(sdmmc_test->card_id);
  112. #ifndef CONFIG_KERNEL_FREERTOS
  113. // void unmount_sdmmc_filesystem(void);
  114. // unmount_sdmmc_filesystem();
  115. #endif
  116. mmc_card_delete(sdmmc_test->card_id);
  117. }
  118. }
  119. }
  120. #endif
  121. //#define WIFI_DEBUG 0
  122. // #define DETECT_BY_GPIO 1
  123. int32_t mmc_test_init(uint32_t host_id, SDC_InitTypeDef *sdc_param, uint32_t scan)
  124. {
  125. struct mmc_host *host;
  126. if (!sdmmc_test) {
  127. sdmmc_test = malloc(sizeof(struct sdmmc_tester));
  128. if (!sdmmc_test) {
  129. rt_kprintf("malloc faild!\n");
  130. return -1;
  131. }
  132. memset(sdmmc_test, 0, sizeof(struct sdmmc_tester));
  133. }
  134. if (sdc_param)
  135. memcpy(&sdmmc_test->sdc_param, sdc_param, sizeof(SDC_InitTypeDef));
  136. #ifdef CONFIG_DETECT_CARD
  137. OS_SemaphoreCreate(&sdmmc_test->card_present_sem, 0, OS_SEMAPHORE_MAX_COUNT);
  138. if (!sdc_param) {
  139. #ifndef DETECT_BY_GPIO
  140. sdmmc_test->sdc_param.cd_mode = CARD_ALWAYS_PRESENT;
  141. #else
  142. sdmmc_test->sdc_param.cd_mode = CARD_DETECT_BY_GPIO_IRQ;
  143. #endif
  144. sdmmc_test->sdc_param.cd_cb = &card_detect;
  145. #ifndef WIFI_DEBUG
  146. sdmmc_test->sdc_param.debug_mask = (ROM_INF_MASK | \
  147. ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK);
  148. #else
  149. sdmmc_test->sdc_param.debug_mask = (ROM_DUMP_MASK | ROM_DBG_MASK | ROM_INF_MASK | \
  150. ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK);
  151. #endif
  152. sdmmc_test->sdc_param.dma_use = 1;
  153. }
  154. #endif
  155. if (!sdc_param)
  156. sdc_param = &sdmmc_test->sdc_param;
  157. #ifdef WIFI_DEBUG
  158. sdmmc_test->card_param.debug_mask = (ROM_DUMP_MASK | ROM_DBG_MASK | ROM_INF_MASK | \
  159. ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK);
  160. #else
  161. sdmmc_test->card_param.debug_mask = (ROM_INF_MASK | \
  162. ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK);
  163. #endif
  164. host = hal_sdc_create(host_id, sdc_param);
  165. host->State = SDC_STATE_RESET;
  166. hal_sdc_init(host);
  167. //HAL_SDC_SetHighSpeed(host);
  168. sdmmc_test->sdc_id = host_id;
  169. sdmmc_test->card_id = host_id;
  170. if (scan && sdmmc_test->sdc_param.cd_mode == CARD_ALWAYS_PRESENT) {
  171. card_detect(1);
  172. return detect_status;
  173. }
  174. return 0;
  175. }
  176. int32_t mmc_test_exit(uint16_t sd_id, uint16_t host_id)
  177. {
  178. struct mmc_card *card;
  179. struct mmc_host *host;
  180. if (!sdmmc_test->card || !sdmmc_test->card->host ||
  181. sdmmc_test->card->id != sd_id || sdmmc_test->sdc_id != host_id) {
  182. if (!sdmmc_test->card || !sdmmc_test->card->host)
  183. rt_kprintf("no card found\n");
  184. else
  185. rt_kprintf("wrong card id:%d<->%d host id:%ld<->%d\n", sd_id, host_id,
  186. HAL_PR_SZ_L(sdmmc_test->card->id), sdmmc_test->sdc_id);
  187. return 0;
  188. }
  189. card = mmc_card_open(sdmmc_test->card_id);
  190. if (card == NULL) {
  191. rt_kprintf("card open fail\n");
  192. } else {
  193. if (mmc_card_present(card)) {
  194. mmc_card_deinit(card);
  195. }
  196. mmc_card_close(sdmmc_test->card_id);
  197. mmc_card_delete(sdmmc_test->card_id);
  198. }
  199. #if 0
  200. host = HAL_SDC_Open(host_id);
  201. HAL_SDC_Close(host_id);
  202. #endif
  203. hal_sdc_deinit(sd_id);
  204. #if 0
  205. HAL_SDC_Destory(host);
  206. #endif
  207. #ifdef CONFIG_DETECT_CARD
  208. OS_SemaphoreDelete(&sdmmc_test->card_present_sem);
  209. #endif
  210. if (sdmmc_test) {
  211. free(sdmmc_test);
  212. sdmmc_test = NULL;
  213. }
  214. return 0;
  215. }
  216. struct mmc_card *mmc_scan_init(uint16_t sd_id, uint16_t sdc_id, SDCard_InitTypeDef *card_param)
  217. {
  218. struct mmc_card *card;
  219. if (!card_param) {
  220. card_param = &sdmmc_test->card_param;
  221. sdmmc_test->card_param.debug_mask = ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK;
  222. }
  223. if (mmc_card_create(sd_id, card_param) != 0) {
  224. rt_kprintf("mmc create fail\n");
  225. return NULL;
  226. }
  227. card = mmc_card_open(sd_id);
  228. if (card == NULL) {
  229. rt_kprintf("mmc open fail\n");
  230. return NULL;
  231. }
  232. if (!mmc_card_present(card)) {
  233. int mmc_ret = mmc_rescan(card, sdc_id);
  234. if (mmc_ret != 0) {
  235. rt_kprintf("mmc scan fail\n");
  236. mmc_card_close(sd_id);
  237. return NULL;
  238. } else {
  239. rt_kprintf("mmc init\n");
  240. }
  241. }
  242. mmc_card_close(sd_id);
  243. sdmmc_test->card = card;
  244. return card;
  245. }
  246. int32_t mmc_test(uint32_t host_id, uint32_t cd_mode, uint32_t sdc_degmask, uint32_t card_dbgmask)
  247. {
  248. int32_t err;
  249. uint32_t i, cnt = 0;
  250. SDC_InitTypeDef sdc_param = { 0 };
  251. sdc_param.cd_mode = cd_mode;
  252. sdc_param.cd_cb = &card_detect;
  253. sdc_param.debug_mask = sdc_degmask;
  254. sdc_param.dma_use = 1;
  255. //if (mmc_test_init(host_id, &sdc_param, 1)) {
  256. if (mmc_test_init(host_id, &sdc_param, 1)) {
  257. return -1;
  258. }
  259. memset((void *)sdmmc_test->wbuf, 0x55, 128);
  260. memset((void *)&sdmmc_test->wbuf[128], 0xaa, 128);
  261. for (i = 0; i < 256; i ++)
  262. sdmmc_test->wbuf[256 + i] = i;
  263. memcpy((void *)&sdmmc_test->wbuf[512], (void *)sdmmc_test->wbuf, 512);
  264. sdmmc_test->card_param.debug_mask = card_dbgmask;
  265. // if (mmc_test_init(host_id, &sdc_param, 1)) {
  266. // return -1;
  267. // }
  268. sdmmc_test->card_param.type = MMC_TYPE_SD;
  269. while (cnt++ < 1) {
  270. uint32_t throuth_mb, throuth_kb;
  271. OS_Time_t tick_use;
  272. rt_kprintf("%s,%d count:%lu\n", __func__, __LINE__, HAL_PR_SZ_L(cnt));
  273. #ifdef CONFIG_DETECT_CARD
  274. if (!mmc_card_present(sdmmc_test->card) || (cd_mode != CARD_ALWAYS_PRESENT))
  275. OS_SemaphoreWait(&sdmmc_test->card_present_sem, OS_WAIT_FOREVER);
  276. #endif
  277. struct mmc_card *card = mmc_card_open(sdmmc_test->card_id);
  278. card->debug_mask = card_dbgmask;
  279. if (card == NULL) {
  280. rt_kprintf("mmc open fail\n");
  281. goto err_out;
  282. }
  283. #ifdef TEST_SD_WRITE
  284. tick_use = OS_GetTicks();
  285. err = mmc_block_write(sdmmc_test->card, sdmmc_test->wbuf, 0, 1);
  286. tick_use = OS_GetTicks() - tick_use;
  287. if (err) {
  288. goto err_out;
  289. } else {
  290. rt_kprintf("%s 1 block write ok, 512B use:%lu ms\n", __func__,
  291. HAL_PR_SZ_L((uint32_t)OS_TicksToMSecs(tick_use)));
  292. }
  293. #endif
  294. memset((void *)sdmmc_test->rbuf, 0, 512);
  295. tick_use = OS_GetTicks();
  296. err = mmc_block_read(sdmmc_test->card, sdmmc_test->rbuf, 0, 1);
  297. tick_use = OS_GetTicks() - tick_use;
  298. if (err) {
  299. goto err_out;
  300. } else {
  301. rt_kprintf("%s 1 block read ok, 512B use:%lu ms\n", __func__,
  302. HAL_PR_SZ_L((uint32_t)OS_TicksToMSecs(tick_use)));
  303. #ifndef TEST_SD_WRITE
  304. print_hex_dump_words(sdmmc_test->rbuf, 512);
  305. #endif
  306. }
  307. #ifdef TEST_SD_WRITE
  308. if (memcmp((void *)sdmmc_test->wbuf, (void *)sdmmc_test->rbuf, 512)) {
  309. goto err_out;
  310. } else
  311. rt_kprintf("%s,%d mmc 1 block rw ok\n", __func__, __LINE__);
  312. tick_use = OS_GetTicks();
  313. for (i = 0; i < READ_WRITE_TOTAL_SIZE/READ_WRITE_SINGLE_SIZE; i++) {
  314. err = mmc_block_write(sdmmc_test->card, sdmmc_test->wbuf, 3 + i * (READ_WRITE_SINGLE_SIZE/512),
  315. READ_WRITE_SINGLE_SIZE/512);
  316. if (err)
  317. break;
  318. if (i % 50 == 0)
  319. rt_kprintf("%s, wirite cnt:%lu\n", __func__, HAL_PR_SZ_L(i));
  320. }
  321. tick_use = OS_GetTicks() - tick_use;
  322. if (err) {
  323. rt_kprintf("%s,%d mmc mult blocks write err!\n", __func__, __LINE__);
  324. goto err_out;
  325. } else {
  326. throuth_kb = READ_WRITE_TOTAL_SIZE/SIZE_1K*1000/(uint32_t)OS_TicksToMSecs(tick_use);
  327. throuth_mb = throuth_kb/1000;
  328. rt_kprintf("%s mult blocks write ok, %d MB use:%lu ms, throughput:%lu.%lu MB/S\n",
  329. __func__, READ_WRITE_TOTAL_SIZE/SIZE_1M, HAL_PR_SZ_L((uint32_t)OS_TicksToMSecs(tick_use)),
  330. HAL_PR_SZ_L(throuth_mb), HAL_PR_SZ_L(throuth_kb - throuth_mb));
  331. }
  332. #endif
  333. tick_use = OS_GetTicks();
  334. for (i = 0; i < READ_WRITE_TOTAL_SIZE/READ_WRITE_SINGLE_SIZE; i++) {
  335. err = mmc_block_read(sdmmc_test->card, sdmmc_test->rbuf, 3 + i * (READ_WRITE_SINGLE_SIZE/512),
  336. READ_WRITE_SINGLE_SIZE/512);
  337. if (err)
  338. break;
  339. }
  340. tick_use = OS_GetTicks() - tick_use;
  341. if (err) {
  342. rt_kprintf("%s,%d mmc mult blocks read err!\n", __func__, __LINE__);
  343. goto err_out;
  344. } else {
  345. throuth_kb = READ_WRITE_TOTAL_SIZE/SIZE_1K*1000/(uint32_t)OS_TicksToMSecs(tick_use);
  346. throuth_mb = throuth_kb/1000;
  347. rt_kprintf("%s mult blocks read ok, %d MB use:%lu ms, throughput:%lu.%lu MB/S\n",
  348. __func__, READ_WRITE_TOTAL_SIZE/SIZE_1M, HAL_PR_SZ_L((uint32_t)OS_TicksToMSecs(tick_use)),
  349. HAL_PR_SZ_L(throuth_mb), HAL_PR_SZ_L(throuth_kb - throuth_mb));
  350. }
  351. memset((void *)sdmmc_test->rbuf, 0, READ_WRITE_SINGLE_SIZE);
  352. err = mmc_block_read(sdmmc_test->card, sdmmc_test->rbuf, 3, READ_WRITE_SINGLE_SIZE/512);
  353. if (err) {
  354. goto err_out;
  355. } else
  356. rt_kprintf("%s %d blocks read ok\n", __func__, READ_WRITE_SINGLE_SIZE/512);
  357. #ifdef TEST_SD_WRITE
  358. if (memcmp((void *)sdmmc_test->wbuf, (void *)sdmmc_test->rbuf, 1024)) { /* check 1024B */
  359. rt_kprintf("%s %d mmc blocks rw failed\n", __func__, READ_WRITE_SINGLE_SIZE/512);
  360. goto err_out;
  361. } else
  362. rt_kprintf("%s %d mmc blocks rw ok\n", __func__, READ_WRITE_SINGLE_SIZE/512);
  363. #endif
  364. mmc_card_close(sdmmc_test->card_id);
  365. OS_MSleep(1000);
  366. }
  367. mmc_test_exit(host_id, sdmmc_test->card_id);
  368. return 0;
  369. err_out:
  370. #ifdef TEST_SD_WRITE
  371. rt_kprintf("%s,%d mmc block rw failed\n", __func__, __LINE__);
  372. rt_kprintf("rbuf:\n");
  373. print_hex_dump_words(sdmmc_test->rbuf, SIZE_1K);
  374. rt_kprintf("wbuf:\n");
  375. print_hex_dump_words(sdmmc_test->wbuf, 512);
  376. #endif
  377. #ifndef CONFIG_DETECT_CARD
  378. out:
  379. #endif
  380. mmc_test_exit(host_id, sdmmc_test->card_id);
  381. return -1;
  382. }
  383. #endif /* TEST_SD */