From 9b37e28bc275e3e672f06bd6126d6807b9658fe0 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 May 2019 16:57:34 +0700 Subject: [PATCH] add InternalFileSystem lib --- .gitignore | 3 - .../Internal_Format/Internal_Format.ino | 52 ++++++ .../Internal_ListFiles/Internal_ListFiles.ino | 124 ++++++++++++++ .../Internal_ReadWrite/Internal_ReadWrite.ino | 82 +++++++++ .../InternalFileSytem/library.properties | 10 ++ .../src/InternalFileSystem.cpp | 153 +++++++++++++++++ .../src/InternalFileSystem.h | 41 +++++ .../InternalFileSytem/src/flash/flash_cache.c | 129 ++++++++++++++ .../InternalFileSytem/src/flash/flash_cache.h | 57 +++++++ .../InternalFileSytem/src/flash/flash_nrf5x.c | 161 ++++++++++++++++++ .../InternalFileSytem/src/flash/flash_nrf5x.h | 91 ++++++++++ 11 files changed, 900 insertions(+), 3 deletions(-) create mode 100644 libraries/InternalFileSytem/examples/Internal_Format/Internal_Format.ino create mode 100644 libraries/InternalFileSytem/examples/Internal_ListFiles/Internal_ListFiles.ino create mode 100644 libraries/InternalFileSytem/examples/Internal_ReadWrite/Internal_ReadWrite.ino create mode 100644 libraries/InternalFileSytem/library.properties create mode 100644 libraries/InternalFileSytem/src/InternalFileSystem.cpp create mode 100644 libraries/InternalFileSytem/src/InternalFileSystem.h create mode 100644 libraries/InternalFileSytem/src/flash/flash_cache.c create mode 100644 libraries/InternalFileSytem/src/flash/flash_cache.h create mode 100644 libraries/InternalFileSytem/src/flash/flash_nrf5x.c create mode 100644 libraries/InternalFileSytem/src/flash/flash_nrf5x.h diff --git a/.gitignore b/.gitignore index 2705565e..121e63ba 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,6 @@ *.app *.pyc -/tools/nrfutil-0.5.2/build/ -/tools/nrfutil-0.5.2/dist/ -/tools/nrfutil-0.5.2/nrfutil.egg-info/ /tools/.idea/ /tools/midi_tests/node_modules diff --git a/libraries/InternalFileSytem/examples/Internal_Format/Internal_Format.ino b/libraries/InternalFileSytem/examples/Internal_Format/Internal_Format.ino new file mode 100644 index 00000000..91870219 --- /dev/null +++ b/libraries/InternalFileSytem/examples/Internal_Format/Internal_Format.ino @@ -0,0 +1,52 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include + +using namespace Adafruit_LittleFS_Namespace; + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("InternalFS Format Example"); + Serial.println(); + + // Wait for user input to run. + Serial.println("This sketch will destroy all of your data in External Flash!"); + Serial.print("Enter any keys to continue:"); + while ( !Serial.available() ) delay(1); + Serial.println(); + Serial.println(); + + // Initialize Internal File System + InternalFS.begin(); + + Serial.print("Formating ... "); + delay(1); // for message appear on monitor + + // Format + InternalFS.format(); + + Serial.println("Done"); +} + +// the loop function runs over and over again forever +void loop() +{ + // nothing to do +} diff --git a/libraries/InternalFileSytem/examples/Internal_ListFiles/Internal_ListFiles.ino b/libraries/InternalFileSytem/examples/Internal_ListFiles/Internal_ListFiles.ino new file mode 100644 index 00000000..54101be8 --- /dev/null +++ b/libraries/InternalFileSytem/examples/Internal_ListFiles/Internal_ListFiles.ino @@ -0,0 +1,124 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include + +using namespace Adafruit_LittleFS_Namespace; + +/* This example print out Internal Flash contents up to + * MAX_LEVEL level of directories (including root) + * WARNING: This example uses recursive call to print out directory tree + * be extra careful, high number of MAX_LEVEL can cause memory overflow + */ +#define MAX_LEVEL 2 + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("InternalFS List Files Example"); + + // Initialize Internal File System + InternalFS.begin(); + + // Print whole directory tree of root whose level is 0 + printTreeDir("/", 0); + + // Print prompt + Serial.println(); + Serial.println("Enter anything to print directory tree:"); +} + +// the loop function runs over and over again forever +void loop() +{ + if ( Serial.available() ) + { + delay(10); // delay for all input arrived + while( Serial.available() ) Serial.read(); + + printTreeDir("/", 0); + + // Print prompt + Serial.println(); + Serial.println("Enter anything to print directory tree:"); + } +} + +/**************************************************************************/ +/*! + @brief Print out whole directory tree of an folder + until the level reach MAX_LEVEL + + @note Recursive call +*/ +/**************************************************************************/ +void printTreeDir(const char* cwd, uint8_t level) +{ + // Open the input folder + File dir(cwd, FILE_READ, InternalFS); + + // Print root + if (level == 0) Serial.println("root"); + + // File within folder + File item(InternalFS); + + // Loop through the directory + while( (item = dir.openNextFile(FILE_READ)) ) + { + // Indentation according to dir level + for(int i=0; i 50 + Serial.print(' '); + + Serial.print( item.size() ); + Serial.println( " Bytes"); + } + + item.close(); + } + + dir.close(); +} diff --git a/libraries/InternalFileSytem/examples/Internal_ReadWrite/Internal_ReadWrite.ino b/libraries/InternalFileSytem/examples/Internal_ReadWrite/Internal_ReadWrite.ino new file mode 100644 index 00000000..c7adaf13 --- /dev/null +++ b/libraries/InternalFileSytem/examples/Internal_ReadWrite/Internal_ReadWrite.ino @@ -0,0 +1,82 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include + +using namespace Adafruit_LittleFS_Namespace; + +#define FILENAME "/adafruit.txt" +#define CONTENTS "Adafruit Little File System test file contents" + +File file(InternalFS); + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Internal Read Write File Example"); + Serial.println(); + + // Wait for user input to run. Otherwise the code will + // always run immediately after flash and create the FILENAME in advance + Serial.print("Enter to any keys to continue:"); + while ( !Serial.available() ) + { + delay(1); + } + Serial.println(); + Serial.println(); + + // Initialize Internal File System + InternalFS.begin(); + + file.open(FILENAME, FILE_READ); + + // file existed + if ( file ) + { + Serial.println(FILENAME " file exists"); + + uint32_t readlen; + char buffer[64] = { 0 }; + readlen = file.read(buffer, sizeof(buffer)); + + buffer[readlen] = 0; + Serial.println(buffer); + file.close(); + }else + { + Serial.print("Open " FILENAME " file to write ... "); + + if( file.open(FILENAME, FILE_WRITE) ) + { + Serial.println("OK"); + file.write(CONTENTS, strlen(CONTENTS)); + file.close(); + }else + { + Serial.println("Failed!"); + } + } + + Serial.println("Done"); +} + +// the loop function runs over and over again forever +void loop() +{ +} diff --git a/libraries/InternalFileSytem/library.properties b/libraries/InternalFileSytem/library.properties new file mode 100644 index 00000000..fd71ba1f --- /dev/null +++ b/libraries/InternalFileSytem/library.properties @@ -0,0 +1,10 @@ +name=Adafruit Internal File System on Bluefruit nRF52 +version=0.10.5 +author=Adafruit +maintainer=Adafruit +sentence=Adafruit Internal File System on Bluefruit nRF52 +paragraph=Adafruit Internal File System on Bluefruit nRF52 +category=Data Storage +url=https://github.com/adafruit/Adafruit_nRF52_Arduino +architectures=* +includes=InternalFileSystem.h diff --git a/libraries/InternalFileSytem/src/InternalFileSystem.cpp b/libraries/InternalFileSytem/src/InternalFileSystem.cpp new file mode 100644 index 00000000..123a0580 --- /dev/null +++ b/libraries/InternalFileSytem/src/InternalFileSystem.cpp @@ -0,0 +1,153 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 hathach 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 "InternalFileSystem.h" +#include "flash/flash_nrf5x.h" + +#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) +#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 format(); + + // mount again if still failed, give up + if ( !Adafruit_LittleFS::begin() ) return false; + } + + return true; +} diff --git a/libraries/InternalFileSytem/src/InternalFileSystem.h b/libraries/InternalFileSytem/src/InternalFileSystem.h new file mode 100644 index 00000000..1a000ac3 --- /dev/null +++ b/libraries/InternalFileSytem/src/InternalFileSystem.h @@ -0,0 +1,41 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 hathach 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 INTERNALFILESYSTEM_H_ +#define INTERNALFILESYSTEM_H_ + +#include "Adafruit_LittleFS.h" + +class InternalFileSystem : public Adafruit_LittleFS +{ + public: + InternalFileSystem(void); + + // overwrite to also perform low level format (sector erase of whole flash region) + bool begin(void); +}; + +extern InternalFileSystem InternalFS; + +#endif /* INTERNALFILESYSTEM_H_ */ diff --git a/libraries/InternalFileSytem/src/flash/flash_cache.c b/libraries/InternalFileSytem/src/flash/flash_cache.c new file mode 100644 index 00000000..5fc17609 --- /dev/null +++ b/libraries/InternalFileSytem/src/flash/flash_cache.c @@ -0,0 +1,129 @@ +/* + * 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/libraries/InternalFileSytem/src/flash/flash_cache.h b/libraries/InternalFileSytem/src/flash/flash_cache.h new file mode 100644 index 00000000..517eb2dc --- /dev/null +++ b/libraries/InternalFileSytem/src/flash/flash_cache.h @@ -0,0 +1,57 @@ +/* + * 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/libraries/InternalFileSytem/src/flash/flash_nrf5x.c b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c new file mode 100644 index 00000000..2a2edb6b --- /dev/null +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.c @@ -0,0 +1,161 @@ +/* + * 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); +} + +//--------------------------------------------------------------------+ +// 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/libraries/InternalFileSytem/src/flash/flash_nrf5x.h b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h new file mode 100644 index 00000000..b6cb4656 --- /dev/null +++ b/libraries/InternalFileSytem/src/flash/flash_nrf5x.h @@ -0,0 +1,91 @@ +/* + * 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 __cplusplus + extern "C" { +#endif + +void flash_nrf5x_flush (void); +bool flash_nrf5x_erase(uint32_t addr); + +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_ */