Explorar o código

fatfs: add new ESP32 targets to external flash example

Adam Múdry %!s(int64=3) %!d(string=hai) anos
pai
achega
e5cff100b7

+ 19 - 14
examples/storage/ext_flash_fatfs/README.md

@@ -1,6 +1,3 @@
-| Supported Targets | ESP32 |
-| ----------------- | ----- |
-
 # FAT FS on External Flash example
 
 (See the README.md file in the upper level 'examples' directory for more information about examples.)
@@ -25,16 +22,24 @@ This example needs an SPI NOR Flash chip connected to the ESP32. The SPI Flash c
 
 Use the following pin assignments:
 
-ESP32 pin     | SPI bus signal | SPI Flash pin 
---------------|----------------|----------------
-GPIO23        | MOSI           | DI
-GPIO19        | MISO           | DO
-GPIO18        | SCLK           | CLK
-GPIO5         | CS             | CMD
-GPIO22        | WP             | WP
-GPIO21        | HD             | HOLD
-GND           |                | GND
-VCC           |                | VCC
+#### Pin assigments
+
+The GPIO pin numbers used to connect an external SPI flash chip can be customized.
+
+In this example it can be done in source code by changing C defines under `Pin mapping` comment at the top of the file.
+
+The table below shows the default pin assignments.
+
+SPI bus signal | SPI Flash pin | ESP32 pin | ESP32S2 pin | ESP32S3 pin | ESP32C3 pin
+---------------|---------------|-----------|-------------|-------------|-------------
+MOSI           | DI            | GPIO23    | GPIO11      | GPIO11      | GPIO7
+MISO           | DO            | GPIO19    | GPIO13      | GPIO13      | GPIO2
+SCLK           | CLK           | GPIO18    | GPIO12      | GPIO12      | GPIO6
+CS             | CMD           | GPIO5     | GPIO10      | GPIO10      | GPIO10
+WP             | WP            | GPIO22    | GPIO14      | GPIO14      | GPIO5
+HD             | HOLD          | GPIO21    | GPIO9       | GPIO9       | GPIO4
+|              | GND           | GND       | GND         | GND         | GND
+|              | VCC           | VCC       | VCC         | VCC         | VCC
 
 ### Build and flash
 
@@ -52,7 +57,7 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui
 
 ## Example output
 
-Here is a typical example console output. 
+Here is a typical example console output.
 
 ```
 I (328) example: Initializing external SPI Flash

+ 34 - 30
examples/storage/ext_flash_fatfs/main/ext_flash_fatfs_example_main.c

@@ -26,6 +26,28 @@
 
 static const char *TAG = "example";
 
+// Pin mapping
+// ESP32 (VSPI)
+#ifdef CONFIG_IDF_TARGET_ESP32
+#define HOST_ID  SPI3_HOST
+#define PIN_MOSI SPI3_IOMUX_PIN_NUM_MOSI
+#define PIN_MISO SPI3_IOMUX_PIN_NUM_MISO
+#define PIN_CLK  SPI3_IOMUX_PIN_NUM_CLK
+#define PIN_CS   SPI3_IOMUX_PIN_NUM_CS
+#define PIN_WP   SPI3_IOMUX_PIN_NUM_WP
+#define PIN_HD   SPI3_IOMUX_PIN_NUM_HD
+#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
+#else // Other chips (SPI2/HSPI)
+#define HOST_ID  SPI2_HOST
+#define PIN_MOSI SPI2_IOMUX_PIN_NUM_MOSI
+#define PIN_MISO SPI2_IOMUX_PIN_NUM_MISO
+#define PIN_CLK  SPI2_IOMUX_PIN_NUM_CLK
+#define PIN_CS   SPI2_IOMUX_PIN_NUM_CS
+#define PIN_WP   SPI2_IOMUX_PIN_NUM_WP
+#define PIN_HD   SPI2_IOMUX_PIN_NUM_HD
+#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
+#endif
+
 // Handle of the wear levelling library instance
 static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
 
@@ -36,7 +58,6 @@ static esp_flash_t* example_init_ext_flash(void);
 static const esp_partition_t* example_add_partition(esp_flash_t* ext_flash, const char* partition_label);
 static void example_list_data_partitions(void);
 static bool example_mount_fatfs(const char* partition_label);
-static void example_get_fatfs_usage(size_t* out_total_bytes, size_t* out_free_bytes);
 
 void app_main(void)
 {
@@ -59,9 +80,9 @@ void app_main(void)
     }
 
     // Print FAT FS size information
-    size_t bytes_total, bytes_free;
-    example_get_fatfs_usage(&bytes_total, &bytes_free);
-    ESP_LOGI(TAG, "FAT FS: %d kB total, %d kB free", bytes_total / 1024, bytes_free / 1024);
+    uint64_t bytes_total, bytes_free;
+    esp_vfs_fat_info(base_path, &bytes_total, &bytes_free);
+    ESP_LOGI(TAG, "FAT FS: %lld kB total, %lld kB free", bytes_total / 1024, bytes_free / 1024);
 
     // Create a file in FAT FS
     ESP_LOGI(TAG, "Opening file");
@@ -95,17 +116,17 @@ void app_main(void)
 static esp_flash_t* example_init_ext_flash(void)
 {
     const spi_bus_config_t bus_config = {
-        .mosi_io_num = VSPI_IOMUX_PIN_NUM_MOSI,
-        .miso_io_num = VSPI_IOMUX_PIN_NUM_MISO,
-        .sclk_io_num = VSPI_IOMUX_PIN_NUM_CLK,
-        .quadhd_io_num = VSPI_IOMUX_PIN_NUM_HD,
-        .quadwp_io_num = VSPI_IOMUX_PIN_NUM_WP,
+        .mosi_io_num = PIN_MOSI,
+        .miso_io_num = PIN_MISO,
+        .sclk_io_num = PIN_CLK,
+        .quadhd_io_num = PIN_HD,
+        .quadwp_io_num = PIN_WP,
     };
 
     const esp_flash_spi_device_config_t device_config = {
-        .host_id = VSPI_HOST,
+        .host_id = HOST_ID,
         .cs_id = 0,
-        .cs_io_num = VSPI_IOMUX_PIN_NUM_CS,
+        .cs_io_num = PIN_CS,
         .io_mode = SPI_FLASH_DIO,
         .freq_mhz = EXAMPLE_FLASH_FREQ_MHZ,
     };
@@ -118,7 +139,8 @@ static esp_flash_t* example_init_ext_flash(void)
     );
 
     // Initialize the SPI bus
-    ESP_ERROR_CHECK(spi_bus_initialize(VSPI_HOST, &bus_config, 1));
+    ESP_LOGI(TAG, "DMA CHANNEL: %d", SPI_DMA_CHAN);
+    ESP_ERROR_CHECK(spi_bus_initialize(HOST_ID, &bus_config, SPI_DMA_CHAN));
 
     // Add device to the SPI bus
     esp_flash_t* ext_flash;
@@ -176,21 +198,3 @@ static bool example_mount_fatfs(const char* partition_label)
     }
     return true;
 }
-
-static void example_get_fatfs_usage(size_t* out_total_bytes, size_t* out_free_bytes)
-{
-    FATFS *fs;
-    size_t free_clusters;
-    int res = f_getfree("0:", &free_clusters, &fs);
-    assert(res == FR_OK);
-    size_t total_sectors = (fs->n_fatent - 2) * fs->csize;
-    size_t free_sectors = free_clusters * fs->csize;
-
-    // assuming the total size is < 4GiB, should be true for SPI Flash
-    if (out_total_bytes != NULL) {
-        *out_total_bytes = total_sectors * fs->ssize;
-    }
-    if (out_free_bytes != NULL) {
-        *out_free_bytes = free_sectors * fs->ssize;
-    }
-}