STM32F042 - Issues entering bootloader from application
The code below shows my current attempt to implement a jump to the bootloader:
#define ApplicationAddress 0x1FFFC400
void JumpBootloader(void)
{
typedef void (*pFunction)(void); // defines function pointer for bootloader jump
/* Set System memory address plus 4 bytes */
uint32_t JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
pFunction Jump_To_Boot = (pFunction) JumpAddress;
// /* Set main stack pointer
// * This must be done last otherwise all other variables might get mangled! */
__set_MSP(*(__IO uint32_t*) ApplicationAddress);
Jump_To_Boot();
}When this function is called with the BOOT pin pulled high, the microcontroller correctly restarts into bootloader mode, although admittedly after a slight delay of about a second (Windows connect and disconnect sounds can be heard).
When called with the BOOT pin floating/pulled low (note I am using this pin to drive an LED during normal operation), the device disconnects after a small delay as before but then fails to connect, giving the error 'Device Descriptor Request Failed'.
I have looked around on other threads/forums and tried various different methods such as de-initialising all peripherals prior to calling the above function, remapping the system memory to 0x0 (__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();) with varying changes in behaviour. For example, the code below simply jumps back to the user application after being called:
void JumpBootloader(void)
{
typedef void (*pFunction)(void);
pFunction JumpToApplication;
uint32_t JumpAddress = 0x1FFFC400;
Device_DeInit();
HAL_RCC_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
JumpToApplication = (void (*)(void)) (*((uint32_t *)(JumpAddress + 4)));
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) JumpAddress);
JumpToApplication();
}
void Device_DeInit(void)
{
USBD_Stop(&hUsbDeviceFS);
USBD_DeInit(&hUsbDeviceFS);
HAL_TIM_Base_Stop_IT(&htim16);
HAL_SPI_DeInit(&hspi1);
HAL_DMA_DeInit(&hdma_spi1_tx);
HAL_DMA_DeInit(&hdma_spi1_rx);
HAL_TIM_Base_MspDeInit(&htim16);
HAL_GPIO_DeInit(LED_USB_GPIO_Port, LED_USB_Pin);
HAL_GPIO_DeInit(LED_AXIOM_GPIO_Port, LED_AXIOM_Pin);
HAL_DeInit();
}Am I missing a vital point here? Are there any pre-requisites I have not met?
If it is of any concern, I am using USB DFU.
Best regards,
James
