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>© COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17
18 // Define to prevent recursive inclusion
19 #define __HAL_PWR_C_
20
21 // Files includes
22 #include "hal_pwr.h"
23 #include "hal_rcc.h"
24 #include "hal_syscfg.h"
25 ////////////////////////////////////////////////////////////////////////////////
26 /// @addtogroup MM32_Hardware_Abstract_Layer
27 /// @{
28
29 ////////////////////////////////////////////////////////////////////////////////
30 /// @addtogroup PWR_HAL
31 /// @{
32
33 ////////////////////////////////////////////////////////////////////////////////
34 /// @addtogroup PWR_Exported_Functions
35 /// @{
36
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief Deinitializes the PWR peripheral registers to their default reset
39 /// values.
40 /// @param None.
41 /// @retval None.
42 ////////////////////////////////////////////////////////////////////////////////
PWR_DeInit(void)43 void PWR_DeInit(void)
44 {
45 exRCC_APB1PeriphReset(RCC_APB1ENR_PWR);
46 }
47
48 ////////////////////////////////////////////////////////////////////////////////
49 /// @brief Enables or disables access to the RTC and backup registers.
50 /// @param state: new state of the access to the RTC and backup
51 /// registers. This parameter can be: ENABLE or DISABLE.
52 /// @retval None.
53 ////////////////////////////////////////////////////////////////////////////////
PWR_BackupAccessCmd(FunctionalState state)54 void PWR_BackupAccessCmd(FunctionalState state)
55 {
56 (state) ? (RCC->BDCR |= RCC_BDCR_DBP) : (RCC->BDCR &= ~RCC_BDCR_DBP);
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 /// @brief Enables or disables the Power Voltage Detector(PVD).
61 /// @param state: new state of the PVD.
62 /// This parameter can be: ENABLE or DISABLE.
63 /// @retval None.
64 ////////////////////////////////////////////////////////////////////////////////
PWR_PVDCmd(FunctionalState state)65 void PWR_PVDCmd(FunctionalState state)
66 {
67 (state) ? (SYSCFG->PDETCSR |= SYSCFG_PDETCSR_PVDE) : (SYSCFG->PDETCSR &= ~SYSCFG_PDETCSR_PVDE);
68 }
69
70 ////////////////////////////////////////////////////////////////////////////////
71 /// @brief Configures the voltage threshold detected by the Power Voltage
72 /// Detector(PVD).
73 /// @param pvd_level: specifies the PVD detection level
74 /// This parameter can be one of the following values:
75 /// @arg emPVD_LEVEL0 : PVD detection level set to 1.7V
76 /// @arg emPVD_LEVEL1 : PVD detection level set to 2.0V
77 /// @arg emPVD_LEVEL2 : PVD detection level set to 2.3V
78 /// @arg emPVD_LEVEL3 : PVD detection level set to 2.6V
79 /// @arg emPVD_LEVEL4 : PVD detection level set to 2.9V
80 /// @arg emPVD_LEVEL5 : PVD detection level set to 3.2V
81 /// @arg emPVD_LEVEL6 : PVD detection level set to 3.5V
82 /// @arg emPVD_LEVEL7 : PVD detection level set to 3.8V
83 /// @arg emPVD_LEVEL8 : PVD detection level set to 4.1V
84 /// @arg emPVD_LEVEL9 : PVD detection level set to 4.4V
85 /// @arg emPVD_LEVEL10: PVD detection level set to 4.7V
86 /// @retval None.
87 ////////////////////////////////////////////////////////////////////////////////
PWR_PVDLevelConfig(emPVD_Level_Typedef pvd_level)88 void PWR_PVDLevelConfig(emPVD_Level_Typedef pvd_level)
89 {
90 SYSCFG->PDETCSR = (SYSCFG->PDETCSR & (~SYSCFG_PDETCSR_PLS)) | pvd_level;
91 }
92 ////////////////////////////////////////////////////////////////////////////////
93 /// @brief Enables or disables the WakeUp Pin functionality.
94 /// @param state: new state of the WakeUp Pin functionality.
95 /// This parameter can be: ENABLE or DISABLE.
96 /// @retval None.
97 ////////////////////////////////////////////////////////////////////////////////
PWR_WakeUpPinCmd(FunctionalState state)98 void PWR_WakeUpPinCmd(FunctionalState state)
99 {
100 (state != DISABLE) ? (PWR->CR2 |= PWR_CR2_EWUP1) : (PWR->CSR &= ~PWR_CR2_EWUP1);
101 }
102
103 ////////////////////////////////////////////////////////////////////////////////
104 /// @brief Enables or disables the WakeUp Pin functionality.
105 /// @param state: new state of the WakeUp Pin functionality.
106 /// This parameter can be: ENABLE or DISABLE.
107 /// @retval None.
108 ////////////////////////////////////////////////////////////////////////////////
PWR_WakeUpPinXCmd(emWUP_Pin_Typedef pin,FunctionalState state)109 void PWR_WakeUpPinXCmd(emWUP_Pin_Typedef pin, FunctionalState state)
110 {
111 (state != DISABLE) ? (PWR->CR2 |= (PWR_CR2_EWUP1 << pin)) : (PWR->CSR &= ~(PWR_CR2_EWUP1 << pin));
112 }
113
114 ////////////////////////////////////////////////////////////////////////////////
115 /// @brief Enters STOP mode.
116 /// @param regulator: specifies the regulator state in STOP mode.
117 /// This parameter can be one of the following values:
118 /// @arg PWR_Regulator_ON: STOP mode with regulator ON
119 /// @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode.
120 /// @param stop_entry: specifies if STOP mode in entered with WFI or WFE
121 /// instruction.
122 /// This parameter can be one of the following values:
123 /// @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
124 /// @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
125 /// @retval None.
126 ////////////////////////////////////////////////////////////////////////////////
PWR_EnterSTOPMode(emPWR_Reg_Stop_mode_Typedef regulator,emPWR_STOP_ModeEn_Typedef stop_entry)127 void PWR_EnterSTOPMode(emPWR_Reg_Stop_mode_Typedef regulator, emPWR_STOP_ModeEn_Typedef stop_entry)
128 {
129
130 MODIFY_REG(PWR->CR, PWR_CR_LDPS, regulator);
131 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
132
133 if(stop_entry == PWR_STOPEntry_WFI) {
134 __WFI();
135 }
136 else {
137 __WFE();
138 }
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 /// @brief Enters STANDBY mode.
143 /// @param None.
144 /// @retval None.
145 ////////////////////////////////////////////////////////////////////////////////
PWR_EnterSTANDBYMode(void)146 void PWR_EnterSTANDBYMode(void)
147 {
148 PWR->CR |= PWR_CR_PDDS;
149 PWR->SCR |= PWR_SCR_CWUF1 | PWR_SCR_CWUF2 | PWR_SCR_CWUF3 | PWR_SCR_CWUF4 | PWR_SCR_CWUF5 | PWR_SCR_CWUF6;
150 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
151
152 #if defined(__CC_ARM)
153 __force_stores();
154 #endif
155 __WFI();
156 }
157 ////////////////////////////////////////////////////////////////////////////////
158 /// @brief Checks whether the specified PWR flag is set or not.
159 /// @param flag: specifies the flag to check.
160 /// This parameter can be one of the following values:
161 /// @arg PWR_FLAG_WU: Wake Up flag
162 /// @arg PWR_FLAG_SB: StandBy flag
163 /// @arg PWR_FLAG_PVDO: PVD Output
164 /// @retval The new state of PWR_FLAG (SET or RESET).
165 ////////////////////////////////////////////////////////////////////////////////
PWR_GetPVDOFlagStatus(u32 flag)166 FlagStatus PWR_GetPVDOFlagStatus(u32 flag)
167 {
168 return (FlagStatus)(SYSCFG->PDETCSR & flag);
169 }
170
171 ////////////////////////////////////////////////////////////////////////////////
172 /// @brief Clears the PWR's pending flags.
173 /// @param flag: specifies the flag to clear.
174 /// This parameter can be one of the following values:
175 /// @arg PWR_FLAG_WU: Wake Up flag
176 /// @arg PWR_FLAG_SB: StandBy flag
177 /// @retval None.
178 ////////////////////////////////////////////////////////////////////////////////
PWR_ClearPVDOFlag(u32 flag)179 void PWR_ClearPVDOFlag(u32 flag)
180 {
181 PWR->CR |= flag << 2;
182 }
183
184 ////////////////////////////////////////////////////////////////////////////////
185 /// @brief Checks whether the specified PWR flag is set or not.
186 /// @param flag: specifies the flag to check.
187 /// This parameter can be one of the following values:
188 /// @arg PWR_FLAG_WU: Wake Up flag
189 /// @arg PWR_FLAG_SB: StandBy flag
190 /// @arg PWR_FLAG_PVDO: PVD Output
191 /// @retval The new state of PWR_FLAG (SET or RESET).
192 ////////////////////////////////////////////////////////////////////////////////
PWR_GetFlagStatus(u32 flag)193 FlagStatus PWR_GetFlagStatus(u32 flag)
194 {
195 return (FlagStatus)(PWR->CSR & flag);
196 }
197
198 ////////////////////////////////////////////////////////////////////////////////
199 /// @brief Clears the PWR's pending flags.
200 /// @param flag: specifies the flag to clear.
201 /// This parameter can be one of the following values:
202 /// @arg PWR_FLAG_WU: Wake Up flag
203 /// @arg PWR_FLAG_SB: StandBy flag
204 /// @retval None.
205 ////////////////////////////////////////////////////////////////////////////////
PWR_ClearFlag(u32 flag)206 void PWR_ClearFlag(u32 flag)
207 {
208 PWR->CR |= flag << 2;
209 }
210
211 /// @}
212
213 /// @}
214
215 /// @}
216