test_sd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include <unistd.h>
  12. #include "sdkconfig.h"
  13. #include "unity.h"
  14. #include "driver/gpio.h"
  15. #include "soc/soc_caps.h"
  16. #if SOC_SDMMC_HOST_SUPPORTED
  17. #include "driver/sdmmc_host.h"
  18. #endif
  19. #include "driver/sdspi_host.h"
  20. #include "driver/sdmmc_defs.h"
  21. #include "sdmmc_cmd.h"
  22. #include "esp_log.h"
  23. #include "esp_heap_caps.h"
  24. #include "esp_rom_gpio.h"
  25. #include "test_utils.h"
  26. #include "soc/gpio_sig_map.h"
  27. #include "soc/gpio_reg.h"
  28. // Currently no runners for S3
  29. #define WITH_SD_TEST (SOC_SDMMC_HOST_SUPPORTED && !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3))
  30. // Currently, no runners for S3
  31. #define WITH_SDSPI_TEST (!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3))
  32. // Can't test eMMC (slot 0) and PSRAM together
  33. #define WITH_EMMC_TEST (SOC_SDMMC_HOST_SUPPORTED && !CONFIG_SPIRAM && !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3))
  34. /* power supply enable pin */
  35. #define SD_TEST_BOARD_VSEL_EN_GPIO 27
  36. /* power supply voltage select pin */
  37. #define SD_TEST_BOARD_VSEL_GPIO 26
  38. #define SD_TEST_BOARD_VSEL_3V3 1
  39. #define SD_TEST_BOARD_VSEL_1V8 0
  40. /* time to wait for reset / power-on */
  41. #define SD_TEST_BOARD_PWR_RST_DELAY_MS 5
  42. #define SD_TEST_BOARD_PWR_ON_DELAY_MS 50
  43. /* gpio which is not connected to actual CD pin, used to simulate CD behavior */
  44. #define CD_WP_TEST_GPIO 18
  45. /* default GPIO selection */
  46. #ifdef CONFIG_IDF_TARGET_ESP32S2
  47. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_35
  48. #define SDSPI_TEST_MISO_PIN GPIO_NUM_37
  49. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_36
  50. #define SDSPI_TEST_CS_PIN GPIO_NUM_34
  51. #elif defined(CONFIG_IDF_TARGET_ESP32C3)
  52. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_4
  53. #define SDSPI_TEST_MISO_PIN GPIO_NUM_6
  54. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_5
  55. #define SDSPI_TEST_CS_PIN GPIO_NUM_1
  56. #else
  57. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_15
  58. #define SDSPI_TEST_MISO_PIN GPIO_NUM_2
  59. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_14
  60. #define SDSPI_TEST_CS_PIN GPIO_NUM_13
  61. #endif
  62. TEST_CASE("MMC_RSP_BITS", "[sd]")
  63. {
  64. uint32_t data[2] = { 0x01234567, 0x89abcdef };
  65. TEST_ASSERT_EQUAL_HEX32(0x7, MMC_RSP_BITS(data, 0, 4));
  66. TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
  67. TEST_ASSERT_EQUAL_HEX32(0xf0, MMC_RSP_BITS(data, 28, 8));
  68. TEST_ASSERT_EQUAL_HEX32(0x3, MMC_RSP_BITS(data, 1, 3));
  69. TEST_ASSERT_EQUAL_HEX32(0x11, MMC_RSP_BITS(data, 59, 5));
  70. }
  71. #if WITH_SD_TEST || WITH_EMMC_TEST
  72. static void sd_test_board_power_on(void)
  73. {
  74. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_OUTPUT);
  75. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, SD_TEST_BOARD_VSEL_3V3);
  76. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_OUTPUT);
  77. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  78. usleep(SD_TEST_BOARD_PWR_RST_DELAY_MS * 1000);
  79. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 1);
  80. usleep(SD_TEST_BOARD_PWR_ON_DELAY_MS * 1000);
  81. }
  82. static void sd_test_board_power_off(void)
  83. {
  84. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  85. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_INPUT);
  86. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, 0);
  87. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_INPUT);
  88. }
  89. static void probe_sd(int slot, int width, int freq_khz, int ddr)
  90. {
  91. sd_test_board_power_on();
  92. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  93. config.slot = slot;
  94. config.max_freq_khz = freq_khz;
  95. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  96. if (width == 1) {
  97. config.flags = SDMMC_HOST_FLAG_1BIT;
  98. slot_config.width = 1;
  99. } else if (width == 4) {
  100. config.flags &= ~SDMMC_HOST_FLAG_8BIT;
  101. slot_config.width = 4;
  102. } else {
  103. assert(!ddr && "host driver does not support 8-line DDR mode yet");
  104. }
  105. if (!ddr) {
  106. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  107. }
  108. TEST_ESP_OK(sdmmc_host_init());
  109. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  110. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  111. TEST_ASSERT_NOT_NULL(card);
  112. TEST_ESP_OK(sdmmc_card_init(&config, card));
  113. sdmmc_card_print_info(stdout, card);
  114. uint8_t* buffer = heap_caps_malloc(512, MALLOC_CAP_DMA);
  115. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 0, 1));
  116. free(buffer);
  117. TEST_ESP_OK(sdmmc_host_deinit());
  118. free(card);
  119. sd_test_board_power_off();
  120. }
  121. #endif //WITH_SD_TEST || WITH_EMMC_TEST
  122. #if WITH_SD_TEST
  123. TEST_CASE("probe SD, slot 1, 4-bit", "[sd][test_env=UT_T1_SDMODE]")
  124. {
  125. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_PROBING, 0);
  126. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_DEFAULT, 0);
  127. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_HIGHSPEED, 0);
  128. }
  129. TEST_CASE("probe SD, slot 1, 1-bit", "[sd][test_env=UT_T1_SDMODE]")
  130. {
  131. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_PROBING, 0);
  132. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_DEFAULT, 0);
  133. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_HIGHSPEED, 0);
  134. }
  135. //No runners for slot 0
  136. TEST_CASE("probe SD, slot 0, 4-bit", "[sd][ignore]")
  137. {
  138. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_PROBING, 0);
  139. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_DEFAULT, 0);
  140. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 0);
  141. }
  142. TEST_CASE("probe SD, slot 0, 1-bit", "[sd][ignore]")
  143. {
  144. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_PROBING, 0);
  145. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_DEFAULT, 0);
  146. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_HIGHSPEED, 0);
  147. }
  148. #endif //WITH_SD_TEST
  149. #if WITH_EMMC_TEST
  150. TEST_CASE("probe eMMC, slot 0, 4-bit", "[sd][test_env=EMMC]")
  151. {
  152. //Test with SDR
  153. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_PROBING, 0);
  154. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_DEFAULT, 0);
  155. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 0);
  156. //Test with DDR
  157. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 1);
  158. }
  159. TEST_CASE("probe eMMC, slot 0, 8-bit", "[sd][test_env=EMMC]")
  160. {
  161. //8-bit DDR not supported yet, test with SDR only
  162. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_PROBING, 0);
  163. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_DEFAULT, 0);
  164. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_HIGHSPEED, 0);
  165. }
  166. #endif // WITH_EMMC_TEST
  167. #if WITH_SDSPI_TEST
  168. #if !WITH_SD_TEST && !WITH_EMMC_TEST
  169. static void sd_test_board_power_on(void)
  170. {
  171. // do nothing
  172. }
  173. static void sd_test_board_power_off(void)
  174. {
  175. // do nothing
  176. }
  177. #endif
  178. static void test_sdspi_init_bus(spi_host_device_t host, int mosi_pin, int miso_pin, int clk_pin, int dma_chan)
  179. {
  180. spi_bus_config_t bus_config = {
  181. .mosi_io_num = mosi_pin,
  182. .miso_io_num = miso_pin,
  183. .sclk_io_num = clk_pin,
  184. .quadwp_io_num = -1,
  185. .quadhd_io_num = -1,
  186. };
  187. esp_err_t err = spi_bus_initialize(host, &bus_config, dma_chan);
  188. TEST_ESP_OK(err);
  189. }
  190. static void test_sdspi_deinit_bus(spi_host_device_t host)
  191. {
  192. esp_err_t err = spi_bus_free(host);
  193. TEST_ESP_OK(err);
  194. }
  195. static void probe_core(int slot)
  196. {
  197. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  198. config.slot = slot;
  199. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  200. TEST_ASSERT_NOT_NULL(card);
  201. TEST_ESP_OK(sdmmc_card_init(&config, card));
  202. sdmmc_card_print_info(stdout, card);
  203. free(card);
  204. }
  205. static void probe_spi(int freq_khz, int pin_miso, int pin_mosi, int pin_sck, int pin_cs)
  206. {
  207. sd_test_board_power_on();
  208. sdspi_dev_handle_t handle;
  209. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  210. dev_config.gpio_cs = pin_cs;
  211. test_sdspi_init_bus(dev_config.host_id, pin_mosi, pin_miso, pin_sck, SPI_DMA_CH_AUTO);
  212. TEST_ESP_OK(sdspi_host_init());
  213. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  214. probe_core(handle);
  215. TEST_ESP_OK(sdspi_host_deinit());
  216. test_sdspi_deinit_bus(dev_config.host_id);
  217. sd_test_board_power_off();
  218. }
  219. static void probe_spi_legacy(int freq_khz, int pin_miso, int pin_mosi, int pin_sck, int pin_cs)
  220. {
  221. sd_test_board_power_on();
  222. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  223. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  224. slot_config.gpio_miso = pin_miso;
  225. slot_config.gpio_mosi = pin_mosi;
  226. slot_config.gpio_sck = pin_sck;
  227. slot_config.gpio_cs = pin_cs;
  228. slot_config.dma_channel = SPI_DMA_CH_AUTO;
  229. TEST_ESP_OK(sdspi_host_init());
  230. TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
  231. probe_core(config.slot);
  232. TEST_ESP_OK(sdspi_host_deinit());
  233. TEST_ESP_OK(spi_bus_free(config.slot));
  234. sd_test_board_power_off();
  235. }
  236. TEST_CASE("probe SD in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  237. {
  238. probe_spi(SDMMC_FREQ_DEFAULT, SDSPI_TEST_MISO_PIN, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_SCLK_PIN, SDSPI_TEST_CS_PIN);
  239. probe_spi_legacy(SDMMC_FREQ_DEFAULT, SDSPI_TEST_MISO_PIN, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_SCLK_PIN, SDSPI_TEST_CS_PIN);
  240. }
  241. // No runner for this
  242. TEST_CASE("probe SD in SPI mode, slot 0", "[sd][ignore]")
  243. {
  244. probe_spi(SDMMC_FREQ_DEFAULT, 7, 11, 6, 10);
  245. probe_spi_legacy(SDMMC_FREQ_DEFAULT, 7, 11, 6, 10);
  246. }
  247. #endif //WITH_SDSPI_TEST
  248. #if WITH_SD_TEST || WITH_SDSPI_TEST || WITH_EMMC_TEST
  249. // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
  250. // from 'rand' with the starting value of 'seed'
  251. static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
  252. srand(seed);
  253. for (size_t i = 0; i < count; ++i) {
  254. uint32_t val = rand();
  255. memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
  256. }
  257. }
  258. // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
  259. // ints generated from 'rand' with the starting value of 'seed'
  260. static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
  261. srand(seed);
  262. for (size_t i = 0; i < count; ++i) {
  263. uint32_t val;
  264. memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
  265. TEST_ASSERT_EQUAL_HEX32(rand(), val);
  266. }
  267. }
  268. static void do_single_write_read_test(sdmmc_card_t* card, size_t start_block,
  269. size_t block_count, size_t alignment, bool performance_log)
  270. {
  271. size_t block_size = card->csd.sector_size;
  272. size_t total_size = block_size * block_count;
  273. printf(" %8d | %3d | %d | %4.1f ", start_block, block_count, alignment, total_size / 1024.0f);
  274. uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
  275. size_t offset = alignment % 4;
  276. uint8_t* c_buffer = (uint8_t*) buffer + offset;
  277. fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  278. struct timeval t_start_wr;
  279. gettimeofday(&t_start_wr, NULL);
  280. TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
  281. struct timeval t_stop_wr;
  282. gettimeofday(&t_stop_wr, NULL);
  283. float time_wr = 1e3f * (t_stop_wr.tv_sec - t_start_wr.tv_sec) + 1e-3f * (t_stop_wr.tv_usec - t_start_wr.tv_usec);
  284. memset(buffer, 0xbb, total_size + 4);
  285. struct timeval t_start_rd;
  286. gettimeofday(&t_start_rd, NULL);
  287. TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
  288. struct timeval t_stop_rd;
  289. gettimeofday(&t_stop_rd, NULL);
  290. float time_rd = 1e3f * (t_stop_rd.tv_sec - t_start_rd.tv_sec) + 1e-3f * (t_stop_rd.tv_usec - t_start_rd.tv_usec);
  291. printf(" | %6.2f | %5.2f | %6.2f | %5.2f\n",
  292. time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
  293. time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
  294. check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  295. free(buffer);
  296. if (performance_log) {
  297. static const char wr_speed_str[] = "SDMMC_WR_SPEED";
  298. static const char rd_speed_str[] = "SDMMC_RD_SPEED";
  299. int aligned = ((alignment % 4) == 0)? 1: 0;
  300. IDF_LOG_PERFORMANCE(wr_speed_str, "%d, blk_n: %d, aligned: %d",
  301. (int)(total_size * 1000 / time_wr), block_count, aligned);
  302. IDF_LOG_PERFORMANCE(rd_speed_str, "%d, blk_n: %d, aligned: %d",
  303. (int)(total_size * 1000 / time_rd), block_count, aligned);
  304. }
  305. }
  306. typedef void (*sd_test_func_t)(sdmmc_card_t* card);
  307. static void test_read_write_performance(sdmmc_card_t* card)
  308. {
  309. sdmmc_card_print_info(stdout, card);
  310. printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  311. const int offset = 0;
  312. const bool do_log = true;
  313. //aligned
  314. do_single_write_read_test(card, offset, 1, 4, do_log);
  315. do_single_write_read_test(card, offset, 4, 4, do_log);
  316. do_single_write_read_test(card, offset, 8, 4, do_log);
  317. do_single_write_read_test(card, offset, 16, 4, do_log);
  318. do_single_write_read_test(card, offset, 32, 4, do_log);
  319. do_single_write_read_test(card, offset, 64, 4, do_log);
  320. do_single_write_read_test(card, offset, 128, 4, do_log);
  321. //unaligned
  322. do_single_write_read_test(card, offset, 1, 1, do_log);
  323. do_single_write_read_test(card, offset, 8, 1, do_log);
  324. do_single_write_read_test(card, offset, 128, 1, do_log);
  325. }
  326. static void test_read_write_with_offset(sdmmc_card_t* card)
  327. {
  328. sdmmc_card_print_info(stdout, card);
  329. printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  330. const bool no_log = false;;
  331. //aligned
  332. do_single_write_read_test(card, 1, 16, 4, no_log);
  333. do_single_write_read_test(card, 16, 32, 4, no_log);
  334. do_single_write_read_test(card, 48, 64, 4, no_log);
  335. do_single_write_read_test(card, 128, 128, 4, no_log);
  336. do_single_write_read_test(card, card->csd.capacity - 64, 32, 4, no_log);
  337. do_single_write_read_test(card, card->csd.capacity - 64, 64, 4, no_log);
  338. do_single_write_read_test(card, card->csd.capacity - 8, 1, 4, no_log);
  339. do_single_write_read_test(card, card->csd.capacity/2, 1, 4, no_log);
  340. do_single_write_read_test(card, card->csd.capacity/2, 4, 4, no_log);
  341. do_single_write_read_test(card, card->csd.capacity/2, 8, 4, no_log);
  342. do_single_write_read_test(card, card->csd.capacity/2, 16, 4, no_log);
  343. do_single_write_read_test(card, card->csd.capacity/2, 32, 4, no_log);
  344. do_single_write_read_test(card, card->csd.capacity/2, 64, 4, no_log);
  345. do_single_write_read_test(card, card->csd.capacity/2, 128, 4, no_log);
  346. //unaligned
  347. do_single_write_read_test(card, card->csd.capacity/2, 1, 1, no_log);
  348. do_single_write_read_test(card, card->csd.capacity/2, 8, 1, no_log);
  349. do_single_write_read_test(card, card->csd.capacity/2, 128, 1, no_log);
  350. }
  351. #endif //WITH_SD_TEST || WITH_SDSPI_TEST || WITH_EMMC_TEST
  352. #if WITH_SD_TEST || WITH_EMMC_TEST
  353. void sd_test_rw_blocks(int slot, int width, sd_test_func_t test_func)
  354. {
  355. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  356. config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  357. config.slot = slot;
  358. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  359. if (width != 0) {
  360. slot_config.width = width;
  361. }
  362. if (slot_config.width == 8) {
  363. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  364. }
  365. TEST_ESP_OK(sdmmc_host_init());
  366. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  367. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  368. TEST_ASSERT_NOT_NULL(card);
  369. TEST_ESP_OK(sdmmc_card_init(&config, card));
  370. test_func(card);
  371. free(card);
  372. TEST_ESP_OK(sdmmc_host_deinit());
  373. }
  374. #endif //WITH_SD_TEST || WITH_EMMC_TEST
  375. #if WITH_SD_TEST
  376. TEST_CASE("SDMMC performance test (SD slot 1, 4 line)", "[sd][test_env=UT_T1_SDMODE]")
  377. {
  378. sd_test_board_power_on();
  379. sd_test_rw_blocks(1, 4, test_read_write_performance);
  380. sd_test_board_power_off();
  381. }
  382. TEST_CASE("SDMMC performance test (SD slot 1, 1 line)", "[sd][test_env=UT_T1_SDMODE]")
  383. {
  384. sd_test_board_power_on();
  385. sd_test_rw_blocks(1, 1, test_read_write_performance);
  386. sd_test_board_power_off();
  387. }
  388. TEST_CASE("SDMMC test read/write with offset (SD slot 1)", "[sd][test_env=UT_T1_SDMODE]")
  389. {
  390. sd_test_board_power_on();
  391. sd_test_rw_blocks(1, 4, test_read_write_with_offset);
  392. sd_test_board_power_off();
  393. }
  394. #endif //WITH_SD_TEST
  395. #if WITH_EMMC_TEST
  396. TEST_CASE("SDMMC performance test (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC]")
  397. {
  398. sd_test_board_power_on();
  399. sd_test_rw_blocks(0, 4, test_read_write_performance);
  400. sd_test_board_power_off();
  401. }
  402. TEST_CASE("SDMMC test read/write with offset (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC]")
  403. {
  404. sd_test_board_power_on();
  405. sd_test_rw_blocks(0, 4, test_read_write_with_offset);
  406. sd_test_board_power_off();
  407. }
  408. TEST_CASE("SDMMC performance test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC]")
  409. {
  410. sd_test_board_power_on();
  411. sd_test_rw_blocks(0, 8, test_read_write_performance);
  412. sd_test_board_power_off();
  413. }
  414. TEST_CASE("SDMMC test read/write with offset (eMMC slot 0, 8 line)", "[sd][test_env=EMMC]")
  415. {
  416. sd_test_board_power_on();
  417. sd_test_rw_blocks(0, 8, test_read_write_with_offset);
  418. sd_test_board_power_off();
  419. }
  420. #endif // WITH_EMMC_TEST
  421. #if WITH_SDSPI_TEST
  422. void sdspi_test_rw_blocks(sd_test_func_t test_func)
  423. {
  424. sd_test_board_power_on();
  425. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  426. sdspi_dev_handle_t handle;
  427. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  428. dev_config.host_id = config.slot;
  429. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  430. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  431. TEST_ESP_OK(sdspi_host_init());
  432. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  433. // This test can only run under 20MHz on ESP32, because the runner connects the card to
  434. // non-IOMUX pins of HSPI.
  435. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  436. TEST_ASSERT_NOT_NULL(card);
  437. TEST_ESP_OK(sdmmc_card_init(&config, card));
  438. test_func(card);
  439. TEST_ESP_OK(sdspi_host_deinit());
  440. free(card);
  441. test_sdspi_deinit_bus(dev_config.host_id);
  442. sd_test_board_power_off();
  443. }
  444. TEST_CASE("SDMMC performance (SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  445. {
  446. sdspi_test_rw_blocks(test_read_write_performance);
  447. }
  448. TEST_CASE("SDMMC test read/write with offset (SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  449. {
  450. sdspi_test_rw_blocks(test_read_write_with_offset);
  451. }
  452. #endif //WITH_SDSPI_TEST
  453. #if WITH_SD_TEST
  454. TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE]")
  455. {
  456. sd_test_board_power_on();
  457. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  458. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  459. TEST_ESP_OK(sdmmc_host_init());
  460. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  461. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  462. TEST_ASSERT_NOT_NULL(card);
  463. TEST_ESP_OK(sdmmc_card_init(&config, card));
  464. const size_t buffer_size = 4096;
  465. const size_t block_count = buffer_size / 512;
  466. const size_t extra = 4;
  467. uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
  468. // Check read behavior: do aligned write, then unaligned read
  469. const uint32_t seed = 0x89abcdef;
  470. fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  471. TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
  472. memset(buffer, 0xcc, buffer_size + extra);
  473. TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
  474. check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  475. // Check write behavior: do unaligned write, then aligned read
  476. fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  477. TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
  478. memset(buffer, 0xcc, buffer_size + extra);
  479. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
  480. check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  481. free(buffer);
  482. free(card);
  483. TEST_ESP_OK(sdmmc_host_deinit());
  484. sd_test_board_power_off();
  485. }
  486. #endif //WITH_SD_TEST
  487. #if WITH_SD_TEST || WITH_SDSPI_TEST
  488. static void test_cd_input(int gpio_cd_num, const sdmmc_host_t* config)
  489. {
  490. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  491. TEST_ASSERT_NOT_NULL(card);
  492. // SDMMC host should have configured CD as input.
  493. // Enable output as well (not using the driver, to avoid touching input
  494. // enable bits).
  495. esp_rom_gpio_connect_out_signal(gpio_cd_num, SIG_GPIO_OUT_IDX, false, false);
  496. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_cd_num));
  497. // Check that card initialization fails if CD is high
  498. REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_cd_num));
  499. usleep(1000);
  500. TEST_ESP_ERR(ESP_ERR_NOT_FOUND, sdmmc_card_init(config, card));
  501. // Check that card initialization succeeds if CD is low
  502. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_cd_num));
  503. usleep(1000);
  504. TEST_ESP_OK(sdmmc_card_init(config, card));
  505. free(card);
  506. }
  507. static void test_wp_input(int gpio_wp_num, const sdmmc_host_t* config)
  508. {
  509. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  510. TEST_ASSERT_NOT_NULL(card);
  511. // SDMMC host should have configured WP as input.
  512. // Enable output as well (not using the driver, to avoid touching input
  513. // enable bits).
  514. esp_rom_gpio_connect_out_signal(gpio_wp_num, SIG_GPIO_OUT_IDX, false, false);
  515. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_wp_num));
  516. // Check that the card can be initialized with WP low
  517. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
  518. TEST_ESP_OK(sdmmc_card_init(config, card));
  519. uint32_t* data = heap_caps_calloc(1, 512, MALLOC_CAP_DMA);
  520. // Check that card write succeeds if WP is high
  521. REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_wp_num));
  522. usleep(1000);
  523. TEST_ESP_OK(sdmmc_write_sectors(card, &data, 0, 1));
  524. // Check that write fails if WP is low
  525. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
  526. usleep(1000);
  527. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_write_sectors(card, &data, 0, 1));
  528. // ...but reads still work
  529. TEST_ESP_OK(sdmmc_read_sectors(card, &data, 0, 1));
  530. free(data);
  531. free(card);
  532. }
  533. #endif //WITH_SD_TEST || WITH_SDSPI_TEST
  534. #if WITH_SD_TEST
  535. TEST_CASE("CD input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  536. {
  537. sd_test_board_power_on();
  538. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  539. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  540. slot_config.gpio_cd = CD_WP_TEST_GPIO;
  541. TEST_ESP_OK(sdmmc_host_init());
  542. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  543. test_cd_input(CD_WP_TEST_GPIO, &config);
  544. TEST_ESP_OK(sdmmc_host_deinit());
  545. sd_test_board_power_off();
  546. }
  547. TEST_CASE("WP input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  548. {
  549. sd_test_board_power_on();
  550. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  551. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  552. slot_config.gpio_wp = CD_WP_TEST_GPIO;
  553. TEST_ESP_OK(sdmmc_host_init());
  554. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  555. test_wp_input(CD_WP_TEST_GPIO, &config);
  556. TEST_ESP_OK(sdmmc_host_deinit());
  557. sd_test_board_power_off();
  558. }
  559. #endif //WITH_SD_TEST
  560. #if WITH_SDSPI_TEST
  561. TEST_CASE("CD input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  562. {
  563. sd_test_board_power_on();
  564. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  565. sdspi_dev_handle_t handle;
  566. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  567. dev_config.host_id = config.slot;
  568. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  569. dev_config.gpio_cd = CD_WP_TEST_GPIO;
  570. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  571. TEST_ESP_OK(sdspi_host_init());
  572. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  573. config.slot = handle;
  574. test_cd_input(CD_WP_TEST_GPIO, &config);
  575. TEST_ESP_OK(sdspi_host_deinit());
  576. test_sdspi_deinit_bus(dev_config.host_id);
  577. sd_test_board_power_off();
  578. }
  579. TEST_CASE("WP input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  580. {
  581. sd_test_board_power_on();
  582. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  583. sdspi_dev_handle_t handle;
  584. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  585. dev_config.host_id = config.slot;
  586. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  587. dev_config.gpio_wp = CD_WP_TEST_GPIO;
  588. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  589. TEST_ESP_OK(sdspi_host_init());
  590. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  591. config.slot = handle;
  592. test_wp_input(CD_WP_TEST_GPIO, &config);
  593. TEST_ESP_OK(sdspi_host_deinit());
  594. test_sdspi_deinit_bus(dev_config.host_id);
  595. sd_test_board_power_off();
  596. }
  597. #endif //WITH_SDSPI_TEST