From 26397c6695a1e276db552848a45d458c25d3d297 Mon Sep 17 00:00:00 2001 From: Sara Souza Date: Thu, 26 Aug 2021 16:59:16 -0300 Subject: [PATCH] xtensa/esp32: Wi-Fi board logic refactoring. This commit removes the initialization of the Wi-Fi partition from the Wi-Fi board logic and moves it to the SPI Flash board code. It creates 2 different partition (one for Wi-Fi and one for general use). It also allows these partitions to be mounted over several FSs. --- arch/xtensa/src/esp32/Kconfig | 16 + .../esp32/common/src/esp32_board_wlan.c | 48 +-- .../esp32/esp32-devkitc/src/esp32_spiflash.c | 387 ++++++++++++++++-- .../esp32-ethernet-kit/src/esp32_spiflash.c | 387 ++++++++++++++++-- .../esp32-wrover-kit/src/esp32_spiflash.c | 387 ++++++++++++++++-- 5 files changed, 1062 insertions(+), 163 deletions(-) diff --git a/arch/xtensa/src/esp32/Kconfig b/arch/xtensa/src/esp32/Kconfig index 4dde172c70..6d3669c10a 100644 --- a/arch/xtensa/src/esp32/Kconfig +++ b/arch/xtensa/src/esp32/Kconfig @@ -1089,6 +1089,7 @@ config ESP32_WIFI_SCAN_RESULT_SIZE config ESP32_WIFI_SAVE_PARAM bool "Save WiFi Parameters" default n + depends on ESP32_SPIFLASH ---help--- If you enable this option, WiFi adapter parameters will be saved into the file system instead of computing them each time. @@ -1109,6 +1110,21 @@ config ESP32_WIFI_FS_MOUNTPT ---help--- Mount point of WiFi storage file system. +config ESP32_WIFI_MTD_OFFSET + hex "Wi-Fi MTD partition offset" + default 0x280000 if !ESP32_HAVE_OTA_PARTITION + default 0x350000 if ESP32_HAVE_OTA_PARTITION + depends on ESP32_WIFI_SAVE_PARAM + ---help--- + This is the base address of the Wi-Fi MTD partition. + +config ESP32_WIFI_MTD_SIZE + hex "Wi-Fi MTD partition size" + default 0xb0000 + depends on ESP32_WIFI_SAVE_PARAM + ---help--- + This is the size of the Wi-Fi MTD partition. + config ESP32_WIFI_STA_DISCONNECT_PM bool "Power Management for station when disconnected" default n diff --git a/boards/xtensa/esp32/common/src/esp32_board_wlan.c b/boards/xtensa/esp32/common/src/esp32_board_wlan.c index b631aa1cc8..0930648efc 100644 --- a/boards/xtensa/esp32/common/src/esp32_board_wlan.c +++ b/boards/xtensa/esp32/common/src/esp32_board_wlan.c @@ -40,45 +40,10 @@ * Pre-processor Definitions ****************************************************************************/ -#define ESP32_MTD_OFFSET CONFIG_ESP32_MTD_OFFSET -#define ESP32_MTD_SIZE CONFIG_ESP32_MTD_SIZE - /**************************************************************************** * Private Functions ****************************************************************************/ -#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM -static int esp32_init_wifi_storage(void) -{ - int ret; - const char *path = "/dev/mtdblock1"; - FAR struct mtd_dev_s *mtd_part; - - mtd_part = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE); - if (!mtd_part) - { - syslog(LOG_ERR, "ERROR: Failed to alloc MTD partition of SPI Flash\n"); - return -1; - } - - ret = register_mtddriver(path, mtd_part, 0777, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to regitser MTD: %d\n", ret); - return -1; - } - - ret = nx_mount(path, CONFIG_ESP32_WIFI_FS_MOUNTPT, "spiffs", 0, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", ret); - return ret; - } - - return 0; -} -#endif - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -99,20 +64,11 @@ int board_wlan_init(void) { int ret = OK; -#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM - ret = esp32_init_wifi_storage(); - if (ret) - { - syslog(LOG_ERR, "ERROR: Failed to initialize WiFi storage\n"); - return ret; - } -#endif - #ifdef ESP32_WLAN_HAS_STA ret = esp32_wlan_sta_initialize(); if (ret) { - syslog(LOG_ERR, "ERROR: Failed to initialize WiFi station\n"); + wlerr("ERROR: Failed to initialize WiFi station\n"); return ret; } #endif @@ -121,7 +77,7 @@ int board_wlan_init(void) ret = esp32_wlan_softap_initialize(); if (ret) { - syslog(LOG_ERR, "ERROR: Failed to initialize WiFi softAP\n"); + wlerr("ERROR: Failed to initialize WiFi softAP\n"); return ret; } #endif diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_spiflash.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_spiflash.c index aac9cbf40f..922322deb2 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_spiflash.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_spiflash.c @@ -48,9 +48,6 @@ #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0])) -#define ESP32_MTD_OFFSET CONFIG_ESP32_MTD_OFFSET -#define ESP32_MTD_SIZE CONFIG_ESP32_MTD_SIZE - /**************************************************************************** * Private Types ****************************************************************************/ @@ -138,8 +135,347 @@ static int init_ota_partitions(void) return ret; } + #endif +/**************************************************************************** + * Name: setup_smartfs + * + * Description: + * Provide a block driver wrapper around MTD partition and mount a + * SMART FS over it. + * + * Parameters: + * smartn - Number used to register the mtd partition: /dev/smartx, where + * x = smartn. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) +static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd, + const char *mnt_pt) +{ + int ret = OK; + char path[22]; + + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + finfo("smart_initialize failed, Trying to erase first...\n"); + ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); + if (ret < 0) + { + ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); + return ret; + } + + finfo("Erase successful, initializing it again.\n"); + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: smart_initialize failed: %d\n", ret); + return ret; + } + } + + if (mnt_pt != NULL) + { + snprintf(path, sizeof(path), "/dev/smart%d", smartn); + + ret = nx_mount(path, mnt_pt, "smartfs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_littlefs + * + * Description: + * Register a mtd driver and mount a Little FS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) +static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, NULL); + if (ret < 0) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, "forceformat"); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + } + + return OK; +} + +#endif + +/**************************************************************************** + * Name: setup_spiffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SPIFFS) +static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "spiffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_nxffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_NXFFS) +static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt) +{ + int ret = OK; + + ret = nxffs_initialize(mtd); + if (ret < 0) + { + ferr("ERROR: NXFFS init failed: %d\n", ret); + return ret; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(NULL, mnt_pt, "nxffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_wifi_partition + * + * Description: + * Initialize partition that is dedicated to Wi-Fi. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_WIFI_SAVE_PARAM) +static int init_wifi_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_WIFI_MTD_OFFSET, + CONFIG_ESP32_WIFI_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(1, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_littlefs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_spiffs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ferr("ERROR: No supported FS selected. Wi-Fi partition " + "should be mounted before Wi-Fi initialization\n"); + +#endif + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_storage_partition + * + * Description: + * Initialize partition that is dedicated to general use. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int init_storage_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_MTD_OFFSET, + CONFIG_ESP32_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(0, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) + + ret = setup_nxffs(mtd, "/mnt"); + if (ret < 0) + { + ferr("ERROR: Failed to setup nxffs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/esp32flash"; + ret = setup_littlefs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/esp32flash"; + ret = setup_spiffs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ret; + } + +#endif + + return ret; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -153,8 +489,7 @@ static int init_ota_partitions(void) int esp32_spiflash_init(void) { - FAR struct mtd_dev_s *mtd; - int ret = ERROR; + int ret = OK; #ifdef CONFIG_ESP32_HAVE_OTA_PARTITION ret = init_ota_partitions(); @@ -164,46 +499,20 @@ int esp32_spiflash_init(void) } #endif - mtd = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE); - -#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) - ret = smart_initialize(0, mtd, NULL); +#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM + ret = init_wifi_partition(); if (ret < 0) { - finfo("smart_initialize failed, Trying to erase first...\n"); - ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); - if (ret < 0) - { - ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); - return ret; - } - - finfo("Erase successful, initializing again\n"); - ret = smart_initialize(0, mtd, NULL); - if (ret < 0) - { - ferr("ERROR: smart_initialize failed: %d\n", ret); - return ret; - } - } - -#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) - ret = nxffs_initialize(mtd); - if (ret < 0) - { - ferr("ERROR: NXFFS init failed: %d\n", ret); - return ret; - } - -#else - ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); - if (ret < 0) - { - ferr("ERROR: Register mtd failed: %d\n", ret); return ret; } #endif + ret = init_storage_partition(); + if (ret < 0) + { + return ret; + } + return ret; } diff --git a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_spiflash.c b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_spiflash.c index c03d9ee6fa..e0bcc7a03f 100644 --- a/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_spiflash.c +++ b/boards/xtensa/esp32/esp32-ethernet-kit/src/esp32_spiflash.c @@ -48,9 +48,6 @@ #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0])) -#define ESP32_MTD_OFFSET CONFIG_ESP32_MTD_OFFSET -#define ESP32_MTD_SIZE CONFIG_ESP32_MTD_SIZE - /**************************************************************************** * Private Types ****************************************************************************/ @@ -138,8 +135,347 @@ static int init_ota_partitions(void) return ret; } + #endif +/**************************************************************************** + * Name: setup_smartfs + * + * Description: + * Provide a block driver wrapper around MTD partition and mount a + * SMART FS over it. + * + * Parameters: + * smartn - Number used to register the mtd partition: /dev/smartx, where + * x = smartn. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) +static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd, + const char *mnt_pt) +{ + int ret = OK; + char path[22]; + + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + finfo("smart_initialize failed, Trying to erase first...\n"); + ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); + if (ret < 0) + { + ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); + return ret; + } + + finfo("Erase successful, initializing it again.\n"); + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: smart_initialize failed: %d\n", ret); + return ret; + } + } + + if (mnt_pt != NULL) + { + snprintf(path, sizeof(path), "/dev/smart%d", smartn); + + ret = nx_mount(path, mnt_pt, "smartfs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_littlefs + * + * Description: + * Register a mtd driver and mount a Little FS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) +static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, NULL); + if (ret < 0) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, "forceformat"); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + } + + return OK; +} + +#endif + +/**************************************************************************** + * Name: setup_spiffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SPIFFS) +static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "spiffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_nxffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_NXFFS) +static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt) +{ + int ret = OK; + + ret = nxffs_initialize(mtd); + if (ret < 0) + { + ferr("ERROR: NXFFS init failed: %d\n", ret); + return ret; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(NULL, mnt_pt, "nxffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_wifi_partition + * + * Description: + * Initialize partition that is dedicated to Wi-Fi. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_WIFI_SAVE_PARAM) +static int init_wifi_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_WIFI_MTD_OFFSET, + CONFIG_ESP32_WIFI_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(1, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_littlefs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_spiffs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ferr("ERROR: No supported FS selected. Wi-Fi partition " + "should be mounted before Wi-Fi initialization\n"); + +#endif + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_storage_partition + * + * Description: + * Initialize partition that is dedicated to general use. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int init_storage_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_MTD_OFFSET, + CONFIG_ESP32_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(0, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) + + ret = setup_nxffs(mtd, "/mnt"); + if (ret < 0) + { + ferr("ERROR: Failed to setup nxffs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/esp32flash"; + ret = setup_littlefs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/esp32flash"; + ret = setup_spiffs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ret; + } + +#endif + + return ret; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -153,8 +489,7 @@ static int init_ota_partitions(void) int esp32_spiflash_init(void) { - FAR struct mtd_dev_s *mtd; - int ret = ERROR; + int ret = OK; #ifdef CONFIG_ESP32_HAVE_OTA_PARTITION ret = init_ota_partitions(); @@ -164,46 +499,20 @@ int esp32_spiflash_init(void) } #endif - mtd = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE); - -#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) - ret = smart_initialize(0, mtd, NULL); +#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM + ret = init_wifi_partition(); if (ret < 0) { - finfo("smart_initialize failed, Trying to erase first...\n"); - ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); - if (ret < 0) - { - ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); - return ret; - } - - finfo("Erase successful, initializing again\n"); - ret = smart_initialize(0, mtd, NULL); - if (ret < 0) - { - ferr("ERROR: smart_initialize failed: %d\n", ret); - return ret; - } - } - -#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) - ret = nxffs_initialize(mtd); - if (ret < 0) - { - ferr("ERROR: NXFFS init failed: %d\n", ret); - return ret; - } - -#else - ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); - if (ret < 0) - { - ferr("ERROR: Register mtd failed: %d\n", ret); return ret; } #endif + ret = init_storage_partition(); + if (ret < 0) + { + return ret; + } + return ret; } diff --git a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_spiflash.c b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_spiflash.c index 67d28d76b3..4443d123b6 100644 --- a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_spiflash.c +++ b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_spiflash.c @@ -48,9 +48,6 @@ #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0])) -#define ESP32_MTD_OFFSET CONFIG_ESP32_MTD_OFFSET -#define ESP32_MTD_SIZE CONFIG_ESP32_MTD_SIZE - /**************************************************************************** * Private Types ****************************************************************************/ @@ -138,8 +135,347 @@ static int init_ota_partitions(void) return ret; } + #endif +/**************************************************************************** + * Name: setup_smartfs + * + * Description: + * Provide a block driver wrapper around MTD partition and mount a + * SMART FS over it. + * + * Parameters: + * smartn - Number used to register the mtd partition: /dev/smartx, where + * x = smartn. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) +static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd, + const char *mnt_pt) +{ + int ret = OK; + char path[22]; + + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + finfo("smart_initialize failed, Trying to erase first...\n"); + ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); + if (ret < 0) + { + ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); + return ret; + } + + finfo("Erase successful, initializing it again.\n"); + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: smart_initialize failed: %d\n", ret); + return ret; + } + } + + if (mnt_pt != NULL) + { + snprintf(path, sizeof(path), "/dev/smart%d", smartn); + + ret = nx_mount(path, mnt_pt, "smartfs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_littlefs + * + * Description: + * Register a mtd driver and mount a Little FS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) +static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, NULL); + if (ret < 0) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, "forceformat"); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + } + + return OK; +} + +#endif + +/**************************************************************************** + * Name: setup_spiffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_SPIFFS) +static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "spiffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_nxffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_SPIFLASH_NXFFS) +static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt) +{ + int ret = OK; + + ret = nxffs_initialize(mtd); + if (ret < 0) + { + ferr("ERROR: NXFFS init failed: %d\n", ret); + return ret; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(NULL, mnt_pt, "nxffs", 0, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to mount the FS volume: %d\n", ret); + return ret; + } + } + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_wifi_partition + * + * Description: + * Initialize partition that is dedicated to Wi-Fi. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESP32_WIFI_SAVE_PARAM) +static int init_wifi_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_WIFI_MTD_OFFSET, + CONFIG_ESP32_WIFI_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(1, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_littlefs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/mtdblock1"; + ret = setup_spiffs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ferr("ERROR: No supported FS selected. Wi-Fi partition " + "should be mounted before Wi-Fi initialization\n"); + +#endif + + return ret; +} + +#endif +/**************************************************************************** + * Name: init_storage_partition + * + * Description: + * Initialize partition that is dedicated to general use. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int init_storage_partition(void) +{ + int ret = OK; + FAR struct mtd_dev_s *mtd; + + mtd = esp32_spiflash_alloc_mtdpart(CONFIG_ESP32_MTD_OFFSET, + CONFIG_ESP32_MTD_SIZE); + if (!mtd) + { + ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + + ret = setup_smartfs(0, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) + + ret = setup_nxffs(mtd, "/mnt"); + if (ret < 0) + { + ferr("ERROR: Failed to setup nxffs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS) + + const char *path = "/dev/esp32flash"; + ret = setup_littlefs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS) + + const char *path = "/dev/esp32flash"; + ret = setup_spiffs(path, mtd, NULL, 0755); + if (ret < 0) + { + ferr("ERROR: Failed to setup spiffs\n"); + return ret; + } + +#else + + ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD: %d\n", ret); + return ret; + } + +#endif + + return ret; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -153,8 +489,7 @@ static int init_ota_partitions(void) int esp32_spiflash_init(void) { - FAR struct mtd_dev_s *mtd; - int ret = ERROR; + int ret = OK; #ifdef CONFIG_ESP32_HAVE_OTA_PARTITION ret = init_ota_partitions(); @@ -164,46 +499,20 @@ int esp32_spiflash_init(void) } #endif - mtd = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE); - -#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) - ret = smart_initialize(0, mtd, NULL); +#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM + ret = init_wifi_partition(); if (ret < 0) { - finfo("smart_initialize failed, Trying to erase first...\n"); - ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); - if (ret < 0) - { - ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); - return ret; - } - - finfo("Erase successful, initializing again\n"); - ret = smart_initialize(0, mtd, NULL); - if (ret < 0) - { - ferr("ERROR: smart_initialize failed: %d\n", ret); - return ret; - } - } - -#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) - ret = nxffs_initialize(mtd); - if (ret < 0) - { - ferr("ERROR: NXFFS init failed: %d\n", ret); - return ret; - } - -#else - ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); - if (ret < 0) - { - ferr("ERROR: Register mtd failed: %d\n", ret); return ret; } #endif + ret = init_storage_partition(); + if (ret < 0) + { + return ret; + } + return ret; }