test_sd.c 17 KB

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