xtensa/esp32: Make SPI Flash initialization common to all ESP32 boards
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
This commit is contained in:
parent
018aa8eb8d
commit
7368f7a2c8
68
boards/xtensa/esp32/common/include/esp32_board_spiflash.h
Normal file
68
boards/xtensa/esp32/common/include/esp32_board_spiflash.h
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* boards/xtensa/esp32/common/include/esp32_board_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 __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_SPIFLASH_H
|
||||||
|
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_SPIFLASH_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef __ASSEMBLY__
|
||||||
|
|
||||||
|
#undef EXTERN
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
#define EXTERN extern "C"
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#else
|
||||||
|
#define EXTERN extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: esp32_spiflash_init
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Initialize the SPI Flash and register the MTD.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* None.
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) is returned on success. A negated errno value is returned
|
||||||
|
* on failure.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int esp32_spiflash_init(void);
|
||||||
|
|
||||||
|
#undef EXTERN
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __ASSEMBLY__ */
|
||||||
|
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_SPIFLASH_H */
|
@ -38,6 +38,10 @@ ifeq ($(CONFIG_SPI_DRIVER),y)
|
|||||||
CSRCS += esp32_board_spidev.c
|
CSRCS += esp32_board_spidev.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
|
||||||
|
CSRCS += esp32_board_spiflash.c
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_ESP32_WIRELESS),y)
|
ifeq ($(CONFIG_ESP32_WIRELESS),y)
|
||||||
CSRCS += esp32_board_wlan.c
|
CSRCS += esp32_board_wlan.c
|
||||||
endif
|
endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* boards/xtensa/esp32/esp32-devkitc/src/esp32_spiflash.c
|
* boards/xtensa/esp32/common/src/esp32_board_spiflash.c
|
||||||
*
|
*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
@ -31,16 +31,19 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
|
#include <nuttx/fs/fs.h>
|
||||||
#include <nuttx/kmalloc.h>
|
#include <nuttx/kmalloc.h>
|
||||||
#include <nuttx/spi/spi.h>
|
|
||||||
#include <nuttx/mtd/mtd.h>
|
#include <nuttx/mtd/mtd.h>
|
||||||
|
#include <nuttx/spi/spi.h>
|
||||||
|
#ifdef CONFIG_ESP32_SPIFLASH_NXFFS
|
||||||
#include <nuttx/fs/nxffs.h>
|
#include <nuttx/fs/nxffs.h>
|
||||||
|
#endif
|
||||||
#ifdef CONFIG_BCH
|
#ifdef CONFIG_BCH
|
||||||
#include <nuttx/drivers/drivers.h>
|
#include <nuttx/drivers/drivers.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "esp32_spiflash.h"
|
#include "esp32_spiflash.h"
|
||||||
#include "esp32-devkitc.h"
|
#include "esp32_board_spiflash.h"
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
@ -53,14 +56,12 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
||||||
|
|
||||||
struct ota_partition_s
|
struct ota_partition_s
|
||||||
{
|
{
|
||||||
uint32_t offset; /* Partition offset from the beginning of MTD */
|
uint32_t offset; /* Partition offset from the beginning of MTD */
|
||||||
uint32_t size; /* Partition size in bytes */
|
uint32_t size; /* Partition size in bytes */
|
||||||
const char *devpath; /* Partition device path */
|
const char *devpath; /* Partition device path */
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -135,7 +136,6 @@ static int init_ota_partitions(void)
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -156,7 +156,7 @@ static int init_ota_partitions(void)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_SMARTFS
|
||||||
static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd,
|
static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd,
|
||||||
const char *mnt_pt)
|
const char *mnt_pt)
|
||||||
{
|
{
|
||||||
@ -197,7 +197,6 @@ static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd,
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -217,7 +216,7 @@ static int setup_smartfs(int smartn, FAR struct mtd_dev_s *mtd,
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_LITTLEFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_LITTLEFS
|
||||||
static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd,
|
static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd,
|
||||||
const char *mnt_pt, int priv)
|
const char *mnt_pt, int priv)
|
||||||
{
|
{
|
||||||
@ -246,7 +245,6 @@ static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd,
|
|||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -266,7 +264,7 @@ static int setup_littlefs(const char *path, FAR struct mtd_dev_s *mtd,
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_SPIFFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_SPIFFS
|
||||||
static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd,
|
static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd,
|
||||||
const char *mnt_pt, int priv)
|
const char *mnt_pt, int priv)
|
||||||
{
|
{
|
||||||
@ -291,7 +289,6 @@ static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd,
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -309,7 +306,7 @@ static int setup_spiffs(const char *path, FAR struct mtd_dev_s *mtd,
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_NXFFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_NXFFS
|
||||||
static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt)
|
static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt)
|
||||||
{
|
{
|
||||||
int ret = OK;
|
int ret = OK;
|
||||||
@ -333,8 +330,8 @@ static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt)
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: init_wifi_partition
|
* Name: init_wifi_partition
|
||||||
*
|
*
|
||||||
@ -346,7 +343,7 @@ static int setup_nxffs(FAR struct mtd_dev_s *mtd, const char *mnt_pt)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_WIFI_SAVE_PARAM)
|
#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM
|
||||||
static int init_wifi_partition(void)
|
static int init_wifi_partition(void)
|
||||||
{
|
{
|
||||||
int ret = OK;
|
int ret = OK;
|
||||||
@ -360,7 +357,7 @@ static int init_wifi_partition(void)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_SMARTFS
|
||||||
|
|
||||||
ret = setup_smartfs(1, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT);
|
ret = setup_smartfs(1, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -369,7 +366,7 @@ static int init_wifi_partition(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS)
|
#elif defined(CONFIG_ESP32_SPIFLASH_LITTLEFS)
|
||||||
|
|
||||||
const char *path = "/dev/mtdblock1";
|
const char *path = "/dev/mtdblock1";
|
||||||
ret = setup_littlefs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777);
|
ret = setup_littlefs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777);
|
||||||
@ -379,7 +376,7 @@ static int init_wifi_partition(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS)
|
#elif defined(CONFIG_ESP32_SPIFLASH_SPIFFS)
|
||||||
|
|
||||||
const char *path = "/dev/mtdblock1";
|
const char *path = "/dev/mtdblock1";
|
||||||
ret = setup_spiffs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777);
|
ret = setup_spiffs(path, mtd, CONFIG_ESP32_WIFI_FS_MOUNTPT, 0777);
|
||||||
@ -398,8 +395,8 @@ static int init_wifi_partition(void)
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: init_storage_partition
|
* Name: init_storage_partition
|
||||||
*
|
*
|
||||||
@ -424,7 +421,7 @@ static int init_storage_partition(void)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS)
|
#ifdef CONFIG_ESP32_SPIFLASH_SMARTFS
|
||||||
|
|
||||||
ret = setup_smartfs(0, mtd, NULL);
|
ret = setup_smartfs(0, mtd, NULL);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -433,7 +430,7 @@ static int init_storage_partition(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS)
|
#elif defined(CONFIG_ESP32_SPIFLASH_NXFFS)
|
||||||
|
|
||||||
ret = setup_nxffs(mtd, "/mnt");
|
ret = setup_nxffs(mtd, "/mnt");
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -442,7 +439,7 @@ static int init_storage_partition(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined (CONFIG_ESP32_SPIFLASH_LITTLEFS)
|
#elif defined(CONFIG_ESP32_SPIFLASH_LITTLEFS)
|
||||||
|
|
||||||
const char *path = "/dev/esp32flash";
|
const char *path = "/dev/esp32flash";
|
||||||
ret = setup_littlefs(path, mtd, NULL, 0755);
|
ret = setup_littlefs(path, mtd, NULL, 0755);
|
||||||
@ -452,7 +449,7 @@ static int init_storage_partition(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined (CONFIG_ESP32_SPIFLASH_SPIFFS)
|
#elif defined(CONFIG_ESP32_SPIFLASH_SPIFFS)
|
||||||
|
|
||||||
const char *path = "/dev/esp32flash";
|
const char *path = "/dev/esp32flash";
|
||||||
ret = setup_spiffs(path, mtd, NULL, 0755);
|
ret = setup_spiffs(path, mtd, NULL, 0755);
|
||||||
@ -484,7 +481,15 @@ static int init_storage_partition(void)
|
|||||||
* Name: esp32_spiflash_init
|
* Name: esp32_spiflash_init
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Initialize the SPIFLASH and register the MTD device.
|
* Initialize the SPI Flash and register the MTD.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* None.
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) is returned on success. A negated errno value is returned
|
||||||
|
* on failure.
|
||||||
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int esp32_spiflash_init(void)
|
int esp32_spiflash_init(void)
|
@ -35,10 +35,6 @@ ifeq ($(CONFIG_MMCSD),y)
|
|||||||
CSRCS += esp32_mmcsd.c
|
CSRCS += esp32_mmcsd.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
|
|
||||||
CSRCS += esp32_spiflash.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(CONFIG_DEV_GPIO),y)
|
ifeq ($(CONFIG_DEV_GPIO),y)
|
||||||
CSRCS += esp32_gpio.c
|
CSRCS += esp32_gpio.c
|
||||||
endif
|
endif
|
||||||
|
@ -101,15 +101,6 @@ int esp32_bringup(void);
|
|||||||
|
|
||||||
int esp32_mmcsd_initialize(int minor);
|
int esp32_mmcsd_initialize(int minor);
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: esp32_spiflash_init
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Initialize the SPIFLASH and register the MTD device.
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int esp32_spiflash_init(void);
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: esp32_gpio_init
|
* Name: esp32_gpio_init
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@ -39,7 +39,6 @@
|
|||||||
#include <nuttx/fs/fs.h>
|
#include <nuttx/fs/fs.h>
|
||||||
#include <nuttx/himem/himem.h>
|
#include <nuttx/himem/himem.h>
|
||||||
|
|
||||||
#include "esp32_spiflash.h"
|
|
||||||
#include "esp32_partition.h"
|
#include "esp32_partition.h"
|
||||||
|
|
||||||
#ifdef CONFIG_USERLED
|
#ifdef CONFIG_USERLED
|
||||||
@ -62,6 +61,10 @@
|
|||||||
# include "esp32_board_wdt.h"
|
# include "esp32_board_wdt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_ESP32_SPIFLASH
|
||||||
|
# include "esp32_board_spiflash.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_BLE
|
#ifdef CONFIG_ESP32_BLE
|
||||||
# include "esp32_ble.h"
|
# include "esp32_ble.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -35,10 +35,6 @@ ifeq ($(CONFIG_MMCSD),y)
|
|||||||
CSRCS += esp32_mmcsd.c
|
CSRCS += esp32_mmcsd.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
|
|
||||||
CSRCS += esp32_spiflash.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(CONFIG_ARCH_BUTTONS),y)
|
ifeq ($(CONFIG_ARCH_BUTTONS),y)
|
||||||
CSRCS += esp32_buttons.c
|
CSRCS += esp32_buttons.c
|
||||||
endif
|
endif
|
||||||
|
@ -39,7 +39,6 @@
|
|||||||
#include <nuttx/fs/fs.h>
|
#include <nuttx/fs/fs.h>
|
||||||
#include <nuttx/himem/himem.h>
|
#include <nuttx/himem/himem.h>
|
||||||
|
|
||||||
#include "esp32_spiflash.h"
|
|
||||||
#include "esp32_partition.h"
|
#include "esp32_partition.h"
|
||||||
|
|
||||||
#ifdef CONFIG_USERLED
|
#ifdef CONFIG_USERLED
|
||||||
@ -62,6 +61,10 @@
|
|||||||
# include "esp32_rt_timer.h"
|
# include "esp32_rt_timer.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_ESP32_SPIFLASH
|
||||||
|
# include "esp32_board_spiflash.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_BLE
|
#ifdef CONFIG_ESP32_BLE
|
||||||
# include "esp32_ble.h"
|
# include "esp32_ble.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,517 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
* boards/xtensa/esp32/esp32-ethernet-kit/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 <nuttx/config.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 "esp32_spiflash.h"
|
|
||||||
#include "esp32-ethernet-kit.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Pre-processor Definitions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Types
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
|
|
||||||
struct ota_partition_s
|
|
||||||
{
|
|
||||||
uint32_t offset; /* Partition offset from the beginning of MTD */
|
|
||||||
uint32_t size; /* Partition size in bytes */
|
|
||||||
const char *devpath; /* Partition device path */
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Function Prototypes
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Data
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static const struct ota_partition_s g_ota_partition_table[] =
|
|
||||||
{
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SCRATCH_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SCRATCH_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SCRATCH_DEVPATH
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Functions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void)
|
|
||||||
{
|
|
||||||
FAR struct mtd_dev_s *mtd;
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
char blockdev[18];
|
|
||||||
#endif
|
|
||||||
int ret = OK;
|
|
||||||
|
|
||||||
for (int i = 0; i < ARRAYSIZE(g_ota_partition_table); ++i)
|
|
||||||
{
|
|
||||||
const struct ota_partition_s *part = &g_ota_partition_table[i];
|
|
||||||
mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size);
|
|
||||||
|
|
||||||
ret = ftl_initialize(i, mtd);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
snprintf(blockdev, 18, "/dev/mtdblock%d", i);
|
|
||||||
|
|
||||||
ret = bchdev_register(blockdev, part->devpath, false);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: esp32_spiflash_init
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Initialize the SPIFLASH and register the MTD device.
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int esp32_spiflash_init(void)
|
|
||||||
{
|
|
||||||
int ret = OK;
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
ret = init_ota_partitions();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM
|
|
||||||
ret = init_wifi_partition();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ret = init_storage_partition();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
@ -41,10 +41,6 @@ ifeq ($(CONFIG_MMCSD),y)
|
|||||||
CSRCS += esp32_mmcsd.c
|
CSRCS += esp32_mmcsd.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
|
|
||||||
CSRCS += esp32_spiflash.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(CONFIG_DEV_GPIO),y)
|
ifeq ($(CONFIG_DEV_GPIO),y)
|
||||||
CSRCS += esp32_gpio.c
|
CSRCS += esp32_gpio.c
|
||||||
endif
|
endif
|
||||||
|
@ -40,7 +40,6 @@
|
|||||||
#include <nuttx/fs/fs.h>
|
#include <nuttx/fs/fs.h>
|
||||||
#include <nuttx/himem/himem.h>
|
#include <nuttx/himem/himem.h>
|
||||||
|
|
||||||
#include "esp32_spiflash.h"
|
|
||||||
#include "esp32_partition.h"
|
#include "esp32_partition.h"
|
||||||
|
|
||||||
#include <arch/board/board.h>
|
#include <arch/board/board.h>
|
||||||
@ -61,6 +60,10 @@
|
|||||||
# include "esp32_board_wdt.h"
|
# include "esp32_board_wdt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_ESP32_SPIFLASH
|
||||||
|
# include "esp32_board_spiflash.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_BLE
|
#ifdef CONFIG_ESP32_BLE
|
||||||
# include "esp32_ble.h"
|
# include "esp32_ble.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,517 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
* boards/xtensa/esp32/esp32-wrover-kit/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 <nuttx/config.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 "esp32_spiflash.h"
|
|
||||||
#include "esp32-wrover-kit.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Pre-processor Definitions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Types
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
|
|
||||||
struct ota_partition_s
|
|
||||||
{
|
|
||||||
uint32_t offset; /* Partition offset from the beginning of MTD */
|
|
||||||
uint32_t size; /* Partition size in bytes */
|
|
||||||
const char *devpath; /* Partition device path */
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Function Prototypes
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Data
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static const struct ota_partition_s g_ota_partition_table[] =
|
|
||||||
{
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SCRATCH_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SCRATCH_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SCRATCH_DEVPATH
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Functions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void)
|
|
||||||
{
|
|
||||||
FAR struct mtd_dev_s *mtd;
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
char blockdev[18];
|
|
||||||
#endif
|
|
||||||
int ret = OK;
|
|
||||||
|
|
||||||
for (int i = 0; i < ARRAYSIZE(g_ota_partition_table); ++i)
|
|
||||||
{
|
|
||||||
const struct ota_partition_s *part = &g_ota_partition_table[i];
|
|
||||||
mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size);
|
|
||||||
|
|
||||||
ret = ftl_initialize(i, mtd);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
snprintf(blockdev, 18, "/dev/mtdblock%d", i);
|
|
||||||
|
|
||||||
ret = bchdev_register(blockdev, part->devpath, false);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: esp32_spiflash_init
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Initialize the SPIFLASH and register the MTD device.
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int esp32_spiflash_init(void)
|
|
||||||
{
|
|
||||||
int ret = OK;
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
ret = init_ota_partitions();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_WIFI_SAVE_PARAM
|
|
||||||
ret = init_wifi_partition();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ret = init_storage_partition();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
@ -35,10 +35,6 @@ ifeq ($(CONFIG_MMCSD),y)
|
|||||||
CSRCS += esp32_mmcsd.c
|
CSRCS += esp32_mmcsd.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
|
|
||||||
CSRCS += esp32_spiflash.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(CONFIG_DEV_GPIO),y)
|
ifeq ($(CONFIG_DEV_GPIO),y)
|
||||||
CSRCS += esp32_gpio.c
|
CSRCS += esp32_gpio.c
|
||||||
endif
|
endif
|
||||||
|
@ -39,7 +39,6 @@
|
|||||||
#include <nuttx/fs/fs.h>
|
#include <nuttx/fs/fs.h>
|
||||||
#include <nuttx/himem/himem.h>
|
#include <nuttx/himem/himem.h>
|
||||||
|
|
||||||
#include "esp32_spiflash.h"
|
|
||||||
#include "esp32_partition.h"
|
#include "esp32_partition.h"
|
||||||
|
|
||||||
#ifdef CONFIG_USERLED
|
#ifdef CONFIG_USERLED
|
||||||
@ -62,6 +61,10 @@
|
|||||||
# include "esp32_board_wdt.h"
|
# include "esp32_board_wdt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_ESP32_SPIFLASH
|
||||||
|
# include "esp32_board_spiflash.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_BLE
|
#ifdef CONFIG_ESP32_BLE
|
||||||
# include "esp32_ble.h"
|
# include "esp32_ble.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,208 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
* boards/xtensa/esp32/ttgo_lora_esp32/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 <nuttx/config.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 "esp32_spiflash.h"
|
|
||||||
#include "ttgo_lora_esp32.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Pre-processor Definitions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#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
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
|
|
||||||
struct ota_partition_s
|
|
||||||
{
|
|
||||||
uint32_t offset; /* Partition offset from the beginning of MTD */
|
|
||||||
uint32_t size; /* Partition size in bytes */
|
|
||||||
const char *devpath; /* Partition device path */
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Function Prototypes
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Data
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static const struct ota_partition_s g_ota_partition_table[] =
|
|
||||||
{
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SLOT_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.offset = CONFIG_ESP32_OTA_SCRATCH_OFFSET,
|
|
||||||
.size = CONFIG_ESP32_OTA_SCRATCH_SIZE,
|
|
||||||
.devpath = CONFIG_ESP32_OTA_SCRATCH_DEVPATH
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Private Functions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
static int init_ota_partitions(void)
|
|
||||||
{
|
|
||||||
FAR struct mtd_dev_s *mtd;
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
char blockdev[18];
|
|
||||||
#endif
|
|
||||||
int ret = OK;
|
|
||||||
|
|
||||||
for (int i = 0; i < ARRAYSIZE(g_ota_partition_table); ++i)
|
|
||||||
{
|
|
||||||
const struct ota_partition_s *part = &g_ota_partition_table[i];
|
|
||||||
mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size);
|
|
||||||
|
|
||||||
ret = ftl_initialize(i, mtd);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_BCH
|
|
||||||
snprintf(blockdev, 18, "/dev/mtdblock%d", i);
|
|
||||||
|
|
||||||
ret = bchdev_register(blockdev, part->devpath, false);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION
|
|
||||||
ret = init_ota_partitions();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mtd = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE);
|
|
||||||
|
|
||||||
#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 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
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user