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