test_sd.c 24 KB

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