test_sd.c 44 KB

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