main/task_sdcard.c

changeset 91
255a75322212
parent 88
7f02dbee58d0
child 92
bac0a860f5dd
equal deleted inserted replaced
90:1397fb0b3a9d 91:255a75322212
29 29
30 static const char *TAG = "task_sdcard"; 30 static const char *TAG = "task_sdcard";
31 static sdmmc_card_t* s_card = NULL; 31 static sdmmc_card_t* s_card = NULL;
32 static uint8_t s_pdrv = 0; 32 static uint8_t s_pdrv = 0;
33 static char * s_base_path = NULL; 33 static char * s_base_path = NULL;
34 34 static uint8_t pdrv = FF_DRV_NOT_USED;
35 BYTE pdrv = 0xFF;
36 35
37 36
38 #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT 37 #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT
39 #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin 38 #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin
40 #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin 39 #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin
213 } 212 }
214 } 213 }
215 214
216 215
217 216
217 static esp_err_t my_init_sdspi_host(int slot, const void *slot_config, int *out_slot)
218 {
219 esp_err_t err = sdspi_host_init_device((const sdspi_device_config_t*)slot_config, out_slot);
220 if (err != ESP_OK) {
221 ESP_LOGE(TAG, "Failed to attach sdspi device onto an SPI bus (rc=0x%x), please initialize the \
222 bus first and check the device parameters.", err);
223 }
224 return err;
225 }
226
227
228
218 /** 229 /**
219 * @brief This is a local modified version of the esp_vfs_fat_sdmmc_mount() function in 230 * @brief This is a local modified version of the esp_vfs_fat_sdmmc_mount() function in
220 * the FreeRTOS components library. It is here so we can better handle errors 231 * the FreeRTOS components library. It is here so we can better handle errors
221 * for our application. 232 * for our application.
222 * @param base_path The mount path 233 * @param base_path The mount path
223 * @param host_config SPI host configuration 234 * @param host_config SPI host configuration
224 * @param slot_config Slot configuration 235 * @param slot_config Slot configuration
225 * @return Error condition. 236 * @return Error condition.
226 */ 237 */
227 esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config) 238 esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config, int *card_handle)
228 { 239 {
229 if (s_card != NULL) { 240 if (s_card != NULL) {
230 return ESP_ERR_INVALID_STATE; 241 return ESP_ERR_INVALID_STATE;
231 } 242 }
232 243
233 // connect SDMMC driver to FATFS 244 // connect SDMMC driver to FATFS
234 pdrv = 0xFF; 245 pdrv = FF_DRV_NOT_USED;
235 if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) { 246 if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED) {
236 ESP_LOGE(TAG, "the maximum count of volumes is already mounted"); 247 ESP_LOGE(TAG, "the maximum count of volumes is already mounted");
237 return ESP_ERR_NO_MEM; 248 return ESP_ERR_NO_MEM;
238 } 249 }
239 250
240 s_base_path = strdup(base_path); 251 s_base_path = strdup(base_path);
253 if (err != ESP_OK) { 264 if (err != ESP_OK) {
254 ESP_LOGE(TAG, "host init returned rc=0x%x", err); 265 ESP_LOGE(TAG, "host init returned rc=0x%x", err);
255 goto fail; 266 goto fail;
256 } 267 }
257 268
258 // configure SD slot 269 // configure SD host
259 if (host_config->flags == SDMMC_HOST_FLAG_SPI) { 270 err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle);
260 err = sdspi_host_init_slot(host_config->slot, (const sdspi_slot_config_t*) slot_config);
261 } else {
262 err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config);
263 }
264 if (err != ESP_OK) { 271 if (err != ESP_OK) {
265 ESP_LOGE(TAG, "slot_config returned rc=0x%x", err); 272 ESP_LOGE(TAG, "init_sdspi_host() rc=0x%x", err);
266 goto fail; 273 goto fail;
267 } 274 }
268 return ESP_OK; 275 return ESP_OK;
269 276
270 fail: 277 fail:
287 */ 294 */
288 esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path, 295 esp_err_t my_esp_vfs_fat_sdmmc_mount(const char* base_path,
289 const sdmmc_host_t* host_config, 296 const sdmmc_host_t* host_config,
290 const void* slot_config, 297 const void* slot_config,
291 const esp_vfs_fat_mount_config_t* mount_config, 298 const esp_vfs_fat_mount_config_t* mount_config,
292 sdmmc_card_t** out_card) 299 sdmmc_card_t** out_card,
300 int card_handle)
293 { 301 {
294 FATFS* fs = NULL; 302 FATFS* fs = NULL;
295 esp_err_t err = ESP_OK; 303 esp_err_t err = ESP_OK;
296 304
297 if (s_card == NULL) { 305 if (s_card == NULL) {
396 { 404 {
397 sdmmc_card_t* card; 405 sdmmc_card_t* card;
398 esp_err_t ret; 406 esp_err_t ret;
399 EventBits_t uxBits; 407 EventBits_t uxBits;
400 char filename[64]; 408 char filename[64];
409 int card_handle = -1; //uninitialized
401 410
402 sdcard_state = malloc(sizeof(SDCARD_State)); 411 sdcard_state = malloc(sizeof(SDCARD_State));
403 sdcard_state->host_ok = false; 412 sdcard_state->host_ok = false;
404 sdcard_state->card_present = false; 413 sdcard_state->card_present = false;
405 sdcard_state->logfile[0] = '\0'; 414 sdcard_state->logfile[0] = '\0';
407 json_log = malloc(sizeof(JSON_log)); 416 json_log = malloc(sizeof(JSON_log));
408 417
409 ESP_LOGI(TAG, "Start SD card"); 418 ESP_LOGI(TAG, "Start SD card");
410 sdmmc_host_t host = SDSPI_HOST_DEFAULT(); 419 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
411 host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. 420 host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT.
412 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); 421 spi_bus_config_t bus_cfg = {
413 slot_config.gpio_miso = SDCARD_PIN_NUM_MISO; 422 .mosi_io_num = SDCARD_PIN_NUM_MOSI,
414 slot_config.gpio_mosi = SDCARD_PIN_NUM_MOSI; 423 .miso_io_num = SDCARD_PIN_NUM_MISO,
415 slot_config.gpio_sck = SDCARD_PIN_NUM_CLK; 424 .sclk_io_num = SDCARD_PIN_NUM_CLK,
416 slot_config.gpio_cs = SDCARD_PIN_NUM_CS; 425 .quadwp_io_num = -1,
417 slot_config.dma_channel = SDCARD_DMA_CHANNEL; 426 .quadhd_io_num = -1,
427 .max_transfer_sz = 4000,
428 };
429 ret = spi_bus_initialize(host.slot, &bus_cfg, SDCARD_DMA_CHANNEL);
430 if (ret != ESP_OK) {
431 ESP_LOGE(TAG, "Failed to initialize bus.");
432 vTaskDelete(NULL);
433 return;
434 }
435
418 // This initializes the slot without card detect (CD) and write protect (WP) signals. 436 // This initializes the slot without card detect (CD) and write protect (WP) signals.
419 // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. 437 // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
438 sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
439 slot_config.gpio_cs = SDCARD_PIN_NUM_CS;
440 slot_config.host_id = host.slot;
420 441
421 /* 442 /*
422 * No errors from the sdmmc_cmd driver. 443 * No errors from the sdmmc_cmd driver.
423 */ 444 */
424 esp_log_level_set("sdmmc_cmd", ESP_LOG_NONE); 445 esp_log_level_set("sdmmc_cmd", ESP_LOG_NONE);
432 .format_if_mount_failed = false, 453 .format_if_mount_failed = false,
433 .max_files = 5, 454 .max_files = 5,
434 .allocation_unit_size = 16 * 1024 455 .allocation_unit_size = 16 * 1024
435 }; 456 };
436 457
437 ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config); 458 ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config, &card_handle);
438 if (ret == ESP_OK) { 459 if (ret == ESP_OK) {
439 ESP_LOGI(TAG, "SPI card interface ready"); 460 ESP_LOGI(TAG, "SPI card interface ready");
440 sdcard_state->host_ok = true; 461 sdcard_state->host_ok = true;
441 } else { 462 } else {
442 ESP_LOGE(TAG, "SPI card interface failed, abort task"); 463 ESP_LOGE(TAG, "SPI card interface failed, abort task");
453 474
454 if (sdcard_state->card_present == false) { 475 if (sdcard_state->card_present == false) {
455 /* 476 /*
456 * If the card is not mounted, try it. 477 * If the card is not mounted, try it.
457 */ 478 */
458 ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); 479 ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card, card_handle);
459 if (ret == ESP_OK) { 480 if (ret == ESP_OK) {
460 ESP_LOGI(TAG, "SD card mounted on /sdcard"); 481 ESP_LOGI(TAG, "SD card mounted on /sdcard");
461 sdcard_state->card_present = true; 482 sdcard_state->card_present = true;
462 483
463 DIR* dir = opendir("/sdcard/w/log"); 484 DIR* dir = opendir("/sdcard/w/log");

mercurial