stm32l4xx_hal_pwr_ex.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 the Power Controller (PWR) peripheral:
  8. * + Extended Initialization and de-initialization functions
  9. * + Extended Peripheral Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32l4xx_hal.h"
  25. /** @addtogroup STM32L4xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup PWREx PWREx
  29. * @brief PWR Extended HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_PWR_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx)
  36. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000000B) /* PH0/PH1/PH3 */
  37. #elif defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  38. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000000B) /* PH0/PH1/PH3 */
  39. #elif defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx)
  40. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x00000003) /* PH0/PH1 */
  41. #elif defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  42. #define PWR_PORTH_AVAILABLE_PINS ((uint32_t)0x0000FFFF) /* PH0..PH15 */
  43. #endif
  44. #if defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  45. #define PWR_PORTI_AVAILABLE_PINS ((uint32_t)0x00000FFF) /* PI0..PI11 */
  46. #endif
  47. /** @defgroup PWR_Extended_Private_Defines PWR Extended Private Defines
  48. * @{
  49. */
  50. /** @defgroup PWREx_PVM_Mode_Mask PWR PVM Mode Mask
  51. * @{
  52. */
  53. #define PVM_MODE_IT ((uint32_t)0x00010000) /*!< Mask for interruption yielded by PVM threshold crossing */
  54. #define PVM_MODE_EVT ((uint32_t)0x00020000) /*!< Mask for event yielded by PVM threshold crossing */
  55. #define PVM_RISING_EDGE ((uint32_t)0x00000001) /*!< Mask for rising edge set as PVM trigger */
  56. #define PVM_FALLING_EDGE ((uint32_t)0x00000002) /*!< Mask for falling edge set as PVM trigger */
  57. /**
  58. * @}
  59. */
  60. /** @defgroup PWREx_TimeOut_Value PWR Extended Flag Setting Time Out Value
  61. * @{
  62. */
  63. #define PWR_FLAG_SETTING_DELAY_US 50UL /*!< Time out value for REGLPF and VOSF flags setting */
  64. /**
  65. * @}
  66. */
  67. /**
  68. * @}
  69. */
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. /* Private function prototypes -----------------------------------------------*/
  73. /* Exported functions --------------------------------------------------------*/
  74. /** @defgroup PWREx_Exported_Functions PWR Extended Exported Functions
  75. * @{
  76. */
  77. /** @defgroup PWREx_Exported_Functions_Group1 Extended Peripheral Control functions
  78. * @brief Extended Peripheral Control functions
  79. *
  80. @verbatim
  81. ===============================================================================
  82. ##### Extended Peripheral Initialization and de-initialization functions #####
  83. ===============================================================================
  84. [..]
  85. @endverbatim
  86. * @{
  87. */
  88. /**
  89. * @brief Return Voltage Scaling Range.
  90. * @retval VOS bit field (PWR_REGULATOR_VOLTAGE_SCALE1 or PWR_REGULATOR_VOLTAGE_SCALE2
  91. * or PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when applicable)
  92. */
  93. uint32_t HAL_PWREx_GetVoltageRange(void)
  94. {
  95. #if defined(PWR_CR5_R1MODE)
  96. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  97. {
  98. return PWR_REGULATOR_VOLTAGE_SCALE2;
  99. }
  100. else if (READ_BIT(PWR->CR5, PWR_CR5_R1MODE) == PWR_CR5_R1MODE)
  101. {
  102. /* PWR_CR5_R1MODE bit set means that Range 1 Boost is disabled */
  103. return PWR_REGULATOR_VOLTAGE_SCALE1;
  104. }
  105. else
  106. {
  107. return PWR_REGULATOR_VOLTAGE_SCALE1_BOOST;
  108. }
  109. #else
  110. return (PWR->CR1 & PWR_CR1_VOS);
  111. #endif
  112. }
  113. /**
  114. * @brief Configure the main internal regulator output voltage.
  115. * @param VoltageScaling specifies the regulator output voltage to achieve
  116. * a tradeoff between performance and power consumption.
  117. * This parameter can be one of the following values:
  118. @if STM32L4S9xx
  119. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1_BOOST when available, Regulator voltage output range 1 boost mode,
  120. * typical output voltage at 1.2 V,
  121. * system frequency up to 120 MHz.
  122. @endif
  123. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1 Regulator voltage output range 1 mode,
  124. * typical output voltage at 1.2 V,
  125. * system frequency up to 80 MHz.
  126. * @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2 Regulator voltage output range 2 mode,
  127. * typical output voltage at 1.0 V,
  128. * system frequency up to 26 MHz.
  129. * @note When moving from Range 1 to Range 2, the system frequency must be decreased to
  130. * a value below 26 MHz before calling HAL_PWREx_ControlVoltageScaling() API.
  131. * When moving from Range 2 to Range 1, the system frequency can be increased to
  132. * a value up to 80 MHz after calling HAL_PWREx_ControlVoltageScaling() API. For
  133. * some devices, the system frequency can be increased up to 120 MHz.
  134. * @note When moving from Range 2 to Range 1, the API waits for VOSF flag to be
  135. * cleared before returning the status. If the flag is not cleared within
  136. * 50 microseconds, HAL_TIMEOUT status is reported.
  137. * @retval HAL Status
  138. */
  139. HAL_StatusTypeDef HAL_PWREx_ControlVoltageScaling(uint32_t VoltageScaling)
  140. {
  141. uint32_t wait_loop_index;
  142. assert_param(IS_PWR_VOLTAGE_SCALING_RANGE(VoltageScaling));
  143. #if defined(PWR_CR5_R1MODE)
  144. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1_BOOST)
  145. {
  146. /* If current range is range 2 */
  147. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  148. {
  149. /* Make sure Range 1 Boost is enabled */
  150. CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);
  151. /* Set Range 1 */
  152. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  153. /* Wait until VOSF is cleared */
  154. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1;
  155. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  156. {
  157. wait_loop_index--;
  158. }
  159. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  160. {
  161. return HAL_TIMEOUT;
  162. }
  163. }
  164. /* If current range is range 1 normal or boost mode */
  165. else
  166. {
  167. /* Enable Range 1 Boost (no issue if bit already reset) */
  168. CLEAR_BIT(PWR->CR5, PWR_CR5_R1MODE);
  169. }
  170. }
  171. else if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  172. {
  173. /* If current range is range 2 */
  174. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) == PWR_REGULATOR_VOLTAGE_SCALE2)
  175. {
  176. /* Make sure Range 1 Boost is disabled */
  177. SET_BIT(PWR->CR5, PWR_CR5_R1MODE);
  178. /* Set Range 1 */
  179. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  180. /* Wait until VOSF is cleared */
  181. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1;
  182. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  183. {
  184. wait_loop_index--;
  185. }
  186. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  187. {
  188. return HAL_TIMEOUT;
  189. }
  190. }
  191. /* If current range is range 1 normal or boost mode */
  192. else
  193. {
  194. /* Disable Range 1 Boost (no issue if bit already set) */
  195. SET_BIT(PWR->CR5, PWR_CR5_R1MODE);
  196. }
  197. }
  198. else
  199. {
  200. /* Set Range 2 */
  201. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE2);
  202. /* No need to wait for VOSF to be cleared for this transition */
  203. /* PWR_CR5_R1MODE bit setting has no effect in Range 2 */
  204. }
  205. #else
  206. /* If Set Range 1 */
  207. if (VoltageScaling == PWR_REGULATOR_VOLTAGE_SCALE1)
  208. {
  209. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE1)
  210. {
  211. /* Set Range 1 */
  212. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE1);
  213. /* Wait until VOSF is cleared */
  214. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
  215. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
  216. {
  217. wait_loop_index--;
  218. }
  219. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF))
  220. {
  221. return HAL_TIMEOUT;
  222. }
  223. }
  224. }
  225. else
  226. {
  227. if (READ_BIT(PWR->CR1, PWR_CR1_VOS) != PWR_REGULATOR_VOLTAGE_SCALE2)
  228. {
  229. /* Set Range 2 */
  230. MODIFY_REG(PWR->CR1, PWR_CR1_VOS, PWR_REGULATOR_VOLTAGE_SCALE2);
  231. /* No need to wait for VOSF to be cleared for this transition */
  232. }
  233. }
  234. #endif
  235. return HAL_OK;
  236. }
  237. /**
  238. * @brief Enable battery charging.
  239. * When VDD is present, charge the external battery on VBAT thru an internal resistor.
  240. * @param ResistorSelection specifies the resistor impedance.
  241. * This parameter can be one of the following values:
  242. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_5 5 kOhms resistor
  243. * @arg @ref PWR_BATTERY_CHARGING_RESISTOR_1_5 1.5 kOhms resistor
  244. * @retval None
  245. */
  246. void HAL_PWREx_EnableBatteryCharging(uint32_t ResistorSelection)
  247. {
  248. assert_param(IS_PWR_BATTERY_RESISTOR_SELECT(ResistorSelection));
  249. /* Specify resistor selection */
  250. MODIFY_REG(PWR->CR4, PWR_CR4_VBRS, ResistorSelection);
  251. /* Enable battery charging */
  252. SET_BIT(PWR->CR4, PWR_CR4_VBE);
  253. }
  254. /**
  255. * @brief Disable battery charging.
  256. * @retval None
  257. */
  258. void HAL_PWREx_DisableBatteryCharging(void)
  259. {
  260. CLEAR_BIT(PWR->CR4, PWR_CR4_VBE);
  261. }
  262. #if defined(PWR_CR2_USV)
  263. /**
  264. * @brief Enable VDDUSB supply.
  265. * @note Remove VDDUSB electrical and logical isolation, once VDDUSB supply is present.
  266. * @retval None
  267. */
  268. void HAL_PWREx_EnableVddUSB(void)
  269. {
  270. SET_BIT(PWR->CR2, PWR_CR2_USV);
  271. }
  272. /**
  273. * @brief Disable VDDUSB supply.
  274. * @retval None
  275. */
  276. void HAL_PWREx_DisableVddUSB(void)
  277. {
  278. CLEAR_BIT(PWR->CR2, PWR_CR2_USV);
  279. }
  280. #endif /* PWR_CR2_USV */
  281. #if defined(PWR_CR2_IOSV)
  282. /**
  283. * @brief Enable VDDIO2 supply.
  284. * @note Remove VDDIO2 electrical and logical isolation, once VDDIO2 supply is present.
  285. * @retval None
  286. */
  287. void HAL_PWREx_EnableVddIO2(void)
  288. {
  289. SET_BIT(PWR->CR2, PWR_CR2_IOSV);
  290. }
  291. /**
  292. * @brief Disable VDDIO2 supply.
  293. * @retval None
  294. */
  295. void HAL_PWREx_DisableVddIO2(void)
  296. {
  297. CLEAR_BIT(PWR->CR2, PWR_CR2_IOSV);
  298. }
  299. #endif /* PWR_CR2_IOSV */
  300. /**
  301. * @brief Enable Internal Wake-up Line.
  302. * @retval None
  303. */
  304. void HAL_PWREx_EnableInternalWakeUpLine(void)
  305. {
  306. SET_BIT(PWR->CR3, PWR_CR3_EIWF);
  307. }
  308. /**
  309. * @brief Disable Internal Wake-up Line.
  310. * @retval None
  311. */
  312. void HAL_PWREx_DisableInternalWakeUpLine(void)
  313. {
  314. CLEAR_BIT(PWR->CR3, PWR_CR3_EIWF);
  315. }
  316. /**
  317. * @brief Enable GPIO pull-up state in Standby and Shutdown modes.
  318. * @note Set the relevant PUy bits of PWR_PUCRx register to configure the I/O in
  319. * pull-up state in Standby and Shutdown modes.
  320. * @note This state is effective in Standby and Shutdown modes only if APC bit
  321. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  322. * @note The configuration is lost when exiting the Shutdown mode due to the
  323. * power-on reset, maintained when exiting the Standby mode.
  324. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  325. * PDy bit of PWR_PDCRx register is cleared unless it is reserved.
  326. * @note Even if a PUy bit to set is reserved, the other PUy bits entered as input
  327. * parameter at the same time are set.
  328. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  329. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  330. * @param GPIONumber Specify the I/O pins numbers.
  331. * This parameter can be one of the following values:
  332. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  333. * I/O pins are available) or the logical OR of several of them to set
  334. * several bits for a given port in a single API call.
  335. * @retval HAL Status
  336. */
  337. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  338. {
  339. HAL_StatusTypeDef status = HAL_OK;
  340. assert_param(IS_PWR_GPIO(GPIO));
  341. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  342. switch (GPIO)
  343. {
  344. case PWR_GPIO_A:
  345. SET_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  346. CLEAR_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  347. break;
  348. case PWR_GPIO_B:
  349. SET_BIT(PWR->PUCRB, GPIONumber);
  350. CLEAR_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  351. break;
  352. case PWR_GPIO_C:
  353. SET_BIT(PWR->PUCRC, GPIONumber);
  354. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  355. break;
  356. #if defined(GPIOD)
  357. case PWR_GPIO_D:
  358. SET_BIT(PWR->PUCRD, GPIONumber);
  359. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  360. break;
  361. #endif
  362. #if defined(GPIOE)
  363. case PWR_GPIO_E:
  364. SET_BIT(PWR->PUCRE, GPIONumber);
  365. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  366. break;
  367. #endif
  368. #if defined(GPIOF)
  369. case PWR_GPIO_F:
  370. SET_BIT(PWR->PUCRF, GPIONumber);
  371. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  372. break;
  373. #endif
  374. #if defined(GPIOG)
  375. case PWR_GPIO_G:
  376. SET_BIT(PWR->PUCRG, GPIONumber);
  377. CLEAR_BIT(PWR->PDCRG, GPIONumber);
  378. break;
  379. #endif
  380. case PWR_GPIO_H:
  381. SET_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  382. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  383. CLEAR_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  384. #else
  385. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  386. #endif
  387. break;
  388. #if defined(GPIOI)
  389. case PWR_GPIO_I:
  390. SET_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  391. CLEAR_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  392. break;
  393. #endif
  394. default:
  395. status = HAL_ERROR;
  396. break;
  397. }
  398. return status;
  399. }
  400. /**
  401. * @brief Disable GPIO pull-up state in Standby mode and Shutdown modes.
  402. * @note Reset the relevant PUy bits of PWR_PUCRx register used to configure the I/O
  403. * in pull-up state in Standby and Shutdown modes.
  404. * @note Even if a PUy bit to reset is reserved, the other PUy bits entered as input
  405. * parameter at the same time are reset.
  406. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A, ..., PWR_GPIO_H
  407. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  408. * @param GPIONumber Specify the I/O pins numbers.
  409. * This parameter can be one of the following values:
  410. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  411. * I/O pins are available) or the logical OR of several of them to reset
  412. * several bits for a given port in a single API call.
  413. * @retval HAL Status
  414. */
  415. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullUp(uint32_t GPIO, uint32_t GPIONumber)
  416. {
  417. HAL_StatusTypeDef status = HAL_OK;
  418. assert_param(IS_PWR_GPIO(GPIO));
  419. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  420. switch (GPIO)
  421. {
  422. case PWR_GPIO_A:
  423. CLEAR_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  424. break;
  425. case PWR_GPIO_B:
  426. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  427. break;
  428. case PWR_GPIO_C:
  429. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  430. break;
  431. #if defined(GPIOD)
  432. case PWR_GPIO_D:
  433. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  434. break;
  435. #endif
  436. #if defined(GPIOE)
  437. case PWR_GPIO_E:
  438. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  439. break;
  440. #endif
  441. #if defined(GPIOF)
  442. case PWR_GPIO_F:
  443. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  444. break;
  445. #endif
  446. #if defined(GPIOG)
  447. case PWR_GPIO_G:
  448. CLEAR_BIT(PWR->PUCRG, GPIONumber);
  449. break;
  450. #endif
  451. case PWR_GPIO_H:
  452. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  453. break;
  454. #if defined(GPIOI)
  455. case PWR_GPIO_I:
  456. CLEAR_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  457. break;
  458. #endif
  459. default:
  460. status = HAL_ERROR;
  461. break;
  462. }
  463. return status;
  464. }
  465. /**
  466. * @brief Enable GPIO pull-down state in Standby and Shutdown modes.
  467. * @note Set the relevant PDy bits of PWR_PDCRx register to configure the I/O in
  468. * pull-down state in Standby and Shutdown modes.
  469. * @note This state is effective in Standby and Shutdown modes only if APC bit
  470. * is set through HAL_PWREx_EnablePullUpPullDownConfig() API.
  471. * @note The configuration is lost when exiting the Shutdown mode due to the
  472. * power-on reset, maintained when exiting the Standby mode.
  473. * @note To avoid any conflict at Standby and Shutdown modes exits, the corresponding
  474. * PUy bit of PWR_PUCRx register is cleared unless it is reserved.
  475. * @note Even if a PDy bit to set is reserved, the other PDy bits entered as input
  476. * parameter at the same time are set.
  477. * @param GPIO Specify the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  478. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  479. * @param GPIONumber Specify the I/O pins numbers.
  480. * This parameter can be one of the following values:
  481. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  482. * I/O pins are available) or the logical OR of several of them to set
  483. * several bits for a given port in a single API call.
  484. * @retval HAL Status
  485. */
  486. HAL_StatusTypeDef HAL_PWREx_EnableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  487. {
  488. HAL_StatusTypeDef status = HAL_OK;
  489. assert_param(IS_PWR_GPIO(GPIO));
  490. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  491. switch (GPIO)
  492. {
  493. case PWR_GPIO_A:
  494. SET_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  495. CLEAR_BIT(PWR->PUCRA, (GPIONumber & (~(PWR_GPIO_BIT_14))));
  496. break;
  497. case PWR_GPIO_B:
  498. SET_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  499. CLEAR_BIT(PWR->PUCRB, GPIONumber);
  500. break;
  501. case PWR_GPIO_C:
  502. SET_BIT(PWR->PDCRC, GPIONumber);
  503. CLEAR_BIT(PWR->PUCRC, GPIONumber);
  504. break;
  505. #if defined(GPIOD)
  506. case PWR_GPIO_D:
  507. SET_BIT(PWR->PDCRD, GPIONumber);
  508. CLEAR_BIT(PWR->PUCRD, GPIONumber);
  509. break;
  510. #endif
  511. #if defined(GPIOE)
  512. case PWR_GPIO_E:
  513. SET_BIT(PWR->PDCRE, GPIONumber);
  514. CLEAR_BIT(PWR->PUCRE, GPIONumber);
  515. break;
  516. #endif
  517. #if defined(GPIOF)
  518. case PWR_GPIO_F:
  519. SET_BIT(PWR->PDCRF, GPIONumber);
  520. CLEAR_BIT(PWR->PUCRF, GPIONumber);
  521. break;
  522. #endif
  523. #if defined(GPIOG)
  524. case PWR_GPIO_G:
  525. SET_BIT(PWR->PDCRG, GPIONumber);
  526. CLEAR_BIT(PWR->PUCRG, GPIONumber);
  527. break;
  528. #endif
  529. case PWR_GPIO_H:
  530. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  531. SET_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  532. #else
  533. SET_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  534. #endif
  535. CLEAR_BIT(PWR->PUCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  536. break;
  537. #if defined(GPIOI)
  538. case PWR_GPIO_I:
  539. SET_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  540. CLEAR_BIT(PWR->PUCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  541. break;
  542. #endif
  543. default:
  544. status = HAL_ERROR;
  545. break;
  546. }
  547. return status;
  548. }
  549. /**
  550. * @brief Disable GPIO pull-down state in Standby and Shutdown modes.
  551. * @note Reset the relevant PDy bits of PWR_PDCRx register used to configure the I/O
  552. * in pull-down state in Standby and Shutdown modes.
  553. * @note Even if a PDy bit to reset is reserved, the other PDy bits entered as input
  554. * parameter at the same time are reset.
  555. * @param GPIO Specifies the IO port. This parameter can be PWR_GPIO_A..PWR_GPIO_H
  556. * (or PWR_GPIO_I depending on the devices) to select the GPIO peripheral.
  557. * @param GPIONumber Specify the I/O pins numbers.
  558. * This parameter can be one of the following values:
  559. * PWR_GPIO_BIT_0, ..., PWR_GPIO_BIT_15 (except for the port where less
  560. * I/O pins are available) or the logical OR of several of them to reset
  561. * several bits for a given port in a single API call.
  562. * @retval HAL Status
  563. */
  564. HAL_StatusTypeDef HAL_PWREx_DisableGPIOPullDown(uint32_t GPIO, uint32_t GPIONumber)
  565. {
  566. HAL_StatusTypeDef status = HAL_OK;
  567. assert_param(IS_PWR_GPIO(GPIO));
  568. assert_param(IS_PWR_GPIO_BIT_NUMBER(GPIONumber));
  569. switch (GPIO)
  570. {
  571. case PWR_GPIO_A:
  572. CLEAR_BIT(PWR->PDCRA, (GPIONumber & (~(PWR_GPIO_BIT_13|PWR_GPIO_BIT_15))));
  573. break;
  574. case PWR_GPIO_B:
  575. CLEAR_BIT(PWR->PDCRB, (GPIONumber & (~(PWR_GPIO_BIT_4))));
  576. break;
  577. case PWR_GPIO_C:
  578. CLEAR_BIT(PWR->PDCRC, GPIONumber);
  579. break;
  580. #if defined(GPIOD)
  581. case PWR_GPIO_D:
  582. CLEAR_BIT(PWR->PDCRD, GPIONumber);
  583. break;
  584. #endif
  585. #if defined(GPIOE)
  586. case PWR_GPIO_E:
  587. CLEAR_BIT(PWR->PDCRE, GPIONumber);
  588. break;
  589. #endif
  590. #if defined(GPIOF)
  591. case PWR_GPIO_F:
  592. CLEAR_BIT(PWR->PDCRF, GPIONumber);
  593. break;
  594. #endif
  595. #if defined(GPIOG)
  596. case PWR_GPIO_G:
  597. CLEAR_BIT(PWR->PDCRG, GPIONumber);
  598. break;
  599. #endif
  600. case PWR_GPIO_H:
  601. #if defined (STM32L496xx) || defined (STM32L4A6xx)
  602. CLEAR_BIT(PWR->PDCRH, ((GPIONumber & PWR_PORTH_AVAILABLE_PINS) & (~(PWR_GPIO_BIT_3))));
  603. #else
  604. CLEAR_BIT(PWR->PDCRH, (GPIONumber & PWR_PORTH_AVAILABLE_PINS));
  605. #endif
  606. break;
  607. #if defined(GPIOI)
  608. case PWR_GPIO_I:
  609. CLEAR_BIT(PWR->PDCRI, (GPIONumber & PWR_PORTI_AVAILABLE_PINS));
  610. break;
  611. #endif
  612. default:
  613. status = HAL_ERROR;
  614. break;
  615. }
  616. return status;
  617. }
  618. /**
  619. * @brief Enable pull-up and pull-down configuration.
  620. * @note When APC bit is set, the I/O pull-up and pull-down configurations defined in
  621. * PWR_PUCRx and PWR_PDCRx registers are applied in Standby and Shutdown modes.
  622. * @note Pull-up set by PUy bit of PWR_PUCRx register is not activated if the corresponding
  623. * PDy bit of PWR_PDCRx register is also set (pull-down configuration priority is higher).
  624. * HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() API's ensure there
  625. * is no conflict when setting PUy or PDy bit.
  626. * @retval None
  627. */
  628. void HAL_PWREx_EnablePullUpPullDownConfig(void)
  629. {
  630. SET_BIT(PWR->CR3, PWR_CR3_APC);
  631. }
  632. /**
  633. * @brief Disable pull-up and pull-down configuration.
  634. * @note When APC bit is cleared, the I/O pull-up and pull-down configurations defined in
  635. * PWR_PUCRx and PWR_PDCRx registers are not applied in Standby and Shutdown modes.
  636. * @retval None
  637. */
  638. void HAL_PWREx_DisablePullUpPullDownConfig(void)
  639. {
  640. CLEAR_BIT(PWR->CR3, PWR_CR3_APC);
  641. }
  642. /**
  643. * @brief Enable Full SRAM2 content retention in Standby mode.
  644. * @retval None
  645. */
  646. void HAL_PWREx_EnableSRAM2ContentRetention(void)
  647. {
  648. (void) HAL_PWREx_SetSRAM2ContentRetention(PWR_FULL_SRAM2_RETENTION);
  649. }
  650. /**
  651. * @brief Disable SRAM2 content retention in Standby mode.
  652. * @retval None
  653. */
  654. void HAL_PWREx_DisableSRAM2ContentRetention(void)
  655. {
  656. (void) HAL_PWREx_SetSRAM2ContentRetention(PWR_NO_SRAM2_RETENTION);
  657. }
  658. /**
  659. * @brief Enable SRAM2 content retention in Standby mode.
  660. * @param SRAM2Size: specifies the SRAM2 size kept in Standby mode
  661. * This parameter can be one of the following values:
  662. * @arg @ref PWR_NO_SRAM2_RETENTION SRAM2 is powered off in Standby mode (SRAM2 content is lost)
  663. * @arg @ref PWR_FULL_SRAM2_RETENTION Full SRAM2 is powered by the low-power regulator in Standby mode
  664. * @arg @ref PWR_4KBYTES_SRAM2_RETENTION Only 4 Kbytes of SRAM2 is powered by the low-power regulator in Standby mode
  665. * @note PWR_4KBYTES_SRAM2_RETENTION parameter is not available on all devices
  666. * @retval HAL Status
  667. */
  668. HAL_StatusTypeDef HAL_PWREx_SetSRAM2ContentRetention(uint32_t SRAM2Size)
  669. {
  670. assert_param(IS_PWR_SRAM2_RETENTION(SRAM2Size));
  671. if (SRAM2Size == PWR_NO_SRAM2_RETENTION)
  672. {
  673. CLEAR_BIT(PWR->CR3, PWR_CR3_RRS);
  674. }
  675. else if (SRAM2Size == PWR_FULL_SRAM2_RETENTION)
  676. {
  677. MODIFY_REG(PWR->CR3, PWR_CR3_RRS, PWR_FULL_SRAM2_RETENTION);
  678. }
  679. #if defined(PWR_CR3_RRS_1)
  680. else if (SRAM2Size == PWR_4KBYTES_SRAM2_RETENTION)
  681. {
  682. MODIFY_REG(PWR->CR3, PWR_CR3_RRS, PWR_4KBYTES_SRAM2_RETENTION);
  683. }
  684. #endif /* PWR_CR3_RRS_1 */
  685. else {
  686. return HAL_ERROR;
  687. }
  688. return HAL_OK;
  689. }
  690. #if defined(PWR_CR3_ENULP)
  691. /**
  692. * @brief Enable Ultra Low Power BORL, BORH and PVD for STOP2 and Standby modes.
  693. * @note All the other modes are not affected by this bit.
  694. * @retval None
  695. */
  696. void HAL_PWREx_EnableBORPVD_ULP(void)
  697. {
  698. SET_BIT(PWR->CR3, PWR_CR3_ENULP);
  699. }
  700. /**
  701. * @brief Disable Ultra Low Power BORL, BORH and PVD for STOP2 and Standby modes.
  702. * @note All the other modes are not affected by this bit
  703. * @retval None
  704. */
  705. void HAL_PWREx_DisableBORPVD_ULP(void)
  706. {
  707. CLEAR_BIT(PWR->CR3, PWR_CR3_ENULP);
  708. }
  709. #endif /* PWR_CR3_ENULP */
  710. #if defined(PWR_CR4_EXT_SMPS_ON)
  711. /**
  712. * @brief Enable the CFLDO working @ 0.95V.
  713. * @note When external SMPS is used & CFLDO operating in Range 2, the regulated voltage of the
  714. * internal CFLDO can be reduced to 0.95V.
  715. * @retval None
  716. */
  717. void HAL_PWREx_EnableExtSMPS_0V95(void)
  718. {
  719. SET_BIT(PWR->CR4, PWR_CR4_EXT_SMPS_ON);
  720. }
  721. /**
  722. * @brief Disable the CFLDO working @ 0.95V
  723. * @note Before SMPS is switched off, the regulated voltage of the
  724. * internal CFLDO shall be set to 1.00V.
  725. * 1.00V. is also default operating Range 2 voltage.
  726. * @retval None
  727. */
  728. void HAL_PWREx_DisableExtSMPS_0V95(void)
  729. {
  730. CLEAR_BIT(PWR->CR4, PWR_CR4_EXT_SMPS_ON);
  731. }
  732. #endif /* PWR_CR4_EXT_SMPS_ON */
  733. #if defined(PWR_CR1_RRSTP)
  734. /**
  735. * @brief Enable SRAM3 content retention in Stop 2 mode.
  736. * @note When RRSTP bit is set, SRAM3 is powered by the low-power regulator in
  737. * Stop 2 mode and its content is kept.
  738. * @retval None
  739. */
  740. void HAL_PWREx_EnableSRAM3ContentRetention(void)
  741. {
  742. SET_BIT(PWR->CR1, PWR_CR1_RRSTP);
  743. }
  744. /**
  745. * @brief Disable SRAM3 content retention in Stop 2 mode.
  746. * @note When RRSTP bit is reset, SRAM3 is powered off in Stop 2 mode
  747. * and its content is lost.
  748. * @retval None
  749. */
  750. void HAL_PWREx_DisableSRAM3ContentRetention(void)
  751. {
  752. CLEAR_BIT(PWR->CR1, PWR_CR1_RRSTP);
  753. }
  754. #endif /* PWR_CR1_RRSTP */
  755. #if defined(PWR_CR3_DSIPDEN)
  756. /**
  757. * @brief Enable pull-down activation on DSI pins.
  758. * @retval None
  759. */
  760. void HAL_PWREx_EnableDSIPinsPDActivation(void)
  761. {
  762. SET_BIT(PWR->CR3, PWR_CR3_DSIPDEN);
  763. }
  764. /**
  765. * @brief Disable pull-down activation on DSI pins.
  766. * @retval None
  767. */
  768. void HAL_PWREx_DisableDSIPinsPDActivation(void)
  769. {
  770. CLEAR_BIT(PWR->CR3, PWR_CR3_DSIPDEN);
  771. }
  772. #endif /* PWR_CR3_DSIPDEN */
  773. #if defined(PWR_CR2_PVME1)
  774. /**
  775. * @brief Enable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  776. * @retval None
  777. */
  778. void HAL_PWREx_EnablePVM1(void)
  779. {
  780. SET_BIT(PWR->CR2, PWR_PVM_1);
  781. }
  782. /**
  783. * @brief Disable the Power Voltage Monitoring 1: VDDUSB versus 1.2V.
  784. * @retval None
  785. */
  786. void HAL_PWREx_DisablePVM1(void)
  787. {
  788. CLEAR_BIT(PWR->CR2, PWR_PVM_1);
  789. }
  790. #endif /* PWR_CR2_PVME1 */
  791. #if defined(PWR_CR2_PVME2)
  792. /**
  793. * @brief Enable the Power Voltage Monitoring 2: VDDIO2 versus 0.9V.
  794. * @retval None
  795. */
  796. void HAL_PWREx_EnablePVM2(void)
  797. {
  798. SET_BIT(PWR->CR2, PWR_PVM_2);
  799. }
  800. /**
  801. * @brief Disable the Power Voltage Monitoring 2: VDDIO2 versus 0.9V.
  802. * @retval None
  803. */
  804. void HAL_PWREx_DisablePVM2(void)
  805. {
  806. CLEAR_BIT(PWR->CR2, PWR_PVM_2);
  807. }
  808. #endif /* PWR_CR2_PVME2 */
  809. /**
  810. * @brief Enable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  811. * @retval None
  812. */
  813. void HAL_PWREx_EnablePVM3(void)
  814. {
  815. SET_BIT(PWR->CR2, PWR_PVM_3);
  816. }
  817. /**
  818. * @brief Disable the Power Voltage Monitoring 3: VDDA versus 1.62V.
  819. * @retval None
  820. */
  821. void HAL_PWREx_DisablePVM3(void)
  822. {
  823. CLEAR_BIT(PWR->CR2, PWR_PVM_3);
  824. }
  825. /**
  826. * @brief Enable the Power Voltage Monitoring 4: VDDA versus 2.2V.
  827. * @retval None
  828. */
  829. void HAL_PWREx_EnablePVM4(void)
  830. {
  831. SET_BIT(PWR->CR2, PWR_PVM_4);
  832. }
  833. /**
  834. * @brief Disable the Power Voltage Monitoring 4: VDDA versus 2.2V.
  835. * @retval None
  836. */
  837. void HAL_PWREx_DisablePVM4(void)
  838. {
  839. CLEAR_BIT(PWR->CR2, PWR_PVM_4);
  840. }
  841. /**
  842. * @brief Configure the Peripheral Voltage Monitoring (PVM).
  843. * @param sConfigPVM: pointer to a PWR_PVMTypeDef structure that contains the
  844. * PVM configuration information.
  845. * @note The API configures a single PVM according to the information contained
  846. * in the input structure. To configure several PVMs, the API must be singly
  847. * called for each PVM used.
  848. * @note Refer to the electrical characteristics of your device datasheet for
  849. * more details about the voltage thresholds corresponding to each
  850. * detection level and to each monitored supply.
  851. * @retval HAL status
  852. */
  853. HAL_StatusTypeDef HAL_PWREx_ConfigPVM(PWR_PVMTypeDef *sConfigPVM)
  854. {
  855. HAL_StatusTypeDef status = HAL_OK;
  856. /* Check the parameters */
  857. assert_param(IS_PWR_PVM_TYPE(sConfigPVM->PVMType));
  858. assert_param(IS_PWR_PVM_MODE(sConfigPVM->Mode));
  859. /* Configure EXTI 35 to 38 interrupts if so required:
  860. scan thru PVMType to detect which PVMx is set and
  861. configure the corresponding EXTI line accordingly. */
  862. switch (sConfigPVM->PVMType)
  863. {
  864. #if defined(PWR_CR2_PVME1)
  865. case PWR_PVM_1:
  866. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  867. __HAL_PWR_PVM1_EXTI_DISABLE_EVENT();
  868. __HAL_PWR_PVM1_EXTI_DISABLE_IT();
  869. __HAL_PWR_PVM1_EXTI_DISABLE_FALLING_EDGE();
  870. __HAL_PWR_PVM1_EXTI_DISABLE_RISING_EDGE();
  871. /* Configure interrupt mode */
  872. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  873. {
  874. __HAL_PWR_PVM1_EXTI_ENABLE_IT();
  875. }
  876. /* Configure event mode */
  877. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  878. {
  879. __HAL_PWR_PVM1_EXTI_ENABLE_EVENT();
  880. }
  881. /* Configure the edge */
  882. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  883. {
  884. __HAL_PWR_PVM1_EXTI_ENABLE_RISING_EDGE();
  885. }
  886. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  887. {
  888. __HAL_PWR_PVM1_EXTI_ENABLE_FALLING_EDGE();
  889. }
  890. break;
  891. #endif /* PWR_CR2_PVME1 */
  892. #if defined(PWR_CR2_PVME2)
  893. case PWR_PVM_2:
  894. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  895. __HAL_PWR_PVM2_EXTI_DISABLE_EVENT();
  896. __HAL_PWR_PVM2_EXTI_DISABLE_IT();
  897. __HAL_PWR_PVM2_EXTI_DISABLE_FALLING_EDGE();
  898. __HAL_PWR_PVM2_EXTI_DISABLE_RISING_EDGE();
  899. /* Configure interrupt mode */
  900. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  901. {
  902. __HAL_PWR_PVM2_EXTI_ENABLE_IT();
  903. }
  904. /* Configure event mode */
  905. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  906. {
  907. __HAL_PWR_PVM2_EXTI_ENABLE_EVENT();
  908. }
  909. /* Configure the edge */
  910. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  911. {
  912. __HAL_PWR_PVM2_EXTI_ENABLE_RISING_EDGE();
  913. }
  914. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  915. {
  916. __HAL_PWR_PVM2_EXTI_ENABLE_FALLING_EDGE();
  917. }
  918. break;
  919. #endif /* PWR_CR2_PVME2 */
  920. case PWR_PVM_3:
  921. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  922. __HAL_PWR_PVM3_EXTI_DISABLE_EVENT();
  923. __HAL_PWR_PVM3_EXTI_DISABLE_IT();
  924. __HAL_PWR_PVM3_EXTI_DISABLE_FALLING_EDGE();
  925. __HAL_PWR_PVM3_EXTI_DISABLE_RISING_EDGE();
  926. /* Configure interrupt mode */
  927. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  928. {
  929. __HAL_PWR_PVM3_EXTI_ENABLE_IT();
  930. }
  931. /* Configure event mode */
  932. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  933. {
  934. __HAL_PWR_PVM3_EXTI_ENABLE_EVENT();
  935. }
  936. /* Configure the edge */
  937. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  938. {
  939. __HAL_PWR_PVM3_EXTI_ENABLE_RISING_EDGE();
  940. }
  941. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  942. {
  943. __HAL_PWR_PVM3_EXTI_ENABLE_FALLING_EDGE();
  944. }
  945. break;
  946. case PWR_PVM_4:
  947. /* Clear any previous config. Keep it clear if no event or IT mode is selected */
  948. __HAL_PWR_PVM4_EXTI_DISABLE_EVENT();
  949. __HAL_PWR_PVM4_EXTI_DISABLE_IT();
  950. __HAL_PWR_PVM4_EXTI_DISABLE_FALLING_EDGE();
  951. __HAL_PWR_PVM4_EXTI_DISABLE_RISING_EDGE();
  952. /* Configure interrupt mode */
  953. if((sConfigPVM->Mode & PVM_MODE_IT) == PVM_MODE_IT)
  954. {
  955. __HAL_PWR_PVM4_EXTI_ENABLE_IT();
  956. }
  957. /* Configure event mode */
  958. if((sConfigPVM->Mode & PVM_MODE_EVT) == PVM_MODE_EVT)
  959. {
  960. __HAL_PWR_PVM4_EXTI_ENABLE_EVENT();
  961. }
  962. /* Configure the edge */
  963. if((sConfigPVM->Mode & PVM_RISING_EDGE) == PVM_RISING_EDGE)
  964. {
  965. __HAL_PWR_PVM4_EXTI_ENABLE_RISING_EDGE();
  966. }
  967. if((sConfigPVM->Mode & PVM_FALLING_EDGE) == PVM_FALLING_EDGE)
  968. {
  969. __HAL_PWR_PVM4_EXTI_ENABLE_FALLING_EDGE();
  970. }
  971. break;
  972. default:
  973. status = HAL_ERROR;
  974. break;
  975. }
  976. return status;
  977. }
  978. /**
  979. * @brief Enter Low-power Run mode
  980. * @note In Low-power Run mode, all I/O pins keep the same state as in Run mode.
  981. * @note When Regulator is set to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  982. * Flash in power-down monde in setting the RUN_PD bit in FLASH_ACR register.
  983. * Additionally, the clock frequency must be reduced below 2 MHz.
  984. * Setting RUN_PD in FLASH_ACR then appropriately reducing the clock frequency must
  985. * be done before calling HAL_PWREx_EnableLowPowerRunMode() API.
  986. * @retval None
  987. */
  988. void HAL_PWREx_EnableLowPowerRunMode(void)
  989. {
  990. /* Set Regulator parameter */
  991. SET_BIT(PWR->CR1, PWR_CR1_LPR);
  992. }
  993. /**
  994. * @brief Exit Low-power Run mode.
  995. * @note Before HAL_PWREx_DisableLowPowerRunMode() completion, the function checks that
  996. * REGLPF has been properly reset (otherwise, HAL_PWREx_DisableLowPowerRunMode
  997. * returns HAL_TIMEOUT status). The system clock frequency can then be
  998. * increased above 2 MHz.
  999. * @retval HAL Status
  1000. */
  1001. HAL_StatusTypeDef HAL_PWREx_DisableLowPowerRunMode(void)
  1002. {
  1003. uint32_t wait_loop_index;
  1004. /* Clear LPR bit */
  1005. CLEAR_BIT(PWR->CR1, PWR_CR1_LPR);
  1006. /* Wait until REGLPF is reset */
  1007. wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
  1008. while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF)) && (wait_loop_index != 0U))
  1009. {
  1010. wait_loop_index--;
  1011. }
  1012. if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
  1013. {
  1014. return HAL_TIMEOUT;
  1015. }
  1016. return HAL_OK;
  1017. }
  1018. /**
  1019. * @brief Enter Stop 0 mode.
  1020. * @note In Stop 0 mode, main and low voltage regulators are ON.
  1021. * @note In Stop 0 mode, all I/O pins keep the same state as in Run mode.
  1022. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  1023. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  1024. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  1025. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  1026. * only to the peripheral requesting it.
  1027. * SRAM1, SRAM2 and register contents are preserved.
  1028. * The BOR is available.
  1029. * @note When exiting Stop 0 mode by issuing an interrupt or a wakeup event,
  1030. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1031. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1032. * @note By keeping the internal regulator ON during Stop 0 mode, the consumption
  1033. * is higher although the startup time is reduced.
  1034. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1035. * This parameter can be one of the following values:
  1036. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1037. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1038. * @retval None
  1039. */
  1040. void HAL_PWREx_EnterSTOP0Mode(uint8_t STOPEntry)
  1041. {
  1042. /* Check the parameters */
  1043. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1044. /* Stop 0 mode with Main Regulator */
  1045. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP0);
  1046. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1047. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1048. /* Select Stop mode entry --------------------------------------------------*/
  1049. if(STOPEntry == PWR_STOPENTRY_WFI)
  1050. {
  1051. /* Request Wait For Interrupt */
  1052. __WFI();
  1053. }
  1054. else
  1055. {
  1056. /* Request Wait For Event */
  1057. __SEV();
  1058. __WFE();
  1059. __WFE();
  1060. }
  1061. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1062. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1063. }
  1064. /**
  1065. * @brief Enter Stop 1 mode.
  1066. * @note In Stop 1 mode, only low power voltage regulator is ON.
  1067. * @note In Stop 1 mode, all I/O pins keep the same state as in Run mode.
  1068. * @note All clocks in the VCORE domain are stopped; the PLL, the MSI,
  1069. * the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  1070. * (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  1071. * after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  1072. * only to the peripheral requesting it.
  1073. * SRAM1, SRAM2 and register contents are preserved.
  1074. * The BOR is available.
  1075. * @note When exiting Stop 1 mode by issuing an interrupt or a wakeup event,
  1076. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1077. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1078. * @note Due to low power mode, an additional startup delay is incurred when waking up from Stop 1 mode.
  1079. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1080. * This parameter can be one of the following values:
  1081. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1082. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1083. * @retval None
  1084. */
  1085. void HAL_PWREx_EnterSTOP1Mode(uint8_t STOPEntry)
  1086. {
  1087. /* Check the parameters */
  1088. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1089. /* Stop 1 mode with Low-Power Regulator */
  1090. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP1);
  1091. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1092. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1093. /* Select Stop mode entry --------------------------------------------------*/
  1094. if(STOPEntry == PWR_STOPENTRY_WFI)
  1095. {
  1096. /* Request Wait For Interrupt */
  1097. __WFI();
  1098. }
  1099. else
  1100. {
  1101. /* Request Wait For Event */
  1102. __SEV();
  1103. __WFE();
  1104. __WFE();
  1105. }
  1106. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1107. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1108. }
  1109. /**
  1110. * @brief Enter Stop 2 mode.
  1111. * @note In Stop 2 mode, only low power voltage regulator is ON.
  1112. * @note In Stop 2 mode, all I/O pins keep the same state as in Run mode.
  1113. * @note All clocks in the VCORE domain are stopped, the PLL, the MSI,
  1114. * the HSI and the HSE oscillators are disabled. Some peripherals with wakeup capability
  1115. * (LCD, LPTIM1, I2C3 and LPUART) can switch on the HSI to receive a frame, and switch off the HSI after
  1116. * receiving the frame if it is not a wakeup frame. In this case the HSI clock is propagated only
  1117. * to the peripheral requesting it.
  1118. * SRAM1, SRAM2 and register contents are preserved.
  1119. * SRAM3 content is preserved depending on RRSTP bit setting (not available on all devices).
  1120. * The BOR is available.
  1121. * The voltage regulator is set in low-power mode but LPR bit must be cleared to enter stop 2 mode.
  1122. * Otherwise, Stop 1 mode is entered.
  1123. * @note When exiting Stop 2 mode by issuing an interrupt or a wakeup event,
  1124. * the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  1125. * is set; the MSI oscillator is selected if STOPWUCK is cleared.
  1126. * @param STOPEntry specifies if Stop mode in entered with WFI or WFE instruction.
  1127. * This parameter can be one of the following values:
  1128. * @arg @ref PWR_STOPENTRY_WFI Enter Stop mode with WFI instruction
  1129. * @arg @ref PWR_STOPENTRY_WFE Enter Stop mode with WFE instruction
  1130. * @retval None
  1131. */
  1132. void HAL_PWREx_EnterSTOP2Mode(uint8_t STOPEntry)
  1133. {
  1134. /* Check the parameter */
  1135. assert_param(IS_PWR_STOP_ENTRY(STOPEntry));
  1136. /* Set Stop mode 2 */
  1137. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP2);
  1138. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1139. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1140. /* Select Stop mode entry --------------------------------------------------*/
  1141. if(STOPEntry == PWR_STOPENTRY_WFI)
  1142. {
  1143. /* Request Wait For Interrupt */
  1144. __WFI();
  1145. }
  1146. else
  1147. {
  1148. /* Request Wait For Event */
  1149. __SEV();
  1150. __WFE();
  1151. __WFE();
  1152. }
  1153. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  1154. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1155. }
  1156. /**
  1157. * @brief Enter Shutdown mode.
  1158. * @note In Shutdown mode, the PLL, the HSI, the MSI, the LSI and the HSE oscillators are switched
  1159. * off. The voltage regulator is disabled and Vcore domain is powered off.
  1160. * SRAM1, SRAM2 and registers contents are lost except for registers in the Backup domain.
  1161. * The BOR is not available.
  1162. * @note The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  1163. * @retval None
  1164. */
  1165. void HAL_PWREx_EnterSHUTDOWNMode(void)
  1166. {
  1167. /* Set Shutdown mode */
  1168. MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_SHUTDOWN);
  1169. /* Set SLEEPDEEP bit of Cortex System Control Register */
  1170. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  1171. /* This option is used to ensure that store operations are completed */
  1172. #if defined ( __CC_ARM)
  1173. __force_stores();
  1174. #endif
  1175. /* Request Wait For Interrupt */
  1176. __WFI();
  1177. }
  1178. /**
  1179. * @brief This function handles the PWR PVD/PVMx interrupt request.
  1180. * @note This API should be called under the PVD_PVM_IRQHandler().
  1181. * @retval None
  1182. */
  1183. void HAL_PWREx_PVD_PVM_IRQHandler(void)
  1184. {
  1185. /* Check PWR exti flag */
  1186. if(__HAL_PWR_PVD_EXTI_GET_FLAG() != 0x0U)
  1187. {
  1188. /* PWR PVD interrupt user callback */
  1189. HAL_PWR_PVDCallback();
  1190. /* Clear PVD exti pending bit */
  1191. __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
  1192. }
  1193. /* Next, successively check PVMx exti flags */
  1194. #if defined(PWR_CR2_PVME1)
  1195. if(__HAL_PWR_PVM1_EXTI_GET_FLAG() != 0x0U)
  1196. {
  1197. /* PWR PVM1 interrupt user callback */
  1198. HAL_PWREx_PVM1Callback();
  1199. /* Clear PVM1 exti pending bit */
  1200. __HAL_PWR_PVM1_EXTI_CLEAR_FLAG();
  1201. }
  1202. #endif /* PWR_CR2_PVME1 */
  1203. #if defined(PWR_CR2_PVME2)
  1204. if(__HAL_PWR_PVM2_EXTI_GET_FLAG() != 0x0U)
  1205. {
  1206. /* PWR PVM2 interrupt user callback */
  1207. HAL_PWREx_PVM2Callback();
  1208. /* Clear PVM2 exti pending bit */
  1209. __HAL_PWR_PVM2_EXTI_CLEAR_FLAG();
  1210. }
  1211. #endif /* PWR_CR2_PVME2 */
  1212. if(__HAL_PWR_PVM3_EXTI_GET_FLAG() != 0x0U)
  1213. {
  1214. /* PWR PVM3 interrupt user callback */
  1215. HAL_PWREx_PVM3Callback();
  1216. /* Clear PVM3 exti pending bit */
  1217. __HAL_PWR_PVM3_EXTI_CLEAR_FLAG();
  1218. }
  1219. if(__HAL_PWR_PVM4_EXTI_GET_FLAG() != 0x0U)
  1220. {
  1221. /* PWR PVM4 interrupt user callback */
  1222. HAL_PWREx_PVM4Callback();
  1223. /* Clear PVM4 exti pending bit */
  1224. __HAL_PWR_PVM4_EXTI_CLEAR_FLAG();
  1225. }
  1226. }
  1227. #if defined(PWR_CR2_PVME1)
  1228. /**
  1229. * @brief PWR PVM1 interrupt callback
  1230. * @retval None
  1231. */
  1232. __weak void HAL_PWREx_PVM1Callback(void)
  1233. {
  1234. /* NOTE : This function should not be modified; when the callback is needed,
  1235. HAL_PWREx_PVM1Callback() API can be implemented in the user file
  1236. */
  1237. }
  1238. #endif /* PWR_CR2_PVME1 */
  1239. #if defined(PWR_CR2_PVME2)
  1240. /**
  1241. * @brief PWR PVM2 interrupt callback
  1242. * @retval None
  1243. */
  1244. __weak void HAL_PWREx_PVM2Callback(void)
  1245. {
  1246. /* NOTE : This function should not be modified; when the callback is needed,
  1247. HAL_PWREx_PVM2Callback() API can be implemented in the user file
  1248. */
  1249. }
  1250. #endif /* PWR_CR2_PVME2 */
  1251. /**
  1252. * @brief PWR PVM3 interrupt callback
  1253. * @retval None
  1254. */
  1255. __weak void HAL_PWREx_PVM3Callback(void)
  1256. {
  1257. /* NOTE : This function should not be modified; when the callback is needed,
  1258. HAL_PWREx_PVM3Callback() API can be implemented in the user file
  1259. */
  1260. }
  1261. /**
  1262. * @brief PWR PVM4 interrupt callback
  1263. * @retval None
  1264. */
  1265. __weak void HAL_PWREx_PVM4Callback(void)
  1266. {
  1267. /* NOTE : This function should not be modified; when the callback is needed,
  1268. HAL_PWREx_PVM4Callback() API can be implemented in the user file
  1269. */
  1270. }
  1271. /**
  1272. * @}
  1273. */
  1274. /**
  1275. * @}
  1276. */
  1277. #endif /* HAL_PWR_MODULE_ENABLED */
  1278. /**
  1279. * @}
  1280. */
  1281. /**
  1282. * @}
  1283. */