/**************************************************************************/ /*! @file InternalFS.cpp @author hathach (tinyusb.org) @section LICENSE Software License Agreement (BSD License) Copyright (c) 2018, Adafruit Industries (adafruit.com) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ #include #include "InternalFS.h" #include "flash/flash_nrf52.h" #ifdef NRF52840_XXAA #define LFS_FLASH_ADDR 0xED000 #else #define LFS_FLASH_ADDR 0x6D000 #endif #define LFS_PAGE_SIZE 4096 #define LFS_FLASH_SIZE (7*LFS_PAGE_SIZE) #define LFS_BLOCK_SIZE 128 #define LFS_BLOCK_PER_PAGE (LFS_PAGE_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; } int _iflash_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; memcpy(buffer, (void*) 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. 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; uint32_t addr = lba2addr(block) + off; flash_nrf52_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. int _iflash_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 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); } bool LittleFS::_f_seek (void* fhdl, uint32_t pos) { return lfs_file_seek(&_lfs, (lfs_file_t*) fhdl, pos, LFS_SEEK_SET) >= 0; } uint32_t LittleFS::_f_position (void* fhdl) { return lfs_file_tell(&_lfs, (lfs_file_t*) fhdl); } uint32_t LittleFS::_f_size (void* fhdl) { return lfs_file_size(&_lfs, (lfs_file_t*) fhdl); } void LittleFS::_f_close (void* fhdl) { lfs_file_close(&_lfs, (lfs_file_t*) fhdl); } File LittleFS::_f_openNextFile (void* fhdl, uint8_t mode) { } void LittleFS::_f_rewindDirectory (void* fhdl) { VERIFY_LFS(lfs_dir_rewind(&_lfs, (lfs_dir_t* ) fhdl),); }