Skip to content

Commit 1fb5f65

Browse files
committed
more nffs porting
1 parent 4950de9 commit 1fb5f65

File tree

6 files changed

+272
-30
lines changed

6 files changed

+272
-30
lines changed

libraries/FileSystem/src/FileIO.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,102 @@
3939
namespace BluefuritLib
4040
{
4141

42+
File::File (FileSystemClass &fs)
43+
: _fs(fs)
44+
{
45+
_hdl = NULL;
46+
}
47+
48+
File::File (char const *_filename, uint8_t _mode, FileSystemClass &fs)
49+
: _fs(fs)
50+
{
51+
_hdl = NULL;
52+
}
53+
54+
File::~File ()
55+
{
56+
57+
}
58+
59+
size_t File::write (uint8_t)
60+
{
61+
62+
}
63+
64+
size_t File::write (uint8_t const *buf, size_t size)
65+
{
66+
67+
}
68+
69+
int File::read ()
70+
{
71+
72+
}
73+
74+
int File::peek ()
75+
{
76+
77+
}
78+
79+
int File::available ()
80+
{
81+
82+
}
83+
84+
void File::flush ()
85+
{
86+
87+
}
88+
89+
int File::read (void *buf, uint16_t nbyte)
90+
{
91+
92+
}
93+
94+
bool File::seek (uint32_t pos)
95+
{
96+
97+
}
98+
99+
uint32_t File::position ()
100+
{
101+
102+
}
103+
104+
uint32_t File::size ()
105+
{
106+
107+
}
108+
109+
void File::close ()
110+
{
111+
112+
}
113+
114+
File::operator bool ()
115+
{
116+
117+
}
118+
119+
char const * File::name ()
120+
{
121+
122+
}
123+
124+
bool File::isDirectory ()
125+
{
126+
127+
}
128+
129+
File File::openNextFile (uint8_t mode)
130+
{
131+
132+
}
133+
134+
void File::rewindDirectory (void)
135+
{
136+
137+
}
138+
139+
42140
}

libraries/FileSystem/src/FileIO.h

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
/**************************************************************************/
20+
/*!
21+
@file FileIO.h
22+
@author hathach (tinyusb.org)
23+
24+
@section LICENSE
25+
26+
Software License Agreement (BSD License)
27+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
28+
*/
29+
/**************************************************************************/
30+
1931
#ifndef __FILEIO_H__
2032
#define __FILEIO_H__
2133

@@ -25,6 +37,8 @@
2537
#define FILE_WRITE 1
2638
#define FILE_APPEND 2
2739

40+
class MynewtNFFS;
41+
2842
namespace BluefuritLib
2943
{
3044

@@ -56,20 +70,13 @@ class File: public Stream
5670

5771
//using Print::write;
5872

59-
private:
60-
void doBuffer ();
61-
uint8_t buffered;
62-
uint8_t readPos;
63-
uint16_t dirPosition;
64-
static int const BUFFER_SIZE = 64;
65-
uint8_t buffer[BUFFER_SIZE];
66-
6773
private:
6874
FileSystemClass &_fs;
69-
String filename;
70-
uint8_t mode;
71-
uint8_t handle;
75+
void* _hdl;
7276

77+
// String filename;
78+
79+
friend class ::MynewtNFFS;
7380
};
7481

7582
class FileSystemClass
@@ -80,7 +87,7 @@ class FileSystemClass
8087
// Open the specified file/directory with the supplied mode (e.g. read or
8188
// write, etc). Returns a File object for interacting with the file.
8289
// Note that currently only one file can be open at a time.
83-
virtual File open (char const *filename, uint8_t mode = FILE_READ) = 0;
90+
virtual File open (char const *filename, uint8_t mode) = 0;
8491

8592
// Methods to determine if the requested file path exists.
8693
virtual bool exists (char const *filepath) = 0;
@@ -93,8 +100,6 @@ class FileSystemClass
93100
virtual bool remove (char const *filepath) = 0;
94101

95102
virtual bool rmdir (char const *filepath) = 0;
96-
97-
friend class File;
98103
};
99104

100105
}

libraries/FileSystem/src/InternalFS.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,13 @@
3737
#include <Arduino.h>
3838
#include "InternalFS.h"
3939

40-
MynewtNFFS InternalFS;
41-
42-
//--------------------------------------------------------------------+
43-
// Mynewt NFFS port
44-
//--------------------------------------------------------------------+
4540
extern "C"
4641
{
47-
nffs_os_mempool_t nffs_file_pool;
48-
nffs_os_mempool_t nffs_dir_pool;
49-
nffs_os_mempool_t nffs_inode_entry_pool;
50-
nffs_os_mempool_t nffs_block_entry_pool;
51-
nffs_os_mempool_t nffs_cache_inode_pool;
52-
nffs_os_mempool_t nffs_cache_block_pool;
42+
int nffs_init(void);
5343
}
5444

45+
MynewtNFFS InternalFS;
46+
5547
MynewtNFFS::MynewtNFFS (void)
5648
{
5749

@@ -64,14 +56,19 @@ MynewtNFFS::~MynewtNFFS ()
6456

6557
bool MynewtNFFS::begin (void)
6658
{
67-
// nffs_config_init();
68-
69-
nffs_cache_clear();
59+
nffs_init();
7060
}
7161

7262
BluefuritLib::File MynewtNFFS::open (char const *filename, uint8_t mode)
7363
{
64+
BluefuritLib::File file(*this);
65+
struct nffs_file* fh = NULL;
66+
67+
VERIFY_STATUS( nffs_file_open(&fh, filename, mode), file);
68+
69+
file._hdl = fh;
7470

71+
return file;
7572
}
7673

7774
bool MynewtNFFS::exists (char const *filepath)

libraries/FileSystem/src/InternalFS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
// Internal Flash uses Apache Mynewt Newtron Flash File System
4343
// https://github.com/apache/mynewt-nffs
44-
#include "mynewt-nffs/include/nffs/nffs.h"
44+
#include "nffs/nffs.h"
4545

4646
class MynewtNFFS: public BluefuritLib::FileSystemClass
4747
{

libraries/FileSystem/src/mynewt-nffs/include/nffs/config.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@
2222

2323
#ifdef ARDUINO_NRF52_ADAFRUIT
2424

25-
typedef void* nffs_os_mempool_t;
25+
#include "common_inc.h"
26+
27+
#define VERIFY_NFFS(_err) \
28+
do { \
29+
uint32_t _status = _err; \
30+
if ( 0 != _status ) { \
31+
cprintf("%s: %d: verify failed, error = %d\n", __PRETTY_FUNCTION__, __LINE__, _status);\
32+
return _status; \
33+
}\
34+
}while(0)\
35+
36+
// use heap for mempool
37+
typedef uint32_t nffs_os_mempool_t;
2638

2739
#define NFFS_CONFIG_USE_HEAP 0
2840
#define NFFS_CONFIG_MAX_AREAS 7

libraries/FileSystem/src/nffs_port.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,29 @@
3434
*/
3535
/**************************************************************************/
3636

37+
#include <stdint.h>
38+
#include <stdbool.h>
39+
3740
#include "kernel.h"
3841
#include "nffs/nffs.h"
42+
#include "common_inc.h"
43+
44+
#ifdef NRF52840_XXAA
45+
#define NFFS_FLASH_ADDR_START 0xED000
46+
#else
47+
#define NFFS_FLASH_ADDR_START 0x6D000
48+
#endif
49+
50+
#define NFFS_FLASH_SIZE (7*4096)
51+
52+
static uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial, uint16_t initial_value, bool pad);
53+
54+
/*
55+
* NFFS code keeps fs state in RAM but access to these structures is not
56+
* thread-safe - we need global lock for each fs operation to guarantee two
57+
* threads won't modify NFFS at the same time.
58+
*/
59+
static SemaphoreHandle_t _nffs_mutex = NULL;
3960

4061
nffs_os_mempool_t nffs_file_pool = sizeof(struct nffs_file);
4162
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)
5980
vPortFree(block);
6081
return 0;
6182
}
83+
84+
85+
int nffs_init(void)
86+
{
87+
_nffs_mutex = xSemaphoreCreateMutex();
88+
89+
nffs_misc_reset();
90+
91+
// return fs_register(FS_NFFS, &nffs_fs);
92+
93+
struct nffs_flash_desc flash_desc =
94+
{
95+
.id = 0,
96+
.sector_count = NRF_FICR->CODESIZE,
97+
.area_offset = NFFS_FLASH_ADDR_START,
98+
.area_size = NRF_FICR->CODEPAGESIZE*7
99+
};
100+
101+
struct nffs_area_desc descs[NFFS_CONFIG_MAX_AREAS + 1];
102+
int cnt = NFFS_CONFIG_MAX_AREAS;
103+
104+
VERIFY_NFFS( nffs_misc_desc_from_flash_area(&flash_desc, &cnt, descs) );
105+
106+
/* Attempt to restore an existing nffs file system from flash. */
107+
int rc = nffs_restore_full(descs);
108+
109+
// If not formatted, format it
110+
if (FS_ECORRUPT == rc)
111+
{
112+
LOG_LV1(NULL, "No FS detected, format");
113+
rc = nffs_format_full(descs);
114+
}
115+
116+
VERIFY_NFFS( rc );
117+
118+
return rc;
119+
}
120+
121+
int nffs_os_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes)
122+
{
123+
(void) id;
124+
return (num_bytes == flash_nrf52_read(dst, address, num_bytes)) ? 0 : -1;
125+
}
126+
127+
int nffs_os_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes)
128+
{
129+
(void) id;
130+
return (num_bytes == flash_nrf52_write(address, src, num_bytes)) ? 0 : -1;
131+
}
132+
133+
int nffs_os_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
134+
{
135+
(void) id;
136+
(void) num_bytes;
137+
138+
return flash_nrf52_erase(address) ? 0 : -1;
139+
}
140+
141+
int nffs_os_flash_info(uint8_t id, uint32_t sector, uint32_t *address, uint32_t *size)
142+
{
143+
(void) id;
144+
145+
*address = sector * NRF_FICR->CODEPAGESIZE;
146+
*size = NRF_FICR->CODEPAGESIZE;
147+
148+
return 0;
149+
}
150+
151+
152+
153+
uint16_t nffs_os_crc16_ccitt(uint16_t initial_crc, const void *buf, int len, int final)
154+
{
155+
return crc16(buf, len, 0x1021, initial_crc, final);
156+
}
157+
158+
/*
159+
* Copyright (c) 2017 Intel Corporation.
160+
*
161+
* SPDX-License-Identifier: Apache-2.0
162+
*/
163+
164+
static uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial, uint16_t initial_value, bool pad)
165+
{
166+
uint16_t crc = initial_value;
167+
size_t padding = pad ? sizeof(crc) : 0;
168+
size_t i, b;
169+
170+
/* src length + padding (if required) */
171+
for (i = 0; i < len + padding; i++) {
172+
173+
for (b = 0; b < 8; b++) {
174+
uint16_t divide = crc & 0x8000;
175+
176+
crc = (crc << 1);
177+
178+
/* choose input bytes or implicit trailing zeros */
179+
if (i < len) {
180+
crc |= !!(src[i] & (0x80 >> b));
181+
}
182+
183+
if (divide) {
184+
crc = crc ^ polynomial;
185+
}
186+
}
187+
}
188+
189+
return crc;
190+
}
191+

0 commit comments

Comments
 (0)