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"
29 TEST_CASE("MMC_RSP_BITS", "[sd]")
31 uint32_t data[2] = { 0x01234567, 0x89abcdef };
32 TEST_ASSERT_EQUAL_HEX32(0x7, MMC_RSP_BITS(data, 0, 4));
33 TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
34 TEST_ASSERT_EQUAL_HEX32(0xf0, MMC_RSP_BITS(data, 28, 8));
35 TEST_ASSERT_EQUAL_HEX32(0x3, MMC_RSP_BITS(data, 1, 3));
36 TEST_ASSERT_EQUAL_HEX32(0x11, MMC_RSP_BITS(data, 59, 5));
39 TEST_CASE("can probe SD (4-bit)", "[sd][test_env=UT_T1_SDMODE]")
41 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
42 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
43 TEST_ESP_OK(sdmmc_host_init());
44 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
45 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
46 TEST_ASSERT_NOT_NULL(card);
47 TEST_ESP_OK(sdmmc_card_init(&config, card));
48 sdmmc_card_print_info(stdout, card);
49 TEST_ESP_OK(sdmmc_host_deinit());
53 TEST_CASE("can probe SD (1-bit)", "[sd][test_env=UT_T1_SDMODE]")
55 //the card DAT3 should be connected to high in SD 1-bit mode
56 //do it by our own GPIO.
57 gpio_config_t conf = {
58 .pin_bit_mask = GPIO_SEL_13,
59 .mode = GPIO_MODE_OUTPUT,
62 .intr_type = GPIO_INTR_DISABLE,
65 gpio_set_level(GPIO_NUM_13, 1);
67 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
68 config.flags = SDMMC_HOST_FLAG_1BIT;
69 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
71 TEST_ESP_OK(sdmmc_host_init());
72 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
73 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
74 TEST_ASSERT_NOT_NULL(card);
75 TEST_ESP_OK(sdmmc_card_init(&config, card));
76 sdmmc_card_print_info(stdout, card);
77 TEST_ESP_OK(sdmmc_host_deinit());
83 TEST_CASE("can probe SD(using SPI)", "[sdspi][test_env=UT_T1_SPIMODE]")
85 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
86 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
87 TEST_ESP_OK(sdspi_host_init());
88 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
89 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
90 TEST_ASSERT_NOT_NULL(card);
91 TEST_ESP_OK(sdmmc_card_init(&config, card));
92 sdmmc_card_print_info(stdout, card);
93 TEST_ESP_OK(sdspi_host_deinit());
97 // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
98 // from 'rand' with the starting value of 'seed'
99 static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
101 for (size_t i = 0; i < count; ++i) {
102 uint32_t val = rand();
103 memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
107 // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
108 // ints generated from 'rand' with the starting value of 'seed'
109 static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
111 for (size_t i = 0; i < count; ++i) {
113 memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
114 TEST_ASSERT_EQUAL_HEX32(rand(), val);
118 static void do_single_write_read_test(sdmmc_card_t* card,
119 size_t start_block, size_t block_count, size_t alignment)
121 size_t block_size = card->csd.sector_size;
122 size_t total_size = block_size * block_count;
123 printf(" %8d | %3d | %d | %4.1f ", start_block, block_count, alignment, total_size / 1024.0f);
125 uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
126 size_t offset = alignment % 4;
127 uint8_t* c_buffer = (uint8_t*) buffer + offset;
128 fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
130 struct timeval t_start_wr;
131 gettimeofday(&t_start_wr, NULL);
132 TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
133 struct timeval t_stop_wr;
134 gettimeofday(&t_stop_wr, NULL);
135 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);
137 memset(buffer, 0xbb, total_size + 4);
139 struct timeval t_start_rd;
140 gettimeofday(&t_start_rd, NULL);
141 TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
142 struct timeval t_stop_rd;
143 gettimeofday(&t_stop_rd, NULL);
144 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);
146 printf(" | %6.2f | %5.2f | %6.2f | %5.2f\n",
147 time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
148 time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
149 check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
153 static void read_write_test(sdmmc_card_t* card)
155 sdmmc_card_print_info(stdout, card);
156 printf(" sector | count | align | size(kB) | wr_time(ms) | wr_speed(MB/s) | rd_time(ms) | rd_speed(MB/s)\n");
157 do_single_write_read_test(card, 0, 1, 4);
158 do_single_write_read_test(card, 0, 4, 4);
159 do_single_write_read_test(card, 1, 16, 4);
160 do_single_write_read_test(card, 16, 32, 4);
161 do_single_write_read_test(card, 48, 64, 4);
162 do_single_write_read_test(card, 128, 128, 4);
163 do_single_write_read_test(card, card->csd.capacity - 64, 32, 4);
164 do_single_write_read_test(card, card->csd.capacity - 64, 64, 4);
165 do_single_write_read_test(card, card->csd.capacity - 8, 1, 4);
166 do_single_write_read_test(card, card->csd.capacity/2, 1, 4);
167 do_single_write_read_test(card, card->csd.capacity/2, 4, 4);
168 do_single_write_read_test(card, card->csd.capacity/2, 8, 4);
169 do_single_write_read_test(card, card->csd.capacity/2, 16, 4);
170 do_single_write_read_test(card, card->csd.capacity/2, 32, 4);
171 do_single_write_read_test(card, card->csd.capacity/2, 64, 4);
172 do_single_write_read_test(card, card->csd.capacity/2, 128, 4);
173 do_single_write_read_test(card, card->csd.capacity/2, 1, 1);
174 do_single_write_read_test(card, card->csd.capacity/2, 8, 1);
175 do_single_write_read_test(card, card->csd.capacity/2, 128, 1);
178 TEST_CASE("can write and read back blocks", "[sd][test_env=UT_T1_SDMODE][ignore]")
180 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
181 config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
182 TEST_ESP_OK(sdmmc_host_init());
183 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
184 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
185 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
186 TEST_ASSERT_NOT_NULL(card);
187 TEST_ESP_OK(sdmmc_card_init(&config, card));
188 read_write_test(card);
190 TEST_ESP_OK(sdmmc_host_deinit());
193 TEST_CASE("can write and read back blocks(using SPI)", "[sdspi][test_env=UT_T1_SPIMODE][ignore]")
195 sdmmc_host_t config = SDSPI_HOST_DEFAULT();
196 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
197 TEST_ESP_OK(sdspi_host_init());
198 TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
199 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
200 TEST_ASSERT_NOT_NULL(card);
201 TEST_ESP_OK(sdmmc_card_init(&config, card));
202 read_write_test(card);
204 TEST_ESP_OK(sdspi_host_deinit());
207 TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE][ignore]")
209 sdmmc_host_t config = SDMMC_HOST_DEFAULT();
210 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
211 TEST_ESP_OK(sdmmc_host_init());
213 TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
214 sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
215 TEST_ASSERT_NOT_NULL(card);
216 TEST_ESP_OK(sdmmc_card_init(&config, card));
218 const size_t buffer_size = 4096;
219 const size_t block_count = buffer_size / 512;
220 const size_t extra = 4;
221 uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
223 // Check read behavior: do aligned write, then unaligned read
224 const uint32_t seed = 0x89abcdef;
225 fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
226 TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
227 memset(buffer, 0xcc, buffer_size + extra);
228 TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
229 check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
231 // Check write behavior: do unaligned write, then aligned read
232 fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
233 TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
234 memset(buffer, 0xcc, buffer_size + extra);
235 TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
236 check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
240 TEST_ESP_OK(sdmmc_host_deinit());