From d485ccc1421609d8991bf9ff2dbcb6d77ace8d33 Mon Sep 17 00:00:00 2001 From: Abdelatif Guettouche Date: Thu, 10 Sep 2020 18:12:02 +0100 Subject: [PATCH] boards/xtensa/esp32/esp32-core: Support for the external FLASH. Signed-off-by: Abdelatif Guettouche --- boards/xtensa/esp32/esp32-core/Kconfig | 19 ++++ boards/xtensa/esp32/esp32-core/src/Makefile | 4 + .../xtensa/esp32/esp32-core/src/esp32-core.h | 9 ++ .../esp32/esp32-core/src/esp32_bringup.c | 4 + .../esp32/esp32-core/src/esp32_spiflash.c | 101 ++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 boards/xtensa/esp32/esp32-core/src/esp32_spiflash.c diff --git a/boards/xtensa/esp32/esp32-core/Kconfig b/boards/xtensa/esp32/esp32-core/Kconfig index aaaa65785c..11b1fbc217 100644 --- a/boards/xtensa/esp32/esp32-core/Kconfig +++ b/boards/xtensa/esp32/esp32-core/Kconfig @@ -44,4 +44,23 @@ config ESP32CORE_FLASH_IMAGE ---help--- Create flash_image.bin mainly used for QEMU. +choice + prompt "SPIFLASH File System" + default ESP32_SPIFLASH_SMARTFS + depends on ESP32_SPIFLASH + + config ESP32_SPIFLASH_SMARTFS + bool "SmartFS" + depends on FS_SMARTFS + config ESP32_SPIFLASH_NXFFS + bool "NXFFS" + depends on FS_NXFFS + config ESP32_SPIFLASH_SPIFFS + bool "SPIFFS" + depends on FS_SPIFFS + config ESP32_SPIFLASH_LITTLEFS + bool "LittleFS" + depends on FS_LITTLEFS +endchoice + endif # ARCH_BOARD_ESP32CORE diff --git a/boards/xtensa/esp32/esp32-core/src/Makefile b/boards/xtensa/esp32/esp32-core/src/Makefile index 1a91f0d589..5ec8d6169e 100644 --- a/boards/xtensa/esp32/esp32-core/src/Makefile +++ b/boards/xtensa/esp32/esp32-core/src/Makefile @@ -53,6 +53,10 @@ ifeq ($(CONFIG_MMCSD),y) CSRCS += esp32_mmcsd.c endif +ifeq ($(CONFIG_ESP32_SPIFLASH),y) +CSRCS += esp32_spiflash.c +endif + SCRIPTIN = $(SCRIPTDIR)$(DELIM)esp32.template SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32_out.ld diff --git a/boards/xtensa/esp32/esp32-core/src/esp32-core.h b/boards/xtensa/esp32/esp32-core/src/esp32-core.h index 21dd36b050..5f0a4c1389 100644 --- a/boards/xtensa/esp32/esp32-core/src/esp32-core.h +++ b/boards/xtensa/esp32/esp32-core/src/esp32-core.h @@ -87,5 +87,14 @@ int esp32_bringup(void); int esp32_mmcsd_initialize(int minor); +/**************************************************************************** + * Name: esp32_spiflash_init + * + * Description: + * Initialize the SPIFLASH and register the MTD device. + ****************************************************************************/ + +int esp32_spiflash_init(void); + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_XTENSA_ESP32_ESP32_CORE_SRC_ESP32_CORE_H */ diff --git a/boards/xtensa/esp32/esp32-core/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-core/src/esp32_bringup.c index 52abe4c8d7..57aa5d7631 100644 --- a/boards/xtensa/esp32/esp32-core/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-core/src/esp32_bringup.c @@ -90,6 +90,10 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_ESP32_SPIFLASH + ret = esp32_spiflash_init(); +#endif + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities. diff --git a/boards/xtensa/esp32/esp32-core/src/esp32_spiflash.c b/boards/xtensa/esp32/esp32-core/src/esp32_spiflash.c new file mode 100644 index 0000000000..3ac6ca674d --- /dev/null +++ b/boards/xtensa/esp32/esp32-core/src/esp32_spiflash.c @@ -0,0 +1,101 @@ +/**************************************************************************** + * boards/xtensa/esp32/esp32-core/src/esp32_spiflash.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "esp32_spiflash.h" +#include "esp32-core.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_spiflash_init + * + * Description: + * Initialize the SPIFLASH and register the MTD device. + ****************************************************************************/ + +int esp32_spiflash_init(void) +{ + FAR struct mtd_dev_s *mtd; + int ret = ERROR; + + mtd = esp32_spiflash_alloc_mtdpart(); + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + ret = smart_initialize(0, 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 successed, initializaing 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 + + return ret; +} +