From 09587ab6c8fd5b8cae0116ddc5a71dbe9e32c416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Sun, 5 Nov 2023 21:33:16 +0100 Subject: [PATCH] Added wrapper to NVM device API for Zephyr flash API. Wrapped Zephyr flash API calls for OpenRTX interface, added additional settings specific to T-TWR Plus. TG-553 --- platform/drivers/NVM/flash_zephyr.c | 72 ++++++++++++++++++++ platform/drivers/NVM/flash_zephyr.h | 52 ++++++++++++++ platform/targets/ttwrplus/CMakeLists.txt | 1 + platform/targets/ttwrplus/ttwrplus_defconfig | 3 + 4 files changed, 128 insertions(+) create mode 100644 platform/drivers/NVM/flash_zephyr.c create mode 100644 platform/drivers/NVM/flash_zephyr.h diff --git a/platform/drivers/NVM/flash_zephyr.c b/platform/drivers/NVM/flash_zephyr.c new file mode 100644 index 00000000..c06cb934 --- /dev/null +++ b/platform/drivers/NVM/flash_zephyr.c @@ -0,0 +1,72 @@ +/*************************************************************************** + * Copyright (C) 2023 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#include +#include +#include "flash_zephyr.h" + +static int nvm_api_read(const struct nvmDevice *dev, uint32_t offset, + void *data, size_t len) +{ + const struct device *fDev = (const struct device *)(dev->config); + + return flash_read(fDev, offset, data, len); +} + +static int nvm_api_write(const struct nvmDevice *dev, uint32_t offset, + const void *data, size_t len) +{ + const struct device *fDev = (const struct device *)(dev->config); + + return flash_write(fDev, offset, data, len); +} + +static int nvm_api_erase(const struct nvmDevice *dev, uint32_t offset, size_t size) +{ + const struct device *fDev = (const struct device *)(dev->config); + + return flash_erase(fDev, offset, size); +} + +static const struct nvmParams *nvm_api_params(const struct nvmDevice *dev) +{ + struct nvmParams *params = (struct nvmParams *)(dev->priv); + const struct device *fDev = (const struct device *)(dev->config); + + // Retrieve write size + const struct flash_parameters *info = flash_get_parameters(fDev); + params->write_size = info->write_block_size; + + // TODO: erase size and erase cycles to be retrieved from the real device. + params->erase_size = 4096; + params->erase_cycles = 100000; + params->type = NVM_FLASH; + + return params; +} + +const struct nvmApi zephyr_flash_api = +{ + .read = nvm_api_read, + .write = nvm_api_write, + .erase = nvm_api_erase, + .sync = NULL, + .params = nvm_api_params +}; diff --git a/platform/drivers/NVM/flash_zephyr.h b/platform/drivers/NVM/flash_zephyr.h new file mode 100644 index 00000000..2c95780a --- /dev/null +++ b/platform/drivers/NVM/flash_zephyr.h @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2023 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef FLASH_ZEPHYR_H +#define FLASH_ZEPHYR_H + +#include + +/** + * Wrapper interface for the Zephyr RTOS flash memory device driver. + */ + +/** + * Device driver API for Zephyr RTOS flash memory. + */ +extern const struct nvmApi zephyr_flash_api; + + +/** + * Instantiate a nonvolatile memory device based on Zephyr RTOS flash device + * driver. + * + * @param name: device name. + * @param alias: devicetree alias of the flash device. + */ +#define ZEPHYR_FLASH_DEVICE_DEFINE(name, alias) \ +static struct nvmParams flash_params_##name; \ +static const struct nvmDevice name = \ +{ \ + .config = DEVICE_DT_GET(DT_ALIAS(alias)), \ + .priv = &flash_params_##name, \ + .api = &zephyr_flash_api \ +}; + +#endif /* FLASH_ZEPHYR_H */ diff --git a/platform/targets/ttwrplus/CMakeLists.txt b/platform/targets/ttwrplus/CMakeLists.txt index e1b58c6f..5404221e 100644 --- a/platform/targets/ttwrplus/CMakeLists.txt +++ b/platform/targets/ttwrplus/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(app ${OPENRTX_ROOT}/platform/drivers/baseband/SA8x8.c ${OPENRTX_ROOT}/platform/drivers/GPS/GPS_ttwrplus.c ${OPENRTX_ROOT}/platform/drivers/audio/audio_ttwrplus.c + ${OPENRTX_ROOT}/platform/drivers/NVM/flash_zephyr.c ${OPENRTX_ROOT}/platform/drivers/stubs/cps_io_stub.c ${OPENRTX_ROOT}/platform/drivers/stubs/nvmem_stub.c diff --git a/platform/targets/ttwrplus/ttwrplus_defconfig b/platform/targets/ttwrplus/ttwrplus_defconfig index cd8dc8da..96084876 100644 --- a/platform/targets/ttwrplus/ttwrplus_defconfig +++ b/platform/targets/ttwrplus/ttwrplus_defconfig @@ -21,3 +21,6 @@ CONFIG_WS2812_STRIP=y CONFIG_WS2812_STRIP_SPI=y CONFIG_PICOLIBC_IO_FLOAT=y CONFIG_POSIX_API=y +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_SOC_FLASH_ESP32=y