From 19da4ff10f74c21ac527abf69ce6abdbe18d069d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=90=83=E6=B2=B9=E7=82=B8=E9=B8=A1?= <1425962791@qq.com> Date: Sun, 8 Feb 2026 17:27:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5api=20,=20jump=5Fto=5Fapp()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootloader/Core/Src/main.c | 16 +------------ bootloader/Ymodem/menu.c | 48 +++++++++++++++++++++++++------------- bootloader/Ymodem/menu.h | 5 ++-- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/bootloader/Core/Src/main.c b/bootloader/Core/Src/main.c index 9554f32..fadbc8f 100644 --- a/bootloader/Core/Src/main.c +++ b/bootloader/Core/Src/main.c @@ -191,21 +191,7 @@ int main(void) { // 4. 校验成功,执行跳转 USB_PutString("Valid APP found, jumping to application...\r\n"); - // 获取APP的堆栈指针和跳转地址 - uint32_t JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); - Jump_To_Application = (pFunction) JumpAddress; - // 在跳转前反初始化所有Bootloader用过的外设 - USER_USB_DEVICE_DeInit(); - HAL_RCC_DeInit(); - HAL_DeInit(); - SysTick->CTRL = 0; - SysTick->LOAD = 0; - SysTick->VAL = 0; - __disable_irq(); - - // 设置APP的堆栈指针并跳转 - __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); - Jump_To_Application(); + jump_to_app(); } } // no legal APP diff --git a/bootloader/Ymodem/menu.c b/bootloader/Ymodem/menu.c index da5f6b1..c4669bf 100644 --- a/bootloader/Ymodem/menu.c +++ b/bootloader/Ymodem/menu.c @@ -229,22 +229,7 @@ void Main_Menu(void) { USB_PutString("Start to execute the user application...\r\n"); HAL_Delay(1000); - USER_USB_DEVICE_DeInit(); - HAL_RCC_DeInit(); - HAL_DeInit(); - //user code here - SysTick->CTRL = 0X00;//禁止SysTick - SysTick->LOAD = 0; - SysTick->VAL = 0; - __disable_irq(); - - //set JumpAddress - JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); - /* Jump to user application */ - Jump_To_Application = (pFunction) JumpAddress; - /* Initialize user application's Stack Pointer */ - __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); - Jump_To_Application(); + jump_to_app(); } else if ((key == 0x34) && (FlashProtection == 1)) { @@ -281,6 +266,37 @@ void Main_Menu(void) } } +/* Public functions ---------------------------------------------------------*/ + +/** + * @brief Jumps to the user application loaded in the Flash memory. + * @param None + * @retval None + */ +void jump_to_app(void) +{ + USER_USB_DEVICE_DeInit(); + // 手动配置USB引脚为GPIO输出,强制拉低DP线以模拟拔出 + USER_USBD_LL_Simulate_Disconnect(); + // 给予PC足够的时间来识别设备断开 + HAL_Delay(1000); + HAL_RCC_DeInit(); + HAL_DeInit(); + //user code here + SysTick->CTRL = 0X00;//禁止SysTick + SysTick->LOAD = 0; + SysTick->VAL = 0; + __disable_irq(); + + //set JumpAddress + JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); + /* Jump to user application */ + Jump_To_Application = (pFunction) JumpAddress; + /* Initialize user application's Stack Pointer */ + __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); + Jump_To_Application(); +} + /** * @} */ diff --git a/bootloader/Ymodem/menu.h b/bootloader/Ymodem/menu.h index 5b082a4..362f27b 100644 --- a/bootloader/Ymodem/menu.h +++ b/bootloader/Ymodem/menu.h @@ -1,6 +1,6 @@ /** ****************************************************************************** - * @file STM32F4xx_IAP/inc/menu.h + * @file STM32F4xx_IAP/inc/menu.h * @author MCD Application Team * @version V1.0.0 * @date 10-October-2011 @@ -17,7 +17,7 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __MENU_H @@ -34,6 +34,7 @@ typedef void (*pFunction)(void); /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ void Main_Menu(void); +void jump_to_app(void); #endif /* __MENU_H */