stm32f10x_pwr.c 8.9 KB

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