Skip to content

Instantly share code, notes, and snippets.

@glegrain
Last active January 8, 2018 23:25
Show Gist options
  • Save glegrain/56dc439cb1842f2d046fc9e838903454 to your computer and use it in GitHub Desktop.
Save glegrain/56dc439cb1842f2d046fc9e838903454 to your computer and use it in GitHub Desktop.
/*
* Jump to System Memory from User code
* NOTE: Code tested on STM32F107
*/
#define SYSTEM_MEMORY_ADDRESS 0x1FFFB000
uint32_t JumpAddress;
/* Disable all peripheral clocks */
RCC->APB1ENR = 0x0;
RCC->APB2ENR = 0x0;
/* Disable all interrupts */
__disable_irq();
/* Clear Enable Interrupts */
NVIC->ICER[0] = (uint32_t) ~0ul;
NVIC->ICER[1] = (uint32_t) ~0ul;
NVIC->ICER[2] = (uint32_t) ~0ul;
/* Clear Pending Interrupts */
NVIC->ICPR[0] = (uint32_t) ~0ul;
NVIC->ICPR[1] = (uint32_t) ~0ul;
NVIC->ICPR[2] = (uint32_t) ~0ul;
/* Disable and reset System Timer */
/* AN2606 - Used to automatically detect the serial baud rate from the host
* for USARTx bootloaders. */
SysTick->CTRL = 0x0;
SysTick->LOAD = 0x0;
SysTick->VAL = 0x0;
/* Reset the RCC clock configuration to the default reset state */
HAL_RCC_DeInit();
/* Load bootloader vector */
JumpAddress = *(__IO uint32_t*) (SYSTEM_MEMORY_ADDRESS + 4);
void (*JumpToBootloader)(void) = (void (*)(void)) JumpAddress;
/* Reinitialize the Stack pointer and jump to bootloader */
__set_MSP(*(__IO uint32_t*) SYSTEM_MEMORY_ADDRESS);
JumpToBootloader();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment