| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * @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 -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - 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 *********************************/
|