Last active
June 19, 2019 05:11
-
-
Save jagannath-sahoo/c497599e5a5790eb036d6459811136c1 to your computer and use it in GitHub Desktop.
Boot Jump Code snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Function to perform jump to system memory boot from user application | |
| * Link: http://www.onarm.com/forum/64128/ | |
| * Call function when you want to jump to system memory | |
| */ | |
| void JumpToBootloader(void) { | |
| void (*SysMemBootJump)(void); | |
| /** | |
| * Step: Set system memory address. | |
| * | |
| * For STM32F411, system memory is on 0x1FFF 0000 | |
| * For other families, check AN2606 document table 110 with descriptions of memory addresses | |
| */ | |
| volatile uint32_t addr = 0x1FFF0000; | |
| /** | |
| * Step: Disable RCC, set it to default (after reset) settings | |
| * Internal clock, no PLL, etc. | |
| */ | |
| HAL_RCC_DeInit(); | |
| /** | |
| * Step: Disable systick timer and reset it to default values | |
| */ | |
| SysTick->CTRL = 0; | |
| SysTick->LOAD = 0; | |
| SysTick->VAL = 0; | |
| /** | |
| * Step: Disable all interrupts | |
| * Link: http://www.keil.com/support/man/docs/armcc/armcc_chr1359124995648.htm | |
| */ | |
| __disable_irq(); | |
| /** | |
| * Step: Remap system memory to address 0x0000 0000 in address space | |
| * For each family registers may be different. | |
| * Check reference manual for each family. | |
| * | |
| * For STM32F4xx, MEMRMP register in SYSCFG is used (bits[1:0]) | |
| * Set and cleared by software. This bit controls the memory internal mapping at | |
| * address 0x0000 0000. After reset these bits take the value selected by the Boot | |
| * pins . | |
| * 00: Main Flash memory mapped at 0x0000 0000 | |
| * 01: System Flash memory mapped at 0x0000 0000 | |
| * 11: Embedded SRAM mapped at 0x0000 0000 | |
| */ | |
| SYSCFG->MEMRMP = 0x01; | |
| //__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); //Call HAL macro to do this for you | |
| /** | |
| * Step: Set jump memory location for system memory | |
| * Use address with 4 bytes offset which specifies jump location where program starts | |
| */ | |
| SysMemBootJump = (void (*)(void)) (*((uint32_t *)(addr + 4))); | |
| /** | |
| * Step: Set main stack pointer. | |
| * This step must be done last otherwise local variables in this function | |
| * don't have proper value since stack pointer is located on different position | |
| * | |
| * Set direct address location which specifies stack pointer in SRAM location | |
| */ | |
| __set_MSP(*(uint32_t *)addr); | |
| /** | |
| * Step: Actually call our function to jump to set location | |
| * This will start system memory execution | |
| */ | |
| SysMemBootJump(); | |
| /** | |
| * Step: Connect USB<->UART converter to dedicated USART pins and test | |
| * and test with bootloader works with STM32 Flash Loader Demonstrator software | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment