hal_pwr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_pwr.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE PWR FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define __HAL_PWR_C_
  19. // Files includes
  20. #include "hal_pwr.h"
  21. #include "hal_rcc.h"
  22. #include "hal_syscfg.h"
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// @addtogroup MM32_Hardware_Abstract_Layer
  25. /// @{
  26. ////////////////////////////////////////////////////////////////////////////////
  27. /// @addtogroup PWR_HAL
  28. /// @{
  29. ////////////////////////////////////////////////////////////////////////////////
  30. /// @addtogroup PWR_Exported_Functions
  31. /// @{
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// @brief Deinitializes the PWR peripheral registers to their default reset
  34. /// values.
  35. /// @param None.
  36. /// @retval None.
  37. ////////////////////////////////////////////////////////////////////////////////
  38. void PWR_DeInit(void)
  39. {
  40. exRCC_APB1PeriphReset(RCC_APB1ENR_PWR);
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////
  43. /// @brief Enables or disables access to the RTC and backup registers.
  44. /// @param state: new state of the access to the RTC and backup
  45. /// registers. This parameter can be: ENABLE or DISABLE.
  46. /// @retval None.
  47. ////////////////////////////////////////////////////////////////////////////////
  48. void PWR_BackupAccessCmd(FunctionalState state)
  49. {
  50. (state) ? (RCC->BDCR |= RCC_BDCR_DBP) : (RCC->BDCR &= ~RCC_BDCR_DBP);
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////
  53. /// @brief Enables or disables the Power Voltage Detector(PVD).
  54. /// @param state: new state of the PVD.
  55. /// This parameter can be: ENABLE or DISABLE.
  56. /// @retval None.
  57. ////////////////////////////////////////////////////////////////////////////////
  58. void PWR_PVDCmd(FunctionalState state)
  59. {
  60. (state) ? (SYSCFG->PDETCSR |= SYSCFG_PDETCSR_PVDE) : (SYSCFG->PDETCSR &= ~SYSCFG_PDETCSR_PVDE);
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////
  63. /// @brief Configures the voltage threshold detected by the Power Voltage
  64. /// Detector(PVD).
  65. /// @param pvd_level: specifies the PVD detection level
  66. /// This parameter can be one of the following values:
  67. /// @arg emPVD_LEVEL0 : PVD detection level set to 1.7V
  68. /// @arg emPVD_LEVEL1 : PVD detection level set to 2.0V
  69. /// @arg emPVD_LEVEL2 : PVD detection level set to 2.3V
  70. /// @arg emPVD_LEVEL3 : PVD detection level set to 2.6V
  71. /// @arg emPVD_LEVEL4 : PVD detection level set to 2.9V
  72. /// @arg emPVD_LEVEL5 : PVD detection level set to 3.2V
  73. /// @arg emPVD_LEVEL6 : PVD detection level set to 3.5V
  74. /// @arg emPVD_LEVEL7 : PVD detection level set to 3.8V
  75. /// @arg emPVD_LEVEL8 : PVD detection level set to 4.1V
  76. /// @arg emPVD_LEVEL9 : PVD detection level set to 4.4V
  77. /// @arg emPVD_LEVEL10: PVD detection level set to 4.7V
  78. /// @retval None.
  79. ////////////////////////////////////////////////////////////////////////////////
  80. void PWR_PVDLevelConfig(emPVD_Level_Typedef pvd_level)
  81. {
  82. SYSCFG->PDETCSR = (SYSCFG->PDETCSR & (~SYSCFG_PDETCSR_PLS)) | pvd_level;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. /// @brief Enables or disables the WakeUp Pin functionality.
  86. /// @param state: new state of the WakeUp Pin functionality.
  87. /// This parameter can be: ENABLE or DISABLE.
  88. /// @retval None.
  89. ////////////////////////////////////////////////////////////////////////////////
  90. void PWR_WakeUpPinCmd(FunctionalState state)
  91. {
  92. (state != DISABLE) ? (PWR->CR2 |= PWR_CR2_EWUP1) : (PWR->CSR &= ~PWR_CR2_EWUP1);
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. /// @brief Enables or disables the WakeUp Pin functionality.
  96. /// @param state: new state of the WakeUp Pin functionality.
  97. /// This parameter can be: ENABLE or DISABLE.
  98. /// @retval None.
  99. ////////////////////////////////////////////////////////////////////////////////
  100. void PWR_WakeUpPinXCmd(emWUP_Pin_Typedef pin, FunctionalState state)
  101. {
  102. (state != DISABLE) ? (PWR->CR2 |= (PWR_CR2_EWUP1 << pin)) : (PWR->CSR &= ~(PWR_CR2_EWUP1 << pin));
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. /// @brief Enters STOP mode.
  106. /// @param regulator: specifies the regulator state in STOP mode.
  107. /// This parameter can be one of the following values:
  108. /// @arg PWR_Regulator_ON: STOP mode with regulator ON
  109. /// @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode.
  110. /// @param stop_entry: specifies if STOP mode in entered with WFI or WFE
  111. /// instruction.
  112. /// This parameter can be one of the following values:
  113. /// @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
  114. /// @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
  115. /// @retval None.
  116. ////////////////////////////////////////////////////////////////////////////////
  117. void PWR_EnterSTOPMode(emPWR_Reg_Stop_mode_Typedef regulator, emPWR_STOP_ModeEn_Typedef stop_entry)
  118. {
  119. MODIFY_REG(PWR->CR, PWR_CR_LDPS, regulator);
  120. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  121. if(stop_entry == PWR_STOPEntry_WFI) {
  122. __WFI();
  123. }
  124. else {
  125. __WFE();
  126. }
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. /// @brief Enters STANDBY mode.
  130. /// @param None.
  131. /// @retval None.
  132. ////////////////////////////////////////////////////////////////////////////////
  133. void PWR_EnterSTANDBYMode(void)
  134. {
  135. PWR->CR |= PWR_CR_PDDS;
  136. PWR->SCR |= PWR_SCR_CWUF1 | PWR_SCR_CWUF2 | PWR_SCR_CWUF3 | PWR_SCR_CWUF4 | PWR_SCR_CWUF5 | PWR_SCR_CWUF6;
  137. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  138. #if defined(__CC_ARM)
  139. __force_stores();
  140. #endif
  141. __WFI();
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////
  144. /// @brief Checks whether the specified PWR flag is set or not.
  145. /// @param flag: specifies the flag to check.
  146. /// This parameter can be one of the following values:
  147. /// @arg PWR_FLAG_WU: Wake Up flag
  148. /// @arg PWR_FLAG_SB: StandBy flag
  149. /// @arg PWR_FLAG_PVDO: PVD Output
  150. /// @retval The new state of PWR_FLAG (SET or RESET).
  151. ////////////////////////////////////////////////////////////////////////////////
  152. FlagStatus PWR_GetPVDOFlagStatus(u32 flag)
  153. {
  154. return (FlagStatus)(SYSCFG->PDETCSR & flag);
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////
  157. /// @brief Clears the PWR's pending flags.
  158. /// @param flag: specifies the flag to clear.
  159. /// This parameter can be one of the following values:
  160. /// @arg PWR_FLAG_WU: Wake Up flag
  161. /// @arg PWR_FLAG_SB: StandBy flag
  162. /// @retval None.
  163. ////////////////////////////////////////////////////////////////////////////////
  164. void PWR_ClearPVDOFlag(u32 flag)
  165. {
  166. PWR->CR |= flag << 2;
  167. }
  168. ////////////////////////////////////////////////////////////////////////////////
  169. /// @brief Checks whether the specified PWR flag is set or not.
  170. /// @param flag: specifies the flag to check.
  171. /// This parameter can be one of the following values:
  172. /// @arg PWR_FLAG_WU: Wake Up flag
  173. /// @arg PWR_FLAG_SB: StandBy flag
  174. /// @arg PWR_FLAG_PVDO: PVD Output
  175. /// @retval The new state of PWR_FLAG (SET or RESET).
  176. ////////////////////////////////////////////////////////////////////////////////
  177. FlagStatus PWR_GetFlagStatus(u32 flag)
  178. {
  179. return (FlagStatus)(PWR->CSR & flag);
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////
  182. /// @brief Clears the PWR's pending flags.
  183. /// @param flag: specifies the flag to clear.
  184. /// This parameter can be one of the following values:
  185. /// @arg PWR_FLAG_WU: Wake Up flag
  186. /// @arg PWR_FLAG_SB: StandBy flag
  187. /// @retval None.
  188. ////////////////////////////////////////////////////////////////////////////////
  189. void PWR_ClearFlag(u32 flag)
  190. {
  191. PWR->CR |= flag << 2;
  192. }
  193. /// @}
  194. /// @}
  195. /// @}