Bootloader for STM32F0 not initializing main stack pointer without printf() call
I have created a bootloader for my STM32F0 chip, and it will boot my firmware just fine, given one condition. I have followed the advice of many here, and I think I've gotten it working the way it is intended to work (set the ROM starting addresses and lengths, remap the vector table to SRAM, set SRAM at 0x00000000, etc.). However, I have run into a strange problem in my code that is baffling me. Using Keil v5 and ARMCLANG v6.14, if it matters.
If I turn off my debug (using EventRecorder to service printf()), I end up in the HardFault_Handler() every time. If I place one printf() that dumps the stack pointer address, everything works perfectly.
By way of example:
void jumpRoutine(void)
{
// Disable global interrupts
__disable_irq();
// Disable all peripheral interrupts
HAL_NVIC_DisableIRQ(SysTick_IRQn);
HAL_NVIC_DisableIRQ(WWDG_IRQn);
.
.
.
HAL_NVIC_DisableIRQ(USB_IRQn);
uint32_t start_addr = (uint32_t)0x08002000;
uint32_t start_addr_ref = (uint32_t)&start_addr;
uint32_t *p_start_addr = (uint32_t *)start_addr_ref;
printf("\nInitializing 0x%08x\n", start_addr_ref); <<------ LINE HAS TO BE HERE TO WORK!
__set_MSP(*p_start_addr);
void (*pApp)(void) = (void (*)(void))(*p_start_addr + 4);
// Jump to firmware
pApp();
}Any explanation as to why this might happen? I've tried changing the variables to be volatile, but that had no effect (didn't think it would, but I tried it). I have disassembled the code, and it is very different between the version with no printf() and the version with printf(). The code size shrinks by about 50% (6KiB to 4KiB) with no printf() included as well.
Could this be timing-related? There's not much to this code, aside from the CubeMX-generated init code and my jump routine.
