2 ******************************************************************************
3 * @file stm32f4xx_hal.c
4 * @author MCD Application Team
7 * @brief HAL module driver.
8 * This is the common part of the HAL initialization
11 ==============================================================================
12 ##### How to use this driver #####
13 ==============================================================================
15 The common HAL driver contains a set of generic and common APIs that can be
16 used by the PPP peripheral drivers and the user to start using the HAL.
18 The HAL contains two APIs' categories:
23 ******************************************************************************
26 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
28 * Redistribution and use in source and binary forms, with or without modification,
29 * are permitted provided that the following conditions are met:
30 * 1. Redistributions of source code must retain the above copyright notice,
31 * this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 * 3. Neither the name of STMicroelectronics nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 ******************************************************************************
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32f4xx_hal.h"
56 /** @addtogroup STM32F4xx_HAL_Driver
61 * @brief HAL module driver.
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
68 * @brief STM32F4xx HAL Driver version number V1.1.0
70 #define __STM32F4xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
71 #define __STM32F4xx_HAL_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
72 #define __STM32F4xx_HAL_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
73 #define __STM32F4xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
74 #define __STM32F4xx_HAL_VERSION ((__STM32F4xx_HAL_VERSION_MAIN << 24)\
75 |(__STM32F4xx_HAL_VERSION_SUB1 << 16)\
76 |(__STM32F4xx_HAL_VERSION_SUB2 << 8 )\
77 |(__STM32F4xx_HAL_VERSION_RC))
79 #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
81 /* ------------ RCC registers bit address in the alias region ----------- */
82 #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
83 /* --- MEMRMP Register ---*/
84 /* Alias word address of UFB_MODE bit */
85 #define MEMRMP_OFFSET SYSCFG_OFFSET
86 #define UFB_MODE_BitNumber ((uint8_t)0x8)
87 #define UFB_MODE_BB (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32) + (UFB_MODE_BitNumber * 4))
89 /* --- CMPCR Register ---*/
90 /* Alias word address of CMP_PD bit */
91 #define CMPCR_OFFSET (SYSCFG_OFFSET + 0x20)
92 #define CMP_PD_BitNumber ((uint8_t)0x00)
93 #define CMPCR_CMP_PD_BB (PERIPH_BB_BASE + (CMPCR_OFFSET * 32) + (CMP_PD_BitNumber * 4))
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 static __IO uint32_t uwTick;
98 /* Private function prototypes -----------------------------------------------*/
99 /* Private functions ---------------------------------------------------------*/
101 /** @defgroup HAL_Private_Functions
105 /** @defgroup HAL_Group1 Initialization and de-initialization Functions
106 * @brief Initialization and de-initialization functions
109 ===============================================================================
110 ##### Initialization and de-initialization functions #####
111 ===============================================================================
112 [..] This section provides functions allowing to:
113 (+) Initializes the Flash interface the NVIC allocation and initial clock
114 configuration. It initializes the systick also when timeout is needed
115 and the backup domain when enabled.
116 (+) de-Initializes common part of the HAL
117 (+) Configure The time base source to have 1ms time base with a dedicated
118 Tick interrupt priority.
119 (++) Systick timer is used by default as source of time base, but user
120 can eventually implement his proper time base source (a general purpose
121 timer for example or other time source), keeping in mind that Time base
122 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
123 handled in milliseconds basis.
124 (++) Time base configuration function (HAL_InitTick ()) is called automatically
125 at the beginning of the program after reset by HAL_Init() or at any time
126 when clock is configured, by HAL_RCC_ClockConfig().
127 (++) Source of time base is configured to generate interrupts at regular
128 time intervals. Care must be taken if HAL_Delay() is called from a
129 peripheral ISR process, the Tick interrupt line must have higher priority
130 (numerically lower) than the peripheral interrupt. Otherwise the caller
131 ISR process will be blocked.
132 (++) functions affecting time base configurations are declared as __weak
133 to make override possible in case of other implementations in user file.
139 * @brief This function is used to initialize the HAL Library; it must be the first
140 * instruction to be executed in the main program (before to call any other
141 * HAL function), it performs the following:
142 * Configure the Flash prefetch, instruction and Data caches.
143 * Configures the SysTick to generate an interrupt each 1 millisecond,
144 * which is clocked by the HSI (at this stage, the clock is not yet
145 * configured and thus the system is running from the internal HSI at 16 MHz).
146 * Set NVIC Group Priority to 4.
147 * Calls the HAL_MspInit() callback function defined in user file
148 * "stm32f4xx_hal_msp.c" to do the global low level hardware initialization
150 * @note SysTick is used as time base for the HAL_Delay() function, the application
151 * need to ensure that the SysTick time base is always set to 1 millisecond
152 * to have correct HAL operation.
156 HAL_StatusTypeDef HAL_Init(void)
158 /* Configure Flash prefetch, Instruction cache, Data cache */
159 #if (INSTRUCTION_CACHE_ENABLE != 0)
160 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
161 #endif /* INSTRUCTION_CACHE_ENABLE */
163 #if (DATA_CACHE_ENABLE != 0)
164 __HAL_FLASH_DATA_CACHE_ENABLE();
165 #endif /* DATA_CACHE_ENABLE */
167 #if (PREFETCH_ENABLE != 0)
168 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
169 #endif /* PREFETCH_ENABLE */
171 /* Set Interrupt Group Priority */
172 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
174 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
175 HAL_InitTick(TICK_INT_PRIORITY);
177 /* Init the low level hardware */
180 /* Return function status */
185 * @brief This function de-Initializes common part of the HAL and stops the systick.
186 * This function is optional.
190 HAL_StatusTypeDef HAL_DeInit(void)
192 /* Reset of all peripherals */
193 __APB1_FORCE_RESET();
194 __APB1_RELEASE_RESET();
196 __APB2_FORCE_RESET();
197 __APB2_RELEASE_RESET();
199 __AHB1_FORCE_RESET();
200 __AHB1_RELEASE_RESET();
202 __AHB2_FORCE_RESET();
203 __AHB2_RELEASE_RESET();
205 __AHB3_FORCE_RESET();
206 __AHB3_RELEASE_RESET();
208 /* De-Init the low level hardware */
211 /* Return function status */
216 * @brief Initializes the MSP.
220 __weak void HAL_MspInit(void)
222 /* NOTE : This function Should not be modified, when the callback is needed,
223 the HAL_MspInit could be implemented in the user file
228 * @brief DeInitializes the MSP.
232 __weak void HAL_MspDeInit(void)
234 /* NOTE : This function Should not be modified, when the callback is needed,
235 the HAL_MspDeInit could be implemented in the user file
240 * @brief This function configures the source of the time base.
241 * The time source is configured to have 1ms time base with a dedicated
242 * Tick interrupt priority.
243 * @note This function is called automatically at the beginning of program after
244 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
245 * @note In the default implementation, SysTick timer is the source of time base.
246 * It is used to generate interrupts at regular time intervals.
247 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
248 * The the SysTick interrupt must have higher priority (numerically lower)
249 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
250 * The function is declared as __weak to be overwritten in case of other
251 * implementation in user file.
252 * @param TickPriority: Tick interrupt priority.
255 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
257 /*Configure the SysTick to have interrupt in 1ms time basis*/
258 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
260 /*Configure the SysTick IRQ priority */
261 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
263 /* Return function status */
271 /** @defgroup HAL_Group2 HAL Control functions
272 * @brief HAL Control functions
275 ===============================================================================
276 ##### HAL Control functions #####
277 ===============================================================================
278 [..] This section provides functions allowing to:
279 (+) Provide a tick value in millisecond
280 (+) Provide a blocking delay in millisecond
281 (+) Suspend the time base source interrupt
282 (+) Resume the time base source interrupt
283 (+) Get the HAL API driver version
284 (+) Get the device identifier
285 (+) Get the device revision identifier
286 (+) Enable/Disable Debug module during SLEEP mode
287 (+) Enable/Disable Debug module during STOP mode
288 (+) Enable/Disable Debug module during STANDBY mode
295 * @brief This function is called to increment a global variable "uwTick"
296 * used as application time base.
297 * @note In the default implementation, this variable is incremented each 1ms
299 * @note This function is declared as __weak to be overwritten in case of other
300 * implementations in user file.
304 __weak void HAL_IncTick(void)
310 * @brief Provides a tick value in millisecond.
311 * @note This function is declared as __weak to be overwritten in case of other
312 * implementations in user file.
316 __weak uint32_t HAL_GetTick(void)
322 * @brief This function provides accurate 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
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.
332 __weak void HAL_Delay(__IO uint32_t Delay)
334 uint32_t tickstart = 0;
335 tickstart = HAL_GetTick();
336 while((HAL_GetTick() - tickstart) < Delay)
342 * @brief Suspend Tick increment.
343 * @note In the default implementation , SysTick timer is the source of time base. It is
344 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
345 * is called, the the SysTick interrupt will be disabled and so Tick increment
347 * @note This function is declared as __weak to be overwritten in case of other
348 * implementations in user file.
352 __weak void HAL_SuspendTick(void)
354 /* Disable SysTick Interrupt */
355 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
359 * @brief Resume Tick increment.
360 * @note In the default implementation , SysTick timer is the source of time base. It is
361 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
362 * is called, the the SysTick interrupt will be enabled and so Tick increment
364 * @note This function is declared as __weak to be overwritten in case of other
365 * implementations in user file.
369 __weak void HAL_ResumeTick(void)
371 /* Enable SysTick Interrupt */
372 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
376 * @brief Returns the HAL revision
378 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
380 uint32_t HAL_GetHalVersion(void)
382 return __STM32F4xx_HAL_VERSION;
386 * @brief Returns the device revision identifier.
388 * @retval Device revision identifier
390 uint32_t HAL_GetREVID(void)
392 return((DBGMCU->IDCODE) >> 16);
396 * @brief Returns the device identifier.
398 * @retval Device identifier
400 uint32_t HAL_GetDEVID(void)
402 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
406 * @brief Enable the Debug Module during SLEEP mode
410 void HAL_EnableDBGSleepMode(void)
412 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
416 * @brief Disable the Debug Module during SLEEP mode
420 void HAL_DisableDBGSleepMode(void)
422 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
426 * @brief Enable the Debug Module during STOP mode
430 void HAL_EnableDBGStopMode(void)
432 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
436 * @brief Disable the Debug Module during STOP mode
440 void HAL_DisableDBGStopMode(void)
442 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
446 * @brief Enable the Debug Module during STANDBY mode
450 void HAL_EnableDBGStandbyMode(void)
452 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
456 * @brief Disable the Debug Module during STANDBY mode
460 void HAL_DisableDBGStandbyMode(void)
462 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
466 * @brief Enables the I/O Compensation Cell.
467 * @note The I/O compensation cell can be used only when the device supply
468 * voltage ranges from 2.4 to 3.6 V.
471 void HAL_EnableCompensationCell(void)
473 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
477 * @brief Power-down the I/O Compensation Cell.
478 * @note The I/O compensation cell can be used only when the device supply
479 * voltage ranges from 2.4 to 3.6 V.
482 void HAL_DisableCompensationCell(void)
484 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
487 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx)
489 * @brief Enables the Internal FLASH Bank Swapping.
491 * @note This function can be used only for STM32F42xxx/43xxx devices.
493 * @note Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
494 * and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
498 void HAL_EnableMemorySwappingBank(void)
500 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
504 * @brief Disables the Internal FLASH Bank Swapping.
506 * @note This function can be used only for STM32F42xxx/43xxx devices.
508 * @note The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000)
509 * and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
513 void HAL_DisableMemorySwappingBank(void)
516 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
518 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
536 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/