|
1 | 1 | #include "bootloader.h"
|
2 |
| - |
3 | 2 | #include "stm32_def.h"
|
4 | 3 | #include "backup.h"
|
| 4 | +#include "usbd_if.h" |
5 | 5 |
|
6 | 6 | #ifdef BL_LEGACY_LEAF
|
7 | 7 | void dtr_togglingHook(uint8_t *buf, uint32_t *len)
|
@@ -37,3 +37,60 @@ void dtr_togglingHook(uint8_t *buf, uint32_t *len)
|
37 | 37 | }
|
38 | 38 | }
|
39 | 39 | #endif /* BL_HID */
|
| 40 | + |
| 41 | +/* Request to jump to system memory boot */ |
| 42 | +void jumpToBootloaderRequested(void) |
| 43 | +{ |
| 44 | + enableBackupDomain(); |
| 45 | + setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, SYSBL_MAGIC_NUMBER_BKP_VALUE); |
| 46 | + NVIC_SystemReset(); |
| 47 | +} |
| 48 | + |
| 49 | +/* Jump to system memory boot from user application */ |
| 50 | +void jumpToBootloader(void) |
| 51 | +{ |
| 52 | + enableBackupDomain(); |
| 53 | + if (getBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX) == SYSBL_MAGIC_NUMBER_BKP_VALUE) { |
| 54 | + setBackupRegister(SYSBL_MAGIC_NUMBER_BKP_INDEX, 0); |
| 55 | + |
| 56 | +#ifdef USBCON |
| 57 | + USBD_reenumerate(); |
| 58 | +#endif |
| 59 | + void (*sysMemBootJump)(void); |
| 60 | + |
| 61 | + /** |
| 62 | + * Get system memory address |
| 63 | + * |
| 64 | + * Available in AN2606 document: |
| 65 | + * Table 116. Bootloader device-dependent parameters |
| 66 | + */ |
| 67 | + volatile uint32_t sysMem_addr = 0x1FFF0000; |
| 68 | + |
| 69 | + /* Remap system Flash memory at address 0x00000000 */ |
| 70 | + __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Set jump memory location for system memory |
| 74 | + * Use address with 4 bytes offset which specifies jump location |
| 75 | + * where program starts |
| 76 | + */ |
| 77 | + sysMemBootJump = (void (*)(void))(*((uint32_t *)(sysMem_addr + 4))); |
| 78 | + |
| 79 | + /** |
| 80 | + * Set main stack pointer. |
| 81 | + * This step must be done last otherwise local variables in this function |
| 82 | + * don't have proper value since stack pointer is located on different position |
| 83 | + * |
| 84 | + * Set direct address location which specifies stack pointer in SRAM location |
| 85 | + */ |
| 86 | + __set_MSP(*(uint32_t *)sysMem_addr); |
| 87 | + |
| 88 | + /** |
| 89 | + * Jump to set location |
| 90 | + * This will start system memory execution |
| 91 | + */ |
| 92 | + sysMemBootJump(); |
| 93 | + |
| 94 | + while (1); |
| 95 | + } |
| 96 | +} |
0 commit comments