stm32l5xx_hal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. ******************************************************************************
  9. * @attention
  10. *
  11. * Copyright (c) 2019 STMicroelectronics.
  12. * All rights reserved.
  13. *
  14. * This software is licensed under terms that can be found in the LICENSE file
  15. * in the root directory of this software component.
  16. * If no LICENSE file comes with this software, it is provided AS-IS.
  17. *
  18. ******************************************************************************
  19. @verbatim
  20. ==============================================================================
  21. ##### How to use this driver #####
  22. ==============================================================================
  23. [..]
  24. The common HAL driver contains a set of generic and common APIs that can be
  25. used by the PPP peripheral drivers and the user to start using the HAL.
  26. [..]
  27. The HAL contains two APIs' categories:
  28. (+) Common HAL APIs
  29. (+) Services HAL APIs
  30. @endverbatim
  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 (0x05U) /*!< [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. HAL_TickFreqTypeDef 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 DeInitialize 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. /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that doesn't take the value zero)*/
  208. if ((uint32_t)uwTickFreq != 0U)
  209. {
  210. /*Configure the SysTick to have interrupt in 1ms time basis*/
  211. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)) == 0U)
  212. {
  213. /* Configure the SysTick IRQ priority */
  214. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  215. {
  216. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  217. uwTickPrio = TickPriority;
  218. }
  219. else
  220. {
  221. status = HAL_ERROR;
  222. }
  223. }
  224. else
  225. {
  226. status = HAL_ERROR;
  227. }
  228. }
  229. else
  230. {
  231. status = HAL_ERROR;
  232. }
  233. /* Return function status */
  234. return status;
  235. }
  236. /**
  237. * @}
  238. */
  239. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  240. * @brief HAL Control functions
  241. *
  242. @verbatim
  243. ===============================================================================
  244. ##### HAL Control functions #####
  245. ===============================================================================
  246. [..] This section provides functions allowing to:
  247. (+) Provide a tick value in millisecond
  248. (+) Provide a blocking delay in millisecond
  249. (+) Suspend the time base source interrupt
  250. (+) Resume the time base source interrupt
  251. (+) Get the HAL API driver version
  252. (+) Get the device identifier
  253. (+) Get the device revision identifier
  254. @endverbatim
  255. * @{
  256. */
  257. /**
  258. * @brief This function is called to increment a global variable "uwTick"
  259. * used as application time base.
  260. * @note In the default implementation, this variable is incremented each 1ms
  261. * in SysTick ISR.
  262. * @note This function is declared as __weak to be overwritten in case of other
  263. * implementations in user file.
  264. * @retval None
  265. */
  266. __weak void HAL_IncTick(void)
  267. {
  268. uwTick += (uint32_t)uwTickFreq;
  269. }
  270. /**
  271. * @brief Provide a tick value in millisecond.
  272. * @note This function is declared as __weak to be overwritten in case of other
  273. * implementations in user file.
  274. * @retval tick value
  275. */
  276. __weak uint32_t HAL_GetTick(void)
  277. {
  278. return uwTick;
  279. }
  280. /**
  281. * @brief This function returns a tick priority.
  282. * @retval tick priority
  283. */
  284. uint32_t HAL_GetTickPrio(void)
  285. {
  286. return uwTickPrio;
  287. }
  288. /**
  289. * @brief Set new tick Freq.
  290. * @param Freq tick frequency
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
  294. {
  295. HAL_StatusTypeDef status = HAL_OK;
  296. HAL_TickFreqTypeDef prevTickFreq;
  297. if (uwTickFreq != Freq)
  298. {
  299. /* Back up uwTickFreq frequency */
  300. prevTickFreq = uwTickFreq;
  301. /* Update uwTickFreq global variable used by HAL_InitTick() */
  302. uwTickFreq = Freq;
  303. /* Apply the new tick Freq */
  304. status = HAL_InitTick(uwTickPrio);
  305. if (status != HAL_OK)
  306. {
  307. /* Restore previous tick frequency */
  308. uwTickFreq = prevTickFreq;
  309. }
  310. }
  311. return status;
  312. }
  313. /**
  314. * @brief Return tick frequency.
  315. * @retval tick period in Hz
  316. */
  317. HAL_TickFreqTypeDef HAL_GetTickFreq(void)
  318. {
  319. return uwTickFreq;
  320. }
  321. /**
  322. * @brief This function provides minimum delay (in milliseconds) based
  323. * on variable incremented.
  324. * @note In the default implementation , SysTick timer is the source of time base.
  325. * It is used to generate interrupts at regular time intervals where uwTick
  326. * is incremented.
  327. * @note This function is declared as __weak to be overwritten in case of other
  328. * implementations in user file.
  329. * @param Delay specifies the delay time length, in milliseconds.
  330. * @retval None
  331. */
  332. __weak void HAL_Delay(uint32_t Delay)
  333. {
  334. uint32_t tickstart = HAL_GetTick();
  335. uint32_t wait = Delay;
  336. /* Add a period to guaranty minimum wait */
  337. if (wait < HAL_MAX_DELAY)
  338. {
  339. wait += (uint32_t)uwTickFreq;
  340. }
  341. while ((HAL_GetTick() - tickstart) < wait)
  342. {
  343. }
  344. }
  345. /**
  346. * @brief Suspend Tick increment.
  347. * @note In the default implementation , SysTick timer is the source of time base. It is
  348. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  349. * is called, the SysTick interrupt will be disabled and so Tick increment
  350. * is suspended.
  351. * @note This function is declared as __weak to be overwritten in case of other
  352. * implementations in user file.
  353. * @retval None
  354. */
  355. __weak void HAL_SuspendTick(void)
  356. {
  357. /* Disable SysTick Interrupt */
  358. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  359. }
  360. /**
  361. * @brief Resume Tick increment.
  362. * @note In the default implementation , SysTick timer is the source of time base. It is
  363. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  364. * is called, the SysTick interrupt will be enabled and so Tick increment
  365. * is resumed.
  366. * @note This function is declared as __weak to be overwritten in case of other
  367. * implementations in user file.
  368. * @retval None
  369. */
  370. __weak void HAL_ResumeTick(void)
  371. {
  372. /* Enable SysTick Interrupt */
  373. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  374. }
  375. /**
  376. * @brief Return the HAL revision.
  377. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  378. */
  379. uint32_t HAL_GetHalVersion(void)
  380. {
  381. return STM32L5XX_HAL_VERSION;
  382. }
  383. /**
  384. * @brief Return the device revision identifier.
  385. * @retval Device revision identifier
  386. */
  387. uint32_t HAL_GetREVID(void)
  388. {
  389. return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> DBGMCU_IDCODE_REV_ID_Pos);
  390. }
  391. /**
  392. * @brief Return the device identifier.
  393. * @retval Device identifier
  394. */
  395. uint32_t HAL_GetDEVID(void)
  396. {
  397. return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
  398. }
  399. /**
  400. * @brief Return the first word of the unique device identifier (UID based on 96 bits)
  401. * @retval Device identifier
  402. */
  403. uint32_t HAL_GetUIDw0(void)
  404. {
  405. return (READ_REG(*((uint32_t *)UID_BASE)));
  406. }
  407. /**
  408. * @brief Return the second word of the unique device identifier (UID based on 96 bits)
  409. * @retval Device identifier
  410. */
  411. uint32_t HAL_GetUIDw1(void)
  412. {
  413. return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  414. }
  415. /**
  416. * @brief Return the third word of the unique device identifier (UID based on 96 bits)
  417. * @retval Device identifier
  418. */
  419. uint32_t HAL_GetUIDw2(void)
  420. {
  421. return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  422. }
  423. /**
  424. * @}
  425. */
  426. /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
  427. * @brief HAL Debug functions
  428. *
  429. @verbatim
  430. ===============================================================================
  431. ##### HAL Debug functions #####
  432. ===============================================================================
  433. [..] This section provides functions allowing to:
  434. (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
  435. (+) Enable/Disable Debug module during STANDBY mode
  436. @endverbatim
  437. * @{
  438. */
  439. /**
  440. * @brief Enable the Debug Module during STOP0/STOP1/STOP2 modes.
  441. * @retval None
  442. */
  443. void HAL_DBGMCU_EnableDBGStopMode(void)
  444. {
  445. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  446. }
  447. /**
  448. * @brief Disable the Debug Module during STOP0/STOP1/STOP2 modes.
  449. * @retval None
  450. */
  451. void HAL_DBGMCU_DisableDBGStopMode(void)
  452. {
  453. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  454. }
  455. /**
  456. * @brief Enable the Debug Module during STANDBY mode.
  457. * @retval None
  458. */
  459. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  460. {
  461. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  462. }
  463. /**
  464. * @brief Disable the Debug Module during STANDBY mode.
  465. * @retval None
  466. */
  467. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  468. {
  469. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  470. }
  471. /**
  472. * @}
  473. */
  474. /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
  475. * @brief HAL SYSCFG configuration functions
  476. *
  477. @verbatim
  478. ===============================================================================
  479. ##### HAL SYSCFG configuration functions #####
  480. ===============================================================================
  481. [..] This section provides functions allowing to:
  482. (+) Start a hardware SRAM2 erase operation
  483. (+) Configure the Voltage reference buffer
  484. (+) Enable/Disable the Voltage reference buffer
  485. (+) Enable/Disable the I/O analog switch voltage booster
  486. (+) Enable/Disable the I/O analog switch supplied by VDD
  487. @endverbatim
  488. * @{
  489. */
  490. /**
  491. * @brief Start a hardware SRAM2 erase operation.
  492. * @note As long as SRAM2 is not erased the SRAM2ER bit will be set.
  493. * This bit is automatically reset at the end of the SRAM2 erase operation.
  494. * @retval None
  495. */
  496. void HAL_SYSCFG_SRAM2Erase(void)
  497. {
  498. /* unlock the write protection of the SRAM2ER bit */
  499. SYSCFG->SKR = 0xCA;
  500. SYSCFG->SKR = 0x53;
  501. /* Starts a hardware SRAM2 erase operation*/
  502. SET_BIT(SYSCFG->SCSR, SYSCFG_SCSR_SRAM2ER);
  503. }
  504. /**
  505. * @brief Configure the internal voltage reference buffer voltage scale.
  506. * @param VoltageScaling specifies the output voltage to achieve
  507. * This parameter can be one of the following values:
  508. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
  509. * This requires VDDA equal to or higher than 2.4 V.
  510. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT1 around 2.5 V.
  511. * This requires VDDA equal to or higher than 2.8 V.
  512. * @note Retrieve the TrimmingValue from factory located at
  513. * VREFBUF_SC0_CAL_ADDR or VREFBUF_SC1_CAL_ADDR addresses.
  514. * @retval None
  515. */
  516. void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
  517. {
  518. uint32_t TrimmingValue;
  519. /* Check the parameters */
  520. assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
  521. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
  522. /* Restrieve Calibration data and store them into trimming field */
  523. if (VoltageScaling == SYSCFG_VREFBUF_VOLTAGE_SCALE0)
  524. {
  525. TrimmingValue = ((uint32_t) *VREFBUF_SC0_CAL_ADDR) & 0x3FU;
  526. }
  527. else
  528. {
  529. TrimmingValue = ((uint32_t) *VREFBUF_SC1_CAL_ADDR) & 0x3FU;
  530. }
  531. assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
  532. HAL_SYSCFG_VREFBUF_TrimmingConfig(TrimmingValue);
  533. }
  534. /**
  535. * @brief Configure the internal voltage reference buffer high impedance mode.
  536. * @param Mode specifies the high impedance mode
  537. * This parameter can be one of the following values:
  538. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
  539. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
  540. * @retval None
  541. */
  542. void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
  543. {
  544. /* Check the parameters */
  545. assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
  546. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
  547. }
  548. /**
  549. * @brief Tune the Internal Voltage Reference buffer (VREFBUF).
  550. * @retval None
  551. */
  552. void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
  553. {
  554. /* Check the parameters */
  555. assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
  556. MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
  557. }
  558. /**
  559. * @brief Enable the Internal Voltage Reference buffer (VREFBUF).
  560. * @retval HAL_OK/HAL_TIMEOUT
  561. */
  562. HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
  563. {
  564. uint32_t tickstart;
  565. SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  566. /* Get Start Tick*/
  567. tickstart = HAL_GetTick();
  568. /* Wait for VRR bit */
  569. while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
  570. {
  571. if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
  572. {
  573. return HAL_TIMEOUT;
  574. }
  575. }
  576. return HAL_OK;
  577. }
  578. /**
  579. * @brief Disable the Internal Voltage Reference buffer (VREFBUF).
  580. *
  581. * @retval None
  582. */
  583. void HAL_SYSCFG_DisableVREFBUF(void)
  584. {
  585. CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  586. }
  587. /**
  588. * @brief Enable the I/O analog switch voltage booster
  589. * @note Insure low VDDA voltage operation with I/O analog switch control
  590. * @retval None
  591. */
  592. void HAL_SYSCFG_EnableIOAnalogBooster(void)
  593. {
  594. MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_BOOSTEN);
  595. }
  596. /**
  597. * @brief Disable the I/O analog switch voltage booster
  598. *
  599. * @retval None
  600. */
  601. void HAL_SYSCFG_DisableIOAnalogBooster(void)
  602. {
  603. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
  604. }
  605. /**
  606. * @brief Enable the I/O analog switch supplied by VDD
  607. * @note To be used when I/O analog switch voltage booster is not enabled
  608. * @retval None
  609. */
  610. void HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)
  611. {
  612. MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_ANASWVDD);
  613. }
  614. /**
  615. * @brief Disable the I/O analog switch supplied by VDD
  616. *
  617. * @retval None
  618. */
  619. void HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)
  620. {
  621. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
  622. }
  623. /**
  624. * @}
  625. */
  626. /** @defgroup HAL_Exported_Functions_Group5 HAL SYSCFG lock management functions
  627. * @brief SYSCFG lock management functions.
  628. *
  629. @verbatim
  630. ===============================================================================
  631. ##### SYSCFG lock functions #####
  632. ===============================================================================
  633. @endverbatim
  634. * @{
  635. */
  636. /**
  637. * @brief Lock the SYSCFG item(s).
  638. * @note Setting lock(s) depends on privilege mode in secure/non-secure code
  639. * Lock(s) cleared only at system reset
  640. * @param Item Item(s) to set lock on.
  641. * This parameter can be a combination of @ref SYSCFG_Lock_items
  642. * @retval None
  643. */
  644. void HAL_SYSCFG_Lock(uint32_t Item)
  645. {
  646. /* Check the parameters */
  647. assert_param(IS_SYSCFG_LOCK_ITEMS(Item));
  648. /* Privilege secure/non-secure locks */
  649. SYSCFG->CNSLCKR = (0xFFFFU & Item); /* non-secure lock item in 16 lowest bits */
  650. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  651. /* Privilege secure only locks */
  652. SYSCFG->CSLCKR = ((0xFFFF0000U & Item) >> 16U); /* Secure-only lock item in 16 highest bits */
  653. #endif /* __ARM_FEATURE_CMSE */
  654. }
  655. /**
  656. * @brief Get the lock state of SYSCFG item.
  657. * @note Getting lock(s) depends on privilege mode in secure/non-secure code
  658. * @param pItem pointer to return locked items
  659. * the return value can be a combination of @ref SYSCFG_Lock_items
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef HAL_SYSCFG_GetLock(uint32_t *pItem)
  663. {
  664. uint32_t tmp_lock;
  665. /* Check null pointer */
  666. if (pItem == NULL)
  667. {
  668. return HAL_ERROR;
  669. }
  670. /* Get the non-secure lock state */
  671. tmp_lock = SYSCFG->CNSLCKR;
  672. /* Get the secure lock state in secure code */
  673. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  674. tmp_lock |= (SYSCFG->CSLCKR << 16U);
  675. #endif /* __ARM_FEATURE_CMSE */
  676. /* Return overall lock status */
  677. *pItem = tmp_lock;
  678. return HAL_OK;
  679. }
  680. /**
  681. * @}
  682. */
  683. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  684. /** @defgroup HAL_Exported_Functions_Group6 HAL SYSCFG attributes management functions
  685. * @brief SYSCFG attributes management functions.
  686. *
  687. @verbatim
  688. ===============================================================================
  689. ##### SYSCFG attributes functions #####
  690. ===============================================================================
  691. @endverbatim
  692. * @{
  693. */
  694. /**
  695. * @brief Configure the SYSCFG item attribute(s).
  696. * @note Available attributes are to secure SYSCFG items, so this function is
  697. * only available in secure
  698. * @param Item Item(s) to set attributes on.
  699. * This parameter can be a one or a combination of @ref SYSCFG_Attributes_items
  700. * @param Attributes specifies the secure/non-secure attributes.
  701. * @retval None
  702. */
  703. void HAL_SYSCFG_ConfigAttributes(uint32_t Item, uint32_t Attributes)
  704. {
  705. uint32_t tmp;
  706. /* Check the parameters */
  707. assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
  708. assert_param(IS_SYSCFG_ATTRIBUTES(Attributes));
  709. tmp = SYSCFG_S->SECCFGR;
  710. /* Set or reset Item */
  711. if ((Attributes & SYSCFG_SEC) != 0x00U)
  712. {
  713. tmp |= Item;
  714. }
  715. else
  716. {
  717. tmp &= ~Item;
  718. }
  719. /* Set secure attributes */
  720. SYSCFG_S->SECCFGR = tmp;
  721. }
  722. /**
  723. * @brief Get the attribute of a SYSCFG item.
  724. * @note Available attributes are to secure SYSCFG items, so this function is
  725. * only available in secure
  726. * @param Item Single item to get secure/non-secure attribute from.
  727. * @param pAttributes pointer to return the attribute.
  728. * @retval HAL status
  729. */
  730. HAL_StatusTypeDef HAL_SYSCFG_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
  731. {
  732. /* Check null pointer */
  733. if (pAttributes == NULL)
  734. {
  735. return HAL_ERROR;
  736. }
  737. /* Check the parameters */
  738. assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
  739. /* Get the secure attribute state */
  740. if ((SYSCFG_S->SECCFGR & Item) != 0U)
  741. {
  742. *pAttributes = SYSCFG_SEC;
  743. }
  744. else
  745. {
  746. *pAttributes = SYSCFG_NSEC;
  747. }
  748. return HAL_OK;
  749. }
  750. /**
  751. * @}
  752. */
  753. #endif /* __ARM_FEATURE_CMSE */
  754. /**
  755. * @}
  756. */
  757. #endif /* HAL_MODULE_ENABLED */
  758. /**
  759. * @}
  760. */
  761. /**
  762. * @}
  763. */