STM32L432KC "can't" create flash filesystem
I have designed a custom pyboard used STM32L432KC as the controller and downloaded both V1.12 and V1.13 NUCLEO_L432KC as the firmare. Neither of them works,the file system can not be loaded. I can not mount the flash.
I already set the following parameters:
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1) // in file mpconfigboard.h
#define MICROPY_HW_ENABLE_USB (1) // in file mpconfigboard.h
#define MICROPY_HW_USB_MSC (1) // in file mpconfigboard.h
#define MICROPY_HW_USB_HID (1) // in file mpconfigboard.h
And
MEMORY // in file stm32l432.ld
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 246K /* sectors 0-114 */
FLASH_FS (rx) : ORIGIN = 0x0803D800, LENGTH = 10K /* sectors 115-127 */ I want 10k file system, therefore ORIGIN = 0x0803D800
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K /* SRAM1, 48K + SRAM2, 16K */
}
And
MICROPY_VFS_FAT = 1 // in file mpconfigboard.mk
I have successfully compiled the code and downloaded it into my L432 board. After I reset the board, I got the following output:

The REPL works fine.
But, there are 2 ERRORS:
(1)Can't create flash system.
(2)Can't mount flash.
I know the flash size for STM32L432KC is only 256kb which is tiny, but 5kb~10kb file system is enough for me. I only need to implement some basic GPIO operations.
I also used the printf function to check where the program went wrong.
in main.c , the following function gives nonzero value of ret which is -5.
int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point)
because the expression
(nlr_push(&nlr) ≠ 0
Here is the complete function:
#if MICROPY_HW_ENABLE_STORAGE
STATIC int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point) {
nlr_buf_t nlr;
mp_int_t ret = -MP_EIO;
nlr_push_test();
if (nlr_push(&nlr) == 0) {
mp_obj_t args[] = { bdev, mount_point };
mp_vfs_mount(2, args, (mp_map_t*)&mp_const_empty_map);
mp_vfs_chdir(mount_point);
ret = 0; // success
nlr_pop();
} else {
mp_obj_base_t *exc = nlr.ret_val;
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
mp_obj_t v = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc));
mp_obj_get_int_maybe(v, &ret); // get errno value
ret = -ret;
}
}
return ret;
}
Why this expression (nlr_push(&nlr) ≠ 0 , how to slove this problem? I think this caused the failure of the flash FS.
Another error came from factoryreset.c . in the following function:
MP_WEAK int factory_reset_create_filesystem(void) {
// LED on to indicate creation of local filesystem
led_state(PYB_LED_GREEN, 1);
uint32_t start_tick = HAL_GetTick();
fs_user_mount_t vfs;
pyb_flash_init_vfs(&vfs);
uint8_t working_buf[FF_MAX_SS];
FRESULT res = f_mkfs(&vfs.fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
if (res != FR_OK) {
mp_printf(&mp_plat_print, "MPY: can't create flash filesystem\n"); // **This is the ERROR message**
return -MP_ENODEV;
}
// Set label
f_setlabel(&vfs.fatfs, MICROPY_HW_FLASH_FS_LABEL);
// Populate the filesystem with factory files
factory_reset_make_files(&vfs.fatfs);
// Keep LED on for at least 200ms
systick_wait_at_least(start_tick, 200);
led_state(PYB_LED_GREEN, 0);
return 0; // success
}
#endif // MICROPY_HW_ENABLE_STORAGE
it means the condition (res != FR_OK) is not satisfied.
Can anyone tell me how to revise the code to creat the file system for this L432 board, thanks.
@dpgeorge @boochow @chrismas9 @davehylands
Making the /flash drive more flexible
@dpgeorge - I could make a PR for it, but I like to stop by your office first... :-)
I have been working with several STM32's and all of them use the internal Flash to store the code and the filesystem. With the memory of the STM32's the amount of FLASH_TEXT and FLASH_FS can be varied in many ways. Up to now I hardcoded it in storage.c (before) and in flashbdev.c (nowadays). It is not very flexible, so I added all the flash segments in the linker file and use these to generate the correct memory addresses, etc. To make it even more flexible I moved the *.ld file into the board directory as I like to keep the board specifics together. So far I did this to suit my own purpose for the H743, F767, F437, L476 and F405.
There are cases where you have enough using FLASH_MEM_SEG1 and FLASH_MEM_SEG2, however just leave out the FLASH_FSx you don't need and everything works fine.
Part of the code in flashbdev.c:
#elif defined(STM32F437xx)
#define CACHE_MEM_START_ADDR (0x10000000) // CCM data RAM, 64k
#define FLASH_SECTOR_SIZE_MAX (0x10000) // 64k max, size of CCM
extern uint8_t _flash_fs_start;
extern uint8_t _flash_fs_end;
#define FLASH_MEM_SEG1_START_ADDR ((long)&_flash_fs_start)
#define FLASH_MEM_SEG1_NUM_BLOCKS ((&_flash_fs_end - &_flash_fs_start) / 512)
#if MICROPY_HW_FLASH_EXTRA
extern uint8_t _flash_fs2_start;
extern uint8_t _flash_fs2_end;
extern uint8_t _flash_fs3_start;
extern uint8_t _flash_fs3_end;
extern uint8_t _flash_fs4_start;
extern uint8_t _flash_fs4_end;
extern uint8_t _flash_fs5_start;
extern uint8_t _flash_fs5_end;
extern uint8_t _flash_fs6_start;
extern uint8_t _flash_fs6_end;
extern uint8_t _flash_fs7_start;
extern uint8_t _flash_fs7_end;
extern uint8_t _flash_fs8_start;
extern uint8_t _flash_fs8_end;
#define FLASH_MEM_SEG2_START_ADDR ((long)&_flash_fs2_start)
#define FLASH_MEM_SEG2_NUM_BLOCKS ((&_flash_fs2_end - &_flash_fs2_start) / 512)
#define FLASH_MEM_SEG3_START_ADDR ((long)&_flash_fs3_start)
#define FLASH_MEM_SEG3_NUM_BLOCKS ((&_flash_fs3_end - &_flash_fs3_start) / 512)
#define FLASH_MEM_SEG4_START_ADDR ((long)&_flash_fs4_start)
#define FLASH_MEM_SEG4_NUM_BLOCKS ((&_flash_fs4_end - &_flash_fs4_start) / 512)
#define FLASH_MEM_SEG5_START_ADDR ((long)&_flash_fs5_start)
#define FLASH_MEM_SEG5_NUM_BLOCKS ((&_flash_fs5_end - &_flash_fs5_start) / 512)
#define FLASH_MEM_SEG6_START_ADDR ((long)&_flash_fs6_start)
#define FLASH_MEM_SEG6_NUM_BLOCKS ((&_flash_fs6_end - &_flash_fs6_start) / 512)
#define FLASH_MEM_SEG7_START_ADDR ((long)&_flash_fs7_start)
#define FLASH_MEM_SEG7_NUM_BLOCKS ((&_flash_fs7_end - &_flash_fs7_start) / 512)
#define FLASH_MEM_SEG8_START_ADDR ((long)&_flash_fs8_start)
#define FLASH_MEM_SEG8_NUM_BLOCKS ((&_flash_fs8_end - &_flash_fs8_start) / 512)
#endif
#endif
Part of the stm32f437.ld:
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 896K /* sectors 5,6,7,8,9,10,11 */
/*
For the filesystem the sector size above CCMRAM size is wasted. Keep in mind the number of blocks
Also there are gaps in between due this
*/
FLASH_FS (rx) : ORIGIN = 0x08100000, LENGTH = 256K /* sectors 12-17 is 16K+16K+16K+16K+64K+64K(128K) = 384 blocks */
FLASH_FS2 (rx) : ORIGIN = 0x08140000, LENGTH = 128K /* sector 18 is 128K, but only 64K be used = 128 blocks */
FLASH_FS3 (rx) : ORIGIN = 0x08160000, LENGTH = 128K /* sector 19 */
FLASH_FS4 (rx) : ORIGIN = 0x08180000, LENGTH = 128K /* sector 20 */
FLASH_FS5 (rx) : ORIGIN = 0x081A0000, LENGTH = 128K /* sector 21 */
FLASH_FS6 (rx) : ORIGIN = 0x081C0000, LENGTH = 128K /* sector 22 */
FLASH_FS7 (rx) : ORIGIN = 0x081E0000, LENGTH = 128K /* sector 23 */
FLASH_FS8 (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sector 1-4 is 16+16+16+64=112K/512 = 224 blocks */
}
/* generate all the labels for the flash drive blocks
only the first 64K can be used from a 128K block due to the CCMRAM size that why the 64K
is subtracted :-)
*/
_flash_fs_start = ORIGIN(FLASH_FS);
_flash_fs_end = ORIGIN(FLASH_FS) + LENGTH(FLASH_FS) - 0x10000;
_flash_fs2_start = ORIGIN(FLASH_FS2);
_flash_fs2_end = ORIGIN(FLASH_FS2) + LENGTH(FLASH_FS2) - 0x10000;
_flash_fs3_start = ORIGIN(FLASH_FS3);
_flash_fs3_end = ORIGIN(FLASH_FS3) + LENGTH(FLASH_FS3) - 0x10000;
_flash_fs4_start = ORIGIN(FLASH_FS4);
_flash_fs4_end = ORIGIN(FLASH_FS4) + LENGTH(FLASH_FS4) - 0x10000;
_flash_fs5_start = ORIGIN(FLASH_FS5);
_flash_fs5_end = ORIGIN(FLASH_FS5) + LENGTH(FLASH_FS5) - 0x10000;
_flash_fs6_start = ORIGIN(FLASH_FS6);
_flash_fs6_end = ORIGIN(FLASH_FS6) + LENGTH(FLASH_FS6) - 0x10000;
_flash_fs7_start = ORIGIN(FLASH_FS7);
_flash_fs7_end = ORIGIN(FLASH_FS7) + LENGTH(FLASH_FS7) - 0x10000;
_flash_fs8_start = ORIGIN(FLASH_FS8);
_flash_fs8_end = ORIGIN(FLASH_FS8) + LENGTH(FLASH_FS8);
For completeness mpconfigboard.mk:
MCU_SERIES = f4
CMSIS_MCU = STM32F437xx
AF_FILE = boards/TARGETBOARD/stm32f437_af.csv
LD_FILES = boards/TARGETBOARD/stm32f437.ld boards/common_ifs.ld
TEXT0_ADDR = 0x08000000
TEXT1_ADDR = 0x08020000