1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_pwr_ex.c
4   * @author  MCD Application Team
5   * @brief   Extended PWR HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of PWR extension peripheral:
8   *           + Peripheral Extended features functions
9   *
10   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2017 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   */
22 
23 /* Includes ------------------------------------------------------------------*/
24 #include "stm32f2xx_hal.h"
25 
26 /** @addtogroup STM32F2xx_HAL_Driver
27   * @{
28   */
29 
30 /** @defgroup PWREx PWREx
31   * @brief PWR HAL module driver
32   * @{
33   */
34 
35 #ifdef HAL_PWR_MODULE_ENABLED
36 
37 /* Private typedef -----------------------------------------------------------*/
38 /* Private define ------------------------------------------------------------*/
39 /** @addtogroup PWREx_Private_Constants
40   * @{
41   */
42 #define PWR_BKPREG_TIMEOUT_VALUE     1000U
43 /**
44   * @}
45   */
46 
47 
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Private functions ---------------------------------------------------------*/
52 /** @defgroup PWREx_Exported_Functions PWR Exported Functions
53   *  @{
54   */
55 
56 /** @defgroup PWREx_Exported_Functions_Group1 Peripheral Extended features functions
57   *  @brief Peripheral Extended features functions
58   *
59 @verbatim
60 
61  ===============================================================================
62                  ##### Peripheral extended features functions #####
63  ===============================================================================
64 
65     *** Main and Backup Regulators configuration ***
66     ================================================
67     [..]
68       (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from
69           the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is
70           retained even in Standby or VBAT mode when the low power backup regulator
71           is enabled. It can be considered as an internal EEPROM when VBAT is
72           always present. You can use the HAL_PWREx_EnableBkUpReg() function to
73           enable the low power backup regulator.
74 
75       (+) When the backup domain is supplied by VDD (analog switch connected to VDD)
76           the backup SRAM is powered from VDD which replaces the VBAT power supply to
77           save battery life.
78 
79       (+) The backup SRAM is not mass erased by a tamper event. It is read
80           protected to prevent confidential data, such as cryptographic private
81           key, from being accessed. The backup SRAM can be erased only through
82           the Flash interface when a protection level change from level 1 to
83           level 0 is requested.
84       -@- Refer to the description of Read protection (RDP) in the Flash
85           programming manual.
86 
87         Refer to the product datasheets for more details.
88 
89     *** FLASH Power Down configuration ****
90     =======================================
91     [..]
92       (+) By setting the FPDS bit in the PWR_CR register by using the
93           HAL_PWREx_EnableFlashPowerDown() function, the Flash memory also enters power
94           down mode when the device enters Stop mode. When the Flash memory
95           is in power down mode, an additional startup delay is incurred when
96           waking up from Stop mode.
97 
98 @endverbatim
99   * @{
100   */
101 
102 /**
103   * @brief Enables the Backup Regulator.
104   * @retval HAL status
105   */
HAL_PWREx_EnableBkUpReg(void)106 HAL_StatusTypeDef HAL_PWREx_EnableBkUpReg(void)
107 {
108   uint32_t tickstart = 0U;
109 
110   *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)ENABLE;
111 
112   /* Get tick */
113   tickstart = HAL_GetTick();
114 
115   /* Wait till Backup regulator ready flag is set */
116   while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)
117   {
118     if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
119     {
120       return HAL_TIMEOUT;
121     }
122   }
123   return HAL_OK;
124 }
125 
126 /**
127   * @brief Disables the Backup Regulator.
128   * @retval HAL status
129   */
HAL_PWREx_DisableBkUpReg(void)130 HAL_StatusTypeDef HAL_PWREx_DisableBkUpReg(void)
131 {
132   uint32_t tickstart = 0U;
133 
134   *(__IO uint32_t *) CSR_BRE_BB = (uint32_t)DISABLE;
135 
136   /* Get tick */
137   tickstart = HAL_GetTick();
138 
139   /* Wait till Backup regulator ready flag is set */
140   while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) != RESET)
141   {
142     if((HAL_GetTick() - tickstart ) > PWR_BKPREG_TIMEOUT_VALUE)
143     {
144       return HAL_TIMEOUT;
145     }
146   }
147   return HAL_OK;
148 }
149 
150 /**
151   * @brief Enables the Flash Power Down in Stop mode.
152   * @retval None
153   */
HAL_PWREx_EnableFlashPowerDown(void)154 void HAL_PWREx_EnableFlashPowerDown(void)
155 {
156   *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)ENABLE;
157 }
158 
159 /**
160   * @brief Disables the Flash Power Down in Stop mode.
161   * @retval None
162   */
HAL_PWREx_DisableFlashPowerDown(void)163 void HAL_PWREx_DisableFlashPowerDown(void)
164 {
165   *(__IO uint32_t *) CR_FPDS_BB = (uint32_t)DISABLE;
166 }
167 
168 /**
169   * @}
170   */
171 
172 /**
173   * @}
174   */
175 #endif /* HAL_PWR_MODULE_ENABLED */
176 /**
177   * @}
178   */
179 
180 /**
181   * @}
182   */
183