test_sd.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 "freertos/FreeRTOS.h"
  27. #include "freertos/task.h"
  28. #include "soc/gpio_sig_map.h"
  29. #include "soc/gpio_reg.h"
  30. // Currently no runners for S3
  31. #define WITH_SD_TEST (SOC_SDMMC_HOST_SUPPORTED && !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3))
  32. // Currently, no runners for S3, C2, and C6
  33. #define WITH_SDSPI_TEST (!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3, ESP32C2, ESP32C6, ESP32H2))
  34. // Can't test eMMC (slot 0) and PSRAM together
  35. #define WITH_EMMC_TEST (SOC_SDMMC_HOST_SUPPORTED && !CONFIG_SPIRAM && !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3))
  36. /* power supply enable pin */
  37. #define SD_TEST_BOARD_VSEL_EN_GPIO 27
  38. /* power supply voltage select pin */
  39. #define SD_TEST_BOARD_VSEL_GPIO 26
  40. #define SD_TEST_BOARD_VSEL_3V3 1
  41. #define SD_TEST_BOARD_VSEL_1V8 0
  42. /* time to wait for reset / power-on */
  43. #define SD_TEST_BOARD_PWR_RST_DELAY_MS 5
  44. #define SD_TEST_BOARD_PWR_ON_DELAY_MS 50
  45. /* gpio which is not connected to actual CD pin, used to simulate CD behavior */
  46. #define CD_WP_TEST_GPIO 18
  47. /* default GPIO selection */
  48. #ifdef CONFIG_IDF_TARGET_ESP32S2
  49. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_35
  50. #define SDSPI_TEST_MISO_PIN GPIO_NUM_37
  51. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_36
  52. #define SDSPI_TEST_CS_PIN GPIO_NUM_34
  53. #elif defined(CONFIG_IDF_TARGET_ESP32C3)
  54. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_4
  55. #define SDSPI_TEST_MISO_PIN GPIO_NUM_6
  56. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_5
  57. #define SDSPI_TEST_CS_PIN GPIO_NUM_1
  58. #else
  59. #define SDSPI_TEST_MOSI_PIN GPIO_NUM_15
  60. #define SDSPI_TEST_MISO_PIN GPIO_NUM_2
  61. #define SDSPI_TEST_SCLK_PIN GPIO_NUM_14
  62. #define SDSPI_TEST_CS_PIN GPIO_NUM_13
  63. #endif
  64. #define GPIO_ACTIVE_LOW 0
  65. #define GPIO_ACTIVE_HIGH 1
  66. TEST_CASE("MMC_RSP_BITS", "[sd]")
  67. {
  68. uint32_t data[2] = { 0x01234567, 0x89abcdef };
  69. TEST_ASSERT_EQUAL_HEX32(0x7, MMC_RSP_BITS(data, 0, 4));
  70. TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
  71. TEST_ASSERT_EQUAL_HEX32(0xf0, MMC_RSP_BITS(data, 28, 8));
  72. TEST_ASSERT_EQUAL_HEX32(0x3, MMC_RSP_BITS(data, 1, 3));
  73. TEST_ASSERT_EQUAL_HEX32(0x11, MMC_RSP_BITS(data, 59, 5));
  74. }
  75. #if WITH_SD_TEST || WITH_EMMC_TEST
  76. static void sd_test_board_power_on(void)
  77. {
  78. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_OUTPUT);
  79. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, SD_TEST_BOARD_VSEL_3V3);
  80. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_OUTPUT);
  81. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  82. usleep(SD_TEST_BOARD_PWR_RST_DELAY_MS * 1000);
  83. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 1);
  84. usleep(SD_TEST_BOARD_PWR_ON_DELAY_MS * 1000);
  85. }
  86. static void sd_test_board_power_off(void)
  87. {
  88. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  89. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_INPUT);
  90. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, 0);
  91. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_INPUT);
  92. }
  93. static void probe_sd(int slot, int width, int freq_khz, int ddr)
  94. {
  95. sd_test_board_power_on();
  96. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  97. config.slot = slot;
  98. config.max_freq_khz = freq_khz;
  99. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  100. if (width == 1) {
  101. config.flags = SDMMC_HOST_FLAG_1BIT;
  102. slot_config.width = 1;
  103. } else if (width == 4) {
  104. config.flags &= ~SDMMC_HOST_FLAG_8BIT;
  105. slot_config.width = 4;
  106. } else {
  107. assert(!ddr && "host driver does not support 8-line DDR mode yet");
  108. }
  109. if (!ddr) {
  110. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  111. }
  112. TEST_ESP_OK(sdmmc_host_init());
  113. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  114. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  115. TEST_ASSERT_NOT_NULL(card);
  116. TEST_ESP_OK(sdmmc_card_init(&config, card));
  117. sdmmc_card_print_info(stdout, card);
  118. uint8_t* buffer = heap_caps_malloc(512, MALLOC_CAP_DMA);
  119. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 0, 1));
  120. free(buffer);
  121. TEST_ESP_OK(sdmmc_host_deinit());
  122. free(card);
  123. sd_test_board_power_off();
  124. }
  125. extern void sdmmc_host_get_clk_dividers(const int freq_khz, int *host_div, int *card_div);
  126. static void sd_test_check_clk_dividers(const int freq_khz, const int expected_host_div, const int expected_card_div)
  127. {
  128. printf(" %6d | %2d | %2d\n", freq_khz, expected_host_div, expected_card_div);
  129. int host_divider, card_divider;
  130. sdmmc_host_get_clk_dividers(freq_khz, &host_divider, &card_divider);
  131. TEST_ASSERT_EQUAL(host_divider, expected_host_div);
  132. TEST_ASSERT_EQUAL(card_divider, expected_card_div);
  133. }
  134. #endif //WITH_SD_TEST || WITH_EMMC_TEST
  135. #if WITH_SD_TEST
  136. TEST_CASE("probe SD, slot 1, 4-bit", "[sd][test_env=UT_T1_SDMODE]")
  137. {
  138. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_PROBING, 0);
  139. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_DEFAULT, 0);
  140. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_HIGHSPEED, 0);
  141. //custom frequency test
  142. probe_sd(SDMMC_HOST_SLOT_1, 4, 10000, 0);
  143. }
  144. TEST_CASE("probe SD, slot 1, 1-bit", "[sd][test_env=UT_T1_SDMODE]")
  145. {
  146. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_PROBING, 0);
  147. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_DEFAULT, 0);
  148. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_HIGHSPEED, 0);
  149. }
  150. //No runners for slot 0
  151. TEST_CASE("probe SD, slot 0, 4-bit", "[sd][ignore]")
  152. {
  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. }
  157. TEST_CASE("probe SD, slot 0, 1-bit", "[sd][ignore]")
  158. {
  159. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_PROBING, 0);
  160. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_DEFAULT, 0);
  161. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_HIGHSPEED, 0);
  162. }
  163. TEST_CASE("SD clock dividers calculation", "[sd][test_env=UT_T1_SDMODE]")
  164. {
  165. printf("Frequency (kHz) | Expected host.div | Expected card.div\n");
  166. sd_test_check_clk_dividers(SDMMC_FREQ_PROBING, 10, 20);
  167. sd_test_check_clk_dividers(SDMMC_FREQ_DEFAULT, 8, 0);
  168. sd_test_check_clk_dividers(SDMMC_FREQ_HIGHSPEED, 4, 0);
  169. sd_test_check_clk_dividers(36000, 5, 0);
  170. sd_test_check_clk_dividers(30000, 6, 0);
  171. sd_test_check_clk_dividers(16000, 10, 0);
  172. sd_test_check_clk_dividers(10000, 2, 4);
  173. sd_test_check_clk_dividers(6000, 2, 7);
  174. sd_test_check_clk_dividers(1000, 2, 40);
  175. sd_test_check_clk_dividers(600, 2, 67);
  176. }
  177. #endif //WITH_SD_TEST
  178. #if WITH_EMMC_TEST
  179. TEST_CASE("probe eMMC, slot 0, 4-bit", "[sd][test_env=EMMC][ignore]")
  180. {
  181. //Test with SDR
  182. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_PROBING, 0);
  183. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_DEFAULT, 0);
  184. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 0);
  185. //Test with DDR
  186. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 1);
  187. }
  188. TEST_CASE("probe eMMC, slot 0, 8-bit", "[sd][test_env=EMMC][ignore]")
  189. {
  190. //8-bit DDR not supported yet, test with SDR only
  191. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_PROBING, 0);
  192. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_DEFAULT, 0);
  193. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_HIGHSPEED, 0);
  194. }
  195. #endif // WITH_EMMC_TEST
  196. #if WITH_SDSPI_TEST
  197. #if !WITH_SD_TEST && !WITH_EMMC_TEST
  198. static void sd_test_board_power_on(void)
  199. {
  200. // do nothing
  201. }
  202. static void sd_test_board_power_off(void)
  203. {
  204. // do nothing
  205. }
  206. #endif
  207. static void test_sdspi_init_bus(spi_host_device_t host, int mosi_pin, int miso_pin, int clk_pin, int dma_chan)
  208. {
  209. spi_bus_config_t bus_config = {
  210. .mosi_io_num = mosi_pin,
  211. .miso_io_num = miso_pin,
  212. .sclk_io_num = clk_pin,
  213. .quadwp_io_num = -1,
  214. .quadhd_io_num = -1,
  215. };
  216. esp_err_t err = spi_bus_initialize(host, &bus_config, dma_chan);
  217. TEST_ESP_OK(err);
  218. }
  219. static void test_sdspi_deinit_bus(spi_host_device_t host)
  220. {
  221. esp_err_t err = spi_bus_free(host);
  222. TEST_ESP_OK(err);
  223. }
  224. static void probe_core(int slot, int freq_khz)
  225. {
  226. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  227. config.slot = slot;
  228. config.max_freq_khz = freq_khz;
  229. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  230. TEST_ASSERT_NOT_NULL(card);
  231. TEST_ESP_OK(sdmmc_card_init(&config, card));
  232. sdmmc_card_print_info(stdout, card);
  233. free(card);
  234. }
  235. static void probe_spi(int freq_khz, int pin_miso, int pin_mosi, int pin_sck, int pin_cs)
  236. {
  237. sd_test_board_power_on();
  238. sdspi_dev_handle_t handle;
  239. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  240. dev_config.gpio_cs = pin_cs;
  241. test_sdspi_init_bus(dev_config.host_id, pin_mosi, pin_miso, pin_sck, SPI_DMA_CH_AUTO);
  242. TEST_ESP_OK(sdspi_host_init());
  243. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  244. probe_core(handle, freq_khz);
  245. TEST_ESP_OK(sdspi_host_deinit());
  246. test_sdspi_deinit_bus(dev_config.host_id);
  247. sd_test_board_power_off();
  248. }
  249. TEST_CASE("probe SD in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  250. {
  251. probe_spi(SDMMC_FREQ_DEFAULT, SDSPI_TEST_MISO_PIN, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_SCLK_PIN, SDSPI_TEST_CS_PIN);
  252. //custom frequency test
  253. probe_spi(10000, SDSPI_TEST_MISO_PIN, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_SCLK_PIN, SDSPI_TEST_CS_PIN);
  254. }
  255. // No runner for this
  256. TEST_CASE("probe SD in SPI mode, slot 0", "[sd][ignore]")
  257. {
  258. probe_spi(SDMMC_FREQ_DEFAULT, 7, 11, 6, 10);
  259. }
  260. #endif //WITH_SDSPI_TEST
  261. #if WITH_SD_TEST || WITH_SDSPI_TEST || WITH_EMMC_TEST
  262. // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
  263. // from 'rand' with the starting value of 'seed'
  264. static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
  265. srand(seed);
  266. for (size_t i = 0; i < count; ++i) {
  267. uint32_t val = rand();
  268. memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
  269. }
  270. }
  271. // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
  272. // ints generated from 'rand' with the starting value of 'seed'
  273. static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
  274. srand(seed);
  275. for (size_t i = 0; i < count; ++i) {
  276. uint32_t val;
  277. memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
  278. TEST_ASSERT_EQUAL_HEX32(rand(), val);
  279. }
  280. }
  281. static void do_single_write_read_test(sdmmc_card_t* card, size_t start_block,
  282. size_t block_count, size_t alignment, bool performance_log)
  283. {
  284. size_t block_size = card->csd.sector_size;
  285. size_t total_size = block_size * block_count;
  286. printf(" %8d | %3d | %d | %4.1f ", start_block, block_count, alignment, total_size / 1024.0f);
  287. uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
  288. size_t offset = alignment % 4;
  289. uint8_t* c_buffer = (uint8_t*) buffer + offset;
  290. fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  291. struct timeval t_start_wr;
  292. gettimeofday(&t_start_wr, NULL);
  293. TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
  294. struct timeval t_stop_wr;
  295. gettimeofday(&t_stop_wr, NULL);
  296. 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);
  297. memset(buffer, 0xbb, total_size + 4);
  298. struct timeval t_start_rd;
  299. gettimeofday(&t_start_rd, NULL);
  300. TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
  301. struct timeval t_stop_rd;
  302. gettimeofday(&t_stop_rd, NULL);
  303. 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);
  304. printf(" | %6.2f | %5.2f | %6.2f | %5.2f\n",
  305. time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
  306. time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
  307. check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  308. free(buffer);
  309. if (performance_log) {
  310. static const char wr_speed_str[] = "SDMMC_WR_SPEED";
  311. static const char rd_speed_str[] = "SDMMC_RD_SPEED";
  312. int aligned = ((alignment % 4) == 0)? 1: 0;
  313. IDF_LOG_PERFORMANCE(wr_speed_str, "%d, blk_n: %d, aligned: %d",
  314. (int)(total_size * 1000 / time_wr), block_count, aligned);
  315. IDF_LOG_PERFORMANCE(rd_speed_str, "%d, blk_n: %d, aligned: %d",
  316. (int)(total_size * 1000 / time_rd), block_count, aligned);
  317. }
  318. }
  319. typedef void (*sd_test_func_t)(sdmmc_card_t* card);
  320. static void test_read_write_performance(sdmmc_card_t* card)
  321. {
  322. sdmmc_card_print_info(stdout, card);
  323. printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  324. const int offset = 0;
  325. const bool do_log = true;
  326. //aligned
  327. do_single_write_read_test(card, offset, 1, 4, do_log);
  328. do_single_write_read_test(card, offset, 4, 4, do_log);
  329. do_single_write_read_test(card, offset, 8, 4, do_log);
  330. do_single_write_read_test(card, offset, 16, 4, do_log);
  331. do_single_write_read_test(card, offset, 32, 4, do_log);
  332. do_single_write_read_test(card, offset, 64, 4, do_log);
  333. do_single_write_read_test(card, offset, 128, 4, do_log);
  334. //unaligned
  335. do_single_write_read_test(card, offset, 1, 1, do_log);
  336. do_single_write_read_test(card, offset, 8, 1, do_log);
  337. do_single_write_read_test(card, offset, 128, 1, do_log);
  338. }
  339. static void test_read_write_with_offset(sdmmc_card_t* card)
  340. {
  341. sdmmc_card_print_info(stdout, card);
  342. printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  343. const bool no_log = false;;
  344. //aligned
  345. do_single_write_read_test(card, 1, 16, 4, no_log);
  346. do_single_write_read_test(card, 16, 32, 4, no_log);
  347. do_single_write_read_test(card, 48, 64, 4, no_log);
  348. do_single_write_read_test(card, 128, 128, 4, no_log);
  349. do_single_write_read_test(card, card->csd.capacity - 64, 32, 4, no_log);
  350. do_single_write_read_test(card, card->csd.capacity - 64, 64, 4, no_log);
  351. do_single_write_read_test(card, card->csd.capacity - 8, 1, 4, no_log);
  352. do_single_write_read_test(card, card->csd.capacity/2, 1, 4, no_log);
  353. do_single_write_read_test(card, card->csd.capacity/2, 4, 4, no_log);
  354. do_single_write_read_test(card, card->csd.capacity/2, 8, 4, no_log);
  355. do_single_write_read_test(card, card->csd.capacity/2, 16, 4, no_log);
  356. do_single_write_read_test(card, card->csd.capacity/2, 32, 4, no_log);
  357. do_single_write_read_test(card, card->csd.capacity/2, 64, 4, no_log);
  358. do_single_write_read_test(card, card->csd.capacity/2, 128, 4, no_log);
  359. //unaligned
  360. do_single_write_read_test(card, card->csd.capacity/2, 1, 1, no_log);
  361. do_single_write_read_test(card, card->csd.capacity/2, 8, 1, no_log);
  362. do_single_write_read_test(card, card->csd.capacity/2, 128, 1, no_log);
  363. }
  364. #endif //WITH_SD_TEST || WITH_SDSPI_TEST || WITH_EMMC_TEST
  365. #if WITH_SD_TEST || WITH_EMMC_TEST
  366. void sd_test_rw_blocks(int slot, int width, sd_test_func_t test_func)
  367. {
  368. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  369. config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  370. config.slot = slot;
  371. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  372. if (width != 0) {
  373. slot_config.width = width;
  374. }
  375. if (slot_config.width == 8) {
  376. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  377. }
  378. TEST_ESP_OK(sdmmc_host_init());
  379. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  380. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  381. TEST_ASSERT_NOT_NULL(card);
  382. TEST_ESP_OK(sdmmc_card_init(&config, card));
  383. test_func(card);
  384. free(card);
  385. TEST_ESP_OK(sdmmc_host_deinit());
  386. }
  387. #endif //WITH_SD_TEST || WITH_EMMC_TEST
  388. #if WITH_SD_TEST
  389. TEST_CASE("SDMMC performance test (SD slot 1, 4 line)", "[sd][test_env=UT_T1_SDMODE]")
  390. {
  391. sd_test_board_power_on();
  392. sd_test_rw_blocks(1, 4, test_read_write_performance);
  393. sd_test_board_power_off();
  394. }
  395. TEST_CASE("SDMMC performance test (SD slot 1, 1 line)", "[sd][test_env=UT_T1_SDMODE]")
  396. {
  397. sd_test_board_power_on();
  398. sd_test_rw_blocks(1, 1, test_read_write_performance);
  399. sd_test_board_power_off();
  400. }
  401. TEST_CASE("SDMMC test read/write with offset (SD slot 1)", "[sd][test_env=UT_T1_SDMODE]")
  402. {
  403. sd_test_board_power_on();
  404. sd_test_rw_blocks(1, 4, test_read_write_with_offset);
  405. sd_test_board_power_off();
  406. }
  407. #endif //WITH_SD_TEST
  408. #if WITH_EMMC_TEST
  409. TEST_CASE("SDMMC performance test (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC][ignore]")
  410. {
  411. sd_test_board_power_on();
  412. sd_test_rw_blocks(0, 4, test_read_write_performance);
  413. sd_test_board_power_off();
  414. }
  415. TEST_CASE("SDMMC test read/write with offset (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC][ignore]")
  416. {
  417. sd_test_board_power_on();
  418. sd_test_rw_blocks(0, 4, test_read_write_with_offset);
  419. sd_test_board_power_off();
  420. }
  421. TEST_CASE("SDMMC performance test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC][ignore]")
  422. {
  423. sd_test_board_power_on();
  424. sd_test_rw_blocks(0, 8, test_read_write_performance);
  425. sd_test_board_power_off();
  426. }
  427. TEST_CASE("SDMMC test read/write with offset (eMMC slot 0, 8 line)", "[sd][test_env=EMMC][ignore]")
  428. {
  429. sd_test_board_power_on();
  430. sd_test_rw_blocks(0, 8, test_read_write_with_offset);
  431. sd_test_board_power_off();
  432. }
  433. #endif // WITH_EMMC_TEST
  434. #if WITH_SDSPI_TEST
  435. void sdspi_test_rw_blocks(sd_test_func_t test_func)
  436. {
  437. sd_test_board_power_on();
  438. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  439. sdspi_dev_handle_t handle;
  440. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  441. dev_config.host_id = config.slot;
  442. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  443. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  444. TEST_ESP_OK(sdspi_host_init());
  445. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  446. // This test can only run under 20MHz on ESP32, because the runner connects the card to
  447. // non-IOMUX pins of HSPI.
  448. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  449. TEST_ASSERT_NOT_NULL(card);
  450. TEST_ESP_OK(sdmmc_card_init(&config, card));
  451. test_func(card);
  452. TEST_ESP_OK(sdspi_host_deinit());
  453. free(card);
  454. test_sdspi_deinit_bus(dev_config.host_id);
  455. sd_test_board_power_off();
  456. }
  457. TEST_CASE("SDMMC performance (SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  458. {
  459. sdspi_test_rw_blocks(test_read_write_performance);
  460. }
  461. TEST_CASE("SDMMC test read/write with offset (SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  462. {
  463. sdspi_test_rw_blocks(test_read_write_with_offset);
  464. }
  465. #endif //WITH_SDSPI_TEST
  466. #if WITH_SD_TEST
  467. TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE]")
  468. {
  469. sd_test_board_power_on();
  470. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  471. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  472. TEST_ESP_OK(sdmmc_host_init());
  473. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  474. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  475. TEST_ASSERT_NOT_NULL(card);
  476. TEST_ESP_OK(sdmmc_card_init(&config, card));
  477. const size_t buffer_size = 4096;
  478. const size_t block_count = buffer_size / 512;
  479. const size_t extra = 4;
  480. uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
  481. // Check read behavior: do aligned write, then unaligned read
  482. const uint32_t seed = 0x89abcdef;
  483. fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  484. TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
  485. memset(buffer, 0xcc, buffer_size + extra);
  486. TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
  487. check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  488. // Check write behavior: do unaligned write, then aligned read
  489. fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  490. TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
  491. memset(buffer, 0xcc, buffer_size + extra);
  492. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
  493. check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  494. free(buffer);
  495. free(card);
  496. TEST_ESP_OK(sdmmc_host_deinit());
  497. sd_test_board_power_off();
  498. }
  499. #endif //WITH_SD_TEST
  500. #if WITH_SD_TEST || WITH_SDSPI_TEST
  501. static void test_cd_input(int gpio_cd_num, const sdmmc_host_t* config)
  502. {
  503. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  504. TEST_ASSERT_NOT_NULL(card);
  505. // SDMMC host should have configured CD as input.
  506. // Enable output as well (not using the driver, to avoid touching input
  507. // enable bits).
  508. esp_rom_gpio_connect_out_signal(gpio_cd_num, SIG_GPIO_OUT_IDX, false, false);
  509. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_cd_num));
  510. // Check that card initialization fails if CD is high
  511. REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_cd_num));
  512. usleep(10000);
  513. TEST_ESP_ERR(ESP_ERR_NOT_FOUND, sdmmc_card_init(config, card));
  514. // Check that card initialization succeeds if CD is low
  515. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_cd_num));
  516. usleep(10000);
  517. esp_err_t err = sdmmc_card_init(config, card);
  518. if (err != ESP_OK) {
  519. usleep(10000);
  520. // Try again, in case the card was not ready yet
  521. err = sdmmc_card_init(config, card);
  522. }
  523. TEST_ESP_OK(err);
  524. free(card);
  525. }
  526. static void test_wp_input(int gpio_wp_num, bool gpio_wp_polarity, const sdmmc_host_t* config)
  527. {
  528. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  529. TEST_ASSERT_NOT_NULL(card);
  530. // SDMMC host should have configured WP as input.
  531. // Enable output as well (not using the driver, to avoid touching input
  532. // enable bits).
  533. esp_rom_gpio_connect_out_signal(gpio_wp_num, SIG_GPIO_OUT_IDX, false, false);
  534. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_wp_num));
  535. // Check that the card can be initialized with WP low
  536. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
  537. TEST_ESP_OK(sdmmc_card_init(config, card));
  538. uint32_t* data = heap_caps_calloc(1, 512, MALLOC_CAP_DMA);
  539. // Check that card write succeeds if WP is high
  540. REG_WRITE((gpio_wp_polarity? GPIO_OUT_W1TC_REG : GPIO_OUT_W1TS_REG), BIT(gpio_wp_num));
  541. usleep(1000);
  542. TEST_ESP_OK(sdmmc_write_sectors(card, &data, 0, 1));
  543. // Check that write fails if WP is low
  544. REG_WRITE((gpio_wp_polarity? GPIO_OUT_W1TS_REG : GPIO_OUT_W1TC_REG), BIT(gpio_wp_num));
  545. usleep(1000);
  546. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_write_sectors(card, &data, 0, 1));
  547. // ...but reads still work
  548. TEST_ESP_OK(sdmmc_read_sectors(card, &data, 0, 1));
  549. free(data);
  550. free(card);
  551. }
  552. #endif //WITH_SD_TEST || WITH_SDSPI_TEST
  553. #if WITH_SD_TEST
  554. TEST_CASE("CD input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  555. {
  556. sd_test_board_power_on();
  557. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  558. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  559. slot_config.gpio_cd = CD_WP_TEST_GPIO;
  560. TEST_ESP_OK(sdmmc_host_init());
  561. usleep(10000);
  562. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  563. test_cd_input(CD_WP_TEST_GPIO, &config);
  564. TEST_ESP_OK(sdmmc_host_deinit());
  565. sd_test_board_power_off();
  566. }
  567. TEST_CASE("WP input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  568. {
  569. sd_test_board_power_on();
  570. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  571. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  572. slot_config.gpio_wp = CD_WP_TEST_GPIO;
  573. bool gpio_wp_polarity = GPIO_ACTIVE_LOW;
  574. if (gpio_wp_polarity) {
  575. slot_config.flags |= SDMMC_SLOT_FLAG_WP_ACTIVE_HIGH;
  576. } else {
  577. slot_config.flags &= ~(SDMMC_SLOT_FLAG_WP_ACTIVE_HIGH);
  578. }
  579. TEST_ESP_OK(sdmmc_host_init());
  580. usleep(10000);
  581. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  582. test_wp_input(slot_config.gpio_wp, gpio_wp_polarity, &config);
  583. TEST_ESP_OK(sdmmc_host_deinit());
  584. sd_test_board_power_off();
  585. }
  586. #endif //WITH_SD_TEST
  587. #if WITH_SDSPI_TEST
  588. TEST_CASE("CD input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  589. {
  590. sd_test_board_power_on();
  591. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  592. sdspi_dev_handle_t handle;
  593. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  594. dev_config.host_id = config.slot;
  595. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  596. dev_config.gpio_cd = CD_WP_TEST_GPIO;
  597. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  598. TEST_ESP_OK(sdspi_host_init());
  599. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  600. config.slot = handle;
  601. test_cd_input(CD_WP_TEST_GPIO, &config);
  602. TEST_ESP_OK(sdspi_host_deinit());
  603. test_sdspi_deinit_bus(dev_config.host_id);
  604. sd_test_board_power_off();
  605. }
  606. TEST_CASE("WP input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  607. {
  608. sd_test_board_power_on();
  609. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  610. sdspi_dev_handle_t handle;
  611. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  612. dev_config.host_id = config.slot;
  613. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  614. dev_config.gpio_wp = CD_WP_TEST_GPIO;
  615. dev_config.gpio_wp_polarity = GPIO_ACTIVE_LOW;
  616. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  617. TEST_ESP_OK(sdspi_host_init());
  618. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  619. config.slot = handle;
  620. test_wp_input(dev_config.gpio_wp, dev_config.gpio_wp_polarity, &config);
  621. TEST_ESP_OK(sdspi_host_deinit());
  622. test_sdspi_deinit_bus(dev_config.host_id);
  623. sd_test_board_power_off();
  624. }
  625. #endif //WITH_SDSPI_TEST
  626. #if WITH_SD_TEST || WITH_EMMC_TEST
  627. #define PATTERN_SEED 0x12345678
  628. #define FLAG_ERASE_TEST_ADJACENT (1 << 0)
  629. #define FLAG_VERIFY_ERASE_STATE (1 << 1)
  630. bool do_sanitize_flag = false;
  631. static void ensure_sector_written(sdmmc_card_t* card, size_t sector,
  632. uint8_t *pattern_buf, uint8_t *temp_buf)
  633. {
  634. size_t block_size = card->csd.sector_size;
  635. TEST_ESP_OK(sdmmc_write_sectors(card, pattern_buf, sector, 1));
  636. memset((void *)temp_buf, 0x00, block_size);
  637. TEST_ESP_OK(sdmmc_read_sectors(card, temp_buf, sector, 1));
  638. check_buffer(PATTERN_SEED, temp_buf, block_size / sizeof(uint32_t));
  639. }
  640. static void ensure_sector_intact(sdmmc_card_t* card, size_t sector,
  641. uint8_t *pattern_buf, uint8_t *temp_buf)
  642. {
  643. size_t block_size = card->csd.sector_size;
  644. memset((void *)temp_buf, 0x00, block_size);
  645. TEST_ESP_OK(sdmmc_read_sectors(card, temp_buf, sector, 1));
  646. check_buffer(PATTERN_SEED, temp_buf, block_size / sizeof(uint32_t));
  647. }
  648. static int32_t ensure_sector_erase(sdmmc_card_t* card, size_t sector,
  649. uint8_t *pattern_buf, uint8_t *temp_buf)
  650. {
  651. size_t block_size = card->csd.sector_size;
  652. memset((void *)temp_buf, 0, block_size);
  653. TEST_ESP_OK(sdmmc_read_sectors(card, temp_buf, sector, 1));
  654. return memcmp(pattern_buf, temp_buf, block_size);
  655. }
  656. static void do_single_erase_test(sdmmc_card_t* card, size_t start_block,
  657. size_t block_count, uint8_t flags, sdmmc_erase_arg_t arg)
  658. {
  659. size_t block_size = card->csd.sector_size;
  660. uint8_t *temp_buf = NULL;
  661. uint8_t *pattern_buf = NULL;
  662. size_t end_block = (start_block + block_count - 1);
  663. /*
  664. * To ensure erase is successful/valid
  665. * selected blocks after erase should have erase state data pattern
  666. * data of blocks adjacent to selected region should remain intact
  667. */
  668. TEST_ESP_OK((start_block + block_count) > card->csd.capacity);
  669. pattern_buf = (uint8_t *)heap_caps_malloc(block_size, MALLOC_CAP_DMA);
  670. TEST_ASSERT_NOT_NULL(pattern_buf);
  671. temp_buf = (uint8_t *)heap_caps_malloc(block_size, MALLOC_CAP_DMA);
  672. TEST_ASSERT_NOT_NULL(temp_buf);
  673. // create pattern buffer
  674. fill_buffer(PATTERN_SEED, pattern_buf, block_size / sizeof(uint32_t));
  675. // check if it's not the first block of device & write/read/verify pattern
  676. if ((flags & FLAG_ERASE_TEST_ADJACENT) && start_block) {
  677. ensure_sector_written(card, (start_block - 1), pattern_buf, temp_buf);
  678. }
  679. ensure_sector_written(card, start_block, pattern_buf, temp_buf);
  680. // check if it's not the last block of device & write/read/verify pattern
  681. if ((flags & FLAG_ERASE_TEST_ADJACENT) && (end_block < (card->csd.capacity - 1))) {
  682. ensure_sector_written(card, (end_block + 1), pattern_buf, temp_buf);
  683. }
  684. // when block count is 1, start and end block is same, hence skip
  685. if (block_count != 1) {
  686. ensure_sector_written(card, end_block, pattern_buf, temp_buf);
  687. }
  688. // fill pattern to (start_block + end_block)/2 in the erase range
  689. if(block_count > 2) {
  690. ensure_sector_written(card, (start_block + end_block)/2, pattern_buf, temp_buf);
  691. }
  692. float total_size = (block_count/1024.0f) * block_size;
  693. printf(" %10d | %10d | %8.1f ", start_block, block_count, total_size);
  694. fflush(stdout);
  695. // erase the blocks
  696. struct timeval t_start_er;
  697. gettimeofday(&t_start_er, NULL);
  698. TEST_ESP_OK(sdmmc_erase_sectors(card, start_block, block_count, arg));
  699. if (do_sanitize_flag) {
  700. TEST_ESP_OK(sdmmc_mmc_sanitize(card, block_count * 500));
  701. }
  702. struct timeval t_stop_wr;
  703. gettimeofday(&t_stop_wr, NULL);
  704. float time_er = 1e3f * (t_stop_wr.tv_sec - t_start_er.tv_sec) + 1e-3f * (t_stop_wr.tv_usec - t_start_er.tv_usec);
  705. printf(" | %8.2f\n", time_er);
  706. // ensure adjacent blocks are not affected
  707. // block before start_block
  708. if ((flags & FLAG_ERASE_TEST_ADJACENT) && start_block) {
  709. ensure_sector_intact(card, (start_block - 1), pattern_buf, temp_buf);
  710. }
  711. // block after end_block
  712. if ((flags & FLAG_ERASE_TEST_ADJACENT) && (end_block < (card->csd.capacity - 1))) {
  713. ensure_sector_intact(card, (end_block + 1), pattern_buf, temp_buf);
  714. }
  715. uint8_t erase_mem_byte = 0xFF;
  716. // ensure all the blocks are erased and are up to after erase state.
  717. if (!card->is_mmc) {
  718. erase_mem_byte = card->scr.erase_mem_state ? 0xFF : 0x00;
  719. } else {
  720. erase_mem_byte = card->ext_csd.erase_mem_state ? 0xFF : 0x00;
  721. }
  722. memset((void *)pattern_buf, erase_mem_byte, block_size);
  723. // as it is block by block comparison, a time taking process. Really long
  724. // when you do erase and verify on complete device.
  725. if (flags & FLAG_VERIFY_ERASE_STATE) {
  726. for (size_t i = 0; i < block_count; i++) {
  727. if (ensure_sector_erase(card, (start_block + i), pattern_buf, temp_buf)) {
  728. printf("Error: Sector %d erase\n", (start_block + i));
  729. break;
  730. }
  731. }
  732. }
  733. free(temp_buf);
  734. free(pattern_buf);
  735. }
  736. #endif // WITH_SD_TEST || WITH_EMMC_TEST
  737. #if WITH_SDSPI_TEST
  738. static void test_sdspi_erase_blocks(size_t start_block, size_t block_count)
  739. {
  740. sd_test_board_power_on();
  741. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  742. sdspi_dev_handle_t handle;
  743. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  744. dev_config.host_id = config.slot;
  745. dev_config.gpio_cs = SDSPI_TEST_CS_PIN;
  746. test_sdspi_init_bus(dev_config.host_id, SDSPI_TEST_MOSI_PIN, SDSPI_TEST_MISO_PIN, SDSPI_TEST_SCLK_PIN, SPI_DMA_CH_AUTO);
  747. TEST_ESP_OK(sdspi_host_init());
  748. TEST_ESP_OK(sdspi_host_init_device(&dev_config, &handle));
  749. // This test can only run under 20MHz on ESP32, because the runner connects the card to
  750. // non-IOMUX pins of HSPI.
  751. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  752. TEST_ASSERT_NOT_NULL(card);
  753. TEST_ESP_OK(sdmmc_card_init(&config, card));
  754. sdmmc_card_print_info(stdout, card);
  755. // Ensure discard operation is not supported in sdspi
  756. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, sdmmc_erase_sectors(card, start_block, block_count, SDMMC_DISCARD_ARG));
  757. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  758. printf("Erasing sectors %d-%d\n", start_block, (start_block + block_count -1));
  759. size_t block_size = card->csd.sector_size;
  760. uint8_t *pattern_buf = (uint8_t *)heap_caps_malloc(block_size, MALLOC_CAP_DMA);
  761. TEST_ASSERT_NOT_NULL(pattern_buf);
  762. uint8_t *temp_buf = (uint8_t *)heap_caps_malloc(block_size, MALLOC_CAP_DMA);
  763. TEST_ASSERT_NOT_NULL(temp_buf);
  764. struct timeval t_start_er;
  765. gettimeofday(&t_start_er, NULL);
  766. TEST_ESP_OK(sdmmc_erase_sectors(card, start_block, block_count, SDMMC_ERASE_ARG));
  767. struct timeval t_stop_wr;
  768. gettimeofday(&t_stop_wr, NULL);
  769. float time_er = 1e3f * (t_stop_wr.tv_sec - t_start_er.tv_sec) + 1e-3f * (t_stop_wr.tv_usec - t_start_er.tv_usec);
  770. printf("Erase duration: %.2fms\n", time_er);
  771. printf("Verifying erase state...\n");
  772. uint8_t erase_mem_byte = 0xFF;
  773. // ensure all the blocks are erased and are up to after erase state.
  774. if (!card->is_mmc) {
  775. erase_mem_byte = card->scr.erase_mem_state ? 0xFF : 0x00;
  776. } else {
  777. erase_mem_byte = card->ext_csd.erase_mem_state ? 0xFF : 0x00;
  778. }
  779. memset((void *)pattern_buf, erase_mem_byte, block_size);
  780. size_t i;
  781. for (i = 0; i < block_count; i++) {
  782. memset((void *)temp_buf, 0, block_size);
  783. TEST_ESP_OK(sdmmc_read_sectors(card, temp_buf, (start_block + i), 1));
  784. if (memcmp(pattern_buf, temp_buf, block_size)) {
  785. printf("Error: Sector %d erase\n", (start_block + i));
  786. break;
  787. }
  788. }
  789. if (i == block_count) {
  790. printf("Sectors erase success\n");
  791. }
  792. TEST_ESP_OK(sdspi_host_deinit());
  793. test_sdspi_deinit_bus(dev_config.host_id);
  794. free(card);
  795. free(temp_buf);
  796. free(pattern_buf);
  797. sd_test_board_power_off();
  798. }
  799. TEST_CASE("SDMMC erase (SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  800. {
  801. test_sdspi_erase_blocks(0, 16);
  802. }
  803. #endif // WITH_SDSPI_TEST
  804. #if WITH_SD_TEST
  805. static void test_sd_erase_blocks(sdmmc_card_t* card)
  806. {
  807. sdmmc_card_print_info(stdout, card);
  808. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  809. printf(" sector | count | size(kB) | er_time(ms) \n");
  810. /*
  811. * bit-0: verify adjacent blocks of given range
  812. * bit-1: verify erase state of blocks in range
  813. */
  814. uint8_t flags = 0;
  815. sdmmc_erase_arg_t arg = SDMMC_ERASE_ARG;
  816. //check for adjacent blocks and erase state of blocks
  817. flags |= (uint8_t)FLAG_ERASE_TEST_ADJACENT | (uint8_t)FLAG_VERIFY_ERASE_STATE;
  818. do_single_erase_test(card, 1, 16, flags, arg);
  819. do_single_erase_test(card, 1, 13, flags, arg);
  820. do_single_erase_test(card, 16, 32, flags, arg);
  821. do_single_erase_test(card, 48, 64, flags, arg);
  822. do_single_erase_test(card, 128, 128, flags, arg);
  823. do_single_erase_test(card, card->csd.capacity - 64, 32, flags, arg);
  824. do_single_erase_test(card, card->csd.capacity - 64, 64, flags, arg);
  825. // single sector erase is failing on different make cards
  826. do_single_erase_test(card, card->csd.capacity - 8, 1, flags, arg);
  827. do_single_erase_test(card, card->csd.capacity/2, 1, flags, arg);
  828. do_single_erase_test(card, card->csd.capacity/2, 4, flags, arg);
  829. do_single_erase_test(card, card->csd.capacity/2, 8, flags, arg);
  830. do_single_erase_test(card, card->csd.capacity/2, 16, flags, arg);
  831. do_single_erase_test(card, card->csd.capacity/2, 32, flags, arg);
  832. do_single_erase_test(card, card->csd.capacity/2, 64, flags, arg);
  833. do_single_erase_test(card, card->csd.capacity/2, 128, flags, arg);
  834. #ifdef SDMMC_FULL_ERASE_TEST
  835. /*
  836. * check for adjacent blocks, do not check erase state of blocks as it is
  837. * time taking process to verify all the blocks.
  838. */
  839. flags &= ~(uint8_t)FLAG_VERIFY_ERASE_STATE; //comment this line to verify after-erase state
  840. // erase complete card
  841. do_single_erase_test(card, 0, card->csd.capacity, flags, arg);
  842. #endif //SDMMC_FULL_ERASE_TEST
  843. }
  844. static void test_sd_discard_blocks(sdmmc_card_t* card)
  845. {
  846. /* MMC discard applies to write blocks */
  847. sdmmc_card_print_info(stdout, card);
  848. /*
  849. * bit-0: verify adjacent blocks of given range
  850. * bit-1: verify erase state of blocks in range
  851. */
  852. uint8_t flags = 0;
  853. sdmmc_erase_arg_t arg = SDMMC_DISCARD_ARG;
  854. /*
  855. * This test does run two tests
  856. * test-1: check, sdmmc_erase_sectors to return ESP_ERR_NOT_SUPPORTED
  857. * when arguments are condition not met. This test runs either the card
  858. * supports discard or not.
  859. *
  860. * test-2: If card supports discard, perform the test accordingly and
  861. * validate the behavior.
  862. *
  863. */
  864. uint32_t prev_discard_support = card->ssr.discard_support;
  865. // overwrite discard_support as not-supported for -ve test
  866. card->ssr.discard_support = 0;
  867. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, sdmmc_erase_sectors(card, 0, 32, arg));
  868. // restore discard_support
  869. card->ssr.discard_support = prev_discard_support;
  870. if (sdmmc_can_discard(card) != ESP_OK ) {
  871. printf("Card/device do not support discard\n");
  872. return;
  873. }
  874. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  875. printf(" sector | count | size(kB) | er_time(ms) \n");
  876. /*
  877. * Check for adjacent blocks only.
  878. * After discard operation, the original data may be remained partially or
  879. * fully accessible to the host dependent on device. Hence do not verify
  880. * the erased state of the blocks.
  881. */
  882. flags |= (uint8_t)FLAG_ERASE_TEST_ADJACENT;
  883. do_single_erase_test(card, 1, 16, flags, arg);
  884. do_single_erase_test(card, 1, 13, flags, arg);
  885. do_single_erase_test(card, 16, 32, flags, arg);
  886. do_single_erase_test(card, 48, 64, flags, arg);
  887. do_single_erase_test(card, 128, 128, flags, arg);
  888. do_single_erase_test(card, card->csd.capacity - 64, 32, flags, arg);
  889. do_single_erase_test(card, card->csd.capacity - 64, 64, flags, arg);
  890. do_single_erase_test(card, card->csd.capacity - 8, 1, flags, arg);
  891. do_single_erase_test(card, card->csd.capacity/2, 1, flags, arg);
  892. do_single_erase_test(card, card->csd.capacity/2, 4, flags, arg);
  893. do_single_erase_test(card, card->csd.capacity/2, 8, flags, arg);
  894. do_single_erase_test(card, card->csd.capacity/2, 16, flags, arg);
  895. do_single_erase_test(card, card->csd.capacity/2, 32, flags, arg);
  896. do_single_erase_test(card, card->csd.capacity/2, 64, flags, arg);
  897. do_single_erase_test(card, card->csd.capacity/2, 128, flags, arg);
  898. }
  899. TEST_CASE("SDMMC erase test (SD slot 1, 1 line)", "[sd][test_env=UT_T1_SDMODE]")
  900. {
  901. sd_test_board_power_on();
  902. sd_test_rw_blocks(1, 1, test_sd_erase_blocks);
  903. sd_test_board_power_off();
  904. }
  905. TEST_CASE("SDMMC erase test (SD slot 1, 4 line)", "[sd][test_env=UT_T1_SDMODE]")
  906. {
  907. sd_test_board_power_on();
  908. sd_test_rw_blocks(1, 4, test_sd_erase_blocks);
  909. sd_test_board_power_off();
  910. }
  911. TEST_CASE("SDMMC discard test (SD slot 1, 4 line)", "[sd][test_env=UT_T1_SDMODE]")
  912. {
  913. sd_test_board_power_on();
  914. sd_test_rw_blocks(1, 4, test_sd_discard_blocks);
  915. sd_test_board_power_off();
  916. }
  917. #endif //WITH_SD_TEST
  918. #if WITH_SD_TEST
  919. TEST_CASE("sdmmc read/write/erase sector shoud return ESP_OK with sector count == 0", "[sd][test_env=UT_T1_SDMODE]")
  920. {
  921. sd_test_board_power_on();
  922. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  923. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  924. TEST_ESP_OK(sdmmc_host_init());
  925. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  926. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  927. TEST_ASSERT_NOT_NULL(card);
  928. TEST_ESP_OK(sdmmc_card_init(&config, card));
  929. TEST_ESP_OK(sdmmc_write_sectors(card, NULL, 0, 0));
  930. TEST_ESP_OK(sdmmc_read_sectors(card, NULL, 0, 0));
  931. TEST_ESP_OK(sdmmc_erase_sectors(card, 0, 0, SDMMC_ERASE_ARG));
  932. free(card);
  933. TEST_ESP_OK(sdmmc_host_deinit());
  934. sd_test_board_power_off();
  935. }
  936. #endif //WITH_SD_TEST
  937. #if WITH_EMMC_TEST
  938. static void test_mmc_sanitize_blocks(sdmmc_card_t* card)
  939. {
  940. /* MMC discard applies to write blocks */
  941. sdmmc_card_print_info(stdout, card);
  942. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  943. if (sdmmc_mmc_can_sanitize(card)) {
  944. printf("Card/device do not support sanitize\n");
  945. return;
  946. }
  947. printf(" sector | count | size(kB) | er_time(ms) \n");
  948. /*
  949. * bit-0: verify adjacent blocks of given range
  950. * bit-1: verify erase state of blocks in range
  951. */
  952. uint8_t flags = 0;
  953. sdmmc_erase_arg_t arg = SDMMC_DISCARD_ARG;
  954. do_sanitize_flag = true;
  955. /*
  956. * Check for adjacent blocks only.
  957. * After discard operation, the original data may be remained partially or
  958. * fully accessible to the host dependent on device. Hence do not verify
  959. * the erased state of the blocks.
  960. *
  961. * Note: After sanitize blocks has to be in erased state
  962. */
  963. flags |= (uint8_t)FLAG_ERASE_TEST_ADJACENT | (uint8_t)FLAG_VERIFY_ERASE_STATE;
  964. do_single_erase_test(card, 1, 16, flags, arg);
  965. do_single_erase_test(card, 1, 13, flags, arg);
  966. do_single_erase_test(card, 16, 32, flags, arg);
  967. do_single_erase_test(card, 48, 64, flags, arg);
  968. do_single_erase_test(card, 128, 128, flags, arg);
  969. do_single_erase_test(card, card->csd.capacity - 64, 32, flags, arg);
  970. do_single_erase_test(card, card->csd.capacity - 64, 64, flags, arg);
  971. do_single_erase_test(card, card->csd.capacity - 8, 1, flags, arg);
  972. do_single_erase_test(card, card->csd.capacity/2, 1, flags, arg);
  973. do_single_erase_test(card, card->csd.capacity/2, 4, flags, arg);
  974. do_single_erase_test(card, card->csd.capacity/2, 8, flags, arg);
  975. do_single_erase_test(card, card->csd.capacity/2, 16, flags, arg);
  976. do_single_erase_test(card, card->csd.capacity/2, 32, flags, arg);
  977. do_single_erase_test(card, card->csd.capacity/2, 64, flags, arg);
  978. do_single_erase_test(card, card->csd.capacity/2, 128, flags, arg);
  979. do_sanitize_flag = false;
  980. }
  981. static void test_mmc_discard_blocks(sdmmc_card_t* card)
  982. {
  983. /* MMC discard applies to write blocks */
  984. sdmmc_card_print_info(stdout, card);
  985. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  986. sdmmc_erase_arg_t arg = SDMMC_DISCARD_ARG;
  987. uint32_t prev_ext_csd = card->ext_csd.rev;
  988. // overwrite discard_support as not-supported for -ve test
  989. card->ext_csd.rev = 0;
  990. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, sdmmc_erase_sectors(card, 0, 32, arg));
  991. // restore discard_support
  992. card->ext_csd.rev = prev_ext_csd;
  993. if (sdmmc_can_discard(card) != ESP_OK) {
  994. printf("Card/device do not support discard\n");
  995. return;
  996. }
  997. printf(" sector | count | size(kB) | er_time(ms) \n");
  998. /*
  999. * bit-0: verify adjacent blocks of given range
  1000. * bit-1: verify erase state of blocks in range
  1001. */
  1002. uint8_t flags = 0;
  1003. /*
  1004. * Check for adjacent blocks only.
  1005. * After discard operation, the original data may be remained partially or
  1006. * fully accessible to the host dependent on device. Hence do not verify
  1007. * the erased state of the blocks.
  1008. */
  1009. flags |= (uint8_t)FLAG_ERASE_TEST_ADJACENT;
  1010. do_single_erase_test(card, 1, 16, flags, arg);
  1011. do_single_erase_test(card, 1, 13, flags, arg);
  1012. do_single_erase_test(card, 16, 32, flags, arg);
  1013. do_single_erase_test(card, 48, 64, flags, arg);
  1014. do_single_erase_test(card, 128, 128, flags, arg);
  1015. do_single_erase_test(card, card->csd.capacity - 64, 32, flags, arg);
  1016. do_single_erase_test(card, card->csd.capacity - 64, 64, flags, arg);
  1017. do_single_erase_test(card, card->csd.capacity - 8, 1, flags, arg);
  1018. do_single_erase_test(card, card->csd.capacity/2, 1, flags, arg);
  1019. do_single_erase_test(card, card->csd.capacity/2, 4, flags, arg);
  1020. do_single_erase_test(card, card->csd.capacity/2, 8, flags, arg);
  1021. do_single_erase_test(card, card->csd.capacity/2, 16, flags, arg);
  1022. do_single_erase_test(card, card->csd.capacity/2, 32, flags, arg);
  1023. do_single_erase_test(card, card->csd.capacity/2, 64, flags, arg);
  1024. do_single_erase_test(card, card->csd.capacity/2, 128, flags, arg);
  1025. }
  1026. static void test_mmc_trim_blocks(sdmmc_card_t* card)
  1027. {
  1028. /* MMC trim applies to write blocks */
  1029. sdmmc_card_print_info(stdout, card);
  1030. printf("block size %d capacity %d\n", card->csd.sector_size, card->csd.capacity);
  1031. sdmmc_erase_arg_t arg = SDMMC_ERASE_ARG;
  1032. uint8_t prev_sec_feature = card->ext_csd.sec_feature;
  1033. // overwrite sec_feature
  1034. card->ext_csd.sec_feature &= ~(EXT_CSD_SEC_GB_CL_EN);
  1035. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, sdmmc_erase_sectors(card, 0, 32, arg));
  1036. // restore sec_feature
  1037. card->ext_csd.sec_feature = prev_sec_feature;
  1038. if (sdmmc_can_trim(card) != ESP_OK) {
  1039. printf("Card/device do not support trim\n");
  1040. return;
  1041. }
  1042. printf(" sector | count | size(kB) | er_time(ms) \n");
  1043. /*
  1044. * bit-0: verify adjacent blocks of given range
  1045. * bit-1: verify erase state of blocks in range
  1046. */
  1047. uint8_t flags = 0;
  1048. //check for adjacent blocks and erase state of blocks
  1049. flags |= (uint8_t)FLAG_ERASE_TEST_ADJACENT | (uint8_t)FLAG_VERIFY_ERASE_STATE;
  1050. do_single_erase_test(card, 1, 16, flags, arg);
  1051. do_single_erase_test(card, 1, 13, flags, arg);
  1052. do_single_erase_test(card, 16, 32, flags, arg);
  1053. do_single_erase_test(card, 48, 64, flags, arg);
  1054. do_single_erase_test(card, 128, 128, flags, arg);
  1055. do_single_erase_test(card, card->csd.capacity - 64, 32, flags, arg);
  1056. do_single_erase_test(card, card->csd.capacity - 64, 64, flags, arg);
  1057. do_single_erase_test(card, card->csd.capacity - 8, 1, flags, arg);
  1058. do_single_erase_test(card, card->csd.capacity/2, 1, flags, arg);
  1059. do_single_erase_test(card, card->csd.capacity/2, 4, flags, arg);
  1060. do_single_erase_test(card, card->csd.capacity/2, 8, flags, arg);
  1061. do_single_erase_test(card, card->csd.capacity/2, 16, flags, arg);
  1062. do_single_erase_test(card, card->csd.capacity/2, 32, flags, arg);
  1063. do_single_erase_test(card, card->csd.capacity/2, 64, flags, arg);
  1064. do_single_erase_test(card, card->csd.capacity/2, 128, flags, arg);
  1065. #ifdef SDMMC_FULL_ERASE_TEST
  1066. /*
  1067. * check for adjacent blocks, do not check erase state of blocks as it is
  1068. * time taking process to verify all the blocks.
  1069. */
  1070. flags &= ~(uint8_t)FLAG_VERIFY_ERASE_STATE; //comment this line to verify after erase state
  1071. // erase complete card
  1072. do_single_erase_test(card, 0, card->csd.capacity, flags, arg);
  1073. #endif //SDMMC_FULL_ERASE_TEST
  1074. }
  1075. TEST_CASE("SDMMC trim test (eMMC slot 0, 4 line)", "[sd][test_env=EMMC][ignore]")
  1076. {
  1077. sd_test_board_power_on();
  1078. sd_test_rw_blocks(0, 4, test_mmc_trim_blocks);
  1079. sd_test_board_power_off();
  1080. }
  1081. TEST_CASE("SDMMC trim test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC][ignore]")
  1082. {
  1083. sd_test_board_power_on();
  1084. sd_test_rw_blocks(0, 8, test_mmc_trim_blocks);
  1085. sd_test_board_power_off();
  1086. }
  1087. TEST_CASE("SDMMC discard test (eMMC slot 0, 4 line)", "[sd][test_env=EMMC][ignore]")
  1088. {
  1089. sd_test_board_power_on();
  1090. sd_test_rw_blocks(0, 4, test_mmc_discard_blocks);
  1091. sd_test_board_power_off();
  1092. }
  1093. TEST_CASE("SDMMC discard test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC][ignore]")
  1094. {
  1095. sd_test_board_power_on();
  1096. sd_test_rw_blocks(0, 8, test_mmc_discard_blocks);
  1097. sd_test_board_power_off();
  1098. }
  1099. TEST_CASE("SDMMC sanitize test (eMMC slot 0, 4 line)", "[sd][test_env=EMMC][ignore]")
  1100. {
  1101. sd_test_board_power_on();
  1102. sd_test_rw_blocks(0, 4, test_mmc_sanitize_blocks);
  1103. sd_test_board_power_off();
  1104. }
  1105. TEST_CASE("SDMMC sanitize test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC][ignore]")
  1106. {
  1107. sd_test_board_power_on();
  1108. sd_test_rw_blocks(0, 8, test_mmc_sanitize_blocks);
  1109. sd_test_board_power_off();
  1110. }
  1111. #endif //WITH_EMMC_TEST