1 /*********************COPYRIGHT(C) 2019 WCH. A11 rights reserved*********************** 2 * File Name : ch32f10x_pwr.c 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2019/10/15 6 * Description : This file provides all the PWR firmware functions. 7 ***************************************************************************************/ 8 #include "ch32f10x_pwr.h" 9 #include "ch32f10x_rcc.h" 10 11 /* PWR registers bit address in the alias region */ 12 #define PWR_OFFSET (PWR_BASE - PERIPH_BASE) 13 14 /* CTLR Register */ 15 /* Alias word address of DBP bit */ 16 #define CTLR_OFFSET (PWR_OFFSET + 0x00) 17 #define DBP_BitNumber 0x08 18 #define CTLR_DBP_BB (PERIPH_BB_BASE + (CTLR_OFFSET * 32) + (DBP_BitNumber * 4)) 19 20 /* Alias word address of PVDE bit */ 21 #define PVDE_BitNumber 0x04 22 #define CTLR_PVDE_BB (PERIPH_BB_BASE + (CTLR_OFFSET * 32) + (PVDE_BitNumber * 4)) 23 24 /* CSR Register */ 25 /* Alias word address of EWUP bit */ 26 #define CSR_OFFSET (PWR_OFFSET + 0x04) 27 #define EWUP_BitNumber 0x08 28 #define CSR_EWUP_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4)) 29 30 /* PWR registers bit mask */ 31 /* CTLR register bit mask */ 32 #define CTLR_DS_MASK ((uint32_t)0xFFFFFFFC) 33 #define CTLR_PLS_MASK ((uint32_t)0xFFFFFF1F) 34 35 /******************************************************************************** 36 * Function Name : PWR_DeInit 37 * Description : Deinitializes the PWR peripheral registers to their default 38 * reset values. 39 * Input : None 40 * Return : None 41 *********************************************************************************/ PWR_DeInit(void)42void PWR_DeInit(void) 43 { 44 RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE); 45 RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE); 46 } 47 48 49 /******************************************************************************** 50 * Function Name : PWR_BackupAccessCmd 51 * Description : Enables or disables access to the RTC and backup registers. 52 * Input : NewState: new state of the access to the RTC and backup registers, 53 * This parameter can be: ENABLE or DISABLE. 54 * Return : None 55 *********************************************************************************/ PWR_BackupAccessCmd(FunctionalState NewState)56void PWR_BackupAccessCmd(FunctionalState NewState) 57 { 58 *(__IO uint32_t *) CTLR_DBP_BB = (uint32_t)NewState; 59 } 60 61 62 /******************************************************************************** 63 * Function Name : PWR_PVDCmd 64 * Description : Enables or disables the Power Voltage Detector(PVD). 65 * Input : NewState: new state of the PVD(ENABLE or DISABLE). 66 * Return : None 67 *********************************************************************************/ PWR_PVDCmd(FunctionalState NewState)68void PWR_PVDCmd(FunctionalState NewState) 69 { 70 *(__IO uint32_t *) CTLR_PVDE_BB = (uint32_t)NewState; 71 } 72 73 74 /******************************************************************************** 75 * Function Name : PWR_PVDLevelConfig 76 * Description : Configures the voltage threshold detected by the Power Voltage 77 * Detector(PVD). 78 * Input : PWR_PVDLevel: specifies the PVD detection level 79 * PWR_PVDLevel_2V2: PVD detection level set to 2.2V 80 * PWR_PVDLevel_2V3: PVD detection level set to 2.3V 81 * PWR_PVDLevel_2V4: PVD detection level set to 2.4V 82 * PWR_PVDLevel_2V5: PVD detection level set to 2.5V 83 * PWR_PVDLevel_2V6: PVD detection level set to 2.6V 84 * PWR_PVDLevel_2V7: PVD detection level set to 2.7V 85 * PWR_PVDLevel_2V8: PVD detection level set to 2.8V 86 * PWR_PVDLevel_2V9: PVD detection level set to 2.9V 87 * Return : None 88 *********************************************************************************/ PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)89void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel) 90 { 91 uint32_t tmpreg = 0; 92 tmpreg = PWR->CTLR; 93 tmpreg &= CTLR_PLS_MASK; 94 tmpreg |= PWR_PVDLevel; 95 PWR->CTLR = tmpreg; 96 } 97 98 99 /******************************************************************************** 100 * Function Name : PWR_WakeUpPinCmd 101 * Description : Enables or disables the WakeUp Pin functionality. 102 * Input : NewState: new state of the WakeUp Pin functionality(ENABLE or DISABLE). 103 * Return : None 104 *********************************************************************************/ PWR_WakeUpPinCmd(FunctionalState NewState)105void PWR_WakeUpPinCmd(FunctionalState NewState) 106 { 107 *(__IO uint32_t *) CSR_EWUP_BB = (uint32_t)NewState; 108 } 109 110 111 /******************************************************************************** 112 * Function Name : PWR_EnterSTOPMode 113 * Description : Enters STOP mode. 114 * Input : PWR_Regulator: specifies the regulator state in STOP mode. 115 * PWR_Regulator_ON: STOP mode with regulator ON 116 * PWR_Regulator_LowPower: STOP mode with regulator in low power mode 117 * PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction. 118 * PWR_STOPEntry_WFI: enter STOP mode with WFI instruction 119 * PWR_STOPEntry_WFE: enter STOP mode with WFE instruction 120 * Return : None 121 *********************************************************************************/ PWR_EnterSTOPMode(uint32_t PWR_Regulator,uint8_t PWR_STOPEntry)122void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry) 123 { 124 uint32_t tmpreg = 0; 125 tmpreg = PWR->CTLR; 126 tmpreg &= CTLR_DS_MASK; 127 tmpreg |= PWR_Regulator; 128 PWR->CTLR = tmpreg; 129 SCB->SCR |= SCB_SCR_SLEEPDEEP; 130 131 if(PWR_STOPEntry == PWR_STOPEntry_WFI) 132 { 133 __WFI(); 134 } 135 else 136 { 137 __WFE(); 138 } 139 140 SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP); 141 } 142 143 /******************************************************************************** 144 * Function Name : PWR_EnterSTANDBYMode 145 * Description : Enters STANDBY mode. 146 * Input : None 147 * Return : None 148 *********************************************************************************/ PWR_EnterSTANDBYMode(void)149void PWR_EnterSTANDBYMode(void) 150 { 151 PWR->CTLR |= PWR_CTLR_CWUF; 152 PWR->CTLR |= PWR_CTLR_PDDS; 153 SCB->SCR |= SCB_SCR_SLEEPDEEP; 154 #if defined ( __CC_ARM ) 155 __force_stores(); 156 #endif 157 __WFI(); 158 } 159 160 161 /******************************************************************************** 162 * Function Name : PWR_GetFlagStatus 163 * Description : Checks whether the specified PWR flag is set or not. 164 * Input : PWR_FLAG: specifies the flag to check. 165 * PWR_FLAG_WU: Wake Up flag 166 * PWR_FLAG_SB: StandBy flag 167 * PWR_FLAG_PVDO: PVD Output 168 * Return : The new state of PWR_FLAG (SET or RESET). 169 *********************************************************************************/ PWR_GetFlagStatus(uint32_t PWR_FLAG)170FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG) 171 { 172 FlagStatus bitstatus = RESET; 173 174 if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET) 175 { 176 bitstatus = SET; 177 } 178 else 179 { 180 bitstatus = RESET; 181 } 182 return bitstatus; 183 } 184 185 186 /******************************************************************************** 187 * Function Name : PWR_ClearFlag 188 * Description : Clears the PWR's pending flags. 189 * Input : PWR_FLAG: specifies the flag to clear. 190 * PWR_FLAG_WU: Wake Up flag 191 * PWR_FLAG_SB: StandBy flag 192 * Return : None 193 *********************************************************************************/ PWR_ClearFlag(uint32_t PWR_FLAG)194void PWR_ClearFlag(uint32_t PWR_FLAG) 195 { 196 PWR->CTLR |= PWR_FLAG << 2; 197 } 198 199