diff --git a/cores/nRF5/linker/nrf52_common.ld b/cores/nRF5/linker/nrf52_common.ld index e51fc3d9..bb60c941 100755 --- a/cores/nRF5/linker/nrf52_common.ld +++ b/cores/nRF5/linker/nrf52_common.ld @@ -171,5 +171,6 @@ SECTIONS /* Check if data + heap + stack exceeds RAM limit */ ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") + ASSERT(__StackLimit >= __bss_end__, "region RAM overflowed with stack") } diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index d66397e4..7a982ffe 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -35,6 +35,8 @@ /**************************************************************************/ #include +#include "FileIO.h" +#include "InternalFS.h" #include "bonding.h" #include "flash/flash_nrf52.h" @@ -52,9 +54,9 @@ *------------------------------------------------------------------*/ #ifdef NRF52840_XXAA -#define BOND_FLASH_ADDR 0xF3000 +#define BOND_FLASH_ADDR 0xED000 #else -#define BOND_FLASH_ADDR 0x73000 +#define BOND_FLASH_ADDR 0x6D000 #endif // TODO make it dynamic later @@ -68,9 +70,14 @@ #define printBondDir(role) #endif +static void get_fname (char* fname, uint8_t role, uint16_t ediv) +{ + sprintf(fname, (role == BLE_GAP_ROLE_PERIPH) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, ediv); +} + void bond_init(void) { - + InternalFS.begin(); } /*------------------------------------------------------------------*/ diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index 79bfc9e9..cf1b1698 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -37,16 +37,79 @@ #include #include "InternalFS.h" -extern "C" + +#ifdef NRF52840_XXAA +#define LFS_FLASH_ADDR 0xED000 +#else +#define LFS_FLASH_ADDR 0x6D000 +#endif + +#define LFS_FLASH_SIZE (4*4096) //(7*4096) +#define LFS_BLOCK_SIZE 128 + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +uint8_t ramdisk[LFS_FLASH_SIZE/LFS_BLOCK_SIZE][LFS_BLOCK_SIZE]; + +int _iflash_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) { - int nffs_init(void); + (void) c; + + memcpy(buffer, ramdisk[block] + off, 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. +int _iflash_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) +{ + (void) c; + + memcpy(ramdisk[block] + off, 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. +int _iflash_erase(const struct lfs_config *c, lfs_block_t block) +{ + (void) c; + return 0; +} + +// Sync the state of the underlying block device. Negative error codes +// are propogated to the user. +int _iflash_sync(const struct lfs_config *c) +{ + (void) c; + return 0; +} + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ LittleFS InternalFS; LittleFS::LittleFS (void) { + _lfs_cfg = ((struct lfs_config) { + .context = NULL, + .read = _iflash_read, + .prog = _iflash_prog, + .erase = _iflash_erase, + .sync = _iflash_sync, + .read_size = LFS_BLOCK_SIZE, + .prog_size = LFS_BLOCK_SIZE, + .block_size = LFS_BLOCK_SIZE, + .block_count = LFS_FLASH_SIZE/LFS_BLOCK_SIZE, + .lookahead = 128 + } ); } LittleFS::~LittleFS () @@ -56,7 +119,20 @@ LittleFS::~LittleFS () bool LittleFS::begin (void) { + int err = lfs_mount(&_lfs, &_lfs_cfg); + PRINT_INT(err); + // reformat if we can't mount the filesystem + // this should only happen on the first boot + if (err) { + err = lfs_format(&_lfs, &_lfs_cfg); + PRINT_INT(err); + err = lfs_mount(&_lfs, &_lfs_cfg); + PRINT_INT(err); + } + PRINT_LOCATION(); + + return true; } BluefuritLib::File LittleFS::open (char const *filename, uint8_t mode) diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 776d600b..ac66d36b 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -57,6 +57,8 @@ class LittleFS: public BluefuritLib::FileSystemClass bool rmdir (char const *filepath); private: + struct lfs_config _lfs_cfg; + lfs_t _lfs; }; diff --git a/libraries/FileSystem/src/littlefs/lfs_util.h b/libraries/FileSystem/src/littlefs/lfs_util.h index b2dc2371..1a566fcb 100644 --- a/libraries/FileSystem/src/littlefs/lfs_util.h +++ b/libraries/FileSystem/src/littlefs/lfs_util.h @@ -24,6 +24,8 @@ #include #include +#include "FreeRTOS.h" + #ifndef LFS_NO_MALLOC #include #endif @@ -161,7 +163,7 @@ void lfs_crc(uint32_t *crc, const void *buffer, size_t size); // Allocate memory, only used if buffers are not provided to littlefs static inline void *lfs_malloc(size_t size) { #ifndef LFS_NO_MALLOC - return malloc(size); + return pvPortMalloc(size); #else (void)size; return NULL; @@ -171,7 +173,7 @@ static inline void *lfs_malloc(size_t size) { // Deallocate memory, only used if buffers are not provided to littlefs static inline void lfs_free(void *p) { #ifndef LFS_NO_MALLOC - free(p); + vPortFree(p); #else (void)p; #endif