stm32/mboot: Add support for littlefs. · csamuelson/circuitpython@67fd58b · GitHub
Skip to content

Commit 67fd58b

Browse files
committed
stm32/mboot: Add support for littlefs.
Mboot now supports FAT, LFS1 and LFS2 filesystems, to load firmware from. The filesystem needed by the board must be explicitly enabled by the configuration variables MBOOT_VFS_FAT, MBOOT_VFS_LFS1 and MBOOT_VFS_LFS2. Boards that previously used FAT implicitly (with MBOOT_FSLOAD enabled) must now add the following config to mpconfigboard.h: #define MBOOT_VFS_FAT (1) Signed-off-by: Damien George <damien@micropython.org>
1 parent 390f329 commit 67fd58b

9 files changed

Lines changed: 309 additions & 28 deletions

File tree

ports/stm32/boards/MIKROE_CLICKER2_STM32/mpconfigboard.h

Lines changed: 1 addition & 0 deletions

ports/stm32/boards/PYBD_SF2/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ extern struct _spi_bdev_t spi_bdev2;
188188

189189
#define MBOOT_USB_AUTODETECT_PORT (1)
190190
#define MBOOT_FSLOAD (1)
191+
#define MBOOT_VFS_FAT (1)
191192

192193
#define MBOOT_I2C_PERIPH_ID 1
193194
#define MBOOT_I2C_SCL (pin_B8)

ports/stm32/mboot/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ CFLAGS += -DSTM32_HAL_H='<stm32$(MCU_SERIES)xx_hal.h>'
7070
CFLAGS += -DBOARD_$(BOARD)
7171
CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR)
7272
CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\"
73+
CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT
74+
CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT
7375
CFLAGS += -DBUILDING_MBOOT=1
7476
CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID)
7577

@@ -91,6 +93,10 @@ endif
9193
$(BUILD)/lib/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN)
9294
LIB_SRC_C = \
9395
lib/libc/string0.c \
96+
lib/littlefs/lfs1.c \
97+
lib/littlefs/lfs1_util.c \
98+
lib/littlefs/lfs2.c \
99+
lib/littlefs/lfs2_util.c \
94100
lib/oofatfs/ff.c \
95101
lib/oofatfs/ffunicode.c \
96102
extmod/uzlib/crc32.c \
@@ -104,6 +110,7 @@ SRC_C = \
104110
fsload.c \
105111
gzstream.c \
106112
vfs_fat.c \
113+
vfs_lfs.c \
107114
drivers/bus/softspi.c \
108115
drivers/bus/softqspi.c \
109116
drivers/memory/spiflash.c \

ports/stm32/mboot/fsload.c

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2019 Damien P. George
6+
* Copyright (c) 2019-2020 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -32,6 +32,10 @@
3232

3333
#if MBOOT_FSLOAD
3434

35+
#if !(MBOOT_VFS_FAT || MBOOT_VFS_LFS1 || MBOOT_VFS_LFS2)
36+
#error Must enable at least one VFS component
37+
#endif
38+
3539
static int fsload_program_file(bool write_to_flash) {
3640
// Parse DFU
3741
uint8_t buf[512];
@@ -183,27 +187,58 @@ int fsload_process(void) {
183187
if (elem[0] == mount_point) {
184188
uint32_t base_addr = get_le32(&elem[2]);
185189
uint32_t byte_len = get_le32(&elem[6]);
190+
int ret;
191+
union {
192+
#if MBOOT_VFS_FAT
193+
vfs_fat_context_t fat;
194+
#endif
195+
#if MBOOT_VFS_LFS1
196+
vfs_lfs1_context_t lfs1;
197+
#endif
198+
#if MBOOT_VFS_LFS2
199+
vfs_lfs2_context_t lfs2;
200+
#endif
201+
} ctx;
202+
const stream_methods_t *methods;
203+
#if MBOOT_VFS_FAT
186204
if (elem[1] == ELEM_MOUNT_FAT) {
187-
vfs_fat_context_t ctx;
188-
int ret = vfs_fat_mount(&ctx, base_addr, byte_len);
205+
ret = vfs_fat_mount(&ctx.fat, base_addr, byte_len);
206+
methods = &vfs_fat_stream_methods;
207+
} else
208+
#endif
209+
#if MBOOT_VFS_LFS1
210+
if (elem[1] == ELEM_MOUNT_LFS1) {
211+
ret = vfs_lfs1_mount(&ctx.lfs1, base_addr, byte_len);
212+
methods = &vfs_lfs1_stream_methods;
213+
} else
214+
#endif
215+
#if MBOOT_VFS_LFS2
216+
if (elem[1] == ELEM_MOUNT_LFS2) {
217+
ret = vfs_lfs2_mount(&ctx.lfs2, base_addr, byte_len);
218+
methods = &vfs_lfs2_stream_methods;
219+
} else
220+
#endif
221+
{
222+
// Unknown filesystem type
223+
return -1;
224+
}
225+
226+
if (ret == 0) {
227+
ret = fsload_validate_and_program_file(&ctx, methods, fname);
228+
}
229+
230+
// Flash LEDs based on success/failure of update
231+
for (int i = 0; i < 4; ++i) {
189232
if (ret == 0) {
190-
ret = fsload_validate_and_program_file(&ctx, &vfs_fat_stream_methods, fname);
233+
led_state_all(7);
234+
} else {
235+
led_state_all(1);
191236
}
192-
// Flash LEDs based on success/failure of update
193-
for (int i = 0; i < 4; ++i) {
194-
if (ret == 0) {
195-
led_state_all(7);
196-
} else {
197-
led_state_all(1);
198-
}
199-
mp_hal_delay_ms(100);
200-
led_state_all(0);
201-
mp_hal_delay_ms(100);
202-
}
203-
return ret;
237+
mp_hal_delay_ms(100);
238+
led_state_all(0);
239+
mp_hal_delay_ms(100);
204240
}
205-
// Unknown filesystem type
206-
return -1;
241+
return ret;
207242
}
208243
elem += elem[-1];
209244
}

ports/stm32/mboot/fwupdate.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Update Mboot or MicroPython from a .dfu.gz file on the board's filesystem
2-
# MIT license; Copyright (c) 2019 Damien P. George
2+
# MIT license; Copyright (c) 2019-2020 Damien P. George
33

44
import struct, time
55
import uzlib, machine, stm
66

7+
# Constants to be used with update_mpy
8+
VFS_FAT = 1
9+
VFS_LFS1 = 2
10+
VFS_LFS2 = 3
711

812
FLASH_KEY1 = 0x45670123
913
FLASH_KEY2 = 0xCDEF89AB
@@ -152,7 +156,7 @@ def update_mboot(filename):
152156
print("Programming finished, can now reset or turn off.")
153157

154158

155-
def update_mpy(filename, fs_base, fs_len):
159+
def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT):
156160
# Check firmware is of .dfu.gz type
157161
try:
158162
with open(filename, "rb") as f:
@@ -166,11 +170,8 @@ def update_mpy(filename, fs_base, fs_len):
166170
ELEM_TYPE_END = 1
167171
ELEM_TYPE_MOUNT = 2
168172
ELEM_TYPE_FSLOAD = 3
169-
ELEM_MOUNT_FAT = 1
170173
mount_point = 1
171-
mount = struct.pack(
172-
"<BBBBLL", ELEM_TYPE_MOUNT, 10, mount_point, ELEM_MOUNT_FAT, fs_base, fs_len
173-
)
174+
mount = struct.pack("<BBBBLL", ELEM_TYPE_MOUNT, 10, mount_point, fs_type, fs_base, fs_len)
174175
fsup = struct.pack("<BBB", ELEM_TYPE_FSLOAD, 1 + len(filename), mount_point) + bytes(
175176
filename, "ascii"
176177
)

ports/stm32/mboot/mboot.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2019 Damien P. George
6+
* Copyright (c) 2019-2020 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -23,6 +23,8 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26+
#ifndef MICROPY_INCLUDED_STM32_MBOOT_MBOOT_H
27+
#define MICROPY_INCLUDED_STM32_MBOOT_MBOOT_H
2628

2729
#include <stdint.h>
2830
#include <stddef.h>
@@ -42,6 +44,8 @@ enum {
4244

4345
enum {
4446
ELEM_MOUNT_FAT = 1,
47+
ELEM_MOUNT_LFS1,
48+
ELEM_MOUNT_LFS2,
4549
};
4650

4751
extern uint8_t _estack[ELEM_DATA_SIZE];
@@ -55,3 +59,5 @@ int do_write(uint32_t addr, const uint8_t *src8, size_t len);
5559

5660
const uint8_t *elem_search(const uint8_t *elem, uint8_t elem_id);
5761
int fsload_process(void);
62+
63+
#endif // MICROPY_INCLUDED_STM32_MBOOT_MBOOT_H

ports/stm32/mboot/vfs.h

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
#ifndef MICROPY_INCLUDED_STM32_MBOOT_VFS_H
2727
#define MICROPY_INCLUDED_STM32_MBOOT_VFS_H
2828

29-
#include "lib/oofatfs/ff.h"
3029
#include "gzstream.h"
30+
#include "mboot.h"
31+
32+
#if MBOOT_VFS_FAT
33+
34+
#include "lib/oofatfs/ff.h"
3135

3236
typedef struct _vfs_fat_context_t {
3337
uint32_t bdev_base_addr;
@@ -40,4 +44,53 @@ extern const stream_methods_t vfs_fat_stream_methods;
4044

4145
int vfs_fat_mount(vfs_fat_context_t *ctx, uint32_t base_addr, uint32_t byte_len);
4246

47+
#endif
48+
49+
#if MBOOT_VFS_LFS1
50+
51+
#include "lib/littlefs/lfs1.h"
52+
53+
#define LFS_READ_SIZE (32)
54+
#define LFS_PROG_SIZE (32)
55+
#define LFS_LOOKAHEAD_SIZE (32)
56+
57+
typedef struct _vfs_lfs1_context_t {
58+
uint32_t bdev_base_addr;
59+
struct lfs1_config config;
60+
lfs1_t lfs;
61+
struct lfs1_file_config filecfg;
62+
uint8_t filebuf[LFS_PROG_SIZE];
63+
lfs1_file_t file;
64+
} vfs_lfs1_context_t;
65+
66+
extern const stream_methods_t vfs_lfs1_stream_methods;
67+
68+
int vfs_lfs1_mount(vfs_lfs1_context_t *ctx, uint32_t base_addr, uint32_t byte_len);
69+
70+
#endif
71+
72+
#if MBOOT_VFS_LFS2
73+
74+
#include "lib/littlefs/lfs2.h"
75+
76+
#define LFS_READ_SIZE (32)
77+
#define LFS_PROG_SIZE (32)
78+
#define LFS_CACHE_SIZE (4 * LFS_READ_SIZE)
79+
#define LFS_LOOKAHEAD_SIZE (32)
80+
81+
typedef struct _vfs_lfs2_context_t {
82+
uint32_t bdev_base_addr;
83+
struct lfs2_config config;
84+
lfs2_t lfs;
85+
struct lfs2_file_config filecfg;
86+
uint8_t filebuf[LFS_CACHE_SIZE]; // lfs2 specific
87+
lfs2_file_t file;
88+
} vfs_lfs2_context_t;
89+
90+
extern const stream_methods_t vfs_lfs2_stream_methods;
91+
92+
int vfs_lfs2_mount(vfs_lfs2_context_t *ctx, uint32_t base_addr, uint32_t byte_len);
93+
94+
#endif
95+
4396
#endif // MICROPY_INCLUDED_STM32_MBOOT_VFS_H

ports/stm32/mboot/vfs_fat.c

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)