Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal.c
blob996fa76f36a6dd5a03febf5f216e979ef2abdc45
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal.c
4 * @author MCD Application Team
5 * @brief HAL module driver.
6 * This is the common part of the HAL initialization
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
20 @endverbatim
21 ******************************************************************************
22 * @attention
24 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
25 * All rights reserved.</center></h2>
27 * This software component is licensed by ST under BSD 3-Clause license,
28 * the "License"; You may not use this file except in compliance with the
29 * License. You may obtain a copy of the License at:
30 * opensource.org/licenses/BSD-3-Clause
32 ******************************************************************************
33 */
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32f7xx_hal.h"
38 /** @addtogroup STM32F7xx_HAL_Driver
39 * @{
42 /** @defgroup HAL HAL
43 * @brief HAL module driver.
44 * @{
47 /* Private typedef -----------------------------------------------------------*/
48 /* Private define ------------------------------------------------------------*/
49 /** @addtogroup HAL_Private_Constants
50 * @{
52 /**
53 * @brief STM32F7xx HAL Driver version number V1.2.7
55 #define __STM32F7xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
56 #define __STM32F7xx_HAL_VERSION_SUB1 (0x02) /*!< [23:16] sub1 version */
57 #define __STM32F7xx_HAL_VERSION_SUB2 (0x07) /*!< [15:8] sub2 version */
58 #define __STM32F7xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
59 #define __STM32F7xx_HAL_VERSION ((__STM32F7xx_HAL_VERSION_MAIN << 24)\
60 |(__STM32F7xx_HAL_VERSION_SUB1 << 16)\
61 |(__STM32F7xx_HAL_VERSION_SUB2 << 8 )\
62 |(__STM32F7xx_HAL_VERSION_RC))
64 #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
65 /**
66 * @}
69 /* Private macro -------------------------------------------------------------*/
70 /* Exported variables ---------------------------------------------------------*/
71 /** @addtogroup HAL_Exported_Variables
72 * @{
74 __IO uint32_t uwTick;
75 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
76 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
77 /**
78 * @}
81 /* Private function prototypes -----------------------------------------------*/
82 /* Private functions ---------------------------------------------------------*/
84 /** @defgroup HAL_Exported_Functions HAL Exported Functions
85 * @{
88 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
89 * @brief Initialization and de-initialization functions
91 @verbatim
92 ===============================================================================
93 ##### Initialization and Configuration functions #####
94 ===============================================================================
95 [..] This section provides functions allowing to:
96 (+) Initializes the Flash interface the NVIC allocation and initial clock
97 configuration. It initializes the systick also when timeout is needed
98 and the backup domain when enabled.
99 (+) De-Initializes common part of the HAL.
100 (+) Configure the time base source to have 1ms time base with a dedicated
101 Tick interrupt priority.
102 (++) SysTick timer is used by default as source of time base, but user
103 can eventually implement his proper time base source (a general purpose
104 timer for example or other time source), keeping in mind that Time base
105 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
106 handled in milliseconds basis.
107 (++) Time base configuration function (HAL_InitTick ()) is called automatically
108 at the beginning of the program after reset by HAL_Init() or at any time
109 when clock is configured, by HAL_RCC_ClockConfig().
110 (++) Source of time base is configured to generate interrupts at regular
111 time intervals. Care must be taken if HAL_Delay() is called from a
112 peripheral ISR process, the Tick interrupt line must have higher priority
113 (numerically lower) than the peripheral interrupt. Otherwise the caller
114 ISR process will be blocked.
115 (++) functions affecting time base configurations are declared as __weak
116 to make override possible in case of other implementations in user file.
117 @endverbatim
118 * @{
122 * @brief This function is used to initialize the HAL Library; it must be the first
123 * instruction to be executed in the main program (before to call any other
124 * HAL function), it performs the following:
125 * Configure the Flash prefetch, and instruction cache through ART accelerator.
126 * Configures the SysTick to generate an interrupt each 1 millisecond,
127 * which is clocked by the HSI (at this stage, the clock is not yet
128 * configured and thus the system is running from the internal HSI at 16 MHz).
129 * Set NVIC Group Priority to 4.
130 * Calls the HAL_MspInit() callback function defined in user file
131 * "stm32f7xx_hal_msp.c" to do the global low level hardware initialization
133 * @note SysTick is used as time base for the HAL_Delay() function, the application
134 * need to ensure that the SysTick time base is always set to 1 millisecond
135 * to have correct HAL operation.
136 * @retval HAL status
138 HAL_StatusTypeDef HAL_Init(void)
140 /* Configure Instruction cache through ART accelerator */
141 #if (ART_ACCLERATOR_ENABLE != 0)
142 __HAL_FLASH_ART_ENABLE();
143 #endif /* ART_ACCLERATOR_ENABLE */
145 /* Set Interrupt Group Priority */
146 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
148 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
149 HAL_InitTick(TICK_INT_PRIORITY);
151 /* Init the low level hardware */
152 HAL_MspInit();
154 /* Return function status */
155 return HAL_OK;
159 * @brief This function de-Initializes common part of the HAL and stops the systick.
160 * This function is optional.
161 * @retval HAL status
163 HAL_StatusTypeDef HAL_DeInit(void)
165 /* Reset of all peripherals */
166 __HAL_RCC_APB1_FORCE_RESET();
167 __HAL_RCC_APB1_RELEASE_RESET();
169 __HAL_RCC_APB2_FORCE_RESET();
170 __HAL_RCC_APB2_RELEASE_RESET();
172 __HAL_RCC_AHB1_FORCE_RESET();
173 __HAL_RCC_AHB1_RELEASE_RESET();
175 __HAL_RCC_AHB2_FORCE_RESET();
176 __HAL_RCC_AHB2_RELEASE_RESET();
178 __HAL_RCC_AHB3_FORCE_RESET();
179 __HAL_RCC_AHB3_RELEASE_RESET();
181 /* De-Init the low level hardware */
182 HAL_MspDeInit();
184 /* Return function status */
185 return HAL_OK;
189 * @brief Initialize the MSP.
190 * @retval None
192 __weak void HAL_MspInit(void)
194 /* NOTE : This function should not be modified, when the callback is needed,
195 the HAL_MspInit could be implemented in the user file
200 * @brief DeInitializes the MSP.
201 * @retval None
203 __weak void HAL_MspDeInit(void)
205 /* NOTE : This function should not be modified, when the callback is needed,
206 the HAL_MspDeInit could be implemented in the user file
211 * @brief This function configures the source of the time base.
212 * The time source is configured to have 1ms time base with a dedicated
213 * Tick interrupt priority.
214 * @note This function is called automatically at the beginning of program after
215 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
216 * @note In the default implementation, SysTick timer is the source of time base.
217 * It is used to generate interrupts at regular time intervals.
218 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
219 * The SysTick interrupt must have higher priority (numerically lower)
220 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
221 * The function is declared as __weak to be overwritten in case of other
222 * implementation in user file.
223 * @param TickPriority Tick interrupt priority.
224 * @retval HAL status
226 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
228 /* Configure the SysTick to have interrupt in 1ms time basis*/
229 if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
231 return HAL_ERROR;
234 /* Configure the SysTick IRQ priority */
235 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
237 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
238 uwTickPrio = TickPriority;
240 else
242 return HAL_ERROR;
245 /* Return function status */
246 return HAL_OK;
250 * @}
253 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
254 * @brief HAL Control functions
256 @verbatim
257 ===============================================================================
258 ##### HAL Control functions #####
259 ===============================================================================
260 [..] This section provides functions allowing to:
261 (+) Provide a tick value in millisecond
262 (+) Provide a blocking delay in millisecond
263 (+) Suspend the time base source interrupt
264 (+) Resume the time base source interrupt
265 (+) Get the HAL API driver version
266 (+) Get the device identifier
267 (+) Get the device revision identifier
268 (+) Enable/Disable Debug module during SLEEP mode
269 (+) Enable/Disable Debug module during STOP mode
270 (+) Enable/Disable Debug module during STANDBY mode
272 @endverbatim
273 * @{
277 * @brief This function is called to increment a global variable "uwTick"
278 * used as application time base.
279 * @note In the default implementation, this variable is incremented each 1ms
280 * in SysTick ISR.
281 * @note This function is declared as __weak to be overwritten in case of other
282 * implementations in user file.
283 * @retval None
285 __weak void HAL_IncTick(void)
287 uwTick += uwTickFreq;
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
296 __weak uint32_t HAL_GetTick(void)
298 return uwTick;
302 * @brief This function returns a tick priority.
303 * @retval tick priority
305 uint32_t HAL_GetTickPrio(void)
307 return uwTickPrio;
311 * @brief Set new tick Freq.
312 * @retval Status
314 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
316 HAL_StatusTypeDef status = HAL_OK;
317 assert_param(IS_TICKFREQ(Freq));
319 if (uwTickFreq != Freq)
321 uwTickFreq = Freq;
323 /* Apply the new tick Freq */
324 status = HAL_InitTick(uwTickPrio);
327 return status;
331 * @brief Return tick frequency.
332 * @retval tick period in Hz
334 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
336 return uwTickFreq;
340 * @brief This function provides minimum delay (in milliseconds) based
341 * on variable incremented.
342 * @note In the default implementation , SysTick timer is the source of time base.
343 * It is used to generate interrupts at regular time intervals where uwTick
344 * is incremented.
345 * @note This function is declared as __weak to be overwritten in case of other
346 * implementations in user file.
347 * @param Delay specifies the delay time length, in milliseconds.
348 * @retval None
350 __weak void HAL_Delay(uint32_t Delay)
352 uint32_t tickstart = HAL_GetTick();
353 uint32_t wait = Delay;
355 /* Add a freq to guarantee minimum wait */
356 if (wait < HAL_MAX_DELAY)
358 wait += (uint32_t)(uwTickFreq);
361 while ((HAL_GetTick() - tickstart) < wait)
367 * @brief Suspend 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
376 __weak void HAL_SuspendTick(void)
378 /* Disable SysTick Interrupt */
379 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
383 * @brief Resume Tick increment.
384 * @note In the default implementation , SysTick timer is the source of time base. It is
385 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
386 * is called, the SysTick interrupt will be enabled and so Tick increment
387 * is resumed.
388 * @note This function is declared as __weak to be overwritten in case of other
389 * implementations in user file.
390 * @retval None
392 __weak void HAL_ResumeTick(void)
394 /* Enable SysTick Interrupt */
395 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
399 * @brief Returns the HAL revision
400 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
402 uint32_t HAL_GetHalVersion(void)
404 return __STM32F7xx_HAL_VERSION;
408 * @brief Returns the device revision identifier.
409 * @retval Device revision identifier
411 uint32_t HAL_GetREVID(void)
413 return((DBGMCU->IDCODE) >> 16U);
417 * @brief Returns the device identifier.
418 * @retval Device identifier
420 uint32_t HAL_GetDEVID(void)
422 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
426 * @brief Returns first word of the unique device identifier (UID based on 96 bits)
427 * @retval Device identifier
429 uint32_t HAL_GetUIDw0(void)
431 return(READ_REG(*((uint32_t *)UID_BASE)));
435 * @brief Returns second word of the unique device identifier (UID based on 96 bits)
436 * @retval Device identifier
438 uint32_t HAL_GetUIDw1(void)
440 return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
444 * @brief Returns third word of the unique device identifier (UID based on 96 bits)
445 * @retval Device identifier
447 uint32_t HAL_GetUIDw2(void)
449 return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
453 * @brief Enable the Debug Module during SLEEP mode
454 * @retval None
456 void HAL_DBGMCU_EnableDBGSleepMode(void)
458 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
462 * @brief Disable the Debug Module during SLEEP mode
463 * @retval None
465 void HAL_DBGMCU_DisableDBGSleepMode(void)
467 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
471 * @brief Enable the Debug Module during STOP mode
472 * @retval None
474 void HAL_DBGMCU_EnableDBGStopMode(void)
476 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
480 * @brief Disable the Debug Module during STOP mode
481 * @retval None
483 void HAL_DBGMCU_DisableDBGStopMode(void)
485 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
489 * @brief Enable the Debug Module during STANDBY mode
490 * @retval None
492 void HAL_DBGMCU_EnableDBGStandbyMode(void)
494 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
498 * @brief Disable the Debug Module during STANDBY mode
499 * @retval None
501 void HAL_DBGMCU_DisableDBGStandbyMode(void)
503 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
507 * @brief Enables the I/O Compensation Cell.
508 * @note The I/O compensation cell can be used only when the device supply
509 * voltage ranges from 2.4 to 3.6 V.
510 * @retval None
512 void HAL_EnableCompensationCell(void)
514 SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;
518 * @brief Power-down the I/O Compensation Cell.
519 * @note The I/O compensation cell can be used only when the device supply
520 * voltage ranges from 2.4 to 3.6 V.
521 * @retval None
523 void HAL_DisableCompensationCell(void)
525 SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);
529 * @brief Enables the FMC Memory Mapping Swapping.
531 * @note SDRAM is accessible at 0x60000000
532 * and NOR/RAM is accessible at 0xC0000000
534 * @retval None
536 void HAL_EnableFMCMemorySwapping(void)
538 SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;
542 * @brief Disables the FMC Memory Mapping Swapping
544 * @note SDRAM is accessible at 0xC0000000 (default mapping)
545 * and NOR/RAM is accessible at 0x60000000 (default mapping)
547 * @retval None
549 void HAL_DisableFMCMemorySwapping(void)
552 SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);
555 #if defined (STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
557 * @brief Enable the Internal FLASH Bank Swapping.
559 * @note This function can be used only for STM32F77xx/STM32F76xx devices.
561 * @note Flash Bank2 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
562 * and Flash Bank1 mapped at 0x08100000 (AXI) (aliased at 0x00300000 (TCM))
564 * @retval None
566 void HAL_EnableMemorySwappingBank(void)
568 SET_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
572 * @brief Disable the Internal FLASH Bank Swapping.
574 * @note This function can be used only for STM32F77xx/STM32F76xx devices.
576 * @note The default state : Flash Bank1 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
577 * and Flash Bank2 mapped at 0x08100000 (AXI)( aliased at 0x00300000 (TCM))
579 * @retval None
581 void HAL_DisableMemorySwappingBank(void)
583 CLEAR_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
585 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
588 * @}
592 * @}
596 * @}
600 * @}
603 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/