From 7d323b30bfef537ba1dc414e0721d3a9d1d5f247 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Fri, 7 Feb 2020 14:10:39 -0800 Subject: [PATCH] [nrf528xx] implement otPlatFlash APIs (#4552) --- .../openthread-core-nrf52811-config.h | 30 +---- .../nrf528xx/nrf52811/platform-config.h | 14 +++ .../openthread-core-nrf52833-config.h | 30 +---- .../nrf528xx/nrf52833/platform-config.h | 14 +++ .../openthread-core-nrf52840-config.h | 30 +---- .../nrf528xx/nrf52840/platform-config.h | 14 +++ examples/platforms/nrf528xx/src/flash.c | 109 ++++++++---------- examples/platforms/nrf528xx/src/flash_nosd.c | 8 -- examples/platforms/nrf528xx/src/flash_sd.c | 1 - 9 files changed, 108 insertions(+), 142 deletions(-) diff --git a/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h b/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h index 8494fd0a6..f32d111d0 100644 --- a/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h +++ b/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h @@ -151,33 +151,15 @@ #endif /** - * @def SETTINGS_CONFIG_BASE_ADDRESS + * @def OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE * - * The base address of settings. + * Define to 1 to enable otPlatFlash* APIs to support non-volatile storage. + * + * When defined to 1, the platform MUST implement the otPlatFlash* APIs instead of the otPlatSettings* APIs. * */ -#ifndef SETTINGS_CONFIG_BASE_ADDRESS -#define SETTINGS_CONFIG_BASE_ADDRESS 0 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_SIZE - * - * The page size of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_SIZE -#define SETTINGS_CONFIG_PAGE_SIZE 4096 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_NUM - * - * The page number of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_NUM -#define SETTINGS_CONFIG_PAGE_NUM 2 +#ifndef OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE +#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 1 #endif /** diff --git a/examples/platforms/nrf528xx/nrf52811/platform-config.h b/examples/platforms/nrf528xx/nrf52811/platform-config.h index e5b05670a..30c58c08d 100644 --- a/examples/platforms/nrf528xx/nrf52811/platform-config.h +++ b/examples/platforms/nrf528xx/nrf52811/platform-config.h @@ -112,6 +112,20 @@ #define RNG_IRQ_PRIORITY 6 #endif +/******************************************************************************* + * @section Platform Flash Configuration + ******************************************************************************/ + +/** + * @def PLATFORM_FLASH_PAGE_NUM + * + * Number of flash pages to use for OpenThread's non-volatile settings. + * + */ +#ifndef PLATFORM_FLASH_PAGE_NUM +#define PLATFORM_FLASH_PAGE_NUM 2 +#endif + /******************************************************************************* * @section Platform FEM Configuration ******************************************************************************/ diff --git a/examples/platforms/nrf528xx/nrf52833/openthread-core-nrf52833-config.h b/examples/platforms/nrf528xx/nrf52833/openthread-core-nrf52833-config.h index 29f4ce9aa..3fab30a50 100644 --- a/examples/platforms/nrf528xx/nrf52833/openthread-core-nrf52833-config.h +++ b/examples/platforms/nrf528xx/nrf52833/openthread-core-nrf52833-config.h @@ -173,33 +173,15 @@ #endif /** - * @def SETTINGS_CONFIG_BASE_ADDRESS + * @def OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE * - * The base address of settings. + * Define to 1 to enable otPlatFlash* APIs to support non-volatile storage. + * + * When defined to 1, the platform MUST implement the otPlatFlash* APIs instead of the otPlatSettings* APIs. * */ -#ifndef SETTINGS_CONFIG_BASE_ADDRESS -#define SETTINGS_CONFIG_BASE_ADDRESS 0 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_SIZE - * - * The page size of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_SIZE -#define SETTINGS_CONFIG_PAGE_SIZE 4096 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_NUM - * - * The page number of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_NUM -#define SETTINGS_CONFIG_PAGE_NUM 4 +#ifndef OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE +#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 1 #endif /** diff --git a/examples/platforms/nrf528xx/nrf52833/platform-config.h b/examples/platforms/nrf528xx/nrf52833/platform-config.h index 552e4cf90..0965b6e93 100644 --- a/examples/platforms/nrf528xx/nrf52833/platform-config.h +++ b/examples/platforms/nrf528xx/nrf52833/platform-config.h @@ -111,6 +111,20 @@ #define RNG_IRQ_PRIORITY 6 #endif +/******************************************************************************* + * @section Platform Flash Configuration + ******************************************************************************/ + +/** + * @def PLATFORM_FLASH_PAGE_NUM + * + * Number of flash pages to use for OpenThread's non-volatile settings. + * + */ +#ifndef PLATFORM_FLASH_PAGE_NUM +#define PLATFORM_FLASH_PAGE_NUM 4 +#endif + /******************************************************************************* * @section Platform FEM Configuration ******************************************************************************/ diff --git a/examples/platforms/nrf528xx/nrf52840/openthread-core-nrf52840-config.h b/examples/platforms/nrf528xx/nrf52840/openthread-core-nrf52840-config.h index 72ebe926f..be29d37c9 100644 --- a/examples/platforms/nrf528xx/nrf52840/openthread-core-nrf52840-config.h +++ b/examples/platforms/nrf528xx/nrf52840/openthread-core-nrf52840-config.h @@ -173,33 +173,15 @@ #endif /** - * @def SETTINGS_CONFIG_BASE_ADDRESS + * @def OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE * - * The base address of settings. + * Define to 1 to enable otPlatFlash* APIs to support non-volatile storage. + * + * When defined to 1, the platform MUST implement the otPlatFlash* APIs instead of the otPlatSettings* APIs. * */ -#ifndef SETTINGS_CONFIG_BASE_ADDRESS -#define SETTINGS_CONFIG_BASE_ADDRESS 0 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_SIZE - * - * The page size of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_SIZE -#define SETTINGS_CONFIG_PAGE_SIZE 4096 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_NUM - * - * The page number of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_NUM -#define SETTINGS_CONFIG_PAGE_NUM 4 +#ifndef OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE +#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 1 #endif /** diff --git a/examples/platforms/nrf528xx/nrf52840/platform-config.h b/examples/platforms/nrf528xx/nrf52840/platform-config.h index b34512d86..f1cacb7c9 100644 --- a/examples/platforms/nrf528xx/nrf52840/platform-config.h +++ b/examples/platforms/nrf528xx/nrf52840/platform-config.h @@ -111,6 +111,20 @@ #define RNG_IRQ_PRIORITY 6 #endif +/******************************************************************************* + * @section Platform Flash Configuration + ******************************************************************************/ + +/** + * @def PLATFORM_FLASH_PAGE_NUM + * + * Number of flash pages to use for OpenThread's non-volatile settings. + * + */ +#ifndef PLATFORM_FLASH_PAGE_NUM +#define PLATFORM_FLASH_PAGE_NUM 4 +#endif + /******************************************************************************* * @section Platform FEM Configuration ******************************************************************************/ diff --git a/examples/platforms/nrf528xx/src/flash.c b/examples/platforms/nrf528xx/src/flash.c index bb3ae9117..13ef823d5 100644 --- a/examples/platforms/nrf528xx/src/flash.c +++ b/examples/platforms/nrf528xx/src/flash.c @@ -26,32 +26,47 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - #include #include #include -#include -#include -#include - #include "platform-nrf5.h" +/** + * @def PLATFORM_FLASH_PAGE_NUM + * + * Number of flash pages to use for OpenThread's non-volatile settings. + * + */ +#ifndef PLATFORM_FLASH_PAGE_NUM +#define PLATFORM_FLASH_PAGE_NUM 4 +#endif + #define FLASH_PAGE_ADDR_MASK 0xFFFFF000 #define FLASH_PAGE_SIZE 4096 static uint32_t sFlashDataStart; static uint32_t sFlashDataEnd; +static uint32_t sSwapSize; -static inline uint32_t mapAddress(uint32_t aAddress) +static inline uint32_t mapAddress(uint8_t aSwapIndex, uint32_t aOffset) { - return aAddress + sFlashDataStart; + uint32_t address; + + address = sFlashDataStart + aOffset; + + if (aSwapIndex) + { + address += sSwapSize; + } + + return address; } -otError utilsFlashInit(void) +void otPlatFlashInit(otInstance *aInstance) { + OT_UNUSED_VARIABLE(aInstance); + #if defined(__CC_ARM) // Temporary solution for Keil compiler. uint32_t const bootloaderAddr = NRF_UICR->NRFFW[0]; @@ -67,7 +82,7 @@ otError utilsFlashInit(void) sFlashDataEnd = pageSize * codeSize; } - sFlashDataStart = sFlashDataEnd - (pageSize * SETTINGS_CONFIG_PAGE_NUM); + sFlashDataStart = sFlashDataEnd - (pageSize * PLATFORM_FLASH_PAGE_NUM); #elif defined(__GNUC__) || defined(__ICCARM__) extern uint32_t __start_ot_flash_data; @@ -82,75 +97,47 @@ otError utilsFlashInit(void) assert((sFlashDataStart % FLASH_PAGE_SIZE) == 0); assert((sFlashDataEnd % FLASH_PAGE_SIZE) == 0); - return OT_ERROR_NONE; + sSwapSize = ((sFlashDataEnd - sFlashDataStart) / FLASH_PAGE_SIZE / 2) * FLASH_PAGE_SIZE; + assert(sSwapSize > 0); } -uint32_t utilsFlashGetSize(void) +uint32_t otPlatFlashGetSwapSize(otInstance *aInstance) { - return sFlashDataEnd - sFlashDataStart; + OT_UNUSED_VARIABLE(aInstance); + + return sSwapSize; } -otError utilsFlashErasePage(uint32_t aAddress) +void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) { - otError error = OT_ERROR_NONE; - otEXPECT_ACTION(aAddress < utilsFlashGetSize(), error = OT_ERROR_INVALID_ARGS); + OT_UNUSED_VARIABLE(aInstance); - error = nrf5FlashPageErase(mapAddress(aAddress & FLASH_PAGE_ADDR_MASK)); + otError error; -exit: - return error; -} - -otError utilsFlashStatusWait(uint32_t aTimeout) -{ - otError error = OT_ERROR_BUSY; - - if (aTimeout == 0) + for (uint32_t offset = 0; offset < sSwapSize; offset += FLASH_PAGE_SIZE) { - if (!nrf5FlashIsBusy()) + error = nrf5FlashPageErase(mapAddress(aSwapIndex, offset)); + assert(error == OT_ERROR_NONE); + + while (nrf5FlashIsBusy()) { - error = OT_ERROR_NONE; } } - else - { - uint32_t startTime = otPlatAlarmMilliGetNow(); - - do - { - if (!nrf5FlashIsBusy()) - { - error = OT_ERROR_NONE; - break; - } - } while (otPlatAlarmMilliGetNow() - startTime < aTimeout); - } - - return error; } -uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize) +void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, const void *aData, uint32_t aSize) { - uint32_t result = 0; - otEXPECT(aData); - otEXPECT(aAddress < utilsFlashGetSize()); - otEXPECT(aSize); + OT_UNUSED_VARIABLE(aInstance); - result = nrf5FlashWrite(mapAddress(aAddress), aData, aSize); + otError error; -exit: - return result; + error = nrf5FlashWrite(mapAddress(aSwapIndex, aOffset), aData, aSize); + assert(error == OT_ERROR_NONE); } -uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize) +void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize) { - uint32_t result = 0; - otEXPECT(aData); - otEXPECT(aAddress < utilsFlashGetSize()); + OT_UNUSED_VARIABLE(aInstance); - memcpy(aData, (uint8_t *)mapAddress(aAddress), aSize); - result = aSize; - -exit: - return result; + memcpy(aData, (uint8_t *)mapAddress(aSwapIndex, aOffset), aSize); } diff --git a/examples/platforms/nrf528xx/src/flash_nosd.c b/examples/platforms/nrf528xx/src/flash_nosd.c index 8cb6b7dee..07fc44d3b 100644 --- a/examples/platforms/nrf528xx/src/flash_nosd.c +++ b/examples/platforms/nrf528xx/src/flash_nosd.c @@ -26,15 +26,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - -#include #include -#include - -#include -#include #include "platform-nrf5.h" #include "drivers/include/nrfx_nvmc.h" diff --git a/examples/platforms/nrf528xx/src/flash_sd.c b/examples/platforms/nrf528xx/src/flash_sd.c index 50d31ab8d..3de69930e 100644 --- a/examples/platforms/nrf528xx/src/flash_sd.c +++ b/examples/platforms/nrf528xx/src/flash_sd.c @@ -36,7 +36,6 @@ #include #include -#include #include "platform-nrf5.h" #include "softdevice.h"