HW_RESET_ATY.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @file HW_RESET_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of soft reset for CMSIS devices
  20. *
  21. * @version
  22. * - 1_01_221029 > ATY
  23. * -# Preliminary version, first Release
  24. * @todo not fix
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_RESET_ATY_C
  28. #define __HW_RESET_ATY_C
  29. #include "HW_RESET_ATY.h"
  30. /******************************* For user *************************************/
  31. /******************************************************************************/
  32. void ResetBeforeJumpToApp(void)
  33. {
  34. /* Disable global interrupts */
  35. __set_PRIMASK(1);
  36. /* Disable SysTick timer and reset it to default values */
  37. SysTick->CTRL = 0;
  38. SysTick->LOAD = 0;
  39. SysTick->VAL = 0;
  40. /* Reset all clock configurations to their default state */
  41. HAL_RCC_DeInit();
  42. /* Disable all interrupts and clear all pending interrupt flags */
  43. for (uint8_t i = 0; i < 8; i++)
  44. {
  45. NVIC->ICER[i] = 0xFFFFFFFF;
  46. NVIC->ICPR[i] = 0xFFFFFFFF;
  47. }
  48. /* Re-enable global interrupts */
  49. __set_PRIMASK(0);
  50. /* Switch to privileged mode and use the Main Stack Pointer (MSP) */
  51. __set_CONTROL(0);
  52. // Optional: Set vector table offset to App's address
  53. SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80;
  54. }
  55. void Bootloader_DeInit(void)
  56. {
  57. // Disable all interrupts
  58. __disable_irq();
  59. // Disable all GPIO clocks
  60. __HAL_RCC_GPIOA_CLK_DISABLE();
  61. __HAL_RCC_GPIOB_CLK_DISABLE();
  62. __HAL_RCC_GPIOC_CLK_DISABLE();
  63. __HAL_RCC_GPIOD_CLK_DISABLE();
  64. __HAL_RCC_GPIOE_CLK_DISABLE();
  65. // __HAL_RCC_GPIOH_CLK_DISABLE();
  66. // Reset RCC to default state
  67. HAL_RCC_DeInit();
  68. // Deinitialize HAL
  69. HAL_DeInit();
  70. // Disable SysTick
  71. SysTick->CTRL = 0;
  72. SysTick->LOAD = 0;
  73. SysTick->VAL = 0;
  74. // Clear all pending interrupts
  75. for (int i = 0; i < 8; i++) {
  76. NVIC->ICER[i] = 0xFFFFFFFF;
  77. NVIC->ICPR[i] = 0xFFFFFFFF;
  78. }
  79. __set_BASEPRI(0);
  80. __set_FAULTMASK(0);
  81. // put at start of application
  82. /* Re-enable global interrupts */
  83. __enable_irq();
  84. /* Switch to privileged mode and use the Main Stack Pointer (MSP) */
  85. __set_CONTROL(0);
  86. // Optional: Set vector table offset to App's address
  87. SCB->VTOR = APPLICATION_ADDRESS & 0x1FFFFF80;
  88. }
  89. /**
  90. * @brief
  91. * @note if the peripheral configurations between Bootloader and App are not consistent,
  92. * the peripherals need to be manually reset after the jump. Otherwise, make sure that
  93. * the peripheral settings used in the Bootloader match those in the App, including
  94. * system clock dividers and other clock-related parameters.
  95. */
  96. void JumpToApp(uint32_t addr)
  97. {
  98. /* execute the new program */
  99. uint32_t JumpAddress = *(__IO uint32_t*) (addr + 4);
  100. /* Jump to user application */
  101. typedef void (*pFunction)(void);
  102. pFunction JumpToApplication = (pFunction)JumpAddress;
  103. /* Initialize user application's Stack Pointer */
  104. __set_MSP(*(__IO uint32_t*) addr);
  105. JumpToApplication();
  106. }
  107. void UsbReconnect(void)
  108. {
  109. // put after SystemClock_Config(); and before MX_GPIO_Init();
  110. // restart usb connect
  111. GPIO_InitTypeDef GPIO_InitStruct;
  112. __HAL_RCC_GPIOA_CLK_ENABLE();
  113. GPIO_InitStruct.Pin = GPIO_PIN_12;
  114. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  115. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  116. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  117. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  118. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  119. HAL_Delay(200);
  120. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
  121. HAL_Delay(200);
  122. }
  123. void BL_JumpToAppFull(uint32_t addr)
  124. {
  125. __disable_irq();
  126. SysTick->CTRL = 0;
  127. SysTick->LOAD = 0;
  128. SysTick->VAL = 0;
  129. HAL_RCC_DeInit();
  130. HAL_DeInit();
  131. for (uint32_t i = 0; i < 8; i++) {
  132. NVIC->ICER[i] = 0xFFFFFFFF;
  133. NVIC->ICPR[i] = 0xFFFFFFFF;
  134. }
  135. __set_BASEPRI(0);
  136. __set_FAULTMASK(0);
  137. SCB->VTOR = addr & 0x1FFFFF80;
  138. __DSB();
  139. __ISB();
  140. uint32_t sp = *(__IO uint32_t*) addr;
  141. uint32_t rv = *(__IO uint32_t*) (addr + 4);
  142. __set_MSP(sp);
  143. ((pFunction)rv)();
  144. }
  145. void APP_StartupInit(void)
  146. {
  147. __set_BASEPRI(0);
  148. __set_FAULTMASK(0);
  149. __enable_irq();
  150. }
  151. #endif /* __HW_RESET_ATY_C */
  152. /******************************** End Of File *********************************/