adding lfs

This commit is contained in:
hathach committed 2018-10-15 23:44:40 +07:00
1 parent c2e1bf11ee
commit 4482a07bbf
5 files changed
+95 -7

No files matched your search

+1
View File
@@ -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")
}
@@ -35,6 +35,8 @@
/**************************************************************************/
#include <Arduino.h>
#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();
}
/*------------------------------------------------------------------*/
+78 -2
View File
@@ -37,16 +37,79 @@
#include <Arduino.h>
#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)
+2
View File
@@ -57,6 +57,8 @@ class LittleFS: public BluefuritLib::FileSystemClass
bool rmdir (char const *filepath);
private:
struct lfs_config _lfs_cfg;
lfs_t _lfs;
};
+4 -2
View File
@@ -24,6 +24,8 @@
#include <stdbool.h>
#include <string.h>
#include "FreeRTOS.h"
#ifndef LFS_NO_MALLOC
#include <stdlib.h>
#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