diff --git a/examples/platforms/utils/CMakeLists.txt b/examples/platforms/utils/CMakeLists.txt index 0526e3745..93c135cc2 100644 --- a/examples/platforms/utils/CMakeLists.txt +++ b/examples/platforms/utils/CMakeLists.txt @@ -31,7 +31,6 @@ add_library(openthread-platform-utils OBJECT logging_rtt.c mac_frame.cpp settings_ram.c - settings_flash.c soft_source_match_table.c ) diff --git a/examples/platforms/utils/Makefile.am b/examples/platforms/utils/Makefile.am index aa29b83e2..8c1b37a9e 100644 --- a/examples/platforms/utils/Makefile.am +++ b/examples/platforms/utils/Makefile.am @@ -40,14 +40,12 @@ libopenthread_platform_utils_a_CPPFLAGS = \ libopenthread_platform_utils_a_SOURCES = \ code_utils.h \ debug_uart.c \ - flash.h \ logging_rtt.c \ logging_rtt.h \ mac_frame.cpp \ mac_frame.h \ settings.h \ settings_ram.c \ - settings_flash.c \ soft_source_match_table.c \ soft_source_match_table.h \ $(NULL) diff --git a/examples/platforms/utils/flash.h b/examples/platforms/utils/flash.h deleted file mode 100644 index 659e787e6..000000000 --- a/examples/platforms/utils/flash.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * @brief - * This file defines the flash interface used by settings.cpp. - */ - -#ifndef UTILS_FLASH_H -#define UTILS_FLASH_H - -#include - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Perform any initialization for flash driver. - * - * @retval ::OT_ERROR_NONE Initialize flash driver success. - * @retval ::OT_ERROR_FAILED Initialize flash driver fail. - */ -otError utilsFlashInit(void); - -/** - * Get the size of flash that can be read/write by the caller. - * The usable flash size is always the multiple of flash page size. - * - * @returns The size of the flash. - */ -uint32_t utilsFlashGetSize(void); - -/** - * Erase one flash page that include the input address. - * This is a non-blocking function. It can work with utilsFlashStatusWait to check when erase is done. - * - * The flash address starts from 0, and this function maps the input address to the physical address of flash for - * erasing. 0 is always mapped to the beginning of one flash page. The input address should never be mapped to the - * firmware space or any other protected flash space. - * - * @param[in] aAddress The start address of the flash to erase. - * - * @retval OT_ERROR_NONE Erase flash operation is started. - * @retval OT_ERROR_FAILED Erase flash operation is not started. - * @retval OT_ERROR_INVALID_ARGS aAddress is out of range of flash or not aligned. - */ -otError utilsFlashErasePage(uint32_t aAddress); - -/** - * Check whether flash is ready or busy. - * - * @param[in] aTimeout The interval in milliseconds waiting for the flash operation to be done and become ready again. - * zero indicates that it is a polling function, and returns current status of flash immediately. - * non-zero indicates that it is blocking there until the operation is done and become ready, or - * timeout expires. - * - * @retval OT_ERROR_NONE Flash is ready for any operation. - * @retval OT_ERROR_BUSY Flash is busy. - */ -otError utilsFlashStatusWait(uint32_t aTimeout); - -/** - * Write flash. The write operation only clears bits, but never set bits. - * - * The flash address starts from 0, and this function maps the input address to the physical address of flash for - * writing. 0 is always mapped to the beginning of one flash page. The input address should never be mapped to the - * firmware space or any other protected flash space. - * - * @param[in] aAddress The start address of the flash to write. - * @param[in] aData The pointer of the data to write. - * @param[in] aSize The size of the data to write. - * - * @returns The actual size of octets write to flash. - * It is expected the same as aSize, and may be less than aSize. - * 0 indicates that something wrong happens when writing. - */ -uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize); - -/** - * Read flash. - * - * The flash address starts from 0, and this function maps the input address to the physical address of flash for - * reading. 0 is always mapped to the beginning of one flash page. The input address should never be mapped to the - * firmware space or any other protected flash space. - * - * @param[in] aAddress The start address of the flash to read. - * @param[Out] aData The pointer of buffer for reading. - * @param[in] aSize The size of the data to read. - * - * @returns The actual size of octets read to buffer. - * It is expected the same as aSize, and may be less than aSize. - * 0 indicates that something wrong happens when reading. - */ -uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // UTILS_FLASH_H diff --git a/examples/platforms/utils/settings.h b/examples/platforms/utils/settings.h index 6cae2bec7..7b4e545eb 100644 --- a/examples/platforms/utils/settings.h +++ b/examples/platforms/utils/settings.h @@ -37,36 +37,6 @@ #include -/** - * @def SETTINGS_CONFIG_BASE_ADDRESS - * - * The base address of settings. - * - */ -#ifndef SETTINGS_CONFIG_BASE_ADDRESS -#define SETTINGS_CONFIG_BASE_ADDRESS 0x39000 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_SIZE - * - * The page size of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_SIZE -#define SETTINGS_CONFIG_PAGE_SIZE 0x800 -#endif - -/** - * @def SETTINGS_CONFIG_PAGE_NUM - * - * The page number of settings. - * - */ -#ifndef SETTINGS_CONFIG_PAGE_NUM -#define SETTINGS_CONFIG_PAGE_NUM 2 -#endif - /** * @def OPENTHREAD_SETTINGS_RAM * diff --git a/examples/platforms/utils/settings_flash.c b/examples/platforms/utils/settings_flash.c deleted file mode 100644 index a75ea0771..000000000 --- a/examples/platforms/utils/settings_flash.c +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * This file implements the OpenThread platform abstraction for non-volatile storage of settings. - * - */ - -#include "settings.h" - -#include -#include -#include -#include - -#include -#include - -#include "utils/code_utils.h" - -#include "flash.h" - -#define OT_FLASH_BLOCK_ADD_BEGIN_FLAG (1 << 0) -#define OT_FLASH_BLOCK_ADD_COMPLETE_FLAG (1 << 1) -#define OT_FLASH_BLOCK_DELETE_FLAG (1 << 2) -#define OT_FLASH_BLOCK_INDEX_0_FLAG (1 << 3) - -#define OT_SETTINGS_FLAG_SIZE 4 -#define OT_SETTINGS_BLOCK_DATA_SIZE 255 - -#define OT_SETTINGS_IN_SWAP 0xbe5cc5ef -#define OT_SETTINGS_IN_USE 0xbe5cc5ee -#define OT_SETTINGS_NOT_USED 0xbe5cc5ec - -OT_TOOL_PACKED_BEGIN -struct settingsBlock -{ - uint16_t key; - uint16_t flag; - uint16_t length; - uint16_t reserved; -} OT_TOOL_PACKED_END; - -#if (SETTINGS_CONFIG_PAGE_NUM <= 1) -#error "Invalid value for `SETTINGS_CONFIG_PAGE_NUM` (should be >= 2)" -#endif - -#if !OPENTHREAD_SETTINGS_RAM - -static uint32_t sSettingsBaseAddress; -static uint32_t sSettingsUsedSize; - -static uint16_t getAlignLength(uint16_t length) -{ - return (length + 3) & 0xfffc; -} - -static void setSettingsFlag(uint32_t aBase, uint32_t aFlag) -{ - utilsFlashWrite(aBase, (uint8_t *)&aFlag, sizeof(aFlag)); -} - -static void initSettings(uint32_t aBase, uint32_t aFlag) -{ - uint32_t address = aBase; - uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; - - while (address < (aBase + settingsSize)) - { - utilsFlashErasePage(address); - utilsFlashStatusWait(1000); - address += SETTINGS_CONFIG_PAGE_SIZE; - } - - setSettingsFlag(aBase, aFlag); -} - -static uint32_t swapSettingsBlock(otInstance *aInstance) -{ - OT_UNUSED_VARIABLE(aInstance); - - uint32_t oldBase = sSettingsBaseAddress; - uint32_t swapAddress = oldBase; - uint32_t usedSize = sSettingsUsedSize; - uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; - - sSettingsBaseAddress = - (swapAddress == SETTINGS_CONFIG_BASE_ADDRESS) ? (swapAddress + settingsSize) : SETTINGS_CONFIG_BASE_ADDRESS; - - initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_SWAP); - sSettingsUsedSize = OT_SETTINGS_FLAG_SIZE; - swapAddress += OT_SETTINGS_FLAG_SIZE; - - while (swapAddress < (oldBase + usedSize)) - { - OT_TOOL_PACKED_BEGIN - struct addSettingsBlock - { - struct settingsBlock block; - uint8_t data[OT_SETTINGS_BLOCK_DATA_SIZE]; - } OT_TOOL_PACKED_END addBlock; - bool valid = true; - - utilsFlashRead(swapAddress, (uint8_t *)(&addBlock.block), sizeof(struct settingsBlock)); - swapAddress += sizeof(struct settingsBlock); - - if (!(addBlock.block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && - (addBlock.block.flag & OT_FLASH_BLOCK_DELETE_FLAG)) - { - uint32_t address = swapAddress + getAlignLength(addBlock.block.length); - - while (address < (oldBase + usedSize)) - { - struct settingsBlock block; - - utilsFlashRead(address, (uint8_t *)(&block), sizeof(block)); - - if (!(block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (block.flag & OT_FLASH_BLOCK_DELETE_FLAG) && - !(block.flag & OT_FLASH_BLOCK_INDEX_0_FLAG) && (block.key == addBlock.block.key)) - { - valid = false; - break; - } - - address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); - } - - if (valid) - { - utilsFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length)); - utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock), - getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)); - sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); - } - } - else if (addBlock.block.flag == 0xff) - { - break; - } - - swapAddress += getAlignLength(addBlock.block.length); - } - - setSettingsFlag(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE); - setSettingsFlag(oldBase, (uint32_t)OT_SETTINGS_NOT_USED); - - return settingsSize - sSettingsUsedSize; -} - -static otError addSetting(otInstance * aInstance, - uint16_t aKey, - bool aIndex0, - const uint8_t *aValue, - uint16_t aValueLength) -{ - otError error = OT_ERROR_NONE; - OT_TOOL_PACKED_BEGIN - struct addSettingsBlock - { - struct settingsBlock block; - uint8_t data[OT_SETTINGS_BLOCK_DATA_SIZE]; - } OT_TOOL_PACKED_END addBlock; - uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; - - addBlock.block.flag = 0xff; - addBlock.block.key = aKey; - - if (aIndex0) - { - addBlock.block.flag &= (~OT_FLASH_BLOCK_INDEX_0_FLAG); - } - - addBlock.block.flag &= (~OT_FLASH_BLOCK_ADD_BEGIN_FLAG); - addBlock.block.length = aValueLength; - - if ((sSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >= settingsSize) - { - otEXPECT_ACTION(swapSettingsBlock(aInstance) >= - (getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)), - error = OT_ERROR_NO_BUFS); - } - - utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock.block), - sizeof(struct settingsBlock)); - - memset(addBlock.data, 0xff, OT_SETTINGS_BLOCK_DATA_SIZE); - memcpy(addBlock.data, aValue, addBlock.block.length); - - utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize + sizeof(struct settingsBlock), (uint8_t *)(addBlock.data), - getAlignLength(addBlock.block.length)); - - addBlock.block.flag &= (~OT_FLASH_BLOCK_ADD_COMPLETE_FLAG); - utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock.block), - sizeof(struct settingsBlock)); - sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); - -exit: - return error; -} - -// settings API -void otPlatSettingsInit(otInstance *aInstance) -{ - OT_UNUSED_VARIABLE(aInstance); - - uint8_t index; - uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; - - sSettingsBaseAddress = SETTINGS_CONFIG_BASE_ADDRESS; - - utilsFlashInit(); - - for (index = 0; index < 2; index++) - { - uint32_t blockFlag; - - sSettingsBaseAddress += settingsSize * index; - utilsFlashRead(sSettingsBaseAddress, (uint8_t *)(&blockFlag), sizeof(blockFlag)); - - if (blockFlag == OT_SETTINGS_IN_USE) - { - break; - } - } - - if (index == 2) - { - initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE); - } - - sSettingsUsedSize = OT_SETTINGS_FLAG_SIZE; - - while (sSettingsUsedSize < settingsSize) - { - struct settingsBlock block; - - utilsFlashRead(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&block), sizeof(block)); - - if (!(block.flag & OT_FLASH_BLOCK_ADD_BEGIN_FLAG)) - { - sSettingsUsedSize += (getAlignLength(block.length) + sizeof(struct settingsBlock)); - } - else - { - break; - } - } -} - -void otPlatSettingsDeinit(otInstance *aInstance) -{ - OT_UNUSED_VARIABLE(aInstance); -} - -otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) -{ - OT_UNUSED_VARIABLE(aInstance); - - otError error = OT_ERROR_NOT_FOUND; - uint32_t address = sSettingsBaseAddress + OT_SETTINGS_FLAG_SIZE; - uint16_t valueLength = 0; - int index = 0; - - while (address < (sSettingsBaseAddress + sSettingsUsedSize)) - { - struct settingsBlock block; - - utilsFlashRead(address, (uint8_t *)(&block), sizeof(block)); - - if (block.key == aKey) - { - if (!(block.flag & OT_FLASH_BLOCK_INDEX_0_FLAG)) - { - index = 0; - } - - if (!(block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (block.flag & OT_FLASH_BLOCK_DELETE_FLAG)) - { - if (index == aIndex) - { - uint16_t readLength = block.length; - - // only perform read if an input buffer was passed in - if (aValue != NULL && aValueLength != NULL) - { - // adjust read length if input buffer length is smaller - if (readLength > *aValueLength) - { - readLength = *aValueLength; - } - - utilsFlashRead(address + sizeof(struct settingsBlock), aValue, readLength); - } - - valueLength = block.length; - error = OT_ERROR_NONE; - } - - index++; - } - } - - address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); - } - - if (aValueLength != NULL) - { - *aValueLength = valueLength; - } - - return error; -} - -otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) -{ - return addSetting(aInstance, aKey, true, aValue, aValueLength); -} - -otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) -{ - uint16_t length; - bool index0; - - index0 = (otPlatSettingsGet(aInstance, aKey, 0, NULL, &length) == OT_ERROR_NOT_FOUND ? true : false); - return addSetting(aInstance, aKey, index0, aValue, aValueLength); -} - -otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) -{ - OT_UNUSED_VARIABLE(aInstance); - - otError error = OT_ERROR_NOT_FOUND; - uint32_t address = sSettingsBaseAddress + OT_SETTINGS_FLAG_SIZE; - int index = 0; - - while (address < (sSettingsBaseAddress + sSettingsUsedSize)) - { - struct settingsBlock block; - - utilsFlashRead(address, (uint8_t *)(&block), sizeof(block)); - - if (block.key == aKey) - { - if (!(block.flag & OT_FLASH_BLOCK_INDEX_0_FLAG)) - { - index = 0; - } - - if (!(block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (block.flag & OT_FLASH_BLOCK_DELETE_FLAG)) - { - if (aIndex == index || aIndex == -1) - { - error = OT_ERROR_NONE; - block.flag &= (~OT_FLASH_BLOCK_DELETE_FLAG); - utilsFlashWrite(address, (uint8_t *)(&block), sizeof(block)); - } - - if (index == 1 && aIndex == 0) - { - block.flag &= (~OT_FLASH_BLOCK_INDEX_0_FLAG); - utilsFlashWrite(address, (uint8_t *)(&block), sizeof(block)); - } - - index++; - } - } - - address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); - } - - return error; -} - -void otPlatSettingsWipe(otInstance *aInstance) -{ - initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE); - otPlatSettingsInit(aInstance); -} - -#endif /* OPENTHREAD_SETTINGS_RAM */