stm32g4xx_hal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_hal.c
  4. * @author MCD Application Team
  5. * @brief HAL module driver.
  6. * This is the common part of the HAL initialization
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2019 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. @verbatim
  19. ==============================================================================
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..]
  23. The common HAL driver contains a set of generic and common APIs that can be
  24. used by the PPP peripheral drivers and the user to start using the HAL.
  25. [..]
  26. The HAL contains two APIs' categories:
  27. (+) Common HAL APIs
  28. (+) Services HAL APIs
  29. @endverbatim
  30. ******************************************************************************
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32g4xx_hal.h"
  34. /** @addtogroup STM32G4xx_HAL_Driver
  35. * @{
  36. */
  37. /** @defgroup HAL HAL
  38. * @brief HAL module driver
  39. * @{
  40. */
  41. #ifdef HAL_MODULE_ENABLED
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /**
  45. * @brief STM32G4xx HAL Driver version number V1.2.5
  46. */
  47. #define __STM32G4xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  48. #define __STM32G4xx_HAL_VERSION_SUB1 (0x02U) /*!< [23:16] sub1 version */
  49. #define __STM32G4xx_HAL_VERSION_SUB2 (0x05U) /*!< [15:8] sub2 version */
  50. #define __STM32G4xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  51. #define __STM32G4xx_HAL_VERSION ((__STM32G4xx_HAL_VERSION_MAIN << 24U)\
  52. |(__STM32G4xx_HAL_VERSION_SUB1 << 16U)\
  53. |(__STM32G4xx_HAL_VERSION_SUB2 << 8U )\
  54. |(__STM32G4xx_HAL_VERSION_RC))
  55. #if defined(VREFBUF)
  56. #define VREFBUF_TIMEOUT_VALUE 10U /* 10 ms */
  57. #endif /* VREFBUF */
  58. /* ------------ SYSCFG registers bit address in the alias region ------------ */
  59. #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
  60. /* --- MEMRMP Register ---*/
  61. /* Alias word address of FB_MODE bit */
  62. #define MEMRMP_OFFSET SYSCFG_OFFSET
  63. #define FB_MODE_BitNumber ((uint8_t)0x8)
  64. #define FB_MODE_BB (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32) + (FB_MODE_BitNumber * 4))
  65. /* --- GPC Register ---*/
  66. /* Alias word address of CCMER bit */
  67. #define SCSR_OFFSET (SYSCFG_OFFSET + 0x18)
  68. #define CCMER_BitNumber ((uint8_t)0x0)
  69. #define SCSR_CCMER_BB (PERIPH_BB_BASE + (SCSR_OFFSET * 32) + (CCMER_BitNumber * 4))
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Exported variables ---------------------------------------------------------*/
  72. /** @defgroup HAL_Exported_Variables HAL Exported Variables
  73. * @{
  74. */
  75. __IO uint32_t uwTick;
  76. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
  77. uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  78. /**
  79. * @}
  80. */
  81. /* Private function prototypes -----------------------------------------------*/
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  84. * @{
  85. */
  86. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  87. * @brief HAL Initialization and de-initialization functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Initialization and Configuration functions #####
  92. ===============================================================================
  93. [..] This section provides functions allowing to:
  94. (+) Initialize the Flash interface the NVIC allocation and initial time base
  95. clock configuration.
  96. (+) De-Initialize common part of the HAL.
  97. (+) Configure the time base source to have 1ms time base with a dedicated
  98. Tick interrupt priority.
  99. (++) SysTick timer is used by default as source of time base, but user
  100. can eventually implement his proper time base source (a general purpose
  101. timer for example or other time source), keeping in mind that Time base
  102. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  103. handled in milliseconds basis.
  104. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  105. at the beginning of the program after reset by HAL_Init() or at any time
  106. when clock is configured, by HAL_RCC_ClockConfig().
  107. (++) Source of time base is configured to generate interrupts at regular
  108. time intervals. Care must be taken if HAL_Delay() is called from a
  109. peripheral ISR process, the Tick interrupt line must have higher priority
  110. (numerically lower) than the peripheral interrupt. Otherwise the caller
  111. ISR process will be blocked.
  112. (++) functions affecting time base configurations are declared as __weak
  113. to make override possible in case of other implementations in user file.
  114. @endverbatim
  115. * @{
  116. */
  117. /**
  118. * @brief This function is used to configure the Flash prefetch, the Instruction and Data caches,
  119. * the time base source, NVIC and any required global low level hardware
  120. * by calling the HAL_MspInit() callback function to be optionally defined in user file
  121. * stm32g4xx_hal_msp.c.
  122. *
  123. * @note HAL_Init() function is called at the beginning of program after reset and before
  124. * the clock configuration.
  125. *
  126. * @note In the default implementation the System Timer (Systick) is used as source of time base.
  127. * The Systick configuration is based on HSI clock, as HSI is the clock
  128. * used after a system Reset and the NVIC configuration is set to Priority group 4.
  129. * Once done, time base tick starts incrementing: the tick variable counter is incremented
  130. * each 1ms in the SysTick_Handler() interrupt handler.
  131. *
  132. * @retval HAL status
  133. */
  134. HAL_StatusTypeDef HAL_Init(void)
  135. {
  136. HAL_StatusTypeDef status = HAL_OK;
  137. /* Configure Flash prefetch, Instruction cache, Data cache */
  138. /* Default configuration at reset is: */
  139. /* - Prefetch disabled */
  140. /* - Instruction cache enabled */
  141. /* - Data cache enabled */
  142. #if (INSTRUCTION_CACHE_ENABLE == 0U)
  143. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  144. #endif /* INSTRUCTION_CACHE_ENABLE */
  145. #if (DATA_CACHE_ENABLE == 0U)
  146. __HAL_FLASH_DATA_CACHE_DISABLE();
  147. #endif /* DATA_CACHE_ENABLE */
  148. #if (PREFETCH_ENABLE != 0U)
  149. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  150. #endif /* PREFETCH_ENABLE */
  151. /* Set Interrupt Group Priority */
  152. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  153. /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  154. if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  155. {
  156. status = HAL_ERROR;
  157. }
  158. else
  159. {
  160. /* Init the low level hardware */
  161. HAL_MspInit();
  162. }
  163. /* Return function status */
  164. return status;
  165. }
  166. /**
  167. * @brief This function de-initializes common part of the HAL and stops the source of time base.
  168. * @note This function is optional.
  169. * @retval HAL status
  170. */
  171. HAL_StatusTypeDef HAL_DeInit(void)
  172. {
  173. /* Reset of all peripherals */
  174. __HAL_RCC_APB1_FORCE_RESET();
  175. __HAL_RCC_APB1_RELEASE_RESET();
  176. __HAL_RCC_APB2_FORCE_RESET();
  177. __HAL_RCC_APB2_RELEASE_RESET();
  178. __HAL_RCC_AHB1_FORCE_RESET();
  179. __HAL_RCC_AHB1_RELEASE_RESET();
  180. __HAL_RCC_AHB2_FORCE_RESET();
  181. __HAL_RCC_AHB2_RELEASE_RESET();
  182. __HAL_RCC_AHB3_FORCE_RESET();
  183. __HAL_RCC_AHB3_RELEASE_RESET();
  184. /* De-Init the low level hardware */
  185. HAL_MspDeInit();
  186. /* Return function status */
  187. return HAL_OK;
  188. }
  189. /**
  190. * @brief Initialize the MSP.
  191. * @retval None
  192. */
  193. __weak void HAL_MspInit(void)
  194. {
  195. /* NOTE : This function should not be modified, when the callback is needed,
  196. the HAL_MspInit could be implemented in the user file
  197. */
  198. }
  199. /**
  200. * @brief DeInitializes the MSP.
  201. * @retval None
  202. */
  203. __weak void HAL_MspDeInit(void)
  204. {
  205. /* NOTE : This function should not be modified, when the callback is needed,
  206. the HAL_MspDeInit could be implemented in the user file
  207. */
  208. }
  209. /**
  210. * @brief This function configures the source of the time base:
  211. * The time source is configured to have 1ms time base with a dedicated
  212. * Tick interrupt priority.
  213. * @note This function is called automatically at the beginning of program after
  214. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  215. * @note In the default implementation, SysTick timer is the source of time base.
  216. * It is used to generate interrupts at regular time intervals.
  217. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  218. * The SysTick interrupt must have higher priority (numerically lower)
  219. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  220. * The function is declared as __weak to be overwritten in case of other
  221. * implementation in user file.
  222. * @param TickPriority: Tick interrupt priority.
  223. * @retval HAL status
  224. */
  225. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  226. {
  227. HAL_StatusTypeDef status = HAL_OK;
  228. if (uwTickFreq != 0U)
  229. {
  230. /* Configure the SysTick to have interrupt in 1ms time basis*/
  231. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) == 0U)
  232. {
  233. /* Configure the SysTick IRQ priority */
  234. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  235. {
  236. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  237. uwTickPrio = TickPriority;
  238. }
  239. else
  240. {
  241. status = HAL_ERROR;
  242. }
  243. }
  244. else
  245. {
  246. status = HAL_ERROR;
  247. }
  248. }
  249. else
  250. {
  251. status = HAL_ERROR;
  252. }
  253. /* Return function status */
  254. return status;
  255. }
  256. /**
  257. * @}
  258. */
  259. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  260. * @brief HAL Control functions
  261. *
  262. @verbatim
  263. ===============================================================================
  264. ##### HAL Control functions #####
  265. ===============================================================================
  266. [..] This section provides functions allowing to:
  267. (+) Provide a tick value in millisecond
  268. (+) Provide a blocking delay in millisecond
  269. (+) Suspend the time base source interrupt
  270. (+) Resume the time base source interrupt
  271. (+) Get the HAL API driver version
  272. (+) Get the device identifier
  273. (+) Get the device revision identifier
  274. @endverbatim
  275. * @{
  276. */
  277. /**
  278. * @brief This function is called to increment a global variable "uwTick"
  279. * used as application time base.
  280. * @note In the default implementation, this variable is incremented each 1ms
  281. * in SysTick ISR.
  282. * @note This function is declared as __weak to be overwritten in case of other
  283. * implementations in user file.
  284. * @retval None
  285. */
  286. __weak void HAL_IncTick(void)
  287. {
  288. uwTick += uwTickFreq;
  289. }
  290. /**
  291. * @brief Provides a tick value in millisecond.
  292. * @note This function is declared as __weak to be overwritten in case of other
  293. * implementations in user file.
  294. * @retval tick value
  295. */
  296. __weak uint32_t HAL_GetTick(void)
  297. {
  298. return uwTick;
  299. }
  300. /**
  301. * @brief This function returns a tick priority.
  302. * @retval tick priority
  303. */
  304. uint32_t HAL_GetTickPrio(void)
  305. {
  306. return uwTickPrio;
  307. }
  308. /**
  309. * @brief Set new tick Freq.
  310. * @retval status
  311. */
  312. HAL_StatusTypeDef HAL_SetTickFreq(uint32_t Freq)
  313. {
  314. HAL_StatusTypeDef status = HAL_OK;
  315. uint32_t prevTickFreq;
  316. assert_param(IS_TICKFREQ(Freq));
  317. if (uwTickFreq != Freq)
  318. {
  319. /* Back up uwTickFreq frequency */
  320. prevTickFreq = uwTickFreq;
  321. /* Update uwTickFreq global variable used by HAL_InitTick() */
  322. uwTickFreq = Freq;
  323. /* Apply the new tick Freq */
  324. status = HAL_InitTick(uwTickPrio);
  325. if (status != HAL_OK)
  326. {
  327. /* Restore previous tick frequency */
  328. uwTickFreq = prevTickFreq;
  329. }
  330. }
  331. return status;
  332. }
  333. /**
  334. * @brief Returns tick frequency.
  335. * @retval Tick frequency.
  336. * Value of @ref HAL_TickFreqTypeDef.
  337. */
  338. uint32_t HAL_GetTickFreq(void)
  339. {
  340. return uwTickFreq;
  341. }
  342. /**
  343. * @brief This function provides minimum delay (in milliseconds) based
  344. * on variable incremented.
  345. * @note In the default implementation , SysTick timer is the source of time base.
  346. * It is used to generate interrupts at regular time intervals where uwTick
  347. * is incremented.
  348. * @note This function is declared as __weak to be overwritten in case of other
  349. * implementations in user file.
  350. * @param Delay specifies the delay time length, in milliseconds.
  351. * @retval None
  352. */
  353. __weak void HAL_Delay(uint32_t Delay)
  354. {
  355. uint32_t tickstart = HAL_GetTick();
  356. uint32_t wait = Delay;
  357. /* Add a freq to guarantee minimum wait */
  358. if (wait < HAL_MAX_DELAY)
  359. {
  360. wait += (uint32_t)(uwTickFreq);
  361. }
  362. while ((HAL_GetTick() - tickstart) < wait)
  363. {
  364. }
  365. }
  366. /**
  367. * @brief Suspends Tick increment.
  368. * @note In the default implementation , SysTick timer is the source of time base. It is
  369. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  370. * is called, the SysTick interrupt will be disabled and so Tick increment
  371. * is suspended.
  372. * @note This function is declared as __weak to be overwritten in case of other
  373. * implementations in user file.
  374. * @retval None
  375. */
  376. __weak void HAL_SuspendTick(void)
  377. {
  378. /* Disable SysTick Interrupt */
  379. CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  380. }
  381. /**
  382. * @brief Resume Tick increment.
  383. * @note In the default implementation , SysTick timer is the source of time base. It is
  384. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  385. * is called, the SysTick interrupt will be enabled and so Tick increment
  386. * is resumed.
  387. * @note This function is declared as __weak to be overwritten in case of other
  388. * implementations in user file.
  389. * @retval None
  390. */
  391. __weak void HAL_ResumeTick(void)
  392. {
  393. /* Enable SysTick Interrupt */
  394. SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  395. }
  396. /**
  397. * @brief Returns the HAL revision.
  398. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  399. */
  400. uint32_t HAL_GetHalVersion(void)
  401. {
  402. return __STM32G4xx_HAL_VERSION;
  403. }
  404. /**
  405. * @brief Returns the device revision identifier.
  406. * @retval Device revision identifier
  407. */
  408. uint32_t HAL_GetREVID(void)
  409. {
  410. return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> 16U);
  411. }
  412. /**
  413. * @brief Returns the device identifier.
  414. * @retval Device identifier
  415. */
  416. uint32_t HAL_GetDEVID(void)
  417. {
  418. return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
  419. }
  420. /**
  421. * @brief Return the first word of the unique device identifier (UID based on 96 bits)
  422. * @retval Device identifier
  423. */
  424. uint32_t HAL_GetUIDw0(void)
  425. {
  426. return (READ_REG(*((uint32_t *)UID_BASE)));
  427. }
  428. /**
  429. * @brief Return the second word of the unique device identifier (UID based on 96 bits)
  430. * @retval Device identifier
  431. */
  432. uint32_t HAL_GetUIDw1(void)
  433. {
  434. return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  435. }
  436. /**
  437. * @brief Return the third word of the unique device identifier (UID based on 96 bits)
  438. * @retval Device identifier
  439. */
  440. uint32_t HAL_GetUIDw2(void)
  441. {
  442. return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  443. }
  444. /**
  445. * @}
  446. */
  447. /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
  448. * @brief HAL Debug functions
  449. *
  450. @verbatim
  451. ===============================================================================
  452. ##### HAL Debug functions #####
  453. ===============================================================================
  454. [..] This section provides functions allowing to:
  455. (+) Enable/Disable Debug module during SLEEP mode
  456. (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
  457. (+) Enable/Disable Debug module during STANDBY mode
  458. @endverbatim
  459. * @{
  460. */
  461. /**
  462. * @brief Enable the Debug Module during SLEEP mode.
  463. * @retval None
  464. */
  465. void HAL_DBGMCU_EnableDBGSleepMode(void)
  466. {
  467. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  468. }
  469. /**
  470. * @brief Disable the Debug Module during SLEEP mode.
  471. * @retval None
  472. */
  473. void HAL_DBGMCU_DisableDBGSleepMode(void)
  474. {
  475. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  476. }
  477. /**
  478. * @brief Enable the Debug Module during STOP0/STOP1/STOP2 modes.
  479. * @retval None
  480. */
  481. void HAL_DBGMCU_EnableDBGStopMode(void)
  482. {
  483. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  484. }
  485. /**
  486. * @brief Disable the Debug Module during STOP0/STOP1/STOP2 modes.
  487. * @retval None
  488. */
  489. void HAL_DBGMCU_DisableDBGStopMode(void)
  490. {
  491. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  492. }
  493. /**
  494. * @brief Enable the Debug Module during STANDBY mode.
  495. * @retval None
  496. */
  497. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  498. {
  499. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  500. }
  501. /**
  502. * @brief Disable the Debug Module during STANDBY mode.
  503. * @retval None
  504. */
  505. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  506. {
  507. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  508. }
  509. /**
  510. * @}
  511. */
  512. /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
  513. * @brief HAL SYSCFG configuration functions
  514. *
  515. @verbatim
  516. ===============================================================================
  517. ##### HAL SYSCFG configuration functions #####
  518. ===============================================================================
  519. [..] This section provides functions allowing to:
  520. (+) Start a hardware CCMSRAM erase operation
  521. (+) Enable/Disable the Internal FLASH Bank Swapping
  522. (+) Configure the Voltage reference buffer
  523. (+) Enable/Disable the Voltage reference buffer
  524. (+) Enable/Disable the I/O analog switch voltage booster
  525. @endverbatim
  526. * @{
  527. */
  528. /**
  529. * @brief Start a hardware CCMSRAM erase operation.
  530. * @note As long as CCMSRAM is not erased the CCMER bit will be set.
  531. * This bit is automatically reset at the end of the CCMSRAM erase operation.
  532. * @retval None
  533. */
  534. void HAL_SYSCFG_CCMSRAMErase(void)
  535. {
  536. /* unlock the write protection of the CCMER bit */
  537. SYSCFG->SKR = 0xCA;
  538. SYSCFG->SKR = 0x53;
  539. /* Starts a hardware CCMSRAM erase operation*/
  540. SET_BIT(SYSCFG->SCSR, SYSCFG_SCSR_CCMER);
  541. }
  542. /**
  543. * @brief Enable the Internal FLASH Bank Swapping.
  544. *
  545. * @note This function can be used only for STM32G4xx devices.
  546. *
  547. * @note Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
  548. * and Flash Bank1 mapped at 0x08040000 (and aliased at 0x00040000)
  549. *
  550. * @retval None
  551. */
  552. void HAL_SYSCFG_EnableMemorySwappingBank(void)
  553. {
  554. SET_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE);
  555. }
  556. /**
  557. * @brief Disable the Internal FLASH Bank Swapping.
  558. *
  559. * @note This function can be used only for STM32G4xx devices.
  560. *
  561. * @note The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000)
  562. * and Flash Bank2 mapped at 0x08040000 (and aliased at 0x00040000)
  563. *
  564. * @retval None
  565. */
  566. void HAL_SYSCFG_DisableMemorySwappingBank(void)
  567. {
  568. CLEAR_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE);
  569. }
  570. #if defined(VREFBUF)
  571. /**
  572. * @brief Configure the internal voltage reference buffer voltage scale.
  573. * @param VoltageScaling: specifies the output voltage to achieve
  574. * This parameter can be one of the following values:
  575. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREFBUF_OUT around 2.048 V.
  576. * This requires VDDA equal to or higher than 2.4 V.
  577. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREFBUF_OUT around 2.5 V.
  578. * This requires VDDA equal to or higher than 2.8 V.
  579. * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE2: VREFBUF_OUT around 2.9 V.
  580. * This requires VDDA equal to or higher than 3.15 V.
  581. * @retval None
  582. */
  583. void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
  584. {
  585. /* Check the parameters */
  586. assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
  587. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
  588. }
  589. /**
  590. * @brief Configure the internal voltage reference buffer high impedance mode.
  591. * @param Mode: specifies the high impedance mode
  592. * This parameter can be one of the following values:
  593. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
  594. * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
  595. * @retval None
  596. */
  597. void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
  598. {
  599. /* Check the parameters */
  600. assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
  601. MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
  602. }
  603. /**
  604. * @brief Tune the Internal Voltage Reference buffer (VREFBUF).
  605. * @param TrimmingValue specifies trimming code for VREFBUF calibration
  606. * This parameter can be a number between Min_Data = 0x00 and Max_Data = 0x3F
  607. * @retval None
  608. */
  609. void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
  610. {
  611. /* Check the parameters */
  612. assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
  613. MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
  614. }
  615. /**
  616. * @brief Enable the Internal Voltage Reference buffer (VREFBUF).
  617. * @retval HAL_OK/HAL_TIMEOUT
  618. */
  619. HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
  620. {
  621. uint32_t tickstart;
  622. SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  623. /* Get Start Tick*/
  624. tickstart = HAL_GetTick();
  625. /* Wait for VRR bit */
  626. while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0x00U)
  627. {
  628. if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
  629. {
  630. return HAL_TIMEOUT;
  631. }
  632. }
  633. return HAL_OK;
  634. }
  635. /**
  636. * @brief Disable the Internal Voltage Reference buffer (VREFBUF).
  637. *
  638. * @retval None
  639. */
  640. void HAL_SYSCFG_DisableVREFBUF(void)
  641. {
  642. CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
  643. }
  644. #endif /* VREFBUF */
  645. /**
  646. * @brief Enable the I/O analog switch voltage booster
  647. *
  648. * @retval None
  649. */
  650. void HAL_SYSCFG_EnableIOSwitchBooster(void)
  651. {
  652. SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
  653. }
  654. /**
  655. * @brief Disable the I/O analog switch voltage booster
  656. *
  657. * @retval None
  658. */
  659. void HAL_SYSCFG_DisableIOSwitchBooster(void)
  660. {
  661. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
  662. }
  663. /**
  664. * @brief Enable the I/O analog switch voltage by VDD
  665. *
  666. * @retval None
  667. */
  668. void HAL_SYSCFG_EnableIOSwitchVDD(void)
  669. {
  670. SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
  671. }
  672. /**
  673. * @brief Disable the I/O analog switch voltage by VDD
  674. *
  675. * @retval None
  676. */
  677. void HAL_SYSCFG_DisableIOSwitchVDD(void)
  678. {
  679. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
  680. }
  681. /** @brief CCMSRAM page write protection enable
  682. * @param Page: This parameter is a long 32bit value and can be a value of @ref SYSCFG_CCMSRAMWRP
  683. * @note write protection can only be disabled by a system reset
  684. * @retval None
  685. */
  686. void HAL_SYSCFG_CCMSRAM_WriteProtectionEnable(uint32_t Page)
  687. {
  688. assert_param(IS_SYSCFG_CCMSRAMWRP_PAGE(Page));
  689. SET_BIT(SYSCFG->SWPR, (uint32_t)(Page));
  690. }
  691. /**
  692. * @}
  693. */
  694. /**
  695. * @}
  696. */
  697. #endif /* HAL_MODULE_ENABLED */
  698. /**
  699. * @}
  700. */
  701. /**
  702. * @}
  703. */