From 1fb5f65dcc49b3b9ab15ca12f725fa697f464420 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 15 Oct 2018 17:11:05 +0700 Subject: [PATCH] more nffs porting --- libraries/FileSystem/src/FileIO.cpp | 98 +++++++++++++ libraries/FileSystem/src/FileIO.h | 33 +++-- libraries/FileSystem/src/InternalFS.cpp | 25 ++-- libraries/FileSystem/src/InternalFS.h | 2 +- .../src/mynewt-nffs/include/nffs/config.h | 14 +- libraries/FileSystem/src/nffs_port.c | 130 ++++++++++++++++++ 6 files changed, 272 insertions(+), 30 deletions(-) diff --git a/libraries/FileSystem/src/FileIO.cpp b/libraries/FileSystem/src/FileIO.cpp index 6a96b3fb..90c9fd6c 100644 --- a/libraries/FileSystem/src/FileIO.cpp +++ b/libraries/FileSystem/src/FileIO.cpp @@ -39,4 +39,102 @@ namespace BluefuritLib { +File::File (FileSystemClass &fs) + : _fs(fs) +{ + _hdl = NULL; +} + +File::File (char const *_filename, uint8_t _mode, FileSystemClass &fs) + : _fs(fs) +{ + _hdl = NULL; +} + +File::~File () +{ + +} + +size_t File::write (uint8_t) +{ + +} + +size_t File::write (uint8_t const *buf, size_t size) +{ + +} + +int File::read () +{ + +} + +int File::peek () +{ + +} + +int File::available () +{ + +} + +void File::flush () +{ + +} + +int File::read (void *buf, uint16_t nbyte) +{ + +} + +bool File::seek (uint32_t pos) +{ + +} + +uint32_t File::position () +{ + +} + +uint32_t File::size () +{ + +} + +void File::close () +{ + +} + +File::operator bool () +{ + +} + +char const * File::name () +{ + +} + +bool File::isDirectory () +{ + +} + +File File::openNextFile (uint8_t mode) +{ + +} + +void File::rewindDirectory (void) +{ + +} + + } diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index 7d123c5c..ee8b77f4 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -16,6 +16,18 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/**************************************************************************/ +/*! + @file FileIO.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + Copyright (c) 2018, Adafruit Industries (adafruit.com) +*/ +/**************************************************************************/ + #ifndef __FILEIO_H__ #define __FILEIO_H__ @@ -25,6 +37,8 @@ #define FILE_WRITE 1 #define FILE_APPEND 2 +class MynewtNFFS; + namespace BluefuritLib { @@ -56,20 +70,13 @@ class File: public Stream //using Print::write; - private: - void doBuffer (); - uint8_t buffered; - uint8_t readPos; - uint16_t dirPosition; - static int const BUFFER_SIZE = 64; - uint8_t buffer[BUFFER_SIZE]; - private: FileSystemClass &_fs; - String filename; - uint8_t mode; - uint8_t handle; + void* _hdl; +// String filename; + + friend class ::MynewtNFFS; }; class FileSystemClass @@ -80,7 +87,7 @@ class FileSystemClass // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. // Note that currently only one file can be open at a time. - virtual File open (char const *filename, uint8_t mode = FILE_READ) = 0; + virtual File open (char const *filename, uint8_t mode) = 0; // Methods to determine if the requested file path exists. virtual bool exists (char const *filepath) = 0; @@ -93,8 +100,6 @@ class FileSystemClass virtual bool remove (char const *filepath) = 0; virtual bool rmdir (char const *filepath) = 0; - - friend class File; }; } diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index 4ac4488d..18101eb2 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -37,21 +37,13 @@ #include #include "InternalFS.h" -MynewtNFFS InternalFS; - -//--------------------------------------------------------------------+ -// Mynewt NFFS port -//--------------------------------------------------------------------+ extern "C" { -nffs_os_mempool_t nffs_file_pool; -nffs_os_mempool_t nffs_dir_pool; -nffs_os_mempool_t nffs_inode_entry_pool; -nffs_os_mempool_t nffs_block_entry_pool; -nffs_os_mempool_t nffs_cache_inode_pool; -nffs_os_mempool_t nffs_cache_block_pool; + int nffs_init(void); } +MynewtNFFS InternalFS; + MynewtNFFS::MynewtNFFS (void) { @@ -64,14 +56,19 @@ MynewtNFFS::~MynewtNFFS () bool MynewtNFFS::begin (void) { -// nffs_config_init(); - - nffs_cache_clear(); + nffs_init(); } BluefuritLib::File MynewtNFFS::open (char const *filename, uint8_t mode) { + BluefuritLib::File file(*this); + struct nffs_file* fh = NULL; + VERIFY_STATUS( nffs_file_open(&fh, filename, mode), file); + + file._hdl = fh; + + return file; } bool MynewtNFFS::exists (char const *filepath) diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 80cda9ea..d843a11c 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -41,7 +41,7 @@ // Internal Flash uses Apache Mynewt Newtron Flash File System // https://github.com/apache/mynewt-nffs -#include "mynewt-nffs/include/nffs/nffs.h" +#include "nffs/nffs.h" class MynewtNFFS: public BluefuritLib::FileSystemClass { diff --git a/libraries/FileSystem/src/mynewt-nffs/include/nffs/config.h b/libraries/FileSystem/src/mynewt-nffs/include/nffs/config.h index 0623daf5..4b1899af 100644 --- a/libraries/FileSystem/src/mynewt-nffs/include/nffs/config.h +++ b/libraries/FileSystem/src/mynewt-nffs/include/nffs/config.h @@ -22,7 +22,19 @@ #ifdef ARDUINO_NRF52_ADAFRUIT -typedef void* nffs_os_mempool_t; +#include "common_inc.h" + +#define VERIFY_NFFS(_err) \ + do { \ + uint32_t _status = _err; \ + if ( 0 != _status ) { \ + cprintf("%s: %d: verify failed, error = %d\n", __PRETTY_FUNCTION__, __LINE__, _status);\ + return _status; \ + }\ + }while(0)\ + +// use heap for mempool +typedef uint32_t nffs_os_mempool_t; #define NFFS_CONFIG_USE_HEAP 0 #define NFFS_CONFIG_MAX_AREAS 7 diff --git a/libraries/FileSystem/src/nffs_port.c b/libraries/FileSystem/src/nffs_port.c index 23552419..5a5a1a62 100644 --- a/libraries/FileSystem/src/nffs_port.c +++ b/libraries/FileSystem/src/nffs_port.c @@ -34,8 +34,29 @@ */ /**************************************************************************/ +#include +#include + #include "kernel.h" #include "nffs/nffs.h" +#include "common_inc.h" + +#ifdef NRF52840_XXAA +#define NFFS_FLASH_ADDR_START 0xED000 +#else +#define NFFS_FLASH_ADDR_START 0x6D000 +#endif + +#define NFFS_FLASH_SIZE (7*4096) + +static uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial, uint16_t initial_value, bool pad); + +/* + * NFFS code keeps fs state in RAM but access to these structures is not + * thread-safe - we need global lock for each fs operation to guarantee two + * threads won't modify NFFS at the same time. + */ +static SemaphoreHandle_t _nffs_mutex = NULL; nffs_os_mempool_t nffs_file_pool = sizeof(struct nffs_file); nffs_os_mempool_t nffs_dir_pool = sizeof(struct nffs_dir); @@ -59,3 +80,112 @@ int nffs_os_mempool_free(nffs_os_mempool_t *pool, void *block) vPortFree(block); return 0; } + + +int nffs_init(void) +{ + _nffs_mutex = xSemaphoreCreateMutex(); + + nffs_misc_reset(); + +// return fs_register(FS_NFFS, &nffs_fs); + + struct nffs_flash_desc flash_desc = + { + .id = 0, + .sector_count = NRF_FICR->CODESIZE, + .area_offset = NFFS_FLASH_ADDR_START, + .area_size = NRF_FICR->CODEPAGESIZE*7 + }; + + struct nffs_area_desc descs[NFFS_CONFIG_MAX_AREAS + 1]; + int cnt = NFFS_CONFIG_MAX_AREAS; + + VERIFY_NFFS( nffs_misc_desc_from_flash_area(&flash_desc, &cnt, descs) ); + + /* Attempt to restore an existing nffs file system from flash. */ + int rc = nffs_restore_full(descs); + + // If not formatted, format it + if (FS_ECORRUPT == rc) + { + LOG_LV1(NULL, "No FS detected, format"); + rc = nffs_format_full(descs); + } + + VERIFY_NFFS( rc ); + + return rc; +} + +int nffs_os_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes) +{ + (void) id; + return (num_bytes == flash_nrf52_read(dst, address, num_bytes)) ? 0 : -1; +} + +int nffs_os_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes) +{ + (void) id; + return (num_bytes == flash_nrf52_write(address, src, num_bytes)) ? 0 : -1; +} + +int nffs_os_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) +{ + (void) id; + (void) num_bytes; + + return flash_nrf52_erase(address) ? 0 : -1; +} + +int nffs_os_flash_info(uint8_t id, uint32_t sector, uint32_t *address, uint32_t *size) +{ + (void) id; + + *address = sector * NRF_FICR->CODEPAGESIZE; + *size = NRF_FICR->CODEPAGESIZE; + + return 0; +} + + + +uint16_t nffs_os_crc16_ccitt(uint16_t initial_crc, const void *buf, int len, int final) +{ + return crc16(buf, len, 0x1021, initial_crc, final); +} + +/* + * Copyright (c) 2017 Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +static uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial, uint16_t initial_value, bool pad) +{ + uint16_t crc = initial_value; + size_t padding = pad ? sizeof(crc) : 0; + size_t i, b; + + /* src length + padding (if required) */ + for (i = 0; i < len + padding; i++) { + + for (b = 0; b < 8; b++) { + uint16_t divide = crc & 0x8000; + + crc = (crc << 1); + + /* choose input bytes or implicit trailing zeros */ + if (i < len) { + crc |= !!(src[i] & (0x80 >> b)); + } + + if (divide) { + crc = crc ^ polynomial; + } + } + } + + return crc; +} +