1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "driver/gpio.h"
20 #include "driver/sdmmc_host.h"
21 #include "driver/sdspi_host.h"
22 #include "driver/sdmmc_defs.h"
23 #include "sdmmc_cmd.h"
25 #include "esp_heap_caps.h"
30 // Can't test eMMC (slot 0) and PSRAM together
31 #ifndef CONFIG_ESP32_SPIRAM_SUPPORT
32 #define WITH_EMMC_TEST
35 /* power supply enable pin */
36 #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
43 /* time to wait for reset / power-on */
44 #define SD_TEST_BOARD_PWR_RST_DELAY_MS 5
45 #define SD_TEST_BOARD_PWR_ON_DELAY_MS 50
47 /* gpio which is not connected to actual CD pin, used to simulate CD behavior */
48 #define CD_WP_TEST_GPIO 18
51 static void sd_test_board_power_on(void)
53 gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_OUTPUT);
54 gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, SD_TEST_BOARD_VSEL_3V3);
55 gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_OUTPUT);
56 gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
57 usleep(SD_TEST_BOARD_PWR_RST_DELAY_MS * 1000);
58 gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 1);
59 usleep(SD_TEST_BOARD_PWR_ON_DELAY_MS * 1000);
62 static void sd_test_board_power_off(void)
64 gpio_set_level(SD_TEST_BOARD_VSEL_EN_GPIO, 0);
65 gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_INPUT);
66 gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, 0);
67 gpio_set_direction(SD_TEST_BOARD_VSEL_EN_GPIO, GPIO_MODE_INPUT);
71 TEST_CASE("MMC_RSP_BITS", "[sd]")
73 uint32_t data[2] = { 0x01234567, 0x89abcdef };
74 TEST_ASSERT_EQUAL_HEX32(0x7, MMC_RSP_BITS(data, 0, 4));
75 TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
76 TEST_ASSERT_EQUAL_HEX32(0xf0, MMC_RSP_BITS(data, 28, 8));
77 TEST_ASSERT_EQUAL_HEX32(0x3, MMC_RSP_BITS(data, 1, 3));
78 TEST_ASSERT_EQUAL_HEX32(0x11, MMC_RSP_BITS(data, 59, 5));
81 static void probe_sd(int slot, int width, int freq_khz, int ddr)
83 sd_test_board_power_on();
84 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
86 config.max_freq_khz = freq_khz;
87 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
89 config.flags = SDMMC_HOST_FLAG_1BIT;
90 slot_config.width = 1;
91 } else if (width == 4) {
92 config.flags &= ~SDMMC_HOST_FLAG_8BIT;
93 slot_config.width = 4;
95 assert(!ddr && "host driver does not support 8-line DDR mode yet");
98 config.flags &= ~SDMMC_HOST_FLAG_DDR;
100 TEST_ESP_OK(sdmmc_host_init());
101 TEST_ESP_OK(sdmmc_host_init_slot(slot, &slot_config));
102 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
103 TEST_ASSERT_NOT_NULL(card);
104 TEST_ESP_OK(sdmmc_card_init(&config, card));
105 sdmmc_card_print_info(stdout, card);
106 uint8_t* buffer = heap_caps_malloc(512, MALLOC_CAP_DMA);
107 TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 0, 1));
109 TEST_ESP_OK(sdmmc_host_deinit());
111 sd_test_board_power_off();
114 static void probe_spi(int freq_khz, int pin_miso, int pin_mosi, int pin_sck, int pin_cs)
116 sd_test_board_power_on();
117 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
118 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
119 slot_config.gpio_miso = pin_miso;
120 slot_config.gpio_mosi = pin_mosi;
121 slot_config.gpio_sck = pin_sck;
122 slot_config.gpio_cs = pin_cs;
124 TEST_ESP_OK(sdspi_host_init());
125 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
126 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
127 TEST_ASSERT_NOT_NULL(card);
128 TEST_ESP_OK(sdmmc_card_init(&config, card));
129 sdmmc_card_print_info(stdout, card);
130 TEST_ESP_OK(sdspi_host_deinit());
132 sd_test_board_power_off();
136 TEST_CASE("probe SD, slot 1, 4-bit", "[sd][test_env=UT_T1_SDMODE]")
138 probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_PROBING, 0);
139 probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_DEFAULT, 0);
140 probe_sd(SDMMC_HOST_SLOT_1, 4, SDMMC_FREQ_HIGHSPEED, 0);
143 TEST_CASE("probe SD, slot 1, 1-bit", "[sd][test_env=UT_T1_SDMODE]")
145 probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_PROBING, 0);
146 probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_DEFAULT, 0);
147 probe_sd(SDMMC_HOST_SLOT_1, 1, SDMMC_FREQ_HIGHSPEED, 0);
150 #ifdef WITH_EMMC_TEST
151 TEST_CASE("probe eMMC, slot 0, 4-bit, DDR", "[sd][test_env=EMMC]")
153 probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 1);
156 TEST_CASE("probe eMMC, slot 0, 8-bit", "[sd][test_env=EMMC]")
158 probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_PROBING, 0);
159 probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_DEFAULT, 0);
160 probe_sd(SDMMC_HOST_SLOT_0, 8, SDMMC_FREQ_HIGHSPEED, 0);
162 #endif // WITH_EMMC_TEST
164 TEST_CASE("probe SD, slot 0, 4-bit", "[sd][test_env=UT_T1_SDCARD][ignore]")
166 probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_PROBING, 0);
167 probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_DEFAULT, 0);
168 probe_sd(SDMMC_HOST_SLOT_0, 4, SDMMC_FREQ_HIGHSPEED, 0);
171 TEST_CASE("probe SD, slot 0, 1-bit", "[sd][test_env=UT_T1_SDCARD][ignore]")
173 probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_PROBING, 0);
174 probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_DEFAULT, 0);
175 probe_sd(SDMMC_HOST_SLOT_0, 1, SDMMC_FREQ_HIGHSPEED, 0);
178 TEST_CASE("probe SD in SPI mode, slot 1", "[sd][test_env=UT_T1_SPIMODE]")
180 probe_spi(SDMMC_FREQ_DEFAULT, 2, 15, 14, 13);
183 TEST_CASE("probe SD in SPI mode, slot 0", "[sd][test_env=UT_T1_SDCARD][ignore]")
185 probe_spi(SDMMC_FREQ_DEFAULT, 7, 11, 6, 10);
189 // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
190 // from 'rand' with the starting value of 'seed'
191 static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
193 for (size_t i = 0; i < count; ++i) {
194 uint32_t val = rand();
195 memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
199 // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
200 // ints generated from 'rand' with the starting value of 'seed'
201 static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
203 for (size_t i = 0; i < count; ++i) {
205 memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
206 TEST_ASSERT_EQUAL_HEX32(rand(), val);
210 static void do_single_write_read_test(sdmmc_card_t* card,
211 size_t start_block, size_t block_count, size_t alignment)
213 size_t block_size = card->csd.sector_size;
214 size_t total_size = block_size * block_count;
215 printf(" %8d | %3d | %d | %4.1f ", start_block, block_count, alignment, total_size / 1024.0f);
217 uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
218 size_t offset = alignment % 4;
219 uint8_t* c_buffer = (uint8_t*) buffer + offset;
220 fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
222 struct timeval t_start_wr;
223 gettimeofday(&t_start_wr, NULL);
224 TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
225 struct timeval t_stop_wr;
226 gettimeofday(&t_stop_wr, NULL);
227 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);
229 memset(buffer, 0xbb, total_size + 4);
231 struct timeval t_start_rd;
232 gettimeofday(&t_start_rd, NULL);
233 TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
234 struct timeval t_stop_rd;
235 gettimeofday(&t_stop_rd, NULL);
236 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);
238 printf(" | %6.2f | %5.2f | %6.2f | %5.2f\n",
239 time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
240 time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
241 check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
245 static void read_write_test(sdmmc_card_t* card)
247 sdmmc_card_print_info(stdout, card);
248 printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
249 do_single_write_read_test(card, 0, 1, 4);
250 do_single_write_read_test(card, 0, 4, 4);
251 do_single_write_read_test(card, 1, 16, 4);
252 do_single_write_read_test(card, 16, 32, 4);
253 do_single_write_read_test(card, 48, 64, 4);
254 do_single_write_read_test(card, 128, 128, 4);
255 do_single_write_read_test(card, card->csd.capacity - 64, 32, 4);
256 do_single_write_read_test(card, card->csd.capacity - 64, 64, 4);
257 do_single_write_read_test(card, card->csd.capacity - 8, 1, 4);
258 do_single_write_read_test(card, card->csd.capacity/2, 1, 4);
259 do_single_write_read_test(card, card->csd.capacity/2, 4, 4);
260 do_single_write_read_test(card, card->csd.capacity/2, 8, 4);
261 do_single_write_read_test(card, card->csd.capacity/2, 16, 4);
262 do_single_write_read_test(card, card->csd.capacity/2, 32, 4);
263 do_single_write_read_test(card, card->csd.capacity/2, 64, 4);
264 do_single_write_read_test(card, card->csd.capacity/2, 128, 4);
265 do_single_write_read_test(card, card->csd.capacity/2, 1, 1);
266 do_single_write_read_test(card, card->csd.capacity/2, 8, 1);
267 do_single_write_read_test(card, card->csd.capacity/2, 128, 1);
270 void test_sd_rw_blocks(int slot, int width)
272 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
273 config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
275 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
277 slot_config.width = width;
279 if (slot_config.width == 8) {
280 config.flags &= ~SDMMC_HOST_FLAG_DDR;
282 TEST_ESP_OK(sdmmc_host_init());
283 TEST_ESP_OK(sdmmc_host_init_slot(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);
289 TEST_ESP_OK(sdmmc_host_deinit());
292 TEST_CASE("SDMMC read/write test (SD slot 1)", "[sd][test_env=UT_T1_SDMODE]")
294 sd_test_board_power_on();
295 test_sd_rw_blocks(1, 4);
296 sd_test_board_power_off();
299 #ifdef WITH_EMMC_TEST
300 TEST_CASE("SDMMC read/write test (eMMC slot 0, 4 line DDR)", "[sd][test_env=EMMC]")
302 sd_test_board_power_on();
303 test_sd_rw_blocks(0, 4);
304 sd_test_board_power_off();
307 TEST_CASE("SDMMC read/write test (eMMC slot 0, 8 line)", "[sd][test_env=EMMC]")
309 sd_test_board_power_on();
310 test_sd_rw_blocks(0, 8);
311 sd_test_board_power_off();
313 #endif // WITH_EMMC_TEST
315 TEST_CASE("SDMMC read/write test (SD slot 1, in SPI mode)", "[sdspi][test_env=UT_T1_SPIMODE]")
317 sd_test_board_power_on();
318 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
319 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
320 TEST_ESP_OK(sdspi_host_init());
321 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
322 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
323 TEST_ASSERT_NOT_NULL(card);
324 TEST_ESP_OK(sdmmc_card_init(&config, card));
325 read_write_test(card);
327 TEST_ESP_OK(sdspi_host_deinit());
328 sd_test_board_power_off();
331 TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE]")
333 sd_test_board_power_on();
334 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
335 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
336 TEST_ESP_OK(sdmmc_host_init());
338 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
339 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
340 TEST_ASSERT_NOT_NULL(card);
341 TEST_ESP_OK(sdmmc_card_init(&config, card));
343 const size_t buffer_size = 4096;
344 const size_t block_count = buffer_size / 512;
345 const size_t extra = 4;
346 uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
348 // Check read behavior: do aligned write, then unaligned read
349 const uint32_t seed = 0x89abcdef;
350 fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
351 TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
352 memset(buffer, 0xcc, buffer_size + extra);
353 TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
354 check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
356 // Check write behavior: do unaligned write, then aligned read
357 fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
358 TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
359 memset(buffer, 0xcc, buffer_size + extra);
360 TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
361 check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
365 TEST_ESP_OK(sdmmc_host_deinit());
366 sd_test_board_power_off();
369 static void test_cd_input(int gpio_cd_num, const sdmmc_host_t* config)
371 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
372 TEST_ASSERT_NOT_NULL(card);
374 // SDMMC host should have configured CD as input.
375 // Enable output as well (not using the driver, to avoid touching input
377 gpio_matrix_out(gpio_cd_num, SIG_GPIO_OUT_IDX, false, false);
378 REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_cd_num));
380 // Check that card initialization fails if CD is high
381 REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_cd_num));
383 TEST_ESP_ERR(ESP_ERR_NOT_FOUND, sdmmc_card_init(config, card));
385 // Check that card initialization succeeds if CD is low
386 REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_cd_num));
388 TEST_ESP_OK(sdmmc_card_init(config, card));
393 TEST_CASE("CD input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
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_cd = 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));
402 test_cd_input(CD_WP_TEST_GPIO, &config);
404 TEST_ESP_OK(sdmmc_host_deinit());
405 sd_test_board_power_off();
408 TEST_CASE("CD input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
410 sd_test_board_power_on();
411 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
412 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
413 slot_config.gpio_cd = CD_WP_TEST_GPIO;
414 TEST_ESP_OK(sdspi_host_init());
415 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
417 test_cd_input(CD_WP_TEST_GPIO, &config);
419 TEST_ESP_OK(sdspi_host_deinit());
420 sd_test_board_power_off();
423 static void test_wp_input(int gpio_wp_num, const sdmmc_host_t* config)
425 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
426 TEST_ASSERT_NOT_NULL(card);
428 // SDMMC host should have configured WP as input.
429 // Enable output as well (not using the driver, to avoid touching input
431 gpio_matrix_out(gpio_wp_num, SIG_GPIO_OUT_IDX, false, false);
432 REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_wp_num));
434 // Check that the card can be initialized with WP low
435 REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
436 TEST_ESP_OK(sdmmc_card_init(config, card));
438 uint32_t* data = heap_caps_calloc(1, 512, MALLOC_CAP_DMA);
440 // Check that card write succeeds if WP is high
441 REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_wp_num));
443 TEST_ESP_OK(sdmmc_write_sectors(card, &data, 0, 1));
445 // Check that write fails if WP is low
446 REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
448 TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_write_sectors(card, &data, 0, 1));
449 // ...but reads still work
450 TEST_ESP_OK(sdmmc_read_sectors(card, &data, 0, 1));
456 TEST_CASE("WP input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
458 sd_test_board_power_on();
459 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
460 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
461 slot_config.gpio_wp = CD_WP_TEST_GPIO;
462 TEST_ESP_OK(sdmmc_host_init());
463 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
465 test_wp_input(CD_WP_TEST_GPIO, &config);
467 TEST_ESP_OK(sdmmc_host_deinit());
468 sd_test_board_power_off();
471 TEST_CASE("WP input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
473 sd_test_board_power_on();
474 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
475 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
476 slot_config.gpio_wp = CD_WP_TEST_GPIO;
477 TEST_ESP_OK(sdspi_host_init());
478 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
480 test_wp_input(CD_WP_TEST_GPIO, &config);
482 TEST_ESP_OK(sdspi_host_deinit());
483 sd_test_board_power_off();