]> granicus.if.org Git - esp-idf/blob - components/sdmmc/test/test_sd.c
1808a0cdd0560b2997348a60d8a81fca243bf1dc
[esp-idf] / components / sdmmc / test / test_sd.c
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
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "unity.h"
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 "soc/gpio_reg.h"
24 #include "sdmmc_cmd.h"
25 #include "esp_log.h"
26 #include "esp_heap_caps.h"
27 #include <time.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 TEST_CASE("MMC_RSP_BITS", "[sd]")
32 {
33     uint32_t data[2] = { 0x01234567, 0x89abcdef };
34     TEST_ASSERT_EQUAL_HEX32(0x7,   MMC_RSP_BITS(data, 0, 4));
35     TEST_ASSERT_EQUAL_HEX32(0x567, MMC_RSP_BITS(data, 0, 12));
36     TEST_ASSERT_EQUAL_HEX32(0xf0,  MMC_RSP_BITS(data, 28, 8));
37     TEST_ASSERT_EQUAL_HEX32(0x3,   MMC_RSP_BITS(data, 1, 3));
38     TEST_ASSERT_EQUAL_HEX32(0x11,  MMC_RSP_BITS(data, 59, 5));
39 }
40
41 TEST_CASE("can probe SD (4-bit)", "[sd][test_env=UT_T1_SDMODE]")
42 {
43     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
44     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
45     TEST_ESP_OK(sdmmc_host_init());
46     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
47     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
48     TEST_ASSERT_NOT_NULL(card);
49     TEST_ESP_OK(sdmmc_card_init(&config, card));
50     sdmmc_card_print_info(stdout, card);
51     TEST_ESP_OK(sdmmc_host_deinit());
52     free(card);
53 }
54
55 TEST_CASE("can probe SD (1-bit)", "[sd][test_env=UT_T1_SDMODE]")
56 {
57     //the card DAT3 should be connected to high in SD 1-bit mode
58     //do it by our own GPIO.
59     gpio_config_t conf = {
60         .pin_bit_mask = GPIO_SEL_13,
61         .mode = GPIO_MODE_OUTPUT,
62         .pull_up_en = 1,
63         .pull_down_en = 0,
64         .intr_type = GPIO_INTR_DISABLE,
65     };
66     gpio_config(&conf);
67     gpio_set_level(GPIO_NUM_13, 1);
68
69     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
70     config.flags = SDMMC_HOST_FLAG_1BIT;
71     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
72     slot_config.width=1;
73     TEST_ESP_OK(sdmmc_host_init());
74     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
75     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
76     TEST_ASSERT_NOT_NULL(card);
77     TEST_ESP_OK(sdmmc_card_init(&config, card));
78     sdmmc_card_print_info(stdout, card);
79     TEST_ESP_OK(sdmmc_host_deinit());
80     free(card);
81 }
82
83
84
85 TEST_CASE("can probe SD(using SPI)", "[sdspi][test_env=UT_T1_SPIMODE]")
86 {
87     sdmmc_host_t config = SDSPI_HOST_DEFAULT();
88     sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
89     TEST_ESP_OK(sdspi_host_init());
90     TEST_ESP_OK(sdspi_host_init_slot(config.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     TEST_ESP_OK(sdspi_host_deinit());
96     free(card);
97 }
98
99 // Fill buffer pointed to by 'dst' with 'count' 32-bit ints generated
100 // from 'rand' with the starting value of 'seed'
101 static void fill_buffer(uint32_t seed, uint8_t* dst, size_t count) {
102     srand(seed);
103     for (size_t i = 0; i < count; ++i) {
104         uint32_t val = rand();
105         memcpy(dst + i * sizeof(uint32_t), &val, sizeof(val));
106     }
107 }
108
109 // Check if the buffer pointed to by 'dst' contains 'count' 32-bit
110 // ints generated from 'rand' with the starting value of 'seed'
111 static void check_buffer(uint32_t seed, const uint8_t* src, size_t count) {
112     srand(seed);
113     for (size_t i = 0; i < count; ++i) {
114         uint32_t val;
115         memcpy(&val, src + i * sizeof(uint32_t), sizeof(val));
116         TEST_ASSERT_EQUAL_HEX32(rand(), val);
117     }
118 }
119
120 static void do_single_write_read_test(sdmmc_card_t* card,
121         size_t start_block, size_t block_count, size_t alignment)
122 {
123     size_t block_size = card->csd.sector_size;
124     size_t total_size = block_size * block_count;
125     printf(" %8d |  %3d  |   %d   |    %4.1f  ", start_block, block_count, alignment, total_size / 1024.0f);
126
127     uint32_t* buffer = heap_caps_malloc(total_size + 4, MALLOC_CAP_DMA);
128     size_t offset = alignment % 4;
129     uint8_t* c_buffer = (uint8_t*) buffer + offset;
130     fill_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
131
132     struct timeval t_start_wr;
133     gettimeofday(&t_start_wr, NULL);
134     TEST_ESP_OK(sdmmc_write_sectors(card, c_buffer, start_block, block_count));
135     struct timeval t_stop_wr;
136     gettimeofday(&t_stop_wr, NULL);
137     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);
138
139     memset(buffer, 0xbb, total_size + 4);
140
141     struct timeval t_start_rd;
142     gettimeofday(&t_start_rd, NULL);
143     TEST_ESP_OK(sdmmc_read_sectors(card, c_buffer, start_block, block_count));
144     struct timeval t_stop_rd;
145     gettimeofday(&t_stop_rd, NULL);
146     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);
147
148     printf(" |   %6.2f    |      %5.2f      |    %6.2f     |     %5.2f\n",
149             time_wr, total_size / (time_wr / 1000) / (1024 * 1024),
150             time_rd, total_size / (time_rd / 1000) / (1024 * 1024));
151     check_buffer(start_block, c_buffer, total_size / sizeof(buffer[0]));
152     free(buffer);
153 }
154
155 static void read_write_test(sdmmc_card_t* card)
156 {
157     sdmmc_card_print_info(stdout, card);
158     printf("  sector  | count | align | size(kB)  | wr_time(ms) | wr_speed(MB/s)  |  rd_time(ms)  | rd_speed(MB/s)\n");
159     do_single_write_read_test(card, 0, 1, 4);
160     do_single_write_read_test(card, 0, 4, 4);
161     do_single_write_read_test(card, 1, 16, 4);
162     do_single_write_read_test(card, 16, 32, 4);
163     do_single_write_read_test(card, 48, 64, 4);
164     do_single_write_read_test(card, 128, 128, 4);
165     do_single_write_read_test(card, card->csd.capacity - 64, 32, 4);
166     do_single_write_read_test(card, card->csd.capacity - 64, 64, 4);
167     do_single_write_read_test(card, card->csd.capacity - 8, 1, 4);
168     do_single_write_read_test(card, card->csd.capacity/2, 1, 4);
169     do_single_write_read_test(card, card->csd.capacity/2, 4, 4);
170     do_single_write_read_test(card, card->csd.capacity/2, 8, 4);
171     do_single_write_read_test(card, card->csd.capacity/2, 16, 4);
172     do_single_write_read_test(card, card->csd.capacity/2, 32, 4);
173     do_single_write_read_test(card, card->csd.capacity/2, 64, 4);
174     do_single_write_read_test(card, card->csd.capacity/2, 128, 4);
175     do_single_write_read_test(card, card->csd.capacity/2, 1, 1);
176     do_single_write_read_test(card, card->csd.capacity/2, 8, 1);
177     do_single_write_read_test(card, card->csd.capacity/2, 128, 1);
178 }
179
180 TEST_CASE("can write and read back blocks", "[sd][test_env=UT_T1_SDMODE]")
181 {
182     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
183     config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
184     TEST_ESP_OK(sdmmc_host_init());
185     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
186     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
187     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
188     TEST_ASSERT_NOT_NULL(card);
189     TEST_ESP_OK(sdmmc_card_init(&config, card));
190     read_write_test(card);
191     free(card);
192     TEST_ESP_OK(sdmmc_host_deinit());
193 }
194
195 TEST_CASE("can write and read back blocks(using SPI)", "[sdspi][test_env=UT_T1_SPIMODE]")
196 {
197     sdmmc_host_t config = SDSPI_HOST_DEFAULT();
198     sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
199     TEST_ESP_OK(sdspi_host_init());
200     TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
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     read_write_test(card);
205     free(card);
206     TEST_ESP_OK(sdspi_host_deinit());
207 }
208
209 TEST_CASE("reads and writes with an unaligned buffer", "[sd][test_env=UT_T1_SDMODE]")
210 {
211     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
212     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
213     TEST_ESP_OK(sdmmc_host_init());
214
215     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
216     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
217     TEST_ASSERT_NOT_NULL(card);
218     TEST_ESP_OK(sdmmc_card_init(&config, card));
219
220     const size_t buffer_size = 4096;
221     const size_t block_count = buffer_size / 512;
222     const size_t extra = 4;
223     uint8_t* buffer = heap_caps_malloc(buffer_size + extra, MALLOC_CAP_DMA);
224
225     // Check read behavior: do aligned write, then unaligned read
226     const uint32_t seed = 0x89abcdef;
227     fill_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
228     TEST_ESP_OK(sdmmc_write_sectors(card, buffer, 0, block_count));
229     memset(buffer, 0xcc, buffer_size + extra);
230     TEST_ESP_OK(sdmmc_read_sectors(card, buffer + 1, 0, block_count));
231     check_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
232
233     // Check write behavior: do unaligned write, then aligned read
234     fill_buffer(seed, buffer + 1, buffer_size / sizeof(uint32_t));
235     TEST_ESP_OK(sdmmc_write_sectors(card, buffer + 1, 8, block_count));
236     memset(buffer, 0xcc, buffer_size + extra);
237     TEST_ESP_OK(sdmmc_read_sectors(card, buffer, 8, block_count));
238     check_buffer(seed, buffer, buffer_size / sizeof(uint32_t));
239
240     free(buffer);
241     free(card);
242     TEST_ESP_OK(sdmmc_host_deinit());
243 }
244
245 static void test_cd_input(int gpio_cd_num, const sdmmc_host_t* config)
246 {
247     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
248     TEST_ASSERT_NOT_NULL(card);
249
250     // SDMMC host should have configured CD as input.
251     // Enable output as well (not using the driver, to avoid touching input
252     // enable bits).
253     gpio_matrix_out(gpio_cd_num, SIG_GPIO_OUT_IDX, false, false);
254     REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_cd_num));
255
256     // Check that card initialization fails if CD is high
257     REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_cd_num));
258     usleep(100);
259     TEST_ESP_ERR(ESP_ERR_NOT_FOUND, sdmmc_card_init(config, card));
260
261     // Check that card initialization succeeds if CD is low
262     REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_cd_num));
263     usleep(100);
264     TEST_ESP_OK(sdmmc_card_init(config, card));
265
266     free(card);
267 }
268
269 TEST_CASE("CD input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
270 {
271     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
272     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
273     const int gpio_cd_num = 5;
274     slot_config.gpio_cd = gpio_cd_num;
275     TEST_ESP_OK(sdmmc_host_init());
276     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
277
278     test_cd_input(gpio_cd_num, &config);
279
280     TEST_ESP_OK(sdmmc_host_deinit());
281 }
282
283 TEST_CASE("CD input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
284 {
285     sdmmc_host_t config = SDSPI_HOST_DEFAULT();
286     sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
287     const int gpio_cd_num = 5;
288     slot_config.gpio_cd = gpio_cd_num;
289     TEST_ESP_OK(sdspi_host_init());
290     TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
291
292     test_cd_input(gpio_cd_num, &config);
293
294     TEST_ESP_OK(sdspi_host_deinit());
295 }
296
297 static void test_wp_input(int gpio_wp_num, const sdmmc_host_t* config)
298 {
299     sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
300     TEST_ASSERT_NOT_NULL(card);
301
302     // SDMMC host should have configured WP as input.
303     // Enable output as well (not using the driver, to avoid touching input
304     // enable bits).
305     gpio_matrix_out(gpio_wp_num, SIG_GPIO_OUT_IDX, false, false);
306     REG_WRITE(GPIO_ENABLE_W1TS_REG, BIT(gpio_wp_num));
307
308     // Check that the card can be initialized with WP low
309     REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
310     TEST_ESP_OK(sdmmc_card_init(config, card));
311
312     uint32_t* data = heap_caps_calloc(1, 512, MALLOC_CAP_DMA);
313
314     // Check that card write succeeds if WP is high
315     REG_WRITE(GPIO_OUT_W1TS_REG, BIT(gpio_wp_num));
316     usleep(100);
317     TEST_ESP_OK(sdmmc_write_sectors(card, &data, 0, 1));
318
319     // Check that write fails if WP is low
320     REG_WRITE(GPIO_OUT_W1TC_REG, BIT(gpio_wp_num));
321     usleep(100);
322     TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_write_sectors(card, &data, 0, 1));
323     // ...but reads still work
324     TEST_ESP_OK(sdmmc_read_sectors(card, &data, 0, 1));
325
326     free(data);
327     free(card);
328 }
329
330 TEST_CASE("WP input works in SD mode", "[sd][test_env=UT_T1_SDMODE]")
331 {
332     sdmmc_host_t config = SDMMC_HOST_DEFAULT();
333     sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
334     const int gpio_wp_num = 5;
335     slot_config.gpio_wp = gpio_wp_num;
336     TEST_ESP_OK(sdmmc_host_init());
337     TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
338
339     test_wp_input(gpio_wp_num, &config);
340
341     TEST_ESP_OK(sdmmc_host_deinit());
342 }
343
344 TEST_CASE("WP input works in SPI mode", "[sd][test_env=UT_T1_SPIMODE]")
345 {
346     sdmmc_host_t config = SDSPI_HOST_DEFAULT();
347     sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
348     const int gpio_wp_num = 5;
349     slot_config.gpio_wp = gpio_wp_num;
350     TEST_ESP_OK(sdspi_host_init());
351     TEST_ESP_OK(sdspi_host_init_slot(config.slot, &slot_config));
352
353     test_wp_input(gpio_wp_num, &config);
354
355     TEST_ESP_OK(sdspi_host_deinit());
356 }