Skip to content

Commit b38c85c

Browse files
Merge pull request #4525 from fahhem/patch-1
Improve the startup code on the STM32F070
2 parents 868adaf + 28e8bc0 commit b38c85c

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/device/TOOLCHAIN_GCC_ARM/startup_stm32f070xb.S

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,40 @@ Reset_Handler:
6565
mov sp, r0 /* set stack pointer */
6666

6767
/* Copy the data segment initializers from flash to SRAM */
68-
movs r1, #0
68+
// Load from _sidata -> _sdata through _edata
69+
// _sidata has a vma = lma in flash at the end of .text
70+
// _sdata has a lma in flash but a vma of ram, so here we move it from where
71+
// it was loaded (lma) into where it will be accessed (vma).
72+
// Register Schema:
73+
// r0 = _sdata, r1 = _edata, r2 = _sidata
74+
// r3 = index (goes from 0 -> _sdata - _edata)
75+
// r4 = temp var for *(_sidata + r3) or (_sdata + r3)
76+
// This is all equivalent to this C:
77+
// int index = 0;
78+
// extern uint32_t *_sdata, *_sidata;
79+
// while (_sdata + index < _edata) {
80+
// *_sdata[index] = *_sidata[index];
81+
// index += 1;
82+
// }
83+
ldr r0, =_sdata
84+
ldr r1, =_edata
85+
ldr r2, =_sidata
86+
movs r3, #0
6987
b LoopCopyDataInit
7088

7189
CopyDataInit:
72-
ldr r3, =_sidata
73-
ldr r3, [r3, r1]
74-
str r3, [r0, r1]
75-
adds r1, r1, #4
90+
ldr r4, [r2, r3]
91+
str r4, [r0, r3]
92+
adds r3, r3, #4
7693

7794
LoopCopyDataInit:
78-
ldr r0, =_sdata
79-
ldr r3, =_edata
80-
adds r2, r0, r1
81-
cmp r2, r3
95+
// while (_sdata + r3 < _edata)
96+
adds r4, r0, r3
97+
// if (r4 < r1) branch to CopyDataInit
98+
cmp r4, r1
8299
bcc CopyDataInit
83100

101+
84102
/* Call the clock system intitialization function.*/
85103
bl SystemInit
86104

0 commit comments

Comments
 (0)