Skip to content

Commit 8b1526e

Browse files
committed
atmel-samd: Add a heap based cache for writing to flash.
The code will fallback to the flash scratch space when the GC cannot allocate us enough memory.
1 parent bb1822f commit 8b1526e

3 files changed

Lines changed: 200 additions & 43 deletions

File tree

atmel-samd/access_vfs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ Ctrl_status vfs_test_unit_ready(void)
5555
}
5656

5757
//! This function returns the address of the last valid sector
58-
//! @param uint32_t_nb_sector Pointer to number of sectors (sector=512 bytes)
58+
//! @param uint32_t_nb_sector Pointer to the last valid sector (sector=512 bytes)
5959
//! @return Ctrl_status
6060
//! It is ready -> CTRL_GOOD
6161
//! Memory unplug -> CTRL_NO_PRESENT
6262
//! Not initialized or changed -> CTRL_BUSY
6363
//! An error occurred -> CTRL_FAIL
64-
Ctrl_status vfs_read_capacity(uint32_t *uint32_t_nb_sector)
64+
Ctrl_status vfs_read_capacity(uint32_t *last_valid_sector)
6565
{
66-
if (disk_ioctl(VFS_INDEX, GET_SECTOR_COUNT, uint32_t_nb_sector) != RES_OK) {
66+
if (disk_ioctl(VFS_INDEX, GET_SECTOR_COUNT, last_valid_sector) != RES_OK) {
6767
return CTRL_FAIL;
6868
}
69-
return CTRL_GOOD;
69+
// Subtract one from the sector count to get the last valid sector.
70+
(*last_valid_sector)--;
71+
return CTRL_GOOD;
7072
}
7173

7274
//! This function returns the write-protected mode

atmel-samd/main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "py/gc.h"
1111

1212
#include "lib/fatfs/ff.h"
13+
#include "lib/fatfs/diskio.h"
1314
#include "lib/utils/pyexec.h"
1415
#include "extmod/fsusermount.h"
1516

@@ -160,6 +161,12 @@ static char *stack_top;
160161
static char heap[16384];
161162

162163
void reset_mp() {
164+
// Sync the file systems in case any used RAM from the GC to cache. As soon
165+
// as we re-init the GC all bets are off on the cache.
166+
disk_ioctl(0, CTRL_SYNC, NULL);
167+
disk_ioctl(1, CTRL_SYNC, NULL);
168+
disk_ioctl(2, CTRL_SYNC, NULL);
169+
163170
#if MICROPY_ENABLE_GC
164171
gc_init(heap, heap + sizeof(heap));
165172
#endif
@@ -185,9 +192,6 @@ int main(int argc, char **argv) {
185192
samd21_init();
186193
#endif
187194

188-
// Initialise the local flash filesystem.
189-
// Create it if needed, mount in on /flash, and set it as current dir.
190-
init_flash_fs();
191195

192196
int stack_dummy;
193197
// Store the location of stack_dummy as an approximation for the top of the
@@ -196,6 +200,11 @@ int main(int argc, char **argv) {
196200
stack_top = (char*)&stack_dummy;
197201
reset_mp();
198202

203+
// Initialise the local flash filesystem after the gc in case we need to
204+
// grab memory from it. Create it if needed, mount in on /flash, and set it
205+
// as current dir.
206+
init_flash_fs();
207+
199208
// Start USB after getting everything going.
200209
#ifdef USB_REPL
201210
udc_start();

0 commit comments

Comments
 (0)