/** * @file HW_RESET_ATY.c * * @param Project DEVICE_GENERAL_ATY_LIB * * @author ATY * * @copyright * - Copyright 2017 - 2026 MZ-ATY * - This code follows: * - MZ-ATY Various Contents Joint Statement - * * https://mengze.top/MZ-ATY_VCJS * - CC 4.0 BY-NC-SA - * * https://creativecommons.org/licenses/by-nc-sa/4.0/ * - Your use will be deemed to have accepted the terms of this statement. * * @brief functions of soft reset for CMSIS devices * * @version * - 1_01_221029 > ATY * -# Preliminary version, first Release * @todo not fix ******************************************************************************** */ #ifndef __HW_RESET_ATY_C #define __HW_RESET_ATY_C #include "HW_RESET_ATY.h" /******************************* For user *************************************/ /******************************************************************************/ void ResetBeforeJumpToApp(void) { /* Disable global interrupts */ __set_PRIMASK(1); /* Disable SysTick timer and reset it to default values */ SysTick->CTRL = 0; SysTick->LOAD = 0; SysTick->VAL = 0; /* Reset all clock configurations to their default state */ HAL_RCC_DeInit(); /* Disable all interrupts and clear all pending interrupt flags */ for (uint8_t i = 0; i < 8; i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; } /* Re-enable global interrupts */ __set_PRIMASK(0); /* Switch to privileged mode and use the Main Stack Pointer (MSP) */ __set_CONTROL(0); // Optional: Set vector table offset to App's address SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80; } void Bootloader_DeInit(void) { // Disable all interrupts __disable_irq(); // Disable all GPIO clocks __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOE_CLK_DISABLE(); // __HAL_RCC_GPIOH_CLK_DISABLE(); // Reset RCC to default state HAL_RCC_DeInit(); // Deinitialize HAL HAL_DeInit(); // Disable SysTick SysTick->CTRL = 0; SysTick->LOAD = 0; SysTick->VAL = 0; // Clear all pending interrupts for (int i = 0; i < 8; i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; } __set_BASEPRI(0); __set_FAULTMASK(0); // put at start of application /* Re-enable global interrupts */ __enable_irq(); /* Switch to privileged mode and use the Main Stack Pointer (MSP) */ __set_CONTROL(0); // Optional: Set vector table offset to App's address SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80; } /** * @brief * @note if the peripheral configurations between Bootloader and App are not consistent, * the peripherals need to be manually reset after the jump. Otherwise, make sure that * the peripheral settings used in the Bootloader match those in the App, including * system clock dividers and other clock-related parameters. */ void JumpToApp(uint32_t addr) { /* execute the new program */ uint32_t JumpAddress = *(__IO uint32_t*) (addr + 4); /* Jump to user application */ typedef void (*pFunction)(void); pFunction JumpToApplication = (pFunction)JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) addr); JumpToApplication(); } void UsbReconnect(void) { // put after SystemClock_Config(); and before MX_GPIO_Init(); // restart usb connect GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET); HAL_Delay(200); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET); HAL_Delay(200); } void BL_JumpToAppFull(uint32_t addr) { __disable_irq(); SysTick->CTRL = 0; SysTick->LOAD = 0; SysTick->VAL = 0; HAL_RCC_DeInit(); HAL_DeInit(); for (uint32_t i = 0; i < 8; i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; } __set_BASEPRI(0); __set_FAULTMASK(0); SCB->VTOR = addr & 0x1FFFFF80; __DSB(); __ISB(); uint32_t sp = *(__IO uint32_t*) addr; uint32_t rv = *(__IO uint32_t*) (addr + 4); __set_MSP(sp); ((pFunction)rv)(); } void APP_StartupInit(void) { __set_BASEPRI(0); __set_FAULTMASK(0); __enable_irq(); } #endif /* __HW_RESET_ATY_C */ /******************************** End Of File *********************************/