main/task_sdcard.c

changeset 91
255a75322212
parent 88
7f02dbee58d0
child 92
bac0a860f5dd
--- a/main/task_sdcard.c	Thu Jun 11 22:53:04 2020 +0200
+++ b/main/task_sdcard.c	Mon May 17 20:44:35 2021 +0200
@@ -31,8 +31,7 @@
 static sdmmc_card_t* 	s_card = NULL;
 static uint8_t		s_pdrv = 0;
 static char		* s_base_path = NULL;
-
-BYTE pdrv = 0xFF;
+static uint8_t		pdrv = FF_DRV_NOT_USED;
 
 
 #define SDCARD_HOST_SLOT	VSPI_HOST		///< HSPI_HOST is used by the TFT
@@ -215,6 +214,18 @@
 
 
 
+static esp_err_t my_init_sdspi_host(int slot, const void *slot_config, int *out_slot)
+{
+    esp_err_t err = sdspi_host_init_device((const sdspi_device_config_t*)slot_config, out_slot);
+    if (err != ESP_OK) {
+        ESP_LOGE(TAG, "Failed to attach sdspi device onto an SPI bus (rc=0x%x), please initialize the \
+bus first and check the device parameters.", err);
+    }
+    return err;
+}
+
+
+
 /**
  * @brief This is a local modified version of the esp_vfs_fat_sdmmc_mount() function in
  *        the FreeRTOS components library. It is here so we can better handle errors
@@ -224,15 +235,15 @@
  * @param slot_config Slot configuration
  * @return Error condition.
  */
-esp_err_t my_vfs_fat_sdmmc_init(const char* base_path, const sdmmc_host_t* host_config, const void* slot_config)
+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)
 {
     if (s_card != NULL) {
 	return ESP_ERR_INVALID_STATE;
     }
 
     // connect SDMMC driver to FATFS
-    pdrv = 0xFF;
-    if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) {
+    pdrv = FF_DRV_NOT_USED;
+    if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED) {
 	ESP_LOGE(TAG, "the maximum count of volumes is already mounted");
 	return ESP_ERR_NO_MEM;
     }
@@ -255,14 +266,10 @@
 	goto fail;
     }
 
-    // configure SD slot
-    if (host_config->flags == SDMMC_HOST_FLAG_SPI) {
-	err = sdspi_host_init_slot(host_config->slot, (const sdspi_slot_config_t*) slot_config);
-    } else {
-	err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config);
-    }
+    // configure SD host
+    err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle);
     if (err != ESP_OK) {
-	ESP_LOGE(TAG, "slot_config returned rc=0x%x", err);
+	ESP_LOGE(TAG, "init_sdspi_host() rc=0x%x", err);
 	goto fail;
     }
     return ESP_OK;
@@ -289,7 +296,8 @@
 	const sdmmc_host_t* host_config,
 	const void* slot_config,
 	const esp_vfs_fat_mount_config_t* mount_config, 
-	sdmmc_card_t** out_card)
+	sdmmc_card_t** out_card,
+	int card_handle)
 {
     FATFS* fs = NULL;
     esp_err_t err = ESP_OK;
@@ -398,6 +406,7 @@
     esp_err_t		ret;
     EventBits_t		uxBits;
     char		filename[64];
+    int			card_handle = -1;   //uninitialized
 
     sdcard_state = malloc(sizeof(SDCARD_State));
     sdcard_state->host_ok = false;
@@ -409,14 +418,26 @@
     ESP_LOGI(TAG, "Start SD card");
     sdmmc_host_t host = SDSPI_HOST_DEFAULT();
     host.slot = SDCARD_HOST_SLOT;	// HSPI_HOST is in use by the TFT.
-    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
-    slot_config.gpio_miso   = SDCARD_PIN_NUM_MISO;
-    slot_config.gpio_mosi   = SDCARD_PIN_NUM_MOSI;
-    slot_config.gpio_sck    = SDCARD_PIN_NUM_CLK;
-    slot_config.gpio_cs     = SDCARD_PIN_NUM_CS;
-    slot_config.dma_channel = SDCARD_DMA_CHANNEL;
+    spi_bus_config_t bus_cfg = {
+        .mosi_io_num = SDCARD_PIN_NUM_MOSI,
+        .miso_io_num = SDCARD_PIN_NUM_MISO,
+        .sclk_io_num = SDCARD_PIN_NUM_CLK,
+        .quadwp_io_num = -1,
+        .quadhd_io_num = -1,
+        .max_transfer_sz = 4000,
+    };
+    ret = spi_bus_initialize(host.slot, &bus_cfg, SDCARD_DMA_CHANNEL);
+    if (ret != ESP_OK) {
+        ESP_LOGE(TAG, "Failed to initialize bus.");
+	vTaskDelete(NULL);
+        return;
+    }
+
     // This initializes the slot without card detect (CD) and write protect (WP) signals.
     // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
+    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
+    slot_config.gpio_cs     = SDCARD_PIN_NUM_CS;
+    slot_config.host_id     = host.slot;
 
     /*
      * No errors from the sdmmc_cmd driver.
@@ -434,7 +455,7 @@
 	.allocation_unit_size = 16 * 1024
     };
 
-    ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config);
+    ret = my_vfs_fat_sdmmc_init("/sdcard", &host, &slot_config, &card_handle);
     if (ret == ESP_OK) {
 	ESP_LOGI(TAG, "SPI card interface ready");
 	sdcard_state->host_ok = true;
@@ -455,7 +476,7 @@
 	    /*
 	     * If the card is not mounted, try it.
 	     */
-	    ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
+	    ret = my_esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card, card_handle);
 	    if (ret == ESP_OK) {
 		ESP_LOGI(TAG, "SD card mounted on /sdcard");
 		sdcard_state->card_present = true;

mercurial