diff --git a/cores/nRF5/common_func.h b/cores/nRF5/common_func.h index 9c7fdd1e..216de87c 100644 --- a/cores/nRF5/common_func.h +++ b/cores/nRF5/common_func.h @@ -121,7 +121,7 @@ #endif int cprintf(const char * format, ...); -const char* dbg_err_str(uint32_t err_id); // TODO move to other place +const char* dbg_err_str(int32_t err_id); // TODO move to other place #if CFG_DEBUG #define LOG_LV1(...) ADALOG(__VA_ARGS__) diff --git a/cores/nRF5/utility/debug.cpp b/cores/nRF5/utility/debug.cpp index fc95e907..7c9024c3 100644 --- a/cores/nRF5/utility/debug.cpp +++ b/cores/nRF5/utility/debug.cpp @@ -444,7 +444,7 @@ lookup_table_t const _strerr_table = .items = _strerr_lookup }; -const char* dbg_err_str(uint32_t err_id) +const char* dbg_err_str(int32_t err_id) { const char * str = (const char *) lookup_find(&_strerr_table, err_id); static char unknown_str[7] = {0}; diff --git a/cores/nRF5/utility/debug.h b/cores/nRF5/utility/debug.h index 47242fb6..9134389a 100644 --- a/cores/nRF5/utility/debug.h +++ b/cores/nRF5/utility/debug.h @@ -45,7 +45,7 @@ #if CFG_DEBUG const char* dbg_ble_event_str(uint16_t evt_id); -const char* dbg_err_str(uint32_t err_id); +const char* dbg_err_str(int32_t err_id); #endif int dbgHeapTotal(void); diff --git a/cores/nRF5/verify.h b/cores/nRF5/verify.h index 0d315086..cb3cd525 100644 --- a/cores/nRF5/verify.h +++ b/cores/nRF5/verify.h @@ -47,8 +47,6 @@ extern "C" { #endif -#define RETURN_VOID - //--------------------------------------------------------------------+ // Compile-time Assert //--------------------------------------------------------------------+ @@ -64,39 +62,42 @@ extern "C" // VERIFY Helper //--------------------------------------------------------------------+ #if CFG_DEBUG >= 1 -// #define VERIFY_MESS(format, ...) cprintf("[%08ld] %s: %d: verify failed\n", get_millis(), __func__, __LINE__) - #define VERIFY_MESS(_status) cprintf("%s: %d: verify failed, error = %s\n", __PRETTY_FUNCTION__, __LINE__, dbg_err_str(_status)); + #define VERIFY_MESS(_status, _funcstr) \ + do { \ + const char* (*_fstr)(int32_t) = _funcstr;\ + cprintf("%s: %d: verify failed, error = ", __PRETTY_FUNCTION__, __LINE__);\ + if (_fstr) cprintf(_fstr(_status)); else cprintf("%d", _status);\ + cprintf("\n");\ + }while(0) #else - #define VERIFY_MESS(_status) + #define VERIFY_MESS(_status, _funcstr) #endif -#define VERIFY_DEFINE(_status, _error) \ - if ( 0 != _status ) { \ - VERIFY_MESS(_status) \ - return _error; \ +#define VERIFY_ERR_DEF(_status, _ret, _funcstr) \ + if ( 0 != _status ) { \ + VERIFY_MESS(_status, _funcstr); \ + return _ret; \ } -/** - * Helper to implement optional parameter for VERIFY Macro family - */ -#define VERIFY_GETARGS(arg1, arg2, arg3, ...) arg3 +// Helper to implement optional parameter for VERIFY Macro family +#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 /*------------------------------------------------------------------*/ /* VERIFY STATUS - * - VERIFY_STS_1ARGS : return status of condition if failed - * - VERIFY_STS_2ARGS : return provided status code if failed + * - VERIFY_ERR_1ARGS : return status of condition if failed + * - VERIFY_ERR_2ARGS : return provided status code if failed *------------------------------------------------------------------*/ -#define VERIFY_STS_1ARGS(sts) \ - do { \ - uint32_t _status = (uint32_t)(sts); \ - VERIFY_DEFINE(_status, _status);\ +#define VERIFY_ERR_1ARGS(_exp, _funcstr) \ + do { \ + int32_t _status = (int32_t)(_exp); \ + VERIFY_ERR_DEF(_status, _status, _funcstr); \ } while(0) -#define VERIFY_STS_2ARGS(sts, _error) \ - do { \ - uint32_t _status = (uint32_t)(sts); \ - VERIFY_DEFINE(_status, _error);\ +#define VERIFY_ERR_2ARGS(_exp, _ret, _funcstr) \ + do { \ + int32_t _status = (int32_t)(_exp); \ + VERIFY_ERR_DEF(_status, _ret, _funcstr); \ } while(0) /** @@ -104,8 +105,9 @@ extern "C" * - status value if called with 1 parameter e.g VERIFY_STATUS(status) * - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode) */ -#define VERIFY_STATUS(...) VERIFY_GETARGS(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__) +#define VERIFY_STATUS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_err_str) +#define VERIFY_ERROR(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL) /*------------------------------------------------------------------*/ /* VERIFY @@ -120,7 +122,7 @@ extern "C" * - false value if called with 1 parameter e.g VERIFY(condition) * - 2 parameter if called with 2 parameters e.g VERIFY(condition, errorcode) */ -#define VERIFY(...) VERIFY_GETARGS(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__) +#define VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__) // TODO VERIFY with final statement diff --git a/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp b/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp index cd27921a..cb53f86d 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp @@ -160,8 +160,8 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt enum { DFU_OTA_MAGIC = 0xB1 }; sd_power_gpregret_clr(0, 0xFF); - VERIFY_STATUS( sd_power_gpregret_set(0, DFU_OTA_MAGIC), RETURN_VOID); - VERIFY_STATUS( sd_softdevice_disable(), RETURN_VOID ); + VERIFY_STATUS( sd_power_gpregret_set(0, DFU_OTA_MAGIC), ); + VERIFY_STATUS( sd_softdevice_disable(), ); // Disable all interrupts #if defined(NRF52832_XXAA) @@ -182,7 +182,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt // NRF_RTC1->TASKS_STOP = 1; // NRF_RTC1->TASKS_CLEAR = 1; - VERIFY_STATUS( sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]), RETURN_VOID); + VERIFY_STATUS( sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]), ); __set_CONTROL(0); // switch to MSP, required if using FreeRTOS bootloader_util_app_start(NRF_UICR->NRFFW[0]); diff --git a/libraries/FileSystem/src/FileIO.cpp b/libraries/FileSystem/src/FileIO.cpp index 90c9fd6c..b0e5ad8e 100644 --- a/libraries/FileSystem/src/FileIO.cpp +++ b/libraries/FileSystem/src/FileIO.cpp @@ -34,21 +34,23 @@ */ /**************************************************************************/ +#include "rtos.h" #include "FileIO.h" namespace BluefuritLib { File::File (FileSystemClass &fs) - : _fs(fs) { + _fs = &fs; _hdl = NULL; } -File::File (char const *_filename, uint8_t _mode, FileSystemClass &fs) - : _fs(fs) +File::File (char const *filename, uint8_t mode, FileSystemClass &fs) { + _fs = &fs; _hdl = NULL; + open(filename, mode); } File::~File () @@ -56,72 +58,90 @@ File::~File () } -size_t File::write (uint8_t) +bool File::open (char const *filename, uint8_t mode) { + // close previous opened + if ( _hdl ) close(); + *this = _fs->open(filename, mode); + return _hdl != NULL; +} + +size_t File::write (uint8_t ch) +{ + return write(&ch, 1); } size_t File::write (uint8_t const *buf, size_t size) { - + return _fs->_f_write(_hdl, buf, size); } -int File::read () +int File::read (void) { - -} - -int File::peek () -{ - -} - -int File::available () -{ - -} - -void File::flush () -{ - + uint8_t ch; + return (read(&ch, 1) > 0) ? ch : -1; } int File::read (void *buf, uint16_t nbyte) +{ + return _fs->_f_read(_hdl, buf, nbyte); +} + +int File::peek (void) { } +int File::available (void) +{ + +} + +void File::flush (void) +{ + _fs->_f_flush(_hdl); +} + + + bool File::seek (uint32_t pos) { } -uint32_t File::position () +uint32_t File::position (void) { } -uint32_t File::size () +uint32_t File::size (void) { } -void File::close () +void File::close (void) { + if ( _hdl ) + { + _fs->_f_close(_hdl); + rtos_free(_hdl); + } + _hdl = NULL; } File::operator bool () { - + return _hdl != NULL; } -char const * File::name () +char const * File::name (void) { } -bool File::isDirectory () +bool File::isDirectory (void) { } diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index 1a71efdd..c20865fe 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -48,30 +48,36 @@ class File: public Stream { public: File (FileSystemClass &fs); - File (char const *_filename, uint8_t _mode, FileSystemClass &fs); + File (char const *filename, uint8_t mode, FileSystemClass &fs); ~File (); - virtual size_t write (uint8_t); + bool open (char const *filename, uint8_t mode); + + virtual size_t write (uint8_t ch); virtual size_t write (uint8_t const *buf, size_t size); - virtual int read (); - virtual int peek (); - virtual int available (); - virtual void flush (); + + virtual int read (void); int read (void *buf, uint16_t nbyte); + + virtual int peek (void); + virtual int available (void); + virtual void flush (void); + bool seek (uint32_t pos); - uint32_t position (); - uint32_t size (); - void close (); - operator bool (); - char const * name (); + uint32_t position (void); + uint32_t size (void); + void close (void); + operator bool (void); + char const * name (void); + bool isDirectory (); File openNextFile (uint8_t mode = FILE_READ); void rewindDirectory (void); - //using Print::write; + using Print::write; private: - FileSystemClass &_fs; + FileSystemClass* _fs; void* _hdl; // String filename; @@ -100,21 +106,29 @@ class FileSystemClass virtual bool remove (char const *filepath) = 0; virtual bool rmdir (char const *filepath) = 0; + + //--------------------------------------------------------------------+ + // Internal API, shouldn't call directly + //--------------------------------------------------------------------+ + virtual size_t _f_write (void* fhdl, uint8_t const *buf, size_t size) = 0; + virtual int _f_read (void* fhdl, void *buf, uint16_t nbyte) = 0; + virtual void _f_flush (void* fhdl) = 0; + virtual void _f_close (void* fhdl) = 0; }; } -// We enclose File and FileSystem classes in namespace BridgeLib to avoid +// We enclose File and FileSystem classes in namespace BluefuritLib to avoid // conflicts with legacy SD library. // This ensure compatibility with older sketches that uses only Bridge lib // (the user can still use File instead of BridgeFile) -//using namespace BridgeLib; -// -//// This allows sketches to use BridgeLib::File together with SD library -//// (you must use BridgeFile instead of File when needed to disambiguate) -//typedef BridgeLib::File BridgeFile; -//typedef BridgeLib::FileSystemClass BridgeFileSystemClass; -//#define BridgeFileSystem BridgeLib::FileSystem +using namespace BluefuritLib; + +// This allows sketches to use BluefuritLib::File together with SD library +// (you must use BridgeFile instead of File when needed to disambiguate) +typedef BluefuritLib::File BluefruitFile; + +#include "InternalFS.h" #endif diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index dd3203ce..646c8cd3 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -53,11 +53,62 @@ //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ -uint8_t ramdisk[LFS_FLASH_SIZE/LFS_BLOCK_SIZE][LFS_BLOCK_SIZE]; + +#if !CFG_DEBUG + +#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL) + +#else + +#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs) + +static const char* dbg_strerr_lfs (int32_t err) +{ + switch ( err ) + { + case LFS_ERR_IO: + return "LFS_ERR_IO"; + + case LFS_ERR_CORRUPT: + return "LFS_ERR_CORRUPT"; + + case LFS_ERR_NOENT: + return "LFS_ERR_NOENT"; + + case LFS_ERR_EXIST: + return "LFS_ERR_EXIST"; + + case LFS_ERR_NOTDIR: + return "LFS_ERR_NOTDIR"; + + case LFS_ERR_ISDIR: + return "LFS_ERR_ISDIR"; + + case LFS_ERR_NOTEMPTY: + return "LFS_ERR_NOTEMPTY"; + + case LFS_ERR_BADF: + return "LFS_ERR_BADF"; + + case LFS_ERR_INVAL: + return "LFS_ERR_INVAL"; + + case LFS_ERR_NOSPC: + return "LFS_ERR_NOSPC"; + + case LFS_ERR_NOMEM: + return "LFS_ERR_NOMEM"; + + default: + return NULL; + } +} + +#endif static inline uint32_t lba2addr(uint32_t block) { - return ((uint32_t)LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; + return ((uint32_t) LFS_FLASH_ADDR) + block * 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) @@ -83,8 +134,6 @@ int _iflash_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, c 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. @@ -120,19 +169,18 @@ LittleFS InternalFS; LittleFS::LittleFS (void) { - _lfs_cfg = ((struct lfs_config) { - .context = NULL, - .read = _iflash_read, - .prog = _iflash_prog, - .erase = _iflash_erase, - .sync = _iflash_sync, + varclr(&_lfs_cfg); + _lfs_cfg.context = NULL; + _lfs_cfg.read = _iflash_read; + _lfs_cfg.prog = _iflash_prog; + _lfs_cfg.erase = _iflash_erase; + _lfs_cfg.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 - } ); + _lfs_cfg.read_size = LFS_BLOCK_SIZE; + _lfs_cfg.prog_size = LFS_BLOCK_SIZE; + _lfs_cfg.block_size = LFS_BLOCK_SIZE; + _lfs_cfg.block_count = LFS_FLASH_SIZE / LFS_BLOCK_SIZE; + _lfs_cfg.lookahead = 128; } LittleFS::~LittleFS () @@ -143,17 +191,14 @@ 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); + if ( LFS_ERR_CORRUPT == err ) + { + LOG_LV1("IFLASH", "Format internal file system"); + VERIFY_LFS(lfs_format(&_lfs, &_lfs_cfg)); + VERIFY_LFS(lfs_mount(&_lfs, &_lfs_cfg)); } - PRINT_LOCATION(); return true; } @@ -162,9 +207,52 @@ BluefuritLib::File LittleFS::open (char const *filename, uint8_t mode) { BluefuritLib::File file(*this); + int flags = (mode == FILE_READ) ? LFS_O_RDONLY : + (mode == FILE_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : + (mode == FILE_APPEND) ? (LFS_O_RDWR | LFS_O_CREAT | LFS_O_APPEND) : 0; + + if ( flags ) + { + lfs_file_t* fhdl = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t)); + int rc = lfs_file_open(&_lfs, fhdl, filename, flags); + + if ( rc ) + { + rtos_free(fhdl); + // print error if error is other than file not existed + if ( rc != LFS_ERR_NOENT ) VERIFY_MESS(rc, dbg_strerr_lfs); + } + else + { + file._hdl = fhdl; + } + } + return file; } +size_t LittleFS::_f_write (void* fhdl, uint8_t const *buf, size_t size) +{ + lfs_ssize_t wrcount = lfs_file_write(&_lfs, (lfs_file_t*) fhdl, buf, size); + VERIFY(wrcount > 0, 0); + return wrcount; +} + +void LittleFS::_f_flush (void* fhdl) +{ + VERIFY_LFS(lfs_file_sync(&_lfs, (lfs_file_t* ) fhdl),); +} + +int LittleFS::_f_read (void* fhdl, void *buf, uint16_t nbyte) +{ + return lfs_file_read(&_lfs, (lfs_file_t*) fhdl, buf, nbyte); +} + +void LittleFS::_f_close (void* fhdl) +{ + lfs_file_close(&_lfs, (lfs_file_t*) fhdl); +} + bool LittleFS::exists (char const *filepath) { diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index ac66d36b..022c336e 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -56,6 +56,11 @@ class LittleFS: public BluefuritLib::FileSystemClass bool remove (char const *filepath); bool rmdir (char const *filepath); + virtual size_t _f_write (void* fhdl, uint8_t const *buf, size_t size); + virtual int _f_read (void* fhdl, void *buf, uint16_t nbyte); + virtual void _f_flush (void* fhdl); + virtual void _f_close (void* fhdl); + private: struct lfs_config _lfs_cfg; lfs_t _lfs;