test_sdmmc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 程序清单:SD/MMC卡 设备使用例程
  12. * 例程导出了 sample_sdmmc 命令到控制终端
  13. * 命令调用格式:sdmmc_sample
  14. * 程序功能:对整个SD/MMC卡进行写和读操作,比较数据是否一致
  15. *
  16. * 注意: 修改函数SystemClock_Config下面参数,
  17. * stcPLLHInit.PLLCFGR_f.PLLN = 120UL - 1UL;
  18. * 改为
  19. * stcPLLHInit.PLLCFGR_f.PLLN = 100UL - 1UL;
  20. */
  21. #include <stdlib.h>
  22. #include <rtthread.h>
  23. #include <rtdevice.h>
  24. #include <board.h>
  25. #if defined(BSP_USING_SDIO)
  26. #define SDMMC_DEVICE_NAME "sd"
  27. #define SDMMC_SECTOR_SIZE 512UL
  28. #define SDMMC_TEST_SECTORS_PER_TIME 100UL
  29. #define SDMMC_TEST_TIME 10UL
  30. #define SDMMC_TEST_SECTORS (SDMMC_TEST_TIME * SDMMC_TEST_SECTORS_PER_TIME)
  31. #define SDMMC_TEST_BUF_SIZE (SDMMC_SECTOR_SIZE * SDMMC_TEST_SECTORS_PER_TIME)
  32. static void sdmmc_thread_entry(void *parameter)
  33. {
  34. rt_ssize_t size;
  35. rt_uint32_t err_count = 0;
  36. rt_device_t sd_device;
  37. rt_uint32_t sector_start;
  38. rt_uint32_t sector_end;;
  39. rt_uint32_t sector_cur_start;
  40. rt_uint32_t sector_cur_end;
  41. rt_uint8_t *sector_rbuf;
  42. rt_uint8_t *sector_wbuf;
  43. sd_device = rt_device_find(SDMMC_DEVICE_NAME);
  44. if (sd_device == RT_NULL)
  45. {
  46. rt_kprintf("no nand device found!\n");
  47. return;
  48. }
  49. if (rt_device_open(sd_device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  50. {
  51. rt_kprintf("fail to open!\n");
  52. return;
  53. }
  54. sector_rbuf = rt_malloc(SDMMC_TEST_BUF_SIZE);
  55. if (sector_rbuf == RT_NULL)
  56. {
  57. rt_kprintf("out of memory!");
  58. return;
  59. }
  60. sector_wbuf = rt_malloc(SDMMC_TEST_BUF_SIZE);
  61. if (sector_wbuf == RT_NULL)
  62. {
  63. rt_free(sector_rbuf);
  64. rt_kprintf("out of memory!");
  65. return;
  66. }
  67. sector_start = ((rt_uint32_t)rand() & 0x00000FFFUL);
  68. sector_end = (sector_start + SDMMC_TEST_SECTORS - 1);
  69. rt_kprintf("sector=[%d, %d]: ......test start...... !\r\n", sector_start, sector_end);
  70. for (sector_cur_start = sector_start; sector_cur_start <= sector_end; sector_cur_start += SDMMC_TEST_SECTORS_PER_TIME)
  71. {
  72. sector_cur_end = sector_cur_start + SDMMC_TEST_SECTORS_PER_TIME - 1UL;
  73. /* initialize buffer data */
  74. rt_memset(sector_rbuf, 0, SDMMC_TEST_BUF_SIZE);
  75. rt_memset(sector_wbuf, (rt_uint8_t)rand(), SDMMC_TEST_BUF_SIZE);
  76. /* write sdmmc */
  77. size = rt_device_write(sd_device, sector_cur_start, sector_wbuf, SDMMC_TEST_SECTORS_PER_TIME);
  78. if (size == SDMMC_TEST_SECTORS_PER_TIME)
  79. {
  80. rt_kprintf("sector=[%d, %d]: ok wr !\r\n", sector_cur_start, sector_cur_end);
  81. }
  82. else
  83. {
  84. err_count++;
  85. rt_kprintf("sector=[%d, %d]: error wr !\r\n", sector_cur_start, sector_cur_end);
  86. continue;
  87. }
  88. /* read sdmmc */
  89. size = rt_device_read(sd_device, sector_cur_start, sector_rbuf, SDMMC_TEST_SECTORS_PER_TIME);
  90. if (size == SDMMC_TEST_SECTORS_PER_TIME)
  91. {
  92. rt_kprintf("sector=[%d, %d]: ok rd !\r\n", sector_cur_start, sector_cur_end);
  93. }
  94. else
  95. {
  96. err_count++;
  97. rt_kprintf("sector=[%d, %d]: error rd !\r\n", sector_cur_start, sector_cur_end);
  98. continue;
  99. }
  100. /* compare data */
  101. if (rt_memcmp(sector_wbuf, sector_rbuf, SDMMC_TEST_BUF_SIZE) == 0)
  102. {
  103. rt_kprintf("sector=[%d, %d]: ok cmp !\r\n", sector_cur_start, sector_cur_end);
  104. }
  105. else
  106. {
  107. err_count++;
  108. rt_kprintf("sector=[%d, %d]: error cmp !\r\n", sector_cur_start, sector_cur_end);
  109. }
  110. }
  111. if (rt_device_close(sd_device) != RT_EOK)
  112. {
  113. rt_kprintf("fail to close!\n");
  114. }
  115. rt_free(sector_rbuf);
  116. rt_free(sector_wbuf);
  117. if (err_count == 0)
  118. {
  119. rt_kprintf("sector=[%d, %d]: ...... test ok...... !\r\n\r\n", sector_start, sector_end);
  120. }
  121. else
  122. {
  123. rt_kprintf("sector=[%d, %d]: ...... test error...... !\r\n\r\n", sector_start, sector_end);
  124. }
  125. }
  126. static void sdmmc_sample(int argc, char *argv[])
  127. {
  128. rt_thread_t thread = rt_thread_create("sdmmc", sdmmc_thread_entry, RT_NULL, 2048, 15, 10);
  129. if (thread != RT_NULL)
  130. {
  131. rt_thread_startup(thread);
  132. }
  133. }
  134. MSH_CMD_EXPORT(sdmmc_sample, sdmmc sample);
  135. #endif