Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F3xx_HAL_Driver / Src / stm32f3xx_hal.c
blob1c3eb9ed4fedd7d898b88faa2b47339ec1a75bc3
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_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 (+) HAL Initialization and de-initialization functions
18 (+) HAL Control functions
20 @endverbatim
21 ******************************************************************************
22 * @attention
24 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 ******************************************************************************
51 /* Includes ------------------------------------------------------------------*/
52 #include "stm32f3xx_hal.h"
54 /** @addtogroup STM32F3xx_HAL_Driver
55 * @{
58 /** @defgroup HAL HAL
59 * @brief HAL module driver.
60 * @{
63 #ifdef HAL_MODULE_ENABLED
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /** @defgroup HAL_Private Constants
68 * @{
70 /**
71 * @brief STM32F3xx HAL Driver version number V1.5.0
73 #define __STM32F3xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
74 #define __STM32F3xx_HAL_VERSION_SUB1 (0x05U) /*!< [23:16] sub1 version */
75 #define __STM32F3xx_HAL_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
76 #define __STM32F3xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
77 #define __STM32F3xx_HAL_VERSION ((__STM32F3xx_HAL_VERSION_MAIN << 24U)\
78 |(__STM32F3xx_HAL_VERSION_SUB1 << 16U)\
79 |(__STM32F3xx_HAL_VERSION_SUB2 << 8U )\
80 |(__STM32F3xx_HAL_VERSION_RC))
82 #define IDCODE_DEVID_MASK (0x00000FFFU)
83 /**
84 * @}
87 /* Private macro -------------------------------------------------------------*/
88 /* Private variables ---------------------------------------------------------*/
89 /** @defgroup HAL_Private_Variables HAL Private Variables
90 * @{
92 __IO uint32_t uwTick;
93 /**
94 * @}
96 /* Private function prototypes -----------------------------------------------*/
97 /* Exported functions ---------------------------------------------------------*/
99 /** @defgroup HAL_Exported_Functions HAL Exported Functions
100 * @{
103 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
104 * @brief Initialization and de-initialization functions
106 @verbatim
107 ===============================================================================
108 ##### Initialization and de-initialization functions #####
109 ===============================================================================
110 [..] This section provides functions allowing to:
111 (+) Initializes the Flash interface, the NVIC allocation and initial clock
112 configuration. It initializes the source of time base also when timeout
113 is needed and the backup domain when enabled.
114 (+) de-Initializes common part of the HAL.
115 (+) Configure The time base source to have 1ms time base with a dedicated
116 Tick interrupt priority.
117 (++) Systick timer is used by default as source of time base, but user
118 can eventually implement his proper time base source (a general purpose
119 timer for example or other time source), keeping in mind that Time base
120 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
121 handled in milliseconds basis.
122 (++) Time base configuration function (HAL_InitTick ()) is called automatically
123 at the beginning of the program after reset by HAL_Init() or at any time
124 when clock is configured, by HAL_RCC_ClockConfig().
125 (++) Source of time base is configured to generate interrupts at regular
126 time intervals. Care must be taken if HAL_Delay() is called from a
127 peripheral ISR process, the Tick interrupt line must have higher priority
128 (numerically lower) than the peripheral interrupt. Otherwise the caller
129 ISR process will be blocked.
130 (++) functions affecting time base configurations are declared as __Weak
131 to make override possible in case of other implementations in user file.
133 @endverbatim
134 * @{
138 * @brief This function configures the Flash prefetch,
139 * Configures time base source, NVIC and Low level hardware
140 * @note This function is called at the beginning of program after reset and before
141 * the clock configuration
143 * @note The Systick configuration is based on HSI clock, as HSI is the clock
144 * used after a system Reset and the NVIC configuration is set to Priority group 4
146 * @note The time base configuration is based on MSI clock when exting from Reset.
147 * Once done, time base tick start incrementing.
148 * In the default implementation,Systick is used as source of time base.
149 * The tick variable is incremented each 1ms in its ISR.
150 * @retval HAL status
152 HAL_StatusTypeDef HAL_Init(void)
154 /* Configure Flash prefetch */
155 #if (PREFETCH_ENABLE != 0U)
156 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
157 #endif /* PREFETCH_ENABLE */
159 /* Set Interrupt Group Priority */
160 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
162 /* Enable systick and configure 1ms tick (default clock after Reset is HSI) */
163 HAL_InitTick(TICK_INT_PRIORITY);
165 /* Init the low level hardware */
166 HAL_MspInit();
168 /* Return function status */
169 return HAL_OK;
173 * @brief This function de-Initializes common part of the HAL and stops the source
174 * of time base.
175 * @note This function is optional.
176 * @retval HAL status
178 HAL_StatusTypeDef HAL_DeInit(void)
180 /* Reset of all peripherals */
181 __HAL_RCC_APB1_FORCE_RESET();
182 __HAL_RCC_APB1_RELEASE_RESET();
184 __HAL_RCC_APB2_FORCE_RESET();
185 __HAL_RCC_APB2_RELEASE_RESET();
187 __HAL_RCC_AHB_FORCE_RESET();
188 __HAL_RCC_AHB_RELEASE_RESET();
190 /* De-Init the low level hardware */
191 HAL_MspDeInit();
193 /* Return function status */
194 return HAL_OK;
198 * @brief Initializes the MSP.
199 * @retval None
201 __weak void HAL_MspInit(void)
203 /* NOTE : This function Should not be modified, when the callback is needed,
204 the HAL_MspInit could be implemented in the user file
209 * @brief DeInitializes the MSP.
210 * @retval None
212 __weak void HAL_MspDeInit(void)
214 /* NOTE : This function Should not be modified, when the callback is needed,
215 the HAL_MspDeInit could be implemented in the user file
220 * @brief This function configures the source of the time base.
221 * The time source is configured to have 1ms time base with a dedicated
222 * Tick interrupt priority.
223 * @note This function is called automatically at the beginning of program after
224 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
225 * @note In the default implementation , SysTick timer is the source of time base.
226 * It is used to generate interrupts at regular time intervals.
227 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
228 * The the SysTick interrupt must have higher priority (numerically lower)
229 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
230 * The function is declared as __Weak to be overwritten in case of other
231 * implementation in user file.
232 * @param TickPriority Tick interrupt priority.
233 * @retval HAL status
235 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
237 /*Configure the SysTick to have interrupt in 1ms time basis*/
238 HAL_SYSTICK_Config(SystemCoreClock / 1000U);
240 /*Configure the SysTick IRQ priority */
241 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
243 /* Return function status */
244 return HAL_OK;
248 * @}
251 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
252 * @brief HAL Control functions
254 @verbatim
255 ===============================================================================
256 ##### HAL Control functions #####
257 ===============================================================================
258 [..] This section provides functions allowing to:
259 (+) Provide a tick value in millisecond
260 (+) Provide a blocking delay in millisecond
261 (+) Suspend the time base source interrupt
262 (+) Resume the time base source interrupt
263 (+) Get the HAL API driver version
264 (+) Get the device identifier
265 (+) Get the device revision identifier
266 (+) Enable/Disable Debug module during Sleep mode
267 (+) Enable/Disable Debug module during STOP mode
268 (+) Enable/Disable Debug module during STANDBY mode
270 @endverbatim
271 * @{
275 * @brief This function is called to increment a global variable "uwTick"
276 * used as application time base.
277 * @note In the default implementation, this variable is incremented each 1ms
278 * in Systick ISR.
279 * @note This function is declared as __weak to be overwritten in case of other
280 * implementations in user file.
281 * @retval None
283 __weak void HAL_IncTick(void)
285 uwTick++;
289 * @brief Povides a tick value in millisecond.
290 * @note The function is declared as __Weak to be overwritten in case of other
291 * implementations in user file.
292 * @retval tick value
294 __weak uint32_t HAL_GetTick(void)
296 return uwTick;
300 * @brief This function provides accurate delay (in milliseconds) based
301 * on variable incremented.
302 * @note In the default implementation , SysTick timer is the source of time base.
303 * It is used to generate interrupts at regular time intervals where uwTick
304 * is incremented.
305 * The function is declared as __Weak to be overwritten in case of other
306 * implementations in user file.
307 * @param Delay specifies the delay time length, in milliseconds.
308 * @retval None
310 __weak void HAL_Delay(__IO uint32_t Delay)
312 uint32_t tickstart = HAL_GetTick();
313 uint32_t wait = Delay;
315 /* Add a period to guarantee minimum wait */
316 if (wait < HAL_MAX_DELAY)
318 wait++;
321 while((HAL_GetTick() - tickstart) < wait)
327 * @brief Suspend Tick increment.
328 * @note In the default implementation , SysTick timer is the source of time base. It is
329 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
330 * is called, the the SysTick interrupt will be disabled and so Tick increment
331 * is suspended.
332 * @note This function is declared as __weak to be overwritten in case of other
333 * implementations in user file.
334 * @retval None
336 __weak void HAL_SuspendTick(void)
339 /* Disable SysTick Interrupt */
340 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
345 * @brief Resume Tick increment.
346 * @note In the default implementation , SysTick timer is the source of time base. It is
347 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
348 * is called, the the SysTick interrupt will be enabled and so Tick increment
349 * is resumed.
350 * The function is declared as __Weak to be overwritten in case of other
351 * implementations in user file.
352 * @retval None
354 __weak void HAL_ResumeTick(void)
356 /* Enable SysTick Interrupt */
357 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
362 * @brief This function returns the HAL revision
363 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
365 uint32_t HAL_GetHalVersion(void)
367 return __STM32F3xx_HAL_VERSION;
371 * @brief Returns the device revision identifier.
372 * @retval Device revision identifier
374 uint32_t HAL_GetREVID(void)
376 return((DBGMCU->IDCODE) >> 16U);
380 * @brief Returns the device identifier.
381 * @retval Device identifier
383 uint32_t HAL_GetDEVID(void)
385 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
389 * @brief Returns first word of the unique device identifier (UID based on 96 bits)
390 * @retval Device identifier
392 uint32_t HAL_GetUIDw0(void)
394 return(READ_REG(*((uint32_t *)UID_BASE)));
398 * @brief Returns second word of the unique device identifier (UID based on 96 bits)
399 * @retval Device identifier
401 uint32_t HAL_GetUIDw1(void)
403 return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
407 * @brief Returns third word of the unique device identifier (UID based on 96 bits)
408 * @retval Device identifier
410 uint32_t HAL_GetUIDw2(void)
412 return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
416 * @brief Enable the Debug Module during SLEEP mode
417 * @retval None
419 void HAL_DBGMCU_EnableDBGSleepMode(void)
421 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
425 * @brief Disable the Debug Module during SLEEP mode
426 * @retval None
428 void HAL_DBGMCU_DisableDBGSleepMode(void)
430 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
434 * @brief Enable the Debug Module during STOP mode
435 * @retval None
437 void HAL_DBGMCU_EnableDBGStopMode(void)
439 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
443 * @brief Disable the Debug Module during STOP mode
444 * @retval None
446 void HAL_DBGMCU_DisableDBGStopMode(void)
448 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
452 * @brief Enable the Debug Module during STANDBY mode
453 * @retval None
455 void HAL_DBGMCU_EnableDBGStandbyMode(void)
457 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
461 * @brief Disable the Debug Module during STANDBY mode
462 * @retval None
464 void HAL_DBGMCU_DisableDBGStandbyMode(void)
466 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
470 * @}
474 * @}
477 #endif /* HAL_MODULE_ENABLED */
479 * @}
483 * @}
486 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/