Skip to content

Commit

Permalink
ESP32C3 support
Browse files Browse the repository at this point in the history
Rename `esp32_*` to `esp32xx_*`.

cesanta/mongoose-os#564
  • Loading branch information
rojer committed Jul 30, 2022
1 parent 5fa85fd commit efd35a9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 50 deletions.
13 changes: 5 additions & 8 deletions include/esp32/esp32_fs.h → include/esp32xx/esp32xx_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* limitations under the License.
*/

#ifndef CS_FW_PLATFORMS_ESP32_SRC_ESP32_FS_H_
#define CS_FW_PLATFORMS_ESP32_SRC_ESP32_FS_H_
#pragma once

#include <esp_vfs.h> /* for esp_vfs_translate_fd */
#include <stdbool.h>
Expand All @@ -27,17 +26,17 @@
extern "C" {
#endif

const esp_partition_t *esp32_find_fs_for_app_slot(int app_slot);
const esp_partition_t *esp32xx_find_fs_for_app_slot(int app_slot);

bool esp32_fs_crypt_init(void);
bool esp32xx_fs_crypt_init(void);

bool esp32_fs_mount_part(const char *label, const char *path,
bool esp32xx_fs_mount_part(const char *label, const char *path,
const char *fs_type, const char *fs_opts);

#define SUBTYPE_TO_SLOT(st) ((st) -ESP_PARTITION_SUBTYPE_OTA(0))
#define NUM_OTA_SLOTS \
(ESP_PARTITION_SUBTYPE_APP_OTA_MAX - ESP_PARTITION_SUBTYPE_APP_OTA_MIN)
int esp32_get_boot_slot();
int esp32xx_get_boot_slot();

/*
* Translate file descriptor returned by open() to the one suitable for use
Expand All @@ -48,5 +47,3 @@ int esp32_get_boot_slot();
#ifdef __cplusplus
}
#endif

#endif /* CS_FW_PLATFORMS_ESP32_SRC_ESP32_FS_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
* limitations under the License.
*/

#ifndef CS_FW_PLATFORMS_ESP32_SRC_ESP32_VFS_DEV_PARTITION_H_
#define CS_FW_PLATFORMS_ESP32_SRC_ESP32_VFS_DEV_PARTITION_H_
#pragma once

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#define MGOS_VFS_DEV_TYPE_ESP32_PARTITION "esp32part"
#define MGOS_VFS_DEV_TYPE_ESP32XX_PARTITION "esp32part"

bool esp32_vfs_dev_partition_register_type(void);
bool esp32xx_vfs_dev_partition_register_type(void);

#ifdef __cplusplus
}
#endif

#endif /* CS_FW_PLATFORMS_ESP32_SRC_ESP32_VFS_DEV_PARTITION_H_ */
14 changes: 14 additions & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ includes:
- include
- include/${platform}

conds:
- when: mos.platform == "esp32"
apply:
sources:
- src/esp32xx
includes:
- include/esp32xx
- when: mos.platform == "esp32c3"
apply:
sources:
- src/esp32xx
includes:
- include/esp32xx

no_implicit_init_deps: true
init_deps: []

Expand Down
35 changes: 21 additions & 14 deletions src/esp32/esp32_fs.c → src/esp32xx/esp32xx_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#include "esp32_fs.h"
#include "esp32xx_fs.h"

#include <errno.h>
#include <stdbool.h>
Expand All @@ -40,9 +40,9 @@
#include "mgos_vfs_fs_spiffs.h"
#include "mgos_vfs_internal.h"

#include "esp32_vfs_dev_partition.h"
#include "esp32xx_vfs_dev_partition.h"

const esp_partition_t *esp32_find_fs_for_app_slot(int slot) {
const esp_partition_t *esp32xx_find_fs_for_app_slot(int slot) {
char ota_fs_part_name[5] = {'f', 's', '_', 0, 0};
const char *fs_part_name = NULL;
/*
Expand All @@ -57,14 +57,14 @@ const esp_partition_t *esp32_find_fs_for_app_slot(int slot) {
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, fs_part_name);
}

int esp32_get_boot_slot() {
int esp32xx_get_boot_slot() {
const esp_partition_t *p = esp_ota_get_running_partition();
if (p == NULL) return -1;
return SUBTYPE_TO_SLOT(p->subtype);
}

bool esp32_fs_mount_part(const char *label, const char *path,
const char *fs_type, const char *fs_opts) {
bool esp32xx_fs_mount_part(const char *label, const char *path,
const char *fs_type, const char *fs_opts) {
bool encrypt = false;
#if CS_SPIFFS_ENABLE_ENCRYPTION
encrypt = esp_flash_encryption_enabled();
Expand All @@ -78,36 +78,36 @@ bool esp32_fs_mount_part(const char *label, const char *path,
return mgos_vfs_mount_dev_name(path, label, fs_type, fs_opts_c);
}

static void esp32_register_partition_devs(void) {
static void esp32xx_register_partition_devs(void) {
esp_partition_iterator_t pit = esp_partition_find(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (pit != NULL) {
const esp_partition_t *p = esp_partition_get(pit);
char dev_opts[100];
struct json_out out = JSON_OUT_BUF(dev_opts, sizeof(dev_opts));
json_printf(&out, "{label: %Q}", p->label);
mgos_vfs_dev_create_and_register(MGOS_VFS_DEV_TYPE_ESP32_PARTITION,
mgos_vfs_dev_create_and_register(MGOS_VFS_DEV_TYPE_ESP32XX_PARTITION,
dev_opts, p->label);
pit = esp_partition_next(pit);
}
}

bool mgos_core_fs_init(void) {
#if CS_SPIFFS_ENABLE_ENCRYPTION
if (esp_flash_encryption_enabled() && !esp32_fs_crypt_init()) {
if (esp_flash_encryption_enabled() && !esp32xx_fs_crypt_init()) {
LOG(LL_ERROR, ("Failed to initialize FS encryption key"));
return MGOS_INIT_FS_INIT_FAILED;
}
#endif
esp32_register_partition_devs();
esp32xx_register_partition_devs();
const esp_partition_t *fs_part =
esp32_find_fs_for_app_slot(esp32_get_boot_slot());
esp32xx_find_fs_for_app_slot(esp32xx_get_boot_slot());
if (fs_part == NULL) {
LOG(LL_ERROR, ("No FS partition"));
return false;
}
return esp32_fs_mount_part(fs_part->label, "/", mgos_vfs_get_root_fs_type(),
mgos_vfs_get_root_fs_opts());
return esp32xx_fs_mount_part(fs_part->label, "/", mgos_vfs_get_root_fs_type(),
mgos_vfs_get_root_fs_opts());
}

bool mgos_vfs_common_init(void) {
Expand All @@ -133,5 +133,12 @@ bool mgos_vfs_common_init(void) {
LOG(LL_ERROR, ("ESP VFS registration failed"));
return false;
}
return esp32_vfs_dev_partition_register_type();
return esp32xx_vfs_dev_partition_register_type();
}

// Temp, for OTA bin libs compatibility. TODO(rojer): Remove once not needed.
const esp_partition_t *esp32_find_fs_for_app_slot(int slot)
__attribute__((alias("esp32xx_find_fs_for_app_slot")));
bool esp32_fs_mount_part(const char *label, const char *path,
const char *fs_type, const char *fs_opts)
__attribute__((alias("esp32xx_fs_mount_part")));
6 changes: 3 additions & 3 deletions src/esp32/esp32_fs_crypt.c → src/esp32xx/esp32xx_fs_crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* limitations under the License.
*/

#include "esp32_fs.h"

#include "spiffs.h"
#include "spiffs_nucleus.h"

Expand All @@ -27,6 +25,8 @@
#include "common/cs_dbg.h"
#include "mgos_hal.h"

#include "esp32xx_fs.h"

/*
* ESP32 contains a hardware-accelerated flash encryption module. It uses a key
* securely stored in eFuses and applies AES-256 to 32-byte blocks of data.
Expand Down Expand Up @@ -55,7 +55,7 @@

mbedtls_aes_context s_aes_ctx_enc, s_aes_ctx_dec;

bool esp32_fs_crypt_init(void) {
bool esp32xx_fs_crypt_init(void) {
uint8_t tmp[32];
uint32_t addr = 0;
for (addr = 0; addr < spi_flash_get_chip_size(); addr += 32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#include "esp32_vfs_dev_partition.h"
#include "esp32xx_vfs_dev_partition.h"

#include <errno.h>
#include <stdbool.h>
Expand All @@ -32,7 +32,7 @@
#include "mgos_vfs.h"
#include "mgos_vfs_dev.h"

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_open(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_open(
struct mgos_vfs_dev *dev, const char *opts) {
enum mgos_vfs_dev_err res = MGOS_VFS_DEV_ERR_INVAL;
char *label = NULL;
Expand All @@ -56,7 +56,7 @@ static enum mgos_vfs_dev_err esp32_vfs_dev_partition_open(
return res;
}

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_read(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_read(
struct mgos_vfs_dev *dev, size_t offset, size_t len, void *dst) {
esp_err_t eres = ESP_OK;
enum mgos_vfs_dev_err res = MGOS_VFS_DEV_ERR_INVAL;
Expand All @@ -72,7 +72,7 @@ static enum mgos_vfs_dev_err esp32_vfs_dev_partition_read(
return res;
}

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_write(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_write(
struct mgos_vfs_dev *dev, size_t offset, size_t len, const void *src) {
esp_err_t eres = ESP_OK;
enum mgos_vfs_dev_err res = MGOS_VFS_DEV_ERR_INVAL;
Expand All @@ -88,7 +88,7 @@ static enum mgos_vfs_dev_err esp32_vfs_dev_partition_write(
return res;
}

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_erase(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_erase(
struct mgos_vfs_dev *dev, size_t offset, size_t len) {
esp_err_t eres = ESP_OK;
enum mgos_vfs_dev_err res = MGOS_VFS_DEV_ERR_INVAL;
Expand All @@ -104,35 +104,35 @@ static enum mgos_vfs_dev_err esp32_vfs_dev_partition_erase(
return res;
}

static size_t esp32_vfs_dev_partition_get_size(struct mgos_vfs_dev *dev) {
static size_t esp32xx_vfs_dev_partition_get_size(struct mgos_vfs_dev *dev) {
const esp_partition_t *p = (esp_partition_t *) dev->dev_data;
return p->size;
}

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_close(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_close(
struct mgos_vfs_dev *dev) {
(void) dev;
return MGOS_VFS_DEV_ERR_NONE;
}

static enum mgos_vfs_dev_err esp32_vfs_dev_partition_get_erase_sizes(
static enum mgos_vfs_dev_err esp32xx_vfs_dev_partition_get_erase_sizes(
struct mgos_vfs_dev *dev, size_t sizes[MGOS_VFS_DEV_NUM_ERASE_SIZES]) {
sizes[0] = SPI_FLASH_SEC_SIZE;
(void) dev;
return MGOS_VFS_DEV_ERR_NONE;
}

static const struct mgos_vfs_dev_ops esp32_vfs_dev_partition_ops = {
.open = esp32_vfs_dev_partition_open,
.read = esp32_vfs_dev_partition_read,
.write = esp32_vfs_dev_partition_write,
.erase = esp32_vfs_dev_partition_erase,
.get_size = esp32_vfs_dev_partition_get_size,
.close = esp32_vfs_dev_partition_close,
.get_erase_sizes = esp32_vfs_dev_partition_get_erase_sizes,
static const struct mgos_vfs_dev_ops esp32xx_vfs_dev_partition_ops = {
.open = esp32xx_vfs_dev_partition_open,
.read = esp32xx_vfs_dev_partition_read,
.write = esp32xx_vfs_dev_partition_write,
.erase = esp32xx_vfs_dev_partition_erase,
.get_size = esp32xx_vfs_dev_partition_get_size,
.close = esp32xx_vfs_dev_partition_close,
.get_erase_sizes = esp32xx_vfs_dev_partition_get_erase_sizes,
};

bool esp32_vfs_dev_partition_register_type(void) {
return mgos_vfs_dev_register_type(MGOS_VFS_DEV_TYPE_ESP32_PARTITION,
&esp32_vfs_dev_partition_ops);
bool esp32xx_vfs_dev_partition_register_type(void) {
return mgos_vfs_dev_register_type(MGOS_VFS_DEV_TYPE_ESP32XX_PARTITION,
&esp32xx_vfs_dev_partition_ops);
}

0 comments on commit efd35a9

Please sign in to comment.