test_sd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "unity.h"
  18. #include "driver/gpio.h"
  19. #include "driver/sdmmc_host.h"
  20. #include "driver/sdspi_host.h"
  21. #include "driver/sdmmc_defs.h"
  22. #include "soc/gpio_reg.h"
  23. #include "sdmmc_cmd.h"
  24. #include "esp_log.h"
  25. #include "esp_heap_caps.h"
  26. #include <time.h>
  27. #include <sys/time.h>
  28. #include <unistd.h>
  29. // Can't test eMMC (slot 0) and PSRAM together
  30. #ifndef CONFIG_SPIRAM_SUPPORT
  31. #define WITH_EMMC_TEST
  32. #endif
  33. /* power supply enable pin */
  34. #define SD_TEST_BOARD_VSEL_EN_GPIO 27
  35. /* power supply voltage select pin */
  36. #define SD_TEST_BOARD_VSEL_GPIO 26
  37. #define SD_TEST_BOARD_VSEL_3V3 1
  38. #define SD_TEST_BOARD_VSEL_1V8 0
  39. /* time to wait for reset / power-on */
  40. #define SD_TEST_BOARD_PWR_RST_DELAY_MS 5
  41. #define SD_TEST_BOARD_PWR_ON_DELAY_MS 50
  42. /* gpio which is not connected to actual CD pin, used to simulate CD behavior */
  43. #define CD_WP_TEST_GPIO 18
  44. static void sd_test_board_power_on()
  45. {
  46. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_OUTPUT);
  47. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, SD_TEST_BOARD_VSEL_3V3);
  48. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_OUTPUT);
  49. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  50. usleep(SD_TEST_BOARD_PWR_RST_DELAY_MS * 1000);
  51. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 1);
  52. usleep(SD_TEST_BOARD_PWR_ON_DELAY_MS * 1000);
  53. }
  54. static void sd_test_board_power_off()
  55. {
  56. gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
  57. gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_INPUT);
  58. gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, 0);
  59. gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_INPUT);
  60. }
  61. TEST_CASE("MMC_RSP_BITS", "[sd]")
  62. {
  63. uint32_t data[2] = { 0x01234567, 0x89abcdef };
  64. TEST_ASSERT_EQUAL_HEX32(0x7, MMC_RSP_BITS(data, 0, 4));
  65. TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
  66. TEST_ASSERT_EQUAL_HEX32(0xf0, MMC_RSP_BITS(data, 28, 8));
  67. TEST_ASSERT_EQUAL_HEX32(0x3, MMC_RSP_BITS(data, 1, 3));
  68. TEST_ASSERT_EQUAL_HEX32(0x11, MMC_RSP_BITS(data, 59, 5));
  69. }
  70. static void probe_sd(int slot, int width, int freq_khz, int ddr)
  71. {
  72. sd_test_board_power_on();
  73. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  74. config.slot = slot;
  75. config.max_freq_khz = freq_khz;
  76. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  77. if (width == 1) {
  78. config.flags = SDMMC_HOST_FLAG_1BIT;
  79. slot_config.width = 1;
  80. } else if (width == 4) {
  81. config.flags &= ~SDMMC_HOST_FLAG_8BIT;
  82. slot_config.width = 4;
  83. } else {
  84. assert(!ddr && "host driver does not support 8-line DDR mode yet");
  85. }
  86. if (!ddr) {
  87. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  88. }
  89. TEST_ESP_OK(sdmmc_host_init());
  90. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  91. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  92. TEST_ASSERT_NOT_NULL(card);
  93. TEST_ESP_OK(sdmmc_card_init(&config, card));
  94. sdmmc_card_print_info(stdout, card);
  95. uint8_t* buffer = heap_caps_malloc(512, MALLOC_CAP_DMA);
  96. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 0, 1));
  97. free(buffer);
  98. TEST_ESP_OK(sdmmc_host_deinit());
  99. free(card);
  100. sd_test_board_power_off();
  101. }
  102. static void probe_spi(int freq_khz, int pin_miso, int pin_mosi, int pin_sck, int pin_cs)
  103. {
  104. sd_test_board_power_on();
  105. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  106. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  107. slot_config.gpio_miso = pin_miso;
  108. slot_config.gpio_mosi = pin_mosi;
  109. slot_config.gpio_sck = pin_sck;
  110. slot_config.gpio_cs = pin_cs;
  111. TEST_ESP_OK(sdspi_host_init());
  112. TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
  113. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  114. TEST_ASSERT_NOT_NULL(card);
  115. TEST_ESP_OK(sdmmc_card_init(&config, card));
  116. sdmmc_card_print_info(stdout, card);
  117. TEST_ESP_OK(sdspi_host_deinit());
  118. free(card);
  119. sd_test_board_power_off();
  120. }
  121. TEST_CASE("probe SD, slot 1, 4-bit", "[sd][test_env=UT_T1_SDMODE]")
  122. {
  123. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_PROBING, 0);
  124. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_DEFAULT, 0);
  125. probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_HIGHSPEED, 0);
  126. }
  127. TEST_CASE("probe SD, slot 1, 1-bit", "[sd][test_env=UT_T1_SDMODE]")
  128. {
  129. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_PROBING, 0);
  130. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_DEFAULT, 0);
  131. probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_HIGHSPEED, 0);
  132. }
  133. #ifdef WITH_EMMC_TEST
  134. TEST_CASE("probe eMMC, slot 0, 4-bit, DDR", "[sd][test_env=EMMC]")
  135. {
  136. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 1);
  137. }
  138. TEST_CASE("probe eMMC, slot 0, 8-bit", "[sd][test_env=EMMC]")
  139. {
  140. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_PROBING, 0);
  141. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_DEFAULT, 0);
  142. probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_HIGHSPEED, 0);
  143. }
  144. #endif // WITH_EMMC_TEST
  145. TEST_CASE("probe SD, slot 0, 4-bit", "[sd][test_env=UT_T1_SDCARD][ignore]")
  146. {
  147. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_PROBING, 0);
  148. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_DEFAULT, 0);
  149. probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 0);
  150. }
  151. TEST_CASE("probe SD, slot 0, 1-bit", "[sd][test_env=UT_T1_SDCARD][ignore]")
  152. {
  153. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_PROBING, 0);
  154. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_DEFAULT, 0);
  155. probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_HIGHSPEED, 0);
  156. }
  157. TEST_CASE("probe SD in SPI mode, slot 1", "[sd][test_env=UT_T1_SPIMODE]")
  158. {
  159. probe_spi(SDMMC_FREQ_DEFAULT, 2, 15, 14, 13);
  160. }
  161. TEST_CASE("probe SD in SPI mode, slot 0", "[sd][test_env=UT_T1_SDCARD][ignore]")
  162. {
  163. probe_spi(SDMMC_FREQ_DEFAULT, 7, 11, 6, 10);
  164. }
  165. // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
  166. // from 'rand' with the starting value of 'seed'
  167. static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
  168. srand(seed);
  169. for (size_t i = 0; i < count; ++i) {
  170. uint32_t val = rand();
  171. memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
  172. }
  173. }
  174. // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
  175. // ints generated from 'rand' with the starting value of 'seed'
  176. static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
  177. srand(seed);
  178. for (size_t i = 0; i < count; ++i) {
  179. uint32_t val;
  180. memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
  181. TEST_ASSERT_EQUAL_HEX32(rand(), val);
  182. }
  183. }
  184. static void do_single_write_read_test(sdmmc_card_t* card,
  185. size_t start_block, size_t block_count, size_t alignment)
  186. {
  187. size_t block_size = card->csd.sector_size;
  188. size_t total_size = block_size * block_count;
  189. printf(" %8d | %3d | %d | %4.1f ", start_block, block_count, alignment, total_size / 1024.0f);
  190. uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
  191. size_t offset = alignment % 4;
  192. uint8_t* c_buffer = (uint8_t*) buffer + offset;
  193. fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  194. struct timeval t_start_wr;
  195. gettimeofday(&t_start_wr, NULL);
  196. TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
  197. struct timeval t_stop_wr;
  198. gettimeofday(&t_stop_wr, NULL);
  199. 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);
  200. memset(buffer, 0xbb, total_size + 4);
  201. struct timeval t_start_rd;
  202. gettimeofday(&t_start_rd, NULL);
  203. TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
  204. struct timeval t_stop_rd;
  205. gettimeofday(&t_stop_rd, NULL);
  206. 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);
  207. printf(" | %6.2f | %5.2f | %6.2f | %5.2f\n",
  208. time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
  209. time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
  210. check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
  211. free(buffer);
  212. }
  213. static void read_write_test(sdmmc_card_t* card)
  214. {
  215. sdmmc_card_print_info(stdout, card);
  216. printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
  217. do_single_write_read_test(card, 0, 1, 4);
  218. do_single_write_read_test(card, 0, 4, 4);
  219. do_single_write_read_test(card, 1, 16, 4);
  220. do_single_write_read_test(card, 16, 32, 4);
  221. do_single_write_read_test(card, 48, 64, 4);
  222. do_single_write_read_test(card, 128, 128, 4);
  223. do_single_write_read_test(card, card->csd.capacity - 64, 32, 4);
  224. do_single_write_read_test(card, card->csd.capacity - 64, 64, 4);
  225. do_single_write_read_test(card, card->csd.capacity - 8, 1, 4);
  226. do_single_write_read_test(card, card->csd.capacity/2, 1, 4);
  227. do_single_write_read_test(card, card->csd.capacity/2, 4, 4);
  228. do_single_write_read_test(card, card->csd.capacity/2, 8, 4);
  229. do_single_write_read_test(card, card->csd.capacity/2, 16, 4);
  230. do_single_write_read_test(card, card->csd.capacity/2, 32, 4);
  231. do_single_write_read_test(card, card->csd.capacity/2, 64, 4);
  232. do_single_write_read_test(card, card->csd.capacity/2, 128, 4);
  233. do_single_write_read_test(card, card->csd.capacity/2, 1, 1);
  234. do_single_write_read_test(card, card->csd.capacity/2, 8, 1);
  235. do_single_write_read_test(card, card->csd.capacity/2, 128, 1);
  236. }
  237. void test_sd_rw_blocks(int slot, int width)
  238. {
  239. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  240. config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  241. config.slot = slot;
  242. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  243. if (width != 0) {
  244. slot_config.width = width;
  245. }
  246. if (slot_config.width == 8) {
  247. config.flags &= ~SDMMC_HOST_FLAG_DDR;
  248. }
  249. TEST_ESP_OK(sdmmc_host_init());
  250. TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
  251. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  252. TEST_ASSERT_NOT_NULL(card);
  253. TEST_ESP_OK(sdmmc_card_init(&config, card));
  254. read_write_test(card);
  255. free(card);
  256. TEST_ESP_OK(sdmmc_host_deinit());
  257. }
  258. TEST_CASE("SDMMC read/write test (SD slot 1)", "[sd][test_env=UT_T1_SDMODE]")
  259. {
  260. sd_test_board_power_on();
  261. test_sd_rw_blocks(1, 4);
  262. sd_test_board_power_off();
  263. }
  264. #ifdef WITH_EMMC_TEST
  265. TEST_CASE("SDMMC read/write test (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC]")
  266. {
  267. sd_test_board_power_on();
  268. test_sd_rw_blocks(0, 4);
  269. sd_test_board_power_off();
  270. }
  271. TEST_CASE("SDMMC read/write test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC]")
  272. {
  273. sd_test_board_power_on();
  274. test_sd_rw_blocks(0, 8);
  275. sd_test_board_power_off();
  276. }
  277. #endif // WITH_EMMC_TEST
  278. TEST_CASE("SDMMC read/write test (SD slot 1, in SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
  279. {
  280. sd_test_board_power_on();
  281. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  282. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  283. TEST_ESP_OK(sdspi_host_init());
  284. TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
  285. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  286. TEST_ASSERT_NOT_NULL(card);
  287. TEST_ESP_OK(sdmmc_card_init(&config, card));
  288. read_write_test(card);
  289. free(card);
  290. TEST_ESP_OK(sdspi_host_deinit());
  291. sd_test_board_power_off();
  292. }
  293. TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE]")
  294. {
  295. sd_test_board_power_on();
  296. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  297. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  298. TEST_ESP_OK(sdmmc_host_init());
  299. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  300. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  301. TEST_ASSERT_NOT_NULL(card);
  302. TEST_ESP_OK(sdmmc_card_init(&config, card));
  303. const size_t buffer_size = 4096;
  304. const size_t block_count = buffer_size / 512;
  305. const size_t extra = 4;
  306. uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
  307. // Check read behavior: do aligned write, then unaligned read
  308. const uint32_t seed = 0x89abcdef;
  309. fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  310. TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
  311. memset(buffer, 0xcc, buffer_size + extra);
  312. TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
  313. check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  314. // Check write behavior: do unaligned write, then aligned read
  315. fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
  316. TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
  317. memset(buffer, 0xcc, buffer_size + extra);
  318. TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
  319. check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
  320. free(buffer);
  321. free(card);
  322. TEST_ESP_OK(sdmmc_host_deinit());
  323. sd_test_board_power_off();
  324. }
  325. static void test_cd_input(int gpio_cd_num, const sdmmc_host_t* config)
  326. {
  327. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  328. TEST_ASSERT_NOT_NULL(card);
  329. // SDMMC host should have configured CD as input.
  330. // Enable output as well (not using the driver, to avoid touching input
  331. // enable bits).
  332. gpio_matrix_out(gpio_cd_num, SIG_GPIO_OUT_IDX, false, false);
  333. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_cd_num));
  334. // Check that card initialization fails if CD is high
  335. REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_cd_num));
  336. usleep(1000);
  337. TEST_ESP_ERR(ESP_ERR_NOT_FOUND, sdmmc_card_init(config, card));
  338. // Check that card initialization succeeds if CD is low
  339. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_cd_num));
  340. usleep(1000);
  341. TEST_ESP_OK(sdmmc_card_init(config, card));
  342. free(card);
  343. }
  344. TEST_CASE("CD input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  345. {
  346. sd_test_board_power_on();
  347. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  348. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  349. slot_config.gpio_cd = CD_WP_TEST_GPIO;
  350. TEST_ESP_OK(sdmmc_host_init());
  351. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  352. test_cd_input(CD_WP_TEST_GPIO, &config);
  353. TEST_ESP_OK(sdmmc_host_deinit());
  354. sd_test_board_power_off();
  355. }
  356. TEST_CASE("CD input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  357. {
  358. sd_test_board_power_on();
  359. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  360. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  361. slot_config.gpio_cd = CD_WP_TEST_GPIO;
  362. TEST_ESP_OK(sdspi_host_init());
  363. TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
  364. test_cd_input(CD_WP_TEST_GPIO, &config);
  365. TEST_ESP_OK(sdspi_host_deinit());
  366. sd_test_board_power_off();
  367. }
  368. static void test_wp_input(int gpio_wp_num, const sdmmc_host_t* config)
  369. {
  370. sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
  371. TEST_ASSERT_NOT_NULL(card);
  372. // SDMMC host should have configured WP as input.
  373. // Enable output as well (not using the driver, to avoid touching input
  374. // enable bits).
  375. gpio_matrix_out(gpio_wp_num, SIG_GPIO_OUT_IDX, false, false);
  376. REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_wp_num));
  377. // Check that the card can be initialized with WP low
  378. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
  379. TEST_ESP_OK(sdmmc_card_init(config, card));
  380. uint32_t* data = heap_caps_calloc(1, 512, MALLOC_CAP_DMA);
  381. // Check that card write succeeds if WP is high
  382. REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_wp_num));
  383. usleep(1000);
  384. TEST_ESP_OK(sdmmc_write_sectors(card, &data, 0, 1));
  385. // Check that write fails if WP is low
  386. REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
  387. usleep(1000);
  388. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_write_sectors(card, &data, 0, 1));
  389. // ...but reads still work
  390. TEST_ESP_OK(sdmmc_read_sectors(card, &data, 0, 1));
  391. free(data);
  392. free(card);
  393. }
  394. TEST_CASE("WP input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
  395. {
  396. sd_test_board_power_on();
  397. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  398. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  399. slot_config.gpio_wp = CD_WP_TEST_GPIO;
  400. TEST_ESP_OK(sdmmc_host_init());
  401. TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
  402. test_wp_input(CD_WP_TEST_GPIO, &config);
  403. TEST_ESP_OK(sdmmc_host_deinit());
  404. sd_test_board_power_off();
  405. }
  406. TEST_CASE("WP input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
  407. {
  408. sd_test_board_power_on();
  409. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  410. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  411. slot_config.gpio_wp = CD_WP_TEST_GPIO;
  412. TEST_ESP_OK(sdspi_host_init());
  413. TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
  414. test_wp_input(CD_WP_TEST_GPIO, &config);
  415. TEST_ESP_OK(sdspi_host_deinit());
  416. sd_test_board_power_off();
  417. }