stm32l5xx_hal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_hal.c
  4. * @author MCD Application Team
  5. * @brief HAL module driver.
  6. * This is the common part of the HAL initialization
  7. *
  8. @verbatim
  9. ==============================================================================
  10. ##### How to use this driver #####
  11. ==============================================================================
  12. [..]
  13. The common HAL driver contains a set of generic and common APIs that can be
  14. used by the PPP peripheral drivers and the user to start using the HAL.
  15. [..]
  16. The HAL contains two APIs' categories:
  17. (+) Common HAL APIs
  18. (+) Services HAL APIs
  19. @endverbatim
  20. ******************************************************************************
  21. * @attention
  22. *
  23. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  24. * All rights reserved.</center></h2>
  25. *
  26. * This software component is licensed by ST under BSD 3-Clause license,
  27. * the "License"; You may not use this file except in compliance with the
  28. * License. You may obtain a copy of the License at:
  29. * opensource.org/licenses/BSD-3-Clause
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32l5xx_hal.h"
  35. /** @addtogroup STM32L5xx_HAL_Driver
  36. * @{
  37. */
  38. /** @defgroup HAL HAL
  39. * @brief HAL module driver
  40. * @{
  41. */
  42. #ifdef HAL_MODULE_ENABLED
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /**
  46. * @brief STM32L5xx HAL Driver version number
  47. */
  48. #define STM32L5XX_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  49. #define STM32L5XX_HAL_VERSION_SUB1 (0x00U) /*!< [23:16] sub1 version */
  50. #define STM32L5XX_HAL_VERSION_SUB2 (0x02U) /*!< [15:8] sub2 version */
  51. #define STM32L5XX_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  52. #define STM32L5XX_HAL_VERSION ((STM32L5XX_HAL_VERSION_MAIN << 24U)\
  53. |(STM32L5XX_HAL_VERSION_SUB1 << 16U)\
  54. |(STM32L5XX_HAL_VERSION_SUB2 << 8U )\
  55. |(STM32L5XX_HAL_VERSION_RC))
  56. #define VREFBUF_TIMEOUT_VALUE 10U /*!< 10 ms (to be confirmed) */
  57. #define VREFBUF_SC0_CAL_ADDR ((uint8_t *) (0x0BFA0579UL)) /*!< Address of VREFBUF trimming value for VRS=0,
  58. VREF_SC0 in STM32L5 datasheet */
  59. #define VREFBUF_SC1_CAL_ADDR ((uint8_t *) (0x0BFA0530UL)) /*!< Address of VREFBUF trimming value for VRS=1,
  60. VREF_SC1 in STM32L5 datasheet */
  61. /* Private macro -------------------------------------------------------------*/
  62. /* Private variables ---------------------------------------------------------*/
  63. /* Private function prototypes -----------------------------------------------*/
  64. /* Exported variables --------------------------------------------------------*/
  65. /** @defgroup HAL_Exported_Variables HAL Exported Variables
  66. * @{
  67. */
  68. __IO uint32_t uwTick;
  69. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
  70. uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  71. /**
  72. * @}
  73. */
  74. /* Exported functions --------------------------------------------------------*/
  75. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  76. * @{
  77. */
  78. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  79. * @brief Initialization and de-initialization functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. ##### Initialization and de-initialization functions #####
  84. ===============================================================================
  85. [..] This section provides functions allowing to:
  86. (+) Initialize the Flash interface the NVIC allocation and initial time base
  87. clock configuration.
  88. (+) De-initialize common part of the HAL.
  89. (+) Configure the time base source to have 1ms time base with a dedicated
  90. Tick interrupt priority.
  91. (++) SysTick timer is used by default as source of time base, but user
  92. can eventually implement his proper time base source (a general purpose
  93. timer for example or other time source), keeping in mind that Time base
  94. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  95. handled in milliseconds basis.
  96. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  97. at the beginning of the program after reset by HAL_Init() or at any time
  98. when clock is configured, by HAL_RCC_ClockConfig().
  99. (++) Source of time base is configured to generate interrupts at regular
  100. time intervals. Care must be taken if HAL_Delay() is called from a
  101. peripheral ISR process, the Tick interrupt line must have higher priority
  102. (numerically lower) than the peripheral interrupt. Otherwise the caller
  103. ISR process will be blocked.
  104. (++) functions affecting time base configurations are declared as __weak
  105. to make override possible in case of other implementations in user file.
  106. @endverbatim
  107. * @{
  108. */
  109. /**
  110. * @brief Configure the time base source, NVIC and any required global low level hardware
  111. * by calling the HAL_MspInit() callback function to be optionally defined in user file
  112. * stm32l5xx_hal_msp.c.
  113. *
  114. * @note HAL_Init() function is called at the beginning of program after reset and before
  115. * the clock configuration.
  116. *
  117. * @note In the default implementation the System Timer (Systick) is used as source of time base.
  118. * The Systick configuration is based on MSI clock, as MSI is the clock
  119. * used after a system Reset and the NVIC configuration is set to Priority group 4.
  120. * Once done, time base tick starts incrementing: the tick variable counter is incremented
  121. * each 1ms in the SysTick_Handler() interrupt handler.
  122. *
  123. * @retval HAL status
  124. */
  125. HAL_StatusTypeDef HAL_Init(void)
  126. {
  127. HAL_StatusTypeDef status = HAL_OK;
  128. /* Set Interrupt Group Priority */
  129. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);
  130. /* Insure time base clock coherency */
  131. SystemCoreClockUpdate();
  132. /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  133. if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  134. {
  135. status = HAL_ERROR;
  136. }
  137. else
  138. {
  139. /* Init the low level hardware */
  140. HAL_MspInit();
  141. }
  142. /* Return function status */
  143. return status;
  144. }
  145. /**
  146. * @brief De-initialize common part of the HAL and stop the source of time base.
  147. * @note This function is optional.
  148. * @retval HAL status
  149. */
  150. HAL_StatusTypeDef HAL_DeInit(void)
  151. {
  152. /* Reset of all peripherals */
  153. __HAL_RCC_APB1_FORCE_RESET();
  154. __HAL_RCC_APB1_RELEASE_RESET();
  155. __HAL_RCC_APB2_FORCE_RESET();
  156. __HAL_RCC_APB2_RELEASE_RESET();
  157. __HAL_RCC_AHB1_FORCE_RESET();
  158. __HAL_RCC_AHB1_RELEASE_RESET();
  159. __HAL_RCC_AHB2_FORCE_RESET();
  160. __HAL_RCC_AHB2_RELEASE_RESET();
  161. __HAL_RCC_AHB3_FORCE_RESET();
  162. __HAL_RCC_AHB3_RELEASE_RESET();
  163. /* De-Init the low level hardware */
  164. HAL_MspDeInit();
  165. /* Return function status */
  166. return HAL_OK;
  167. }
  168. /**
  169. * @brief Initialize the MSP.
  170. * @retval None
  171. */
  172. __weak void HAL_MspInit(void)
  173. {
  174. /* NOTE : This function should not be modified, when the callback is needed,
  175. the HAL_MspInit could be implemented in the user file
  176. */
  177. }
  178. /**
  179. * @brief DeInitialize the MSP.
  180. * @retval None
  181. */
  182. __weak void HAL_MspDeInit(void)
  183. {
  184. /* NOTE : This function should not be modified, when the callback is needed,
  185. the HAL_MspDeInit could be implemented in the user file
  186. */
  187. }
  188. /**
  189. * @brief This function configures the source of the time base:
  190. * The time source is configured to have 1ms time base with a dedicated
  191. * Tick interrupt priority.
  192. * @note This function is called automatically at the beginning of program after
  193. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  194. * @note In the default implementation, SysTick timer is the source of time base.
  195. * It is used to generate interrupts at regular time intervals.
  196. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  197. * The SysTick interrupt must have higher priority (numerically lower)
  198. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  199. * The function is declared as __weak to be overwritten in case of other
  200. * implementation in user file.
  201. * @param TickPriority Tick interrupt priority.
  202. * @retval HAL status
  203. */
  204. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  205. {
  206. HAL_StatusTypeDef status = HAL_OK;
  207. if (uwTickFreq != 0U)
  208. {
  209. /*Configure the SysTick to have interrupt in 1ms time basis*/
  210. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) == 0U)
  211. {
  212. /* Configure the SysTick IRQ priority */
  213. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  214. {
  215. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  216. uwTickPrio = TickPriority;
  217. }
  218. else
  219. {
  220. status = HAL_ERROR;
  221. }
  222. }
  223. else
  224. {
  225. status = HAL_ERROR;
  226. }
  227. }
  228. else
  229. {
  230. status = HAL_ERROR;
  231. }
  232. /* Return function status */
  233. return status;
  234. }
  235. /**
  236. * @}
  237. */
  238. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  239. * @brief HAL Control functions
  240. *
  241. @verbatim
  242. ===============================================================================
  243. ##### HAL Control functions #####
  244. ===============================================================================
  245. [..] This section provides functions allowing to:
  246. (+) Provide a tick value in millisecond
  247. (+) Provide a blocking delay in millisecond
  248. (+) Suspend the time base source interrupt
  249. (+) Resume the time base source interrupt
  250. (+) Get the HAL API driver version
  251. (+) Get the device identifier
  252. (+) Get the device revision identifier
  253. @endverbatim
  254. * @{
  255. */
  256. /**
  257. * @brief This function is called to increment a global variable "uwTick"
  258. * used as application time base.
  259. * @note In the default implementation, this variable is incremented each 1ms
  260. * in SysTick ISR.
  261. * @note This function is declared as __weak to be overwritten in case of other
  262. * implementations in user file.
  263. * @retval None
  264. */
  265. __weak void HAL_IncTick(void)
  266. {
  267. uwTick += uwTickFreq;
  268. }
  269. /**
  270. * @brief Provide a tick value in millisecond.
  271. * @note This function is declared as __weak to be overwritten in case of other
  272. * implementations in user file.
  273. * @retval tick value
  274. */
  275. __weak uint32_t HAL_GetTick(void)
  276. {
  277. return uwTick++;
  278. }
  279. /**
  280. * @brief This function returns a tick priority.
  281. * @retval tick priority
  282. */
  283. uint32_t HAL_GetTickPrio(void)
  284. {
  285. return uwTickPrio;
  286. }
  287. /**
  288. * @brief Set new tick Freq.
  289. * @param Freq tick frequency
  290. * @retval HAL status
  291. */
  292. HAL_StatusTypeDef HAL_SetTickFreq(uint32_t Freq)
  293. {
  294. HAL_StatusTypeDef status = HAL_OK;
  295. assert_param(IS_TICKFREQ(Freq));
  296. if (uwTickFreq != Freq)
  297. {
  298. /* Apply the new tick Freq */
  299. status = HAL_InitTick(uwTickPrio);
  300. if (status == HAL_OK)
  301. {
  302. uwTickFreq = Freq;
  303. }
  304. }
  305. return status;
  306. }
  307. /**
  308. * @brief Return tick frequency.
  309. * @retval tick period in Hz
  310. */
  311. uint32_t HAL_GetTickFreq(void)
  312. {
  313. return uwTickFreq;
  314. }
  315. /**
  316. * @brief This function provides minimum delay (in milliseconds) based
  317. * on variable incremented.
  318. * @note In the default implementation , SysTick timer is the source of time base.
  319. * It is used to generate interrupts at regular time intervals where uwTick
  320. * is incremented.
  321. * @note This function is declared as __weak to be overwritten in case of other
  322. * implementations in user file.
  323. * @param Delay specifies the delay time length, in milliseconds.
  324. * @retval None
  325. */
  326. __weak void HAL_Delay(uint32_t Delay)
  327. {
  328. uint32_t tickstart = HAL_GetTick();
  329. uint32_t wait = Delay;
  330. /* Add a period to guaranty minimum wait */
  331. if (wait < HAL_MAX_DELAY)
  332. {
  333. wait += (uint32_t)(uwTickFreq);
  334. }
  335. while((HAL_GetTick() - tickstart) < wait)
  336. {
  337. }
  338. }
  339. /**
  340. * @brief Suspend Tick increment.
  341. * @note In the default implementation , SysTick timer is the source of time base. It is
  342. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  343. * is called, the SysTick interrupt will be disabled and so Tick increment
  344. * is suspended.
  345. * @note This function is declared as __weak to be overwritten in case of other
  346. * implementations in user file.
  347. * @retval None
  348. */
  349. __weak void HAL_SuspendTick(void)
  350. {
  351. /* Disable SysTick Interrupt */
  352. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  353. }
  354. /**
  355. * @brief Resume Tick increment.
  356. * @note In the default implementation , SysTick timer is the source of time base. It is
  357. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  358. * is called, the SysTick interrupt will be enabled and so Tick increment
  359. * is resumed.
  360. * @note This function is declared as __weak to be overwritten in case of other
  361. * implementations in user file.
  362. * @retval None
  363. */
  364. __weak void HAL_ResumeTick(void)
  365. {
  366. /* Enable SysTick Interrupt */
  367. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  368. }
  369. /**
  370. * @brief Return the HAL revision.
  371. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  372. */
  373. uint32_t HAL_GetHalVersion(void)
  374. {
  375. return STM32L5XX_HAL_VERSION;
  376. }
  377. /**
  378. * @brief Return the device revision identifier.
  379. * @retval Device revision identifier
  380. */
  381. uint32_t HAL_GetREVID(void)
  382. {
  383. return((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> DBGMCU_IDCODE_REV_ID_Pos);
  384. }
  385. /**
  386. * @brief Return the device identifier.
  387. * @retval Device identifier
  388. */
  389. uint32_t HAL_GetDEVID(void)
  390. {
  391. return(DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
  392. }
  393. /**
  394. * @brief Return the first word of the unique device identifier (UID based on 96 bits)
  395. * @retval Device identifier
  396. */
  397. uint32_t HAL_GetUIDw0(void)
  398. {
  399. return(READ_REG(*((uint32_t *)UID_BASE)));
  400. }
  401. /**
  402. * @brief Return the second word of the unique device identifier (UID based on 96 bits)
  403. * @retval Device identifier
  404. */
  405. uint32_t HAL_GetUIDw1(void)
  406. {
  407. return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  408. }
  409. /**
  410. * @brief Return the third word of the unique device identifier (UID based on 96 bits)
  411. * @retval Device identifier
  412. */
  413. uint32_t HAL_GetUIDw2(void)
  414. {
  415. return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  416. }
  417. /**
  418. * @}
  419. */
  420. /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
  421. * @brief HAL Debug functions
  422. *
  423. @verbatim
  424. ===============================================================================
  425. ##### HAL Debug functions #####
  426. ===============================================================================
  427. [..] This section provides functions allowing to:
  428. (+) Enable/Disable Debug module during SLEEP mode
  429. (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
  430. (+) Enable/Disable Debug module during STANDBY mode
  431. @endverbatim
  432. * @{
  433. */
  434. /**
  435. * @brief Enable the Debug Module during SLEEP mode.
  436. * @retval None
  437. */
  438. void HAL_DBGMCU_EnableDBGSleepMode(void)
  439. {
  440. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  441. }
  442. /**
  443. * @brief Disable the Debug Module during SLEEP mode.
  444. * @retval None
  445. */
  446. void HAL_DBGMCU_DisableDBGSleepMode(void)
  447. {
  448. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  449. }
  450. /**
  451. * @brief Enable the Debug Module during STOP0/STOP1/STOP2 modes.
  452. * @retval None
  453. */
  454. void HAL_DBGMCU_EnableDBGStopMode(void)
  455. {
  456. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  457. }
  458. /**
  459. * @brief Disable the Debug Module during STOP0/STOP1/STOP2 modes.
  460. * @retval None
  461. */
  462. void HAL_DBGMCU_DisableDBGStopMode(void)
  463. {
  464. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  465. }
  466. /**
  467. * @brief Enable the Debug Module during STANDBY mode.
  468. * @retval None
  469. */
  470. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  471. {
  472. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  473. }
  474. /**
  475. * @brief Disable the Debug Module during STANDBY mode.
  476. * @retval None
  477. */
  478. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  479. {
  480. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  481. }
  482. /**
  483. * @}
  484. */
  485. /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
  486. * @brief HAL SYSCFG configuration functions
  487. *
  488. @verbatim
  489. ===============================================================================
  490. ##### HAL SYSCFG configuration functions #####
  491. ===============================================================================
  492. [..] This section provides functions allowing to:
  493. (+) Start a hardware SRAM2 erase operation
  494. (+) Configure the Voltage reference buffer
  495. (+) Enable/Disable the Voltage reference buffer
  496. (+) Enable/Disable the I/O analog switch voltage booster
  497. (+) Enable/Disable the I/O analog switch supplied by VDD
  498. @endverbatim
  499. * @{
  500. */
  501. /**
  502. * @brief Start a hardware SRAM2 erase operation.
  503. * @note As long as SRAM2 is not erased the SRAM2ER bit will be set.
  504. * This bit is automatically reset at the end of the SRAM2 erase operation.
  505. * @retval None
  506. */
  507. void HAL_SYSCFG_SRAM2Erase(void)
  508. {
  509. /* unlock the write protection of the SRAM2ER bit */
  510. SYSCFG->SKR = 0xCA;
  511. SYSCFG->SKR = 0x53;
  512. /* Starts a hardware SRAM2 erase operation*/
  513. SET_BIT(SYSCFG->SCSR, SYSCFG_SCSR_SRAM2ER);
  514. }
  515. /**
  516. * @brief Configure the internal voltage reference buffer voltage scale.
  517. * @param VoltageScaling specifies the output voltage to achieve
  518. * This parameter can be one of the following values:
  519. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
  520. * This requires VDDA equal to or higher than 2.4 V.
  521. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT1 around 2.5 V.
  522. * This requires VDDA equal to or higher than 2.8 V.
  523. * @note Retrieve the TrimmingValue from factory located at
  524. * VREFBUF_SC0_CAL_ADDR or VREFBUF_SC1_CAL_ADDR addresses.
  525. * @retval None
  526. */
  527. void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
  528. {
  529. uint32_t TrimmingValue;
  530. /* Check the parameters */
  531. assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
  532. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
  533. /* Restrieve Calibration data and store them into trimming field */
  534. if (VoltageScaling == SYSCFG_VREFBUF_VOLTAGE_SCALE0)
  535. {
  536. TrimmingValue = ((uint32_t) *VREFBUF_SC0_CAL_ADDR) & 0x3FU;
  537. }
  538. else
  539. {
  540. TrimmingValue = ((uint32_t) *VREFBUF_SC1_CAL_ADDR) & 0x3FU;
  541. }
  542. assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
  543. HAL_SYSCFG_VREFBUF_TrimmingConfig(TrimmingValue);
  544. }
  545. /**
  546. * @brief Configure the internal voltage reference buffer high impedance mode.
  547. * @param Mode specifies the high impedance mode
  548. * This parameter can be one of the following values:
  549. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
  550. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
  551. * @retval None
  552. */
  553. void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
  554. {
  555. /* Check the parameters */
  556. assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
  557. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
  558. }
  559. /**
  560. * @brief Tune the Internal Voltage Reference buffer (VREFBUF).
  561. * @retval None
  562. */
  563. void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
  564. {
  565. /* Check the parameters */
  566. assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
  567. MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
  568. }
  569. /**
  570. * @brief Enable the Internal Voltage Reference buffer (VREFBUF).
  571. * @retval HAL_OK/HAL_TIMEOUT
  572. */
  573. HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
  574. {
  575. uint32_t tickstart;
  576. SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  577. /* Get Start Tick*/
  578. tickstart = HAL_GetTick();
  579. /* Wait for VRR bit */
  580. while(READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
  581. {
  582. if((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
  583. {
  584. return HAL_TIMEOUT;
  585. }
  586. }
  587. return HAL_OK;
  588. }
  589. /**
  590. * @brief Disable the Internal Voltage Reference buffer (VREFBUF).
  591. *
  592. * @retval None
  593. */
  594. void HAL_SYSCFG_DisableVREFBUF(void)
  595. {
  596. CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  597. }
  598. /**
  599. * @brief Enable the I/O analog switch voltage booster
  600. * @note Insure low VDDA voltage operation with I/O analog switch control
  601. * @retval None
  602. */
  603. void HAL_SYSCFG_EnableIOAnalogBooster(void)
  604. {
  605. MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_BOOSTEN);
  606. }
  607. /**
  608. * @brief Disable the I/O analog switch voltage booster
  609. *
  610. * @retval None
  611. */
  612. void HAL_SYSCFG_DisableIOAnalogBooster(void)
  613. {
  614. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
  615. }
  616. /**
  617. * @brief Enable the I/O analog switch supplied by VDD
  618. * @note To be used when I/O analog switch voltage booster is not enabled
  619. * @retval None
  620. */
  621. void HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)
  622. {
  623. MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_ANASWVDD);
  624. }
  625. /**
  626. * @brief Disable the I/O analog switch supplied by VDD
  627. *
  628. * @retval None
  629. */
  630. void HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)
  631. {
  632. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
  633. }
  634. /**
  635. * @}
  636. */
  637. /** @defgroup HAL_Exported_Functions_Group5 HAL SYSCFG lock management functions
  638. * @brief SYSCFG lock management functions.
  639. *
  640. @verbatim
  641. ===============================================================================
  642. ##### SYSCFG lock functions #####
  643. ===============================================================================
  644. @endverbatim
  645. * @{
  646. */
  647. /**
  648. * @brief Lock the SYSCFG item(s).
  649. * @note Setting lock(s) depends on privilege mode in secure/non-secure code
  650. * Lock(s) cleared only at system reset
  651. * @param Item Item(s) to set lock on.
  652. * This parameter can be a combination of @ref SYSCFG_Lock_items
  653. * @retval None
  654. */
  655. void HAL_SYSCFG_Lock(uint32_t Item)
  656. {
  657. /* Check the parameters */
  658. assert_param(IS_SYSCFG_LOCK_ITEMS(Item));
  659. /* Privilege secure/non-secure locks */
  660. SYSCFG->CNSLCKR = (0xFFFFU & Item); /* non-secure lock item in 16 lowest bits */
  661. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  662. /* Privilege secure only locks */
  663. SYSCFG->CSLCKR = ((0xFFFF0000U & Item) >> 16U); /* Secure-only lock item in 16 highest bits */
  664. #endif /* __ARM_FEATURE_CMSE */
  665. }
  666. /**
  667. * @brief Get the lock state of SYSCFG item.
  668. * @note Getting lock(s) depends on privilege mode in secure/non-secure code
  669. * @param pItem pointer to return locked items
  670. * the return value can be a combination of @ref SYSCFG_Lock_items
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_SYSCFG_GetLock(uint32_t *pItem)
  674. {
  675. uint32_t tmp_lock;
  676. /* Check null pointer */
  677. if(pItem == NULL)
  678. {
  679. return HAL_ERROR;
  680. }
  681. /* Get the non-secure lock state */
  682. tmp_lock = SYSCFG->CNSLCKR;
  683. /* Get the secure lock state in secure code */
  684. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  685. tmp_lock |= (SYSCFG->CSLCKR << 16U);
  686. #endif /* __ARM_FEATURE_CMSE */
  687. /* Return overall lock status */
  688. *pItem = tmp_lock;
  689. return HAL_OK;
  690. }
  691. /**
  692. * @}
  693. */
  694. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  695. /** @defgroup HAL_Exported_Functions_Group6 HAL SYSCFG attributes management functions
  696. * @brief SYSCFG attributes management functions.
  697. *
  698. @verbatim
  699. ===============================================================================
  700. ##### SYSCFG attributes functions #####
  701. ===============================================================================
  702. @endverbatim
  703. * @{
  704. */
  705. /**
  706. * @brief Configure the SYSCFG item attribute(s).
  707. * @note Available attributes are to secure SYSCFG items, so this function is
  708. * only available in secure
  709. * @param Item Item(s) to set attributes on.
  710. * This parameter can be a one or a combination of @ref SYSCFG_Attributes_items
  711. * @param Attributes specifies the secure/non-secure attributes.
  712. * @retval None
  713. */
  714. void HAL_SYSCFG_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  715. {
  716. uint32_t tmp;
  717. /* Check the parameters */
  718. assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
  719. assert_param(IS_SYSCFG_ATTRIBUTES(Attributes));
  720. tmp = SYSCFG_S->SECCFGR;
  721. /* Set or reset Item */
  722. if((Attributes & SYSCFG_SEC) != 0x00U)
  723. {
  724. tmp |= Item;
  725. }
  726. else
  727. {
  728. tmp &= ~Item;
  729. }
  730. /* Set secure attributes */
  731. SYSCFG_S->SECCFGR = tmp;
  732. }
  733. /**
  734. * @brief Get the attribute of a SYSCFG item.
  735. * @note Available attributes are to secure SYSCFG items, so this function is
  736. * only available in secure
  737. * @param Item Single item to get secure/non-secure attribute from.
  738. * @param pAttributes pointer to return the attribute.
  739. * @retval HAL status
  740. */
  741. HAL_StatusTypeDef HAL_SYSCFG_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  742. {
  743. /* Check null pointer */
  744. if(pAttributes == NULL)
  745. {
  746. return HAL_ERROR;
  747. }
  748. /* Check the parameters */
  749. assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
  750. /* Get the secure attribute state */
  751. if((SYSCFG_S->SECCFGR & Item) != 0U)
  752. {
  753. *pAttributes = SYSCFG_SEC;
  754. }
  755. else
  756. {
  757. *pAttributes = SYSCFG_NSEC;
  758. }
  759. return HAL_OK;
  760. }
  761. /**
  762. * @}
  763. */
  764. #endif /* __ARM_FEATURE_CMSE */
  765. /**
  766. * @}
  767. */
  768. #endif /* HAL_MODULE_ENABLED */
  769. /**
  770. * @}
  771. */
  772. /**
  773. * @}
  774. */
  775. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/