@@ -20,20 +20,13 @@ static _BlockDevice *Default_SPI_Init(unsigned offset, unsigned used_size, unsig
20
20
{
21
21
static _SpiFlash spi;
22
22
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];
26
23
27
24
spi.Init (offset, used_size, erase_size);
28
25
blk.blk_read = &spi.Read ;
29
26
blk.blk_write = &spi.WritePage ;
30
27
blk.blk_erase = &spi.Erase ;
31
28
blk.blk_sync = &spi.Sync ;
32
29
33
- blk.read_cache = read_cache;
34
- blk.write_cache = prog_cache;
35
- blk.lookahead_cache = lookahead_cache;
36
-
37
30
return &blk;
38
31
}
39
32
@@ -97,6 +90,10 @@ static int _flash_sync(const struct lfs_config *cfg) {
97
90
static int _flash_create (struct lfs_config *cfg, struct littlefs_flash_config *flashcfg)
98
91
{
99
92
_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];
100
97
101
98
if (!blk) {
102
99
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
123
120
cfg->block_cycles = 400 ;
124
121
125
122
// 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
+ }
130
135
// set up block device operations
131
136
cfg->read = _flash_read;
132
137
cfg->prog = _flash_prog;
0 commit comments