summaryrefslogtreecommitdiff
path: root/bsp/wch/arm/libraries/CH32F10x_StdPeriph_Driver/StdPeriph_Driver/src/ch32f10x_pwr.c
blob: ddda0b5e6c297823c3d30bdf98d9170e18c9c86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*********************COPYRIGHT(C)  2019 WCH. A11 rights reserved***********************
 * File Name          : ch32f10x_pwr.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2019/10/15
 * Description        : This file provides all the PWR firmware functions.
 ***************************************************************************************/
#include "ch32f10x_pwr.h"
#include "ch32f10x_rcc.h"

/* PWR registers bit address in the alias region */
#define PWR_OFFSET               (PWR_BASE - PERIPH_BASE)

/* CTLR Register */
/* Alias word address of DBP bit */
#define CTLR_OFFSET              (PWR_OFFSET + 0x00)
#define DBP_BitNumber            0x08
#define CTLR_DBP_BB              (PERIPH_BB_BASE + (CTLR_OFFSET * 32) + (DBP_BitNumber * 4))

/* Alias word address of PVDE bit */
#define PVDE_BitNumber           0x04
#define CTLR_PVDE_BB             (PERIPH_BB_BASE + (CTLR_OFFSET * 32) + (PVDE_BitNumber * 4))

/* CSR Register */
/* Alias word address of EWUP bit */
#define CSR_OFFSET               (PWR_OFFSET + 0x04)
#define EWUP_BitNumber           0x08
#define CSR_EWUP_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4))

/* PWR registers bit mask */
/* CTLR register bit mask */
#define CTLR_DS_MASK             ((uint32_t)0xFFFFFFFC)
#define CTLR_PLS_MASK            ((uint32_t)0xFFFFFF1F)

/********************************************************************************
 * Function Name  : PWR_DeInit
 * Description    : Deinitializes the PWR peripheral registers to their default 
 *                  reset values. 
 * Input          : None
 * Return         : None
 *********************************************************************************/	
void PWR_DeInit(void)
{
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);
}


/********************************************************************************
 * Function Name  : PWR_BackupAccessCmd
 * Description    : Enables or disables access to the RTC and backup registers.  
 * Input          : NewState: new state of the access to the RTC and backup registers,
 *                            This parameter can be: ENABLE or DISABLE. 
 * Return         : None
 *********************************************************************************/	
void PWR_BackupAccessCmd(FunctionalState NewState)
{
  *(__IO uint32_t *) CTLR_DBP_BB = (uint32_t)NewState;
}


/********************************************************************************
 * Function Name  : PWR_PVDCmd
 * Description    : Enables or disables the Power Voltage Detector(PVD).
 * Input          : NewState: new state of the PVD(ENABLE or DISABLE).
 * Return         : None
 *********************************************************************************/	
void PWR_PVDCmd(FunctionalState NewState)
{
  *(__IO uint32_t *) CTLR_PVDE_BB = (uint32_t)NewState;
}


/********************************************************************************
 * Function Name  : PWR_PVDLevelConfig
 * Description    : Configures the voltage threshold detected by the Power Voltage 
 *                  Detector(PVD).
 * Input          : PWR_PVDLevel: specifies the PVD detection level
 *                     PWR_PVDLevel_2V2: PVD detection level set to 2.2V
 *                     PWR_PVDLevel_2V3: PVD detection level set to 2.3V
 *                     PWR_PVDLevel_2V4: PVD detection level set to 2.4V
 *                     PWR_PVDLevel_2V5: PVD detection level set to 2.5V
 *                     PWR_PVDLevel_2V6: PVD detection level set to 2.6V
 *                     PWR_PVDLevel_2V7: PVD detection level set to 2.7V
 *                     PWR_PVDLevel_2V8: PVD detection level set to 2.8V
 *                     PWR_PVDLevel_2V9: PVD detection level set to 2.9V
 * Return         : None
 *********************************************************************************/	
void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)
{
  uint32_t tmpreg = 0;
  tmpreg = PWR->CTLR;
  tmpreg &= CTLR_PLS_MASK;
  tmpreg |= PWR_PVDLevel;
  PWR->CTLR = tmpreg;
}


/********************************************************************************
 * Function Name  : PWR_WakeUpPinCmd
 * Description    : Enables or disables the WakeUp Pin functionality.
 * Input          : NewState: new state of the WakeUp Pin functionality(ENABLE or DISABLE).
 * Return         : None
 *********************************************************************************/	
void PWR_WakeUpPinCmd(FunctionalState NewState)
{
  *(__IO uint32_t *) CSR_EWUP_BB = (uint32_t)NewState;
}


/********************************************************************************
 * Function Name  : PWR_EnterSTOPMode
 * Description    : Enters STOP mode.
 * Input          : PWR_Regulator: specifies the regulator state in STOP mode.
 *                    PWR_Regulator_ON: STOP mode with regulator ON
 *                    PWR_Regulator_LowPower: STOP mode with regulator in low power mode  
 *                  PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
 *                    PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
 *                    PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
 * Return         : None
 *********************************************************************************/	
void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
{
  uint32_t tmpreg = 0;
  tmpreg = PWR->CTLR;
  tmpreg &= CTLR_DS_MASK;
  tmpreg |= PWR_Regulator;
  PWR->CTLR = tmpreg;
  SCB->SCR |= SCB_SCR_SLEEPDEEP;
  
  if(PWR_STOPEntry == PWR_STOPEntry_WFI)
  {   
    __WFI();
  }
  else
  {
    __WFE();
  }
  
  SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);  
}

/********************************************************************************
 * Function Name  : PWR_EnterSTANDBYMode
 * Description    : Enters STANDBY mode.
 * Input          : None
 * Return         : None
 *********************************************************************************/	
void PWR_EnterSTANDBYMode(void)
{
  PWR->CTLR |= PWR_CTLR_CWUF;
  PWR->CTLR |= PWR_CTLR_PDDS;
  SCB->SCR |= SCB_SCR_SLEEPDEEP;
#if defined ( __CC_ARM   )
  __force_stores();
#endif
  __WFI();
}


/********************************************************************************
 * Function Name  : PWR_GetFlagStatus
 * Description    : Checks whether the specified PWR flag is set or not.
 * Input          : PWR_FLAG: specifies the flag to check.
 *                     PWR_FLAG_WU: Wake Up flag
 *                     PWR_FLAG_SB: StandBy flag
 *                     PWR_FLAG_PVDO: PVD Output
 * Return         : The new state of PWR_FLAG (SET or RESET).
 *********************************************************************************/
FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
{
  FlagStatus bitstatus = RESET;
  
  if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;
}


/********************************************************************************
 * Function Name  : PWR_ClearFlag
 * Description    : Clears the PWR's pending flags.
 * Input          : PWR_FLAG: specifies the flag to clear.
 *                     PWR_FLAG_WU: Wake Up flag
 *                     PWR_FLAG_SB: StandBy flag
 * Return         : None
 *********************************************************************************/
void PWR_ClearFlag(uint32_t PWR_FLAG)
{        
  PWR->CTLR |=  PWR_FLAG << 2;
}