Description
Today, I noticed some startup customizations in the Malyan M200 and Mx00 variants, of which I wonder if they are really needed. The way they work now, might pose complications for implementing a custom reset handler for #710, so I wonder if these customizations cannot be removed, or generalized.
There's two things here:
- It remaps the 0x0 address block to RAM rather than flash:
Arduino_Core_STM32/variants/MALYANMx00_F070CB/variant.cpp
Lines 101 to 108 in 213a3d5
- It has a custom startup assembly code.
Looking at the latter, it seems the changes compared to the default scripts are minimal. It seems there used be an "enable fan" command in there, but it is commented out. The actual changes do noot seem so relevant: disabling/enabling interrupts (cpsid
/ cpsie
), some barriers (dsb
/ isb
) and a breakpoint (bkpt
) in an unreachable place. The M200 startup actually adds an initialization of the stack pointer (which should not actually be needed, since a reset already does that), but looking at other default startup code all these load the stack pointer, except for all of the F1 startup code. So I doubt any of these changes are intentional, they might just be the result o basing off an older or newer version of the startup scripts.
Here's the startup code diffs (with comment and whitespace changes removed):
$ diff variants/MALYANM200_F103CB/startup_M200_f103xb.S system/Drivers/CMSIS/Device/ST/STM
32F1xx/Source/Templates/gcc/startup_stm32f103xb.s -u
@@ -77,14 +60,6 @@
.weak Reset_Handler
.type Reset_Handler, %function
Reset_Handler:
-/* Initialize the stack pointer and shut down bootloader interrupts */
- /* bl turnOnFan */
- ldr r0, =_estack
- mov sp, r0 /* set stack pointer */
- cpsid i
- dsb
- isb
- cpsid f
/* Copy the data segment initializers from flash to SRAM */
movs r1, #0
@@ -116,11 +91,6 @@
/* Call the clock system intitialization function.*/
bl SystemInit
-
-/* Re-enable handlers */
-cpsie i
-cpsie f
-
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
@@ -140,7 +110,6 @@
Default_Handler:
Infinite_Loop:
b Infinite_Loop
- BKPT #0
.size Default_Handler, .-Default_Handler
/******************************************************************************
*
$ diff variants/MALYANMx00_F070CB/startup_Mx00_f070xb.S system/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/startup_stm32f070xb.s
@@ -62,13 +49,9 @@
.weak Reset_Handler
.type Reset_Handler, %function
Reset_Handler:
-/* bl manualFan */
ldr r0, =_estack
mov sp, r0 /* set stack pointer */
- cpsid i
- cpsid f
-
/* Copy the data segment initializers from flash to SRAM */
ldr r0, =_sdata
ldr r1, =_edata
@@ -85,7 +68,7 @@
adds r4, r0, r3
cmp r4, r1
bcc CopyDataInit
-
+
/* Zero fill the bss segment. */
ldr r2, =_sbss
ldr r4, =_ebss
@@ -102,10 +85,6 @@
/* Call the clock system intitialization function.*/
bl SystemInit
-
- cpsie i
- cpsie f
-
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
Looking at the history, relevant PRs are #382, #422 and #354. The last one mentions "the startup assembler must account for some oddities of the bootloader which is already running". It suspect, this means that there is a bootloader in regular flash, which lives in the first 8K, so the linker script is modified to put the sketch after the bootloader. Since the ISR table lives at 0x0, the 0x0 block must be remapped to RAM, and the ISR vector table copied there. Finally, because maybe the bootloader neglects to initialize the SP (as the hardware normally does), the reset handler must do this.
@xC0000005, can you confirm or shed more light here? In particular, how much of this applies to which variant? It seems Mx00 remaps memory and copies the ISR table, but M200 does not (but it does leave 8K of memory free?). Is the bootloader different and does that maybe handle remapping already?