stm32f10x_pwr.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_pwr.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the PWR firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x_pwr.h"
  23. #include "stm32f10x_rcc.h"
  24. /** @addtogroup STM32F10x_StdPeriph_Driver
  25. * @{
  26. */
  27. /** @defgroup PWR
  28. * @brief PWR driver modules
  29. * @{
  30. */
  31. /** @defgroup PWR_Private_TypesDefinitions
  32. * @{
  33. */
  34. /**
  35. * @}
  36. */
  37. /** @defgroup PWR_Private_Defines
  38. * @{
  39. */
  40. /* --------- PWR registers bit address in the alias region ---------- */
  41. #define PWR_OFFSET (PWR_BASE - PERIPH_BASE)
  42. /* --- CR Register ---*/
  43. /* Alias word address of DBP bit */
  44. #define CR_OFFSET (PWR_OFFSET + 0x00)
  45. #define DBP_BitNumber 0x08
  46. #define CR_DBP_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))
  47. /* Alias word address of PVDE bit */
  48. #define PVDE_BitNumber 0x04
  49. #define CR_PVDE_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PVDE_BitNumber * 4))
  50. /* --- CSR Register ---*/
  51. /* Alias word address of EWUP bit */
  52. #define CSR_OFFSET (PWR_OFFSET + 0x04)
  53. #define EWUP_BitNumber 0x08
  54. #define CSR_EWUP_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4))
  55. /* ------------------ PWR registers bit mask ------------------------ */
  56. /* CR register bit mask */
  57. #define CR_DS_MASK ((uint32_t)0xFFFFFFFC)
  58. #define CR_PLS_MASK ((uint32_t)0xFFFFFF1F)
  59. /**
  60. * @}
  61. */
  62. /** @defgroup PWR_Private_Macros
  63. * @{
  64. */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup PWR_Private_Variables
  69. * @{
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup PWR_Private_FunctionPrototypes
  75. * @{
  76. */
  77. /**
  78. * @}
  79. */
  80. /** @defgroup PWR_Private_Functions
  81. * @{
  82. */
  83. /**
  84. * @brief Deinitializes the PWR peripheral registers to their default reset values.
  85. * @param None
  86. * @retval None
  87. */
  88. void PWR_DeInit(void)
  89. {
  90. RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
  91. RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);
  92. }
  93. /**
  94. * @brief Enables or disables access to the RTC and backup registers.
  95. * @param NewState: new state of the access to the RTC and backup registers.
  96. * This parameter can be: ENABLE or DISABLE.
  97. * @retval None
  98. */
  99. void PWR_BackupAccessCmd(FunctionalState NewState)
  100. {
  101. /* Check the parameters */
  102. assert_param(IS_FUNCTIONAL_STATE(NewState));
  103. *(__IO uint32_t *) CR_DBP_BB = (uint32_t)NewState;
  104. }
  105. /**
  106. * @brief Enables or disables the Power Voltage Detector(PVD).
  107. * @param NewState: new state of the PVD.
  108. * This parameter can be: ENABLE or DISABLE.
  109. * @retval None
  110. */
  111. void PWR_PVDCmd(FunctionalState NewState)
  112. {
  113. /* Check the parameters */
  114. assert_param(IS_FUNCTIONAL_STATE(NewState));
  115. *(__IO uint32_t *) CR_PVDE_BB = (uint32_t)NewState;
  116. }
  117. /**
  118. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  119. * @param PWR_PVDLevel: specifies the PVD detection level
  120. * This parameter can be one of the following values:
  121. * @arg PWR_PVDLevel_2V2: PVD detection level set to 2.2V
  122. * @arg PWR_PVDLevel_2V3: PVD detection level set to 2.3V
  123. * @arg PWR_PVDLevel_2V4: PVD detection level set to 2.4V
  124. * @arg PWR_PVDLevel_2V5: PVD detection level set to 2.5V
  125. * @arg PWR_PVDLevel_2V6: PVD detection level set to 2.6V
  126. * @arg PWR_PVDLevel_2V7: PVD detection level set to 2.7V
  127. * @arg PWR_PVDLevel_2V8: PVD detection level set to 2.8V
  128. * @arg PWR_PVDLevel_2V9: PVD detection level set to 2.9V
  129. * @retval None
  130. */
  131. void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)
  132. {
  133. uint32_t tmpreg = 0;
  134. /* Check the parameters */
  135. assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
  136. tmpreg = PWR->CR;
  137. /* Clear PLS[7:5] bits */
  138. tmpreg &= CR_PLS_MASK;
  139. /* Set PLS[7:5] bits according to PWR_PVDLevel value */
  140. tmpreg |= PWR_PVDLevel;
  141. /* Store the new value */
  142. PWR->CR = tmpreg;
  143. }
  144. /**
  145. * @brief Enables or disables the WakeUp Pin functionality.
  146. * @param NewState: new state of the WakeUp Pin functionality.
  147. * This parameter can be: ENABLE or DISABLE.
  148. * @retval None
  149. */
  150. void PWR_WakeUpPinCmd(FunctionalState NewState)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_FUNCTIONAL_STATE(NewState));
  154. *(__IO uint32_t *) CSR_EWUP_BB = (uint32_t)NewState;
  155. }
  156. /**
  157. * @brief Enters STOP mode.
  158. * @param PWR_Regulator: specifies the regulator state in STOP mode.
  159. * This parameter can be one of the following values:
  160. * @arg PWR_Regulator_ON: STOP mode with regulator ON
  161. * @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode
  162. * @param PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
  163. * This parameter can be one of the following values:
  164. * @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
  165. * @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
  166. * @retval None
  167. */
  168. void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
  169. {
  170. uint32_t tmpreg = 0;
  171. /* Check the parameters */
  172. assert_param(IS_PWR_REGULATOR(PWR_Regulator));
  173. assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
  174. /* Select the regulator state in STOP mode ---------------------------------*/
  175. tmpreg = PWR->CR;
  176. /* Clear PDDS and LPDS bits */
  177. tmpreg &= CR_DS_MASK;
  178. /* Set LPDS bit according to PWR_Regulator value */
  179. tmpreg |= PWR_Regulator;
  180. /* Store the new value */
  181. PWR->CR = tmpreg;
  182. /* Set SLEEPDEEP bit of Cortex System Control Register */
  183. SCB->SCR |= SCB_SCR_SLEEPDEEP;
  184. /* Select STOP mode entry --------------------------------------------------*/
  185. if(PWR_STOPEntry == PWR_STOPEntry_WFI)
  186. {
  187. /* Request Wait For Interrupt */
  188. __WFI();
  189. }
  190. else
  191. {
  192. /* Request Wait For Event */
  193. __WFE();
  194. }
  195. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  196. SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);
  197. }
  198. /**
  199. * @brief Enters STANDBY mode.
  200. * @param None
  201. * @retval None
  202. */
  203. void PWR_EnterSTANDBYMode(void)
  204. {
  205. /* Clear Wake-up flag */
  206. PWR->CR |= PWR_CR_CWUF;
  207. /* Select STANDBY mode */
  208. PWR->CR |= PWR_CR_PDDS;
  209. /* Set SLEEPDEEP bit of Cortex System Control Register */
  210. SCB->SCR |= SCB_SCR_SLEEPDEEP;
  211. /* This option is used to ensure that store operations are completed */
  212. #if defined ( __CC_ARM )
  213. __force_stores();
  214. #endif
  215. /* Request Wait For Interrupt */
  216. __WFI();
  217. }
  218. /**
  219. * @brief Checks whether the specified PWR flag is set or not.
  220. * @param PWR_FLAG: specifies the flag to check.
  221. * This parameter can be one of the following values:
  222. * @arg PWR_FLAG_WU: Wake Up flag
  223. * @arg PWR_FLAG_SB: StandBy flag
  224. * @arg PWR_FLAG_PVDO: PVD Output
  225. * @retval The new state of PWR_FLAG (SET or RESET).
  226. */
  227. FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
  228. {
  229. FlagStatus bitstatus = RESET;
  230. /* Check the parameters */
  231. assert_param(IS_PWR_GET_FLAG(PWR_FLAG));
  232. if ((PWR->CSR & PWR_FLAG) != (uint32_t)RESET)
  233. {
  234. bitstatus = SET;
  235. }
  236. else
  237. {
  238. bitstatus = RESET;
  239. }
  240. /* Return the flag status */
  241. return bitstatus;
  242. }
  243. /**
  244. * @brief Clears the PWR's pending flags.
  245. * @param PWR_FLAG: specifies the flag to clear.
  246. * This parameter can be one of the following values:
  247. * @arg PWR_FLAG_WU: Wake Up flag
  248. * @arg PWR_FLAG_SB: StandBy flag
  249. * @retval None
  250. */
  251. void PWR_ClearFlag(uint32_t PWR_FLAG)
  252. {
  253. /* Check the parameters */
  254. assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG));
  255. PWR->CR |= PWR_FLAG << 2;
  256. }
  257. /**
  258. * @}
  259. */
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/