From 187ddc6058e7109cc72a6fe78b6e3aca554a2ced Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 May 2019 16:48:48 +0700 Subject: [PATCH] move InternalFS to its own subclass of LFS --- cores/nRF5/Arduino.h | 1 - cores/nRF5/flash/flash_cache.c | 129 ------------- cores/nRF5/flash/flash_cache.h | 57 ------ cores/nRF5/flash/flash_nrf5x.c | 171 ------------------ cores/nRF5/flash/flash_nrf5x.h | 100 ---------- .../examples/LFS_Format/LFS_Format.ino | 5 +- .../examples/LFS_ListFiles/LFS_ListFiles.ino | 1 + .../examples/LFS_ReadWrite/LFS_ReadWrite.ino | 2 + .../src/Adafruit_LittleFS.cpp | 91 ---------- .../Adafruit_LittleFS/src/Adafruit_LittleFS.h | 4 - .../examples/Peripheral/bleuart/bleuart.ino | 2 + .../Bluefruit52Lib/src/utility/bonding.cpp | 4 +- 12 files changed, 9 insertions(+), 558 deletions(-) delete mode 100644 cores/nRF5/flash/flash_cache.c delete mode 100644 cores/nRF5/flash/flash_cache.h delete mode 100644 cores/nRF5/flash/flash_nrf5x.c delete mode 100644 cores/nRF5/flash/flash_nrf5x.h diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 9e7b4f80..8ac06a86 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -66,7 +66,6 @@ void suspendLoop(void); #include "utility/debug.h" #include "utility/utilities.h" #include "utility/AdaCallback.h" -#include "flash/flash_nrf5x.h" #ifdef USE_TINYUSB #include "Adafruit_TinyUSB_Core.h" diff --git a/cores/nRF5/flash/flash_cache.c b/cores/nRF5/flash/flash_cache.c deleted file mode 100644 index 5fc17609..00000000 --- a/cores/nRF5/flash/flash_cache.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "flash_cache.h" -#include "common_func.h" -#include "variant.h" -#include "wiring_digital.h" - -//--------------------------------------------------------------------+ -// MACRO TYPEDEF CONSTANT ENUM DECLARATION -//--------------------------------------------------------------------+ -static inline uint32_t page_addr_of (uint32_t addr) -{ - return addr & ~(FLASH_CACHE_SIZE - 1); -} - -static inline uint32_t page_offset_of (uint32_t addr) -{ - return addr & (FLASH_CACHE_SIZE - 1); -} - -uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len) -{ - uint8_t const * src8 = (uint8_t const *) src; - uint32_t remain = len; - - // Program up to page boundary each loop - while ( remain ) - { - uint32_t const page_addr = page_addr_of(dst); - uint32_t const offset = page_offset_of(dst); - - uint32_t wr_bytes = FLASH_CACHE_SIZE - offset; - wr_bytes = min32(remain, wr_bytes); - - // Page changes, flush old and update new cache - if ( page_addr != fc->cache_addr ) - { - flash_cache_flush(fc); - fc->cache_addr = page_addr; - - // read a whole page from flash - fc->read(fc->cache_buf, page_addr, FLASH_CACHE_SIZE); - } - - memcpy(fc->cache_buf + offset, src8, wr_bytes); - - // adjust for next run - src8 += wr_bytes; - remain -= wr_bytes; - dst += wr_bytes; - } - - return len - remain; -} - -void flash_cache_flush (flash_cache_t* fc) -{ - if ( fc->cache_addr == FLASH_CACHE_INVALID_ADDR ) return; - - // skip erase & program if verify() exists, and memory matches - if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE)) ) - { - // indicator - ledOn(LED_BUILTIN); - - fc->erase(fc->cache_addr); - fc->program(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE); - - ledOff(LED_BUILTIN); - } - - fc->cache_addr = FLASH_CACHE_INVALID_ADDR; -} - -void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count) -{ - // overwrite with cache value if available - if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) && - !(addr < fc->cache_addr && addr + count <= fc->cache_addr) && - !(addr >= fc->cache_addr + FLASH_CACHE_SIZE) ) - { - int dst_off = fc->cache_addr - addr; - int src_off = 0; - - if ( dst_off < 0 ) - { - src_off = -dst_off; - dst_off = 0; - } - - int cache_bytes = minof(FLASH_CACHE_SIZE-src_off, count - dst_off); - - // start to cached - if ( dst_off ) fc->read(dst, addr, dst_off); - - // cached - memcpy(dst + dst_off, fc->cache_buf + src_off, cache_bytes); - - // cached to end - int copied = dst_off + cache_bytes; - if ( copied < count ) fc->read(dst + copied, addr + copied, count - copied); - } - else - { - fc->read(dst, (void*) addr, count); - } -} diff --git a/cores/nRF5/flash/flash_cache.h b/cores/nRF5/flash/flash_cache.h deleted file mode 100644 index 517eb2dc..00000000 --- a/cores/nRF5/flash/flash_cache.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef FLASH_CACHE_H_ -#define FLASH_CACHE_H_ - -#include -#include - -#define FLASH_CACHE_SIZE 4096 // must be a erasable page size -#define FLASH_CACHE_INVALID_ADDR 0xffffffff - -typedef struct -{ - bool (*erase) (uint32_t addr); - uint32_t (*program) (uint32_t dst, void const * src, uint32_t len); - uint32_t (*read) (void* dst, uint32_t src, uint32_t len); - bool (*verify) (uint32_t addr, void const * buf, uint32_t len); - - uint32_t cache_addr; - uint8_t* cache_buf; -} flash_cache_t; - -#ifdef __cplusplus -extern "C" { -#endif - -uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count); -void flash_cache_flush (flash_cache_t* fc); -void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count); - -#ifdef __cplusplus - } -#endif - -#endif /* FLASH_CACHE_H_ */ diff --git a/cores/nRF5/flash/flash_nrf5x.c b/cores/nRF5/flash/flash_nrf5x.c deleted file mode 100644 index 6c7014a3..00000000 --- a/cores/nRF5/flash/flash_nrf5x.c +++ /dev/null @@ -1,171 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "flash_nrf5x.h" -#include "flash_cache.h" -#include "nrf_sdm.h" -#include "nrf_soc.h" -#include "rtos.h" - -//--------------------------------------------------------------------+ -// MACRO TYPEDEF CONSTANT ENUM DECLARATION -//--------------------------------------------------------------------+ -static SemaphoreHandle_t _sem = NULL; - -void flash_nrf5x_event_cb (uint32_t event) -{ -// if (event != NRF_EVT_FLASH_OPERATION_SUCCESS) LOG_LV1("IFLASH", "Flash op Failed"); - if ( _sem ) xSemaphoreGive(_sem); -} - -// Flash Abstraction Layer -static bool fal_erase (uint32_t addr); -static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len); -static uint32_t fal_read (void* dst, uint32_t src, uint32_t len); -static bool fal_verify (uint32_t addr, void const * buf, uint32_t len); - -static uint8_t _cache_buffer[FLASH_CACHE_SIZE] __attribute__((aligned(4))); - -static flash_cache_t _cache = { - .erase = fal_erase, - .program = fal_program, - .read = fal_read, - .verify = fal_verify, - .cache_addr = FLASH_CACHE_INVALID_ADDR, - .cache_buf = _cache_buffer -}; - -//--------------------------------------------------------------------+ -// Application API -//--------------------------------------------------------------------+ -void flash_nrf5x_flush (void) -{ - flash_cache_flush(&_cache); -} - -uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len) -{ - // TODO prevent write SD + bootloader region - return flash_cache_write(&_cache, dst, src, len); -} - -uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len) -{ - // return cache value if available - flash_cache_read(&_cache, dst, src, len); - return len; -} - -bool flash_nrf5x_erase(uint32_t addr) -{ - return fal_erase(addr); -} - -bool flash_nrf5x_erase_all(void) -{ - for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE ) - { - VERIFY( flash_nrf5x_erase(addr) ); - } - - return true; -} - -//--------------------------------------------------------------------+ -// HAL for caching -//--------------------------------------------------------------------+ -bool fal_erase (uint32_t addr) -{ - // Init semaphore for first call - if ( _sem == NULL ) - { - _sem = xSemaphoreCreateCounting(10, 0); - VERIFY(_sem); - } - - // retry if busy - uint32_t err; - while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(addr / FLASH_NRF52_PAGE_SIZE)) ) - { - delay(1); - } - VERIFY_STATUS(err, false); - - // wait for async event if SD is enabled - uint8_t sd_en = 0; - (void) sd_softdevice_is_enabled(&sd_en); - - if ( sd_en ) xSemaphoreTake(_sem, portMAX_DELAY); - - return true; -} - -static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len) -{ - // wait for async event if SD is enabled - uint8_t sd_en = 0; - (void) sd_softdevice_is_enabled(&sd_en); - - uint32_t err; - - // Somehow S140 v6.1.1 assert an error when writing a whole page - // https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert - // Workaround: write half page at a time. -#if NRF52832_XXAA - while ( NRF_ERROR_BUSY == (err = sd_flash_write((uint32_t*) dst, (uint32_t const *) src, len/4)) ) - { - delay(1); - } - VERIFY_STATUS(err, 0); - - if ( sd_en ) xSemaphoreTake(_sem, portMAX_DELAY); -#else - while ( NRF_ERROR_BUSY == (err = sd_flash_write((uint32_t*) dst, (uint32_t const *) src, len/8)) ) - { - delay(1); - } - VERIFY_STATUS(err, 0); - if ( sd_en ) xSemaphoreTake(_sem, portMAX_DELAY); - - while ( NRF_ERROR_BUSY == (err = sd_flash_write((uint32_t*) (dst+ len/2), (uint32_t const *) (src + len/2), len/8)) ) - { - delay(1); - } - VERIFY_STATUS(err, 0); - if ( sd_en ) xSemaphoreTake(_sem, portMAX_DELAY); -#endif - - return len; -} - -static uint32_t fal_read (void* dst, uint32_t src, uint32_t len) -{ - memcpy(dst, (void*) src, len); - return len; -} - -static bool fal_verify (uint32_t addr, void const * buf, uint32_t len) -{ - return 0 == memcmp((void*) addr, buf, len); -} diff --git a/cores/nRF5/flash/flash_nrf5x.h b/cores/nRF5/flash/flash_nrf5x.h deleted file mode 100644 index 57cd2676..00000000 --- a/cores/nRF5/flash/flash_nrf5x.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef FLASH_NRF52_H_ -#define FLASH_NRF52_H_ - -#include "common_inc.h" - -#define FLASH_NRF52_PAGE_SIZE 4096 - -#ifdef NRF52840_XXAA -#define LFS_FLASH_ADDR 0xED000 -#else -#define LFS_FLASH_ADDR 0x6D000 -#endif - -#define LFS_FLASH_TOTAL_SIZE (7*FLASH_NRF52_PAGE_SIZE) - -#ifdef __cplusplus - extern "C" { -#endif - -void flash_nrf5x_flush (void); -bool flash_nrf5x_erase(uint32_t addr); -bool flash_nrf5x_erase_all(void); - -uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len); -uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len); - -//--------------------------------------------------------------------+ -// Write helper -//--------------------------------------------------------------------+ -static inline uint32_t flash_nrf5x_write8 (uint32_t dst, uint8_t num) -{ - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); -} - -static inline uint32_t flash_nrf5x_write16 (uint32_t dst, uint8_t num) -{ - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); -} -static inline uint32_t flash_nrf5x_write32 (uint32_t dst, uint8_t num) -{ - flash_nrf5x_write(dst, &num, sizeof(num)); - return sizeof(num); -} - -//--------------------------------------------------------------------+ -// Read helper -//--------------------------------------------------------------------+ -static inline uint8_t flash_nrf5x_read8 (uint32_t src) -{ - uint8_t num; - flash_nrf5x_read(&num, src, sizeof(num)); - return num; -} - -static inline uint16_t flash_nrf5x_read16 (uint32_t src) -{ - uint16_t num; - flash_nrf5x_read(&num, src, sizeof(num)); - return num; -} - -static inline uint16_t flash_nrf5x_read32 (uint32_t src) -{ - uint32_t num; - flash_nrf5x_read(&num, src, sizeof(num)); - return num; -} - - -#ifdef __cplusplus - } -#endif - -#endif /* FLASH_NRF52_H_ */ diff --git a/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino b/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino index 4d447c7e..91870219 100644 --- a/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino +++ b/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino @@ -13,6 +13,7 @@ *********************************************************************/ #include +#include using namespace Adafruit_LittleFS_Namespace; @@ -37,10 +38,6 @@ void setup() Serial.print("Formating ... "); delay(1); // for message appear on monitor - - // Erase all sectors of internal flash region for Filesystem. - // Low level format - flash_nrf5x_erase_all(); // Format InternalFS.format(); diff --git a/libraries/Adafruit_LittleFS/examples/LFS_ListFiles/LFS_ListFiles.ino b/libraries/Adafruit_LittleFS/examples/LFS_ListFiles/LFS_ListFiles.ino index 167f85f6..54101be8 100644 --- a/libraries/Adafruit_LittleFS/examples/LFS_ListFiles/LFS_ListFiles.ino +++ b/libraries/Adafruit_LittleFS/examples/LFS_ListFiles/LFS_ListFiles.ino @@ -13,6 +13,7 @@ *********************************************************************/ #include +#include using namespace Adafruit_LittleFS_Namespace; diff --git a/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino b/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino index 89b530e1..c7adaf13 100644 --- a/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino +++ b/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino @@ -13,6 +13,8 @@ *********************************************************************/ #include +#include + using namespace Adafruit_LittleFS_Namespace; #define FILENAME "/adafruit.txt" diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index 98c698f8..9dcf47f3 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -24,101 +24,10 @@ #include #include - #include "Adafruit_LittleFS.h" -#include "flash/flash_nrf5x.h" using namespace Adafruit_LittleFS_Namespace; -#define LFS_BLOCK_SIZE 128 - -//--------------------------------------------------------------------+ -// LFS Disk IO -//--------------------------------------------------------------------+ - -static inline uint32_t lba2addr(uint32_t block) -{ - return ((uint32_t) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; -} - -static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) -{ - (void) c; - - uint32_t addr = lba2addr(block) + off; - flash_nrf5x_read(buffer, addr, size); - - return 0; -} - -// Program a region in a block. The block must have previously -// been erased. Negative error codes are propogated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, - lfs_size_t size) -{ - (void) c; - - uint32_t addr = lba2addr(block) + off; - flash_nrf5x_write(addr, buffer, size); - - return 0; -} - -// Erase a block. A block must be erased before being programmed. -// The state of an erased block is undefined. Negative error codes -// are propogated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _internal_flash_erase (const struct lfs_config *c, lfs_block_t block) -{ - (void) c; - - uint32_t addr = lba2addr(block); - - // implement as write 0xff to whole block address - for(int i=0; i +#include +#include // BLE Service BLEDfu bledfu; // OTA DFU service diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index 2d6991dd..0479ddf6 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -35,7 +35,9 @@ /**************************************************************************/ #include -#include "Adafruit_LittleFS.h" +#include +#include + #include "bonding.h" #include "bluefruit.h"