1 /*!
2  * @file        apm32s10x_pmu.c
3  *
4  * @brief       This file provides all the PMU firmware functions.
5  *
6  * @version     V1.0.1
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2022-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be usefull and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Includes */
27 #include "apm32s10x_pmu.h"
28 #include "apm32s10x_rcm.h"
29 
30 /** @addtogroup APM32S10x_StdPeriphDriver
31   @{
32 */
33 
34 /** @addtogroup PMU_Driver PMU Driver
35   @{
36 */
37 
38 /** @defgroup  PMU_Functions Functions
39   @{
40 */
41 
42 /*!
43  * @brief     Reset the PMU peripheral register.
44  *
45  * @param     None
46  *
47  * @retval    None
48  */
PMU_Reset(void)49 void PMU_Reset(void)
50 {
51     RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_PMU);
52     RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_PMU);
53 }
54 
55 /*!
56  * @brief     Enable access to the RTC and backup registers.
57  *
58  * @param     None
59  *
60  * @retval    None
61  */
PMU_EnableBackupAccess(void)62 void PMU_EnableBackupAccess(void)
63 {
64     PMU->CTRL_B.BPWEN = ENABLE ;
65 }
66 
67 /*!
68  * @brief     Disable access to the RTC and backup registers.
69  *
70  * @param     None
71  *
72  * @retval    None
73  */
PMU_DisableBackupAccess(void)74 void PMU_DisableBackupAccess(void)
75 {
76     PMU->CTRL_B.BPWEN = DISABLE;
77 }
78 
79 /*!
80  * @brief     Enable the Power Voltage Detector(PVD).
81  *
82  * @param     None
83  *
84  * @retval    None
85  */
PMU_EnablePVD(void)86 void PMU_EnablePVD(void)
87 {
88     PMU->CTRL_B.PVDEN = ENABLE;
89 }
90 
91 /*!
92  * @brief     Disable the Power Voltage Detector(PVD).
93  *
94  * @param     None
95  *
96  * @retval    None
97  */
PMU_DisablePVD(void)98 void PMU_DisablePVD(void)
99 {
100     PMU->CTRL_B.PVDEN = DISABLE;
101 }
102 
103 /*!
104  * @brief     Configure a voltage threshold detected by a power supply voltage detector (PVD).
105  *
106  * @param     level��specifies the PVD detection level
107  *                   This parameter can be one of the following values:
108  *                   @arg PMU_PVD_LEVEL_2V2 : Config PVD detection level to 2.2V
109  *                   @arg PMU_PVD_LEVEL_2V3 : Config PVD detection level to 2.3V
110  *                   @arg PMU_PVD_LEVEL_2V4 : Config PVD detection level to 2.4V
111  *                   @arg PMU_PVD_LEVEL_2V5 : Config PVD detection level to 2.5V
112  *                   @arg PMU_PVD_LEVEL_2V6 : Config PVD detection level to 2.6V
113  *                   @arg PMU_PVD_LEVEL_2V7 : Config PVD detection level to 2.7V
114  *                   @arg PMU_PVD_LEVEL_2V8 : Config PVD detection level to 2.8V
115  *                   @arg PMU_PVD_LEVEL_2V9 : Config PVD detection level to 2.9V
116  *
117  * @retval    None
118  */
PMU_ConfigPVDLevel(PMU_PVD_LEVEL_T level)119 void PMU_ConfigPVDLevel(PMU_PVD_LEVEL_T level)
120 {
121     /* Clear PLS[7:5] bits */
122     PMU->CTRL_B.PLSEL = 0x0000;
123     /* Store the new value */
124     PMU->CTRL_B.PLSEL = level;
125 }
126 
127 /*!
128  * @brief     Enable the WakeUp Pin functionality.
129  *
130  * @param     None
131  *
132  * @retval    None
133  */
PMU_EnableWakeUpPin(void)134 void PMU_EnableWakeUpPin(void)
135 {
136     PMU->CSTS_B.WKUPCFG = ENABLE ;
137 }
138 
139 /*!
140  * @brief     Disable the WakeUp Pin functionality.
141  *
142  * @param     None
143  *
144  * @retval    None
145  */
PMU_DisableWakeUpPin(void)146 void PMU_DisableWakeUpPin(void)
147 {
148     PMU->CSTS_B.WKUPCFG = DISABLE ;
149 }
150 
151 /*!
152  * @brief     Enter STOP mode.
153  *
154  * @param     regulator: specifies the regulator state in STOP mode.
155  *                       This parameter can be one of the following values:
156  *                         @arg PMU_REGULATOR_ON      : STOP mode with regulator ON
157  *                         @arg PMU_REGULATOR_LOWPOWER: STOP mode with regulator in low power mode
158  *
159  * @param     entry: specifies if STOP mode in entered with WFI or WFE instruction.
160  *                   This parameter can be one of the following values:
161  *                     @arg PMU_STOP_ENTRY_WFI: Enter STOP mode with WFI instruction
162  *                     @arg PMU_STOP_ENTRY_WFE: Enter STOP mode with WFE instruction
163  *
164  * @retval    None
165  */
PMU_EnterSTOPMode(PMU_REGULATOR_T regulator,PMU_STOP_ENTRY_T entry)166 void PMU_EnterSTOPMode(PMU_REGULATOR_T regulator, PMU_STOP_ENTRY_T entry)
167 {
168     /* Clear PDDSCFG and LPDSCFG bits */
169     PMU->CTRL_B.PDDSCFG = 0x00;
170     PMU->CTRL_B.LPDSCFG = 0x00;
171     /* Set LPDSCFG bit according to regulator value */
172     PMU->CTRL_B.LPDSCFG = regulator;
173     /* Set Cortex System Control Register */
174     SCB->SCR |= (uint32_t)0x04;
175     /* Select STOP mode entry*/
176     if (entry == PMU_STOP_ENTRY_WFI)
177     {
178         /* Request Wait For Interrupt */
179         __WFI();
180     }
181     else
182     {
183         /* Request Wait For Event */
184         __WFE();
185     }
186 
187     /* Reset SLEEPDEEP bit of Cortex System Control Register */
188     SCB->SCR &= (uint32_t)~((uint32_t)0x04);
189 }
190 
191 /*!
192  * @brief     Enter STANDBY mode.
193  *
194  * @param     None
195  *
196  * @retval    None
197  */
PMU_EnterSTANDBYMode(void)198 void PMU_EnterSTANDBYMode(void)
199 {
200     /* Clear Wake-up flag */
201     PMU->CTRL_B.WUFLGCLR = BIT_SET;
202     /* Select STANDBY mode */
203     PMU->CTRL_B.PDDSCFG = BIT_SET;
204     /* Set Cortex System Control Register */
205     SCB->SCR |= (uint32_t)0x04;
206 #if defined ( __CC_ARM   )
207     __force_stores();
208 #endif
209     /* Request Wait For Interrupt */
210     __WFI();
211 
212 }
213 
214 /*!
215  * @brief     Read the specified PWR flag is set or not.
216  *
217  * @param     flag��Reads the status of specifies the flag.
218  *                  This parameter can be one of the following values:
219  *                    @arg PMU_FLAG_WUE : Wake Up flag
220  *                    @arg PMU_FLAG_SB  : StandBy flag
221  *                    @arg PMU_FLAG_PVDO: PVD Output flag
222  *
223  * @retval    The new state of PMU_FLAG (SET or RESET).
224  */
PMU_ReadStatusFlag(PMU_FLAG_T flag)225 uint8_t PMU_ReadStatusFlag(PMU_FLAG_T flag)
226 {
227     uint8_t BitStatus = BIT_RESET;
228 
229     if (flag == PMU_FLAG_WUE)
230     {
231         BitStatus = PMU->CSTS_B.WUEFLG;
232     }
233     else if (flag == PMU_FLAG_SB)
234     {
235         BitStatus = PMU->CSTS_B.SBFLG;
236     }
237     else if (flag == PMU_FLAG_PVDO)
238     {
239         BitStatus = PMU->CSTS_B.PVDOFLG;
240     }
241     return BitStatus;
242 }
243 
244 /*!
245  * @brief     Clears the PWR's pending flags.
246  *
247  * @param     flag��Clears the status of specifies the flag.
248  *                  This parameter can be one of the following values:
249  *                    @arg PMU_FLAG_WUE : Wake Up flag
250  *                    @arg PMU_FLAG_SB  : StandBy flag
251  *
252  * @retval    None
253  */
PMU_ClearStatusFlag(PMU_FLAG_T flag)254 void PMU_ClearStatusFlag(PMU_FLAG_T flag)
255 {
256     if (flag == PMU_FLAG_WUE)
257     {
258         PMU->CTRL_B.WUFLGCLR = BIT_SET;
259     }
260     else if (flag == PMU_FLAG_SB)
261     {
262         PMU->CTRL_B.SBFLGCLR = BIT_SET;
263     }
264 }
265 
266 /**@} end of group PMU_Functions */
267 /**@} end of group PMU_Driver */
268 /**@} end of group APM32S10x_StdPeriphDriver */
269