diff --git a/libraries/nffs/src/NewtNffs.cpp b/libraries/nffs/src/NewtNffs.cpp index 69eff1f6..1a7cb8cb 100644 --- a/libraries/nffs/src/NewtNffs.cpp +++ b/libraries/nffs/src/NewtNffs.cpp @@ -36,16 +36,51 @@ #include "NewtNffs.h" +#include "syscfg/syscfg.h" +#include "hal/hal_flash.h" + +#define NFFS_AREA_MAX (7) + NewtNffs Nffs; NewtNffs::NewtNffs(void) { - errnum = FS_EOK; + errnum = FS_EOK; + _initialized = false; } -void NewtNffs::begin(void) +bool NewtNffs::begin(void) { - nffs_pkg_init(); + // Follow code in nffs_pkg_init() + if (_initialized) return true; + + // Init flash + hal_flash_init(); + + /* Initialize nffs's internal state. */ + errnum = nffs_init(); + VERIFY_STATUS( errnum, false ); + + /* Convert the set of flash blocks we intend to use for nffs into an array + * of nffs area descriptors. + */ + struct nffs_area_desc descs[NFFS_AREA_MAX + 1]; + int cnt = NFFS_AREA_MAX; + + errnum = nffs_misc_desc_from_flash_area( MYNEWT_VAL(NFFS_FLASH_AREA), &cnt, descs); + VERIFY_STATUS( errnum, false ); + + /* Attempt to restore an existing nffs file system from flash. */ + errnum = nffs_detect(descs); + + // If not formatted, format it + if (FS_ECORRUPT == errnum) + { + errnum = nffs_format(descs); + VERIFY_STATUS( errnum, false ); + } + + return true; } bool NewtNffs::format(void) diff --git a/libraries/nffs/src/NewtNffs.h b/libraries/nffs/src/NewtNffs.h index 22e6c815..f72d5334 100644 --- a/libraries/nffs/src/NewtNffs.h +++ b/libraries/nffs/src/NewtNffs.h @@ -47,12 +47,15 @@ class NewtNffs { +private: + bool _initialized; + public: int errnum; NewtNffs(void); - void begin(void); + bool begin(void); // Format the flash bool format(void); diff --git a/libraries/nffs/src/fs/nffs/include/nffs/nffs.h b/libraries/nffs/src/fs/nffs/include/nffs/nffs.h index f49114ca..35baeb5a 100644 --- a/libraries/nffs/src/fs/nffs/include/nffs/nffs.h +++ b/libraries/nffs/src/fs/nffs/include/nffs/nffs.h @@ -65,8 +65,6 @@ int nffs_format(const struct nffs_area_desc *area_descs); int nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad); -void nffs_pkg_init(void); - #ifdef __cplusplus } #endif diff --git a/libraries/nffs/src/fs/nffs/src/nffs.c b/libraries/nffs/src/fs/nffs/src/nffs.c index 67a6f44e..2c74c1f8 100644 --- a/libraries/nffs/src/fs/nffs/src/nffs.c +++ b/libraries/nffs/src/fs/nffs/src/nffs.c @@ -22,18 +22,6 @@ #include #include -#define NFFS_AREA_MAX 8 - -#define SYSINIT_ASSERT_ACTIVE() -#define SYSINIT_PANIC_ASSERT(rc) do \ -{ \ - if (!(rc)) { \ - SYSINIT_PANIC(); \ - } \ -} while (0) -#define SYSINIT_PANIC() return // assert(0) - -#include "syscfg/syscfg.h" //#include "sysinit/sysinit.h" //#include "sysflash/sysflash.h" //#include "bsp/bsp.h" @@ -756,23 +744,17 @@ nffs_init(void) return 0; } +#if 0 void nffs_pkg_init(void) { - static bool already_init = false; - struct nffs_area_desc descs[NFFS_AREA_MAX + 1]; int cnt; int rc; - if (already_init) return; - already_init = true; - /* Ensure this function only gets called by sysinit. */ SYSINIT_ASSERT_ACTIVE(); - hal_flash_init(); - /* Initialize nffs's internal state. */ rc = nffs_init(); SYSINIT_PANIC_ASSERT(rc == 0); @@ -815,3 +797,4 @@ nffs_pkg_init(void) break; } } +#endif