mirror of
https://github.com/apache/nuttx.git
synced 2026-09-24 08:45:04 +08:00
ESP32S2: Add support to SPI Flash
This commit is contained in:
committed by
Xiang Xiao
parent
dc08798764
commit
1d88d5a370
@@ -158,6 +158,10 @@ config ESP32S2_FLASH_8M
|
||||
bool
|
||||
default n
|
||||
|
||||
config ESP32S2_FLASH_16M
|
||||
bool
|
||||
default n
|
||||
|
||||
config ESP32S2_PSRAM_2M
|
||||
bool
|
||||
default n
|
||||
@@ -749,6 +753,35 @@ config ESP32S2_OTA_SCRATCH_DEVPATH
|
||||
|
||||
endif
|
||||
|
||||
if ESP32S2_SPIFLASH
|
||||
|
||||
comment "General storage MTD configuration"
|
||||
|
||||
config ESP32S2_MTD
|
||||
bool "MTD driver"
|
||||
default y
|
||||
select MTD
|
||||
select MTD_BYTE_WRITE
|
||||
select MTD_PARTITION
|
||||
---help---
|
||||
Initialize an MTD driver for the ESP32-S2 SPI Flash, which will
|
||||
add an entry at /dev for application access from userspace.
|
||||
|
||||
config ESP32S2_SPIFLASH_MTD_BLKSIZE
|
||||
int "Storage MTD block size"
|
||||
default 256
|
||||
depends on ESP32S2_MTD
|
||||
|
||||
config ESP32S2_STORAGE_MTD_DEBUG
|
||||
bool "Storage MTD Debug"
|
||||
default n
|
||||
depends on ESP32S2_MTD && DEBUG_FS_INFO
|
||||
---help---
|
||||
If this option is enabled, Storage MTD driver read and write functions
|
||||
will output input parameters and return values (if applicable).
|
||||
|
||||
endif # ESP32S2_SPIFLASH
|
||||
|
||||
endmenu # SPI Flash Configuration
|
||||
|
||||
menu "SPI RAM Configuration"
|
||||
|
||||
@@ -68,6 +68,22 @@ ifeq ($(CONFIG_ESP32S2_SPI),y)
|
||||
CHIP_CSRCS += esp32s2_spi.c
|
||||
endif
|
||||
|
||||
#ifeq ($(CONFIG_ESP32S2_SPIFLASH),y)
|
||||
#CHIP_CSRCS += esp32s2_spicache.c
|
||||
#CHIP_CSRCS += esp32s2_spiflash.c
|
||||
#endif
|
||||
|
||||
ifeq ($(CONFIG_ESP32S2_SPIFLASH),y)
|
||||
CHIP_CSRCS += esp32s2_spiflash.c
|
||||
ifeq ($(CONFIG_ESP32S2_MTD),y)
|
||||
CHIP_CSRCS += esp32s2_spiflash_mtd.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ESP32S2_PARTITION_TABLE),y)
|
||||
CHIP_CSRCS += esp32_partition.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ESP32S2_TIMER),y)
|
||||
CHIP_CSRCS += esp32s2_tim.c
|
||||
ifeq ($(CONFIG_TIMER),y)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,94 @@
|
||||
/****************************************************************************
|
||||
* arch/xtensa/src/esp32s2/esp32s2_spiflash.h
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_H
|
||||
#define __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <nuttx/mtd/mtd.h>
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: spi_flash_read_encrypted
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Read data from Encrypted Flash.
|
||||
*
|
||||
* If flash encryption is enabled, this function will transparently
|
||||
* decrypt data as it is read.
|
||||
* If flash encryption is not enabled, this function behaves the same as
|
||||
* spi_flash_read().
|
||||
*
|
||||
* See esp_flash_encryption_enabled() for a function to check if flash
|
||||
* encryption is enabled.
|
||||
*
|
||||
* Parameters:
|
||||
* addr - source address of the data in Flash.
|
||||
* buffer - pointer to the destination buffer
|
||||
* size - length of data
|
||||
*
|
||||
* Returned Values: esp_err_t
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int spi_flash_read_encrypted(uint32_t addr, void *buffer, uint32_t size);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: esp32s2_spiflash_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize ESP32-S3 SPI flash driver.
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if success or a negative value if fail.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int esp32s2_spiflash_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#undef EXTERN
|
||||
|
||||
#endif /* __ASSEMBLY__ */
|
||||
#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_H */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,108 @@
|
||||
/****************************************************************************
|
||||
* arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.h
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_MTD_H
|
||||
#define __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_MTD_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <nuttx/mtd/mtd.h>
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: esp32s2_spiflash_mtd
|
||||
*
|
||||
* Description:
|
||||
* Get SPI Flash MTD.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* ESP32-S3 SPI Flash MTD pointer.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct mtd_dev_s *esp32s2_spiflash_mtd(void);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: esp32s2_spiflash_encrypt_mtd
|
||||
*
|
||||
* Description:
|
||||
* Get SPI Flash encryption MTD.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* SPI Flash encryption MTD pointer.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct mtd_dev_s *esp32s2_spiflash_encrypt_mtd(void);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: esp32s2_spiflash_alloc_mtdpart
|
||||
*
|
||||
* Description:
|
||||
* Allocate an MTD partition from the ESP32-S3 SPI Flash.
|
||||
*
|
||||
* Input Parameters:
|
||||
* mtd_offset - MTD Partition offset from the base address in SPI Flash.
|
||||
* mtd_size - Size for the MTD partition.
|
||||
* encrypted - Flag indicating whether the newly allocated partition will
|
||||
* have its content encrypted.
|
||||
*
|
||||
* Returned Value:
|
||||
* SPI Flash MTD data pointer if success or NULL if fail.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct mtd_dev_s *esp32s2_spiflash_alloc_mtdpart(uint32_t mtd_offset,
|
||||
uint32_t mtd_size,
|
||||
bool encrypted);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#undef EXTERN
|
||||
|
||||
#endif /* __ASSEMBLY__ */
|
||||
#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SPIFLASH_MTD_H */
|
||||
@@ -34,6 +34,8 @@ extern "C"
|
||||
|
||||
#define REG_SPI_MEM_BASE(i) (DR_REG_SPI0_BASE - (i) * 0x1000)
|
||||
|
||||
#define SPI_MEM_CMD_REG(i) (REG_SPI_MEM_BASE(i) + 0x0)
|
||||
|
||||
/* SPI_MEM_FLASH_READ : R/W/SC ;bitpos:[31] ;default: 1'b0 ; */
|
||||
|
||||
/* Description: Read flash enable. Read flash operation will be triggered
|
||||
|
||||
@@ -115,6 +115,13 @@
|
||||
#define SYSTEM_CLK_EN_DEDICATED_GPIO_V 0x00000001
|
||||
#define SYSTEM_CLK_EN_DEDICATED_GPIO_S 7
|
||||
|
||||
/* SYSTEM_CLK_EN_ASSIST_DEBUG : R/W ;bitpos:[6] ;default: 0 */
|
||||
|
||||
#define SYSTEM_CLK_EN_ASSIST_DEBUG (BIT(6))
|
||||
#define SYSTEM_CLK_EN_ASSIST_DEBUG_M (BIT(6))
|
||||
#define SYSTEM_CLK_EN_ASSIST_DEBUG_V 0x1
|
||||
#define SYSTEM_CLK_EN_ASSIST_DEBUG_S 6
|
||||
|
||||
/* SYSTEM_CPU_PERI_RST_EN_REG register
|
||||
* CPU peripheral reset register
|
||||
*/
|
||||
@@ -130,6 +137,13 @@
|
||||
#define SYSTEM_RST_EN_DEDICATED_GPIO_V 0x00000001
|
||||
#define SYSTEM_RST_EN_DEDICATED_GPIO_S 7
|
||||
|
||||
/* SYSTEM_RST_EN_ASSIST_DEBUG : R/W ;bitpos:[6] ;default: 1 ; */
|
||||
|
||||
#define SYSTEM_RST_EN_ASSIST_DEBUG (BIT(6))
|
||||
#define SYSTEM_RST_EN_ASSIST_DEBUG_M (BIT(6))
|
||||
#define SYSTEM_RST_EN_ASSIST_DEBUG_V 0x1
|
||||
#define SYSTEM_RST_EN_ASSIST_DEBUG_S 6
|
||||
|
||||
/* SYSTEM_CPU_PER_CONF_REG register
|
||||
* CPU peripheral clock configuration register
|
||||
*/
|
||||
@@ -1101,6 +1115,20 @@
|
||||
#define SYSTEM_LPCLK_SEL_RTC_SLOW_V 0x00000001
|
||||
#define SYSTEM_LPCLK_SEL_RTC_SLOW_S 24
|
||||
|
||||
/* SYSTEM_BT_LPCK_DIV_A : R/W ;bitpos:[23:12] ;default: 1 */
|
||||
|
||||
#define SYSTEM_BT_LPCK_DIV_A 0x00000FFF
|
||||
#define SYSTEM_BT_LPCK_DIV_A_M ((SYSTEM_BT_LPCK_DIV_A_V)<<(SYSTEM_BT_LPCK_DIV_A_S))
|
||||
#define SYSTEM_BT_LPCK_DIV_A_V 0xFFF
|
||||
#define SYSTEM_BT_LPCK_DIV_A_S 12
|
||||
|
||||
/* SYSTEM_BT_LPCK_DIV_B : R/W ;bitpos:[11:0] ;default: 1 */
|
||||
|
||||
#define SYSTEM_BT_LPCK_DIV_B 0x00000FFF
|
||||
#define SYSTEM_BT_LPCK_DIV_B_M ((SYSTEM_BT_LPCK_DIV_B_V)<<(SYSTEM_BT_LPCK_DIV_B_S))
|
||||
#define SYSTEM_BT_LPCK_DIV_B_V 0xFFF
|
||||
#define SYSTEM_BT_LPCK_DIV_B_S 0
|
||||
|
||||
/* SYSTEM_CPU_INTR_FROM_CPU_0_REG register
|
||||
* CPU interrupt controlling register 0
|
||||
*/
|
||||
@@ -1200,6 +1228,7 @@
|
||||
#define SYSTEM_RSA_MEM_PD_M (SYSTEM_RSA_MEM_PD_V << SYSTEM_RSA_MEM_PD_S)
|
||||
#define SYSTEM_RSA_MEM_PD_V 0x00000001
|
||||
#define SYSTEM_RSA_MEM_PD_S 0
|
||||
#define SYSTEM_RSA_PD SYSTEM_RSA_MEM_PD
|
||||
|
||||
/* SYSTEM_BUSTOEXTMEM_ENA_REG register
|
||||
* EDMA enable register
|
||||
|
||||
@@ -143,17 +143,6 @@ typedef struct
|
||||
uint16_t data;
|
||||
} esp_rom_spiflash_common_cmd_t;
|
||||
|
||||
/**
|
||||
* Global ROM spiflash data, as used by legacy SPI flash functions
|
||||
*/
|
||||
|
||||
struct spiflash_legacy_data_s
|
||||
{
|
||||
esp32s2_spiflash_chip_t chip;
|
||||
uint8_t dummy_len_plus[3];
|
||||
uint8_t sig_matrix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Structure holding SPI flash access critical sections management functions.
|
||||
*
|
||||
@@ -484,7 +473,7 @@ esp_rom_spiflash_config_clk(uint8_t freqdiv,
|
||||
*
|
||||
* Please do not call this function in SDK.
|
||||
*
|
||||
* Input Paramater:
|
||||
* Input Parameter:
|
||||
* esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a
|
||||
* command.
|
||||
*
|
||||
@@ -1011,7 +1000,10 @@ void spi_flash_enable_cache(uint32_t cpuid);
|
||||
* Public Data
|
||||
*****************************************************************************/
|
||||
|
||||
extern const struct spiflash_legacy_data_s *rom_spiflash_legacy_data;
|
||||
/* Global esp32s2_spiflash_chip_t structure used by ROM functions */
|
||||
|
||||
extern esp32s2_spiflash_chip_t g_rom_flashchip;
|
||||
extern uint8_t g_rom_spiflash_dummy_len_plus[];
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -12,6 +12,47 @@ config ESP32S2_MERGE_BINS
|
||||
This is only useful when the path to binary files (e.g. bootloader)
|
||||
is provided via the ESPTOOL_BINDIR variable.
|
||||
|
||||
choice ESP32S2_SPIFLASH_FS
|
||||
prompt "Mount SPI Flash MTD on bring-up"
|
||||
default ESP32S2_SPIFLASH_SMARTFS
|
||||
depends on ESP32S2_SPIFLASH
|
||||
optional
|
||||
---help---
|
||||
Mount the SPI Flash MTD with the selected File System format on board
|
||||
bring-up.
|
||||
If not selected, the MTD will be registered as a device node on /dev.
|
||||
|
||||
config ESP32S2_SPIFLASH_SMARTFS
|
||||
bool "SmartFS"
|
||||
select FS_SMARTFS
|
||||
select MTD_SMART
|
||||
depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
comment "SmartFS not supported with Flash Encryption"
|
||||
depends on ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
config ESP32S2_SPIFLASH_NXFFS
|
||||
bool "NXFFS"
|
||||
select FS_NXFFS
|
||||
depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
comment "NXFFS not supported with Flash Encryption"
|
||||
depends on ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
config ESP32S2_SPIFLASH_SPIFFS
|
||||
bool "SPIFFS"
|
||||
select FS_SPIFFS
|
||||
depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
comment "SPIFFS not supported with Flash Encryption"
|
||||
depends on ESP32S2_SECURE_FLASH_ENC_ENABLED
|
||||
|
||||
config ESP32S2_SPIFLASH_LITTLEFS
|
||||
bool "LittleFS"
|
||||
select FS_LITTLEFS
|
||||
|
||||
endchoice
|
||||
|
||||
config ESP32S2_LCD_OVERCLOCK
|
||||
bool "Run LCD at higher clock speed than allowed"
|
||||
default n
|
||||
|
||||
@@ -76,7 +76,7 @@ PROVIDE ( Cache_Lock_ICache_Items = 0x40018950 );
|
||||
PROVIDE ( Cache_Mask_All = 0x40018458 );
|
||||
PROVIDE ( cache_memory_baseaddrs = 0x3ffaf020 );
|
||||
PROVIDE ( Cache_MMU_Init = 0x40018dd8 );
|
||||
PROVIDE ( Cache_Resume_DCache = 0x40018d3c );
|
||||
PROVIDE ( cache_resume_dcache = 0x40018d3c );
|
||||
PROVIDE ( Cache_Resume_DCache_Autoload = 0x4001850c );
|
||||
PROVIDE ( cache_resume_icache = 0x40018cdc );
|
||||
PROVIDE ( Cache_Resume_ICache_Autoload = 0x400184c4 );
|
||||
@@ -85,7 +85,7 @@ PROVIDE ( Cache_Set_Default_Mode = 0x4001810c );
|
||||
PROVIDE ( cache_set_icache_mode = 0x4001803c );
|
||||
PROVIDE ( Cache_Start_DCache_Preload = 0x400185c4 );
|
||||
PROVIDE ( Cache_Start_ICache_Preload = 0x40018530 );
|
||||
PROVIDE ( Cache_Suspend_DCache = 0x40018d04 );
|
||||
PROVIDE ( cache_suspend_dcache = 0x40018d04 );
|
||||
PROVIDE ( Cache_Suspend_DCache_Autoload = 0x400184e0 );
|
||||
PROVIDE ( cache_suspend_icache = 0x40018ca4 );
|
||||
PROVIDE ( Cache_Suspend_ICache_Autoload = 0x40018498 );
|
||||
@@ -348,7 +348,7 @@ PROVIDE ( ets_write_char_uart = 0x4000f998 );
|
||||
PROVIDE ( exc_cause_table = 0x3ffacbe8 );
|
||||
PROVIDE ( FilePacketSendDeflatedReqMsgProc = 0x40011ed8 );
|
||||
PROVIDE ( FilePacketSendReqMsgProc = 0x40011bd8 );
|
||||
PROVIDE ( flashchip = 0x3ffffd38 );
|
||||
PROVIDE ( g_rom_flashchip = 0x3ffffd38 );
|
||||
PROVIDE ( FlashDwnLdDeflatedStartMsgProc = 0x40011e80 );
|
||||
PROVIDE ( FlashDwnLdParamCfgMsgProc = 0x40011cc0 );
|
||||
PROVIDE ( FlashDwnLdStartMsgProc = 0x40011b74 );
|
||||
|
||||
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_WATCHDOG),y)
|
||||
CSRCS += esp32s2_board_wdt.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ESP32S2_SPIFLASH),y)
|
||||
CSRCS += esp32s2_board_spiflash.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_SENSORS_MAX6675),y)
|
||||
CSRCS += esp32s2_max6675.c
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
/****************************************************************************
|
||||
* boards/xtensa/esp32s2/common/src/esp32s2_board_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 <nuttx/config.h>
|
||||
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include "inttypes.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/spi/spi.h>
|
||||
#include <nuttx/mtd/mtd.h>
|
||||
#include <nuttx/fs/nxffs.h>
|
||||
#ifdef CONFIG_BCH
|
||||
#include <nuttx/drivers/drivers.h>
|
||||
#endif
|
||||
|
||||
#include "esp32s2_spiflash.h"
|
||||
#include "esp32s2_spiflash_mtd.h"
|
||||
#include "esp32s2-saola-1.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ESP32S2_STORAGE_MTD_OFFSET
|
||||
# define CONFIG_ESP32S2_STORAGE_MTD_OFFSET 0x10000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_ESP32S2_STORAGE_MTD_SIZE
|
||||
# define CONFIG_ESP32S2_STORAGE_MTD_SIZE 0x100000
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* 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_ESP32S2_SPIFLASH_SMARTFS)
|
||||
static int setup_smartfs(int smartn, 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_ESP32S2_SPIFLASH_LITTLEFS)
|
||||
static int setup_littlefs(const char *path, 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_ESP32S2_SPIFLASH_SPIFFS)
|
||||
static int setup_spiffs(const char *path, 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_ESP32S2_SPIFLASH_NXFFS)
|
||||
static int setup_nxffs(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_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;
|
||||
struct mtd_dev_s *mtd;
|
||||
|
||||
mtd = esp32s2_spiflash_alloc_mtdpart(CONFIG_ESP32S2_STORAGE_MTD_OFFSET,
|
||||
CONFIG_ESP32S2_STORAGE_MTD_SIZE,
|
||||
false);
|
||||
if (!mtd)
|
||||
{
|
||||
ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
#if defined (CONFIG_ESP32S2_SPIFLASH_SMARTFS)
|
||||
|
||||
ret = setup_smartfs(0, mtd, "/data");
|
||||
if (ret < 0)
|
||||
{
|
||||
ferr("ERROR: Failed to setup smartfs\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#elif defined (CONFIG_ESP32S2_SPIFLASH_NXFFS)
|
||||
|
||||
ret = setup_nxffs(mtd, "/data");
|
||||
if (ret < 0)
|
||||
{
|
||||
ferr("ERROR: Failed to setup nxffs\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#elif defined (CONFIG_ESP32S2_SPIFLASH_LITTLEFS)
|
||||
|
||||
const char *path = "/dev/esp32s2flash";
|
||||
ret = setup_littlefs(path, mtd, "/data", 0755);
|
||||
if (ret < 0)
|
||||
{
|
||||
ferr("ERROR: Failed to setup littlefs\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#elif defined (CONFIG_ESP32S2_SPIFLASH_SPIFFS)
|
||||
|
||||
const char *path = "/dev/esp32s2flash";
|
||||
ret = setup_spiffs(path, mtd, "/data", 0755);
|
||||
if (ret < 0)
|
||||
{
|
||||
ferr("ERROR: Failed to setup spiffs\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
ret = register_mtddriver("/dev/esp32s2flash", mtd, 0755, NULL);
|
||||
if (ret < 0)
|
||||
{
|
||||
ferr("ERROR: Failed to register MTD: %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: board_spiflash_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize the SPIFLASH and register the MTD device.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int board_spiflash_init(void)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
ret = esp32s2_spiflash_init();
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = init_storage_partition();
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -81,6 +81,18 @@
|
||||
|
||||
int esp32s2_bringup(void);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: board_spiflash_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize the SPIFLASH and register the MTD device.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_ESP32S2_SPIFLASH
|
||||
int board_spiflash_init(void);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: esp32s2_gpio_init
|
||||
*
|
||||
|
||||
@@ -145,6 +145,14 @@ int esp32s2_bringup(void)
|
||||
}
|
||||
#endif /* CONFIG_ESP32S2_LEDC */
|
||||
|
||||
#ifdef CONFIG_ESP32S2_SPIFLASH
|
||||
ret = board_spiflash_init();
|
||||
if (ret)
|
||||
{
|
||||
syslog(LOG_ERR, "ERROR: Failed to initialize SPI Flash\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_DEV_GPIO
|
||||
ret = esp32s2_gpio_init();
|
||||
if (ret < 0)
|
||||
|
||||
Reference in New Issue
Block a user