Skip to content

Commit 070ec32

Browse files
committed
revised block device structure
1 parent 0917374 commit 070ec32

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Version 7.0.0
88
- Fixed incorrect handling of simple for loops with unsigned variables
99
- Improved some debug error messages
1010
- Only use the stack for variables whose address is actually taken (thanks to Ada for this!)
11+
- Modified _BlockDevice data structure (used for littlefs, but someday may be used for fatfs too)
1112

1213
Version 6.9.10
1314
- Fix compilation error when GETWORD is replaced by MOV or MUL

include/filesys/littlefs/lfswrapper.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@ static _BlockDevice *Default_SPI_Init(unsigned offset, unsigned used_size, unsig
2020
{
2121
static _SpiFlash spi;
2222
static _BlockDevice blk;
23-
static char read_cache[SPI_PROG_SIZE];
24-
static char prog_cache[SPI_PROG_SIZE];
25-
static char lookahead_cache[SPI_PROG_SIZE];
2623

2724
spi.Init(offset, used_size, erase_size);
2825
blk.blk_read = &spi.Read;
2926
blk.blk_write = &spi.WritePage;
3027
blk.blk_erase = &spi.Erase;
3128
blk.blk_sync = &spi.Sync;
3229

33-
blk.read_cache = read_cache;
34-
blk.write_cache = prog_cache;
35-
blk.lookahead_cache = lookahead_cache;
36-
3730
return &blk;
3831
}
3932

@@ -97,6 +90,10 @@ static int _flash_sync(const struct lfs_config *cfg) {
9790
static int _flash_create(struct lfs_config *cfg, struct littlefs_flash_config *flashcfg)
9891
{
9992
_BlockDevice *blk = flashcfg->dev;
93+
static bool default_cache_used = false;
94+
static char read_cache[SPI_PROG_SIZE];
95+
static char prog_cache[SPI_PROG_SIZE];
96+
static char lookahead_cache[SPI_PROG_SIZE];
10097

10198
if (!blk) {
10299
blk = Default_SPI_Init(flashcfg->offset, flashcfg->used_size, flashcfg->erase_size);
@@ -123,10 +120,18 @@ static int _flash_create(struct lfs_config *cfg, struct littlefs_flash_config *f
123120
cfg->block_cycles = 400;
124121

125122
// buffers
126-
cfg->read_buffer = blk->read_cache;
127-
cfg->prog_buffer = blk->write_cache;
128-
cfg->lookahead_buffer = blk->lookahead_cache;
129-
123+
if (default_cache_used) {
124+
// dynamically allocate more memory
125+
int blksize = SPI_PROG_SIZE;
126+
cfg->read_buffer = malloc(blksize);
127+
cfg->prog_buffer = malloc(blksize);
128+
cfg->lookahead_buffer = malloc(blksize);
129+
} else {
130+
cfg->read_buffer = read_cache;
131+
cfg->prog_buffer = prog_cache;
132+
cfg->lookahead_buffer = lookahead_cache;
133+
default_cache_used = true;
134+
}
130135
// set up block device operations
131136
cfg->read = _flash_read;
132137
cfg->prog = _flash_prog;

include/sys/vfs.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,23 @@ int _mkfs_parallaxfs(void) _IMPL("filesys/parallax/parallaxfs_vfs.c");
5757
/* block read/write device */
5858
typedef struct block_device {
5959
/* functions for doing I/O */
60+
61+
/* read an arbitrary number of bytes (need not be block aligned */
6062
int (*blk_read)(void *dst, unsigned long flashAdr, unsigned long size);
63+
64+
/* write & erase blocks (erase may be a no-op if not required) */
6165
int (*blk_write)(void *src, unsigned long flashAdr); // write one block
6266
int (*blk_erase)(unsigned long flashAdr); // erase one block
67+
68+
/* sync to device */
6369
int (*blk_sync)(void);
6470

65-
/* buffers for the I/O */
66-
char *read_cache;
67-
char *write_cache;
68-
char *lookahead_cache;
71+
/* general ioctl */
72+
int (*blk_ioctl)(int request, void *arg);
73+
74+
/* block size */
75+
int blksize;
76+
6977
} _BlockDevice;
7078

7179
/* structure for configuring a littlefs flash file system */

0 commit comments

Comments
 (0)