2 ******************************************************************************
3 * @file stm32f7xx_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) 2017 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 "stm32f7xx_hal.h"
56 /** @addtogroup STM32F7xx_HAL_Driver
61 * @brief HAL module driver.
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /** @addtogroup HAL_Private_Constants
71 * @brief STM32F7xx HAL Driver version number V1.2.2
73 #define __STM32F7xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
74 #define __STM32F7xx_HAL_VERSION_SUB1 (0x02) /*!< [23:16] sub1 version */
75 #define __STM32F7xx_HAL_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */
76 #define __STM32F7xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
77 #define __STM32F7xx_HAL_VERSION ((__STM32F7xx_HAL_VERSION_MAIN << 24)\
78 |(__STM32F7xx_HAL_VERSION_SUB1 << 16)\
79 |(__STM32F7xx_HAL_VERSION_SUB2 << 8 )\
80 |(__STM32F7xx_HAL_VERSION_RC))
82 #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
87 /* Private macro -------------------------------------------------------------*/
88 /* Private variables ---------------------------------------------------------*/
89 /** @addtogroup HAL_Private_Variables
97 /* Private function prototypes -----------------------------------------------*/
98 /* Private functions ---------------------------------------------------------*/
100 /** @defgroup HAL_Exported_Functions HAL Exported Functions
104 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
105 * @brief Initialization and de-initialization functions
108 ===============================================================================
109 ##### Initialization and de-initialization functions #####
110 ===============================================================================
111 [..] This section provides functions allowing to:
112 (+) Initializes the Flash interface the NVIC allocation and initial clock
113 configuration. It initializes the systick also when timeout is needed
114 and the backup domain when enabled.
115 (+) de-Initializes common part of the HAL
116 (+) Configure The time base source to have 1ms time base with a dedicated
117 Tick interrupt priority.
118 (++) Systick timer is used by default as source of time base, but user
119 can eventually implement his proper time base source (a general purpose
120 timer for example or other time source), keeping in mind that Time base
121 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
122 handled in milliseconds basis.
123 (++) Time base configuration function (HAL_InitTick ()) is called automatically
124 at the beginning of the program after reset by HAL_Init() or at any time
125 when clock is configured, by HAL_RCC_ClockConfig().
126 (++) Source of time base is configured to generate interrupts at regular
127 time intervals. Care must be taken if HAL_Delay() is called from a
128 peripheral ISR process, the Tick interrupt line must have higher priority
129 (numerically lower) than the peripheral interrupt. Otherwise the caller
130 ISR process will be blocked.
131 (++) functions affecting time base configurations are declared as __weak
132 to make override possible in case of other implementations in user file.
138 * @brief This function is used to initialize the HAL Library; it must be the first
139 * instruction to be executed in the main program (before to call any other
140 * HAL function), it performs the following:
141 * Configure the Flash prefetch, and instruction cache through ART accelerator.
142 * Configures the SysTick to generate an interrupt each 1 millisecond,
143 * which is clocked by the HSI (at this stage, the clock is not yet
144 * configured and thus the system is running from the internal HSI at 16 MHz).
145 * Set NVIC Group Priority to 4.
146 * Calls the HAL_MspInit() callback function defined in user file
147 * "stm32f7xx_hal_msp.c" to do the global low level hardware initialization
149 * @note SysTick is used as time base for the HAL_Delay() function, the application
150 * need to ensure that the SysTick time base is always set to 1 millisecond
151 * to have correct HAL operation.
154 HAL_StatusTypeDef
HAL_Init(void)
156 /* Configure Flash prefetch and Instruction cache through ART accelerator */
157 #if (ART_ACCLERATOR_ENABLE != 0)
158 __HAL_FLASH_ART_ENABLE();
159 #endif /* ART_ACCLERATOR_ENABLE */
161 /* Set Interrupt Group Priority */
162 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4
);
164 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
165 HAL_InitTick(TICK_INT_PRIORITY
);
167 /* Init the low level hardware */
170 /* Return function status */
175 * @brief This function de-Initializes common part of the HAL and stops the systick.
176 * This function is optional.
179 HAL_StatusTypeDef
HAL_DeInit(void)
181 /* Reset of all peripherals */
182 __HAL_RCC_APB1_FORCE_RESET();
183 __HAL_RCC_APB1_RELEASE_RESET();
185 __HAL_RCC_APB2_FORCE_RESET();
186 __HAL_RCC_APB2_RELEASE_RESET();
188 __HAL_RCC_AHB1_FORCE_RESET();
189 __HAL_RCC_AHB1_RELEASE_RESET();
191 __HAL_RCC_AHB2_FORCE_RESET();
192 __HAL_RCC_AHB2_RELEASE_RESET();
194 __HAL_RCC_AHB3_FORCE_RESET();
195 __HAL_RCC_AHB3_RELEASE_RESET();
197 /* De-Init the low level hardware */
200 /* Return function status */
205 * @brief Initializes the MSP.
208 __weak
void HAL_MspInit(void)
210 /* NOTE : This function Should not be modified, when the callback is needed,
211 the HAL_MspInit could be implemented in the user file
216 * @brief DeInitializes the MSP.
219 __weak
void HAL_MspDeInit(void)
221 /* NOTE : This function Should not be modified, when the callback is needed,
222 the HAL_MspDeInit could be implemented in the user file
227 * @brief This function configures the source of the time base.
228 * The time source is configured to have 1ms time base with a dedicated
229 * Tick interrupt priority.
230 * @note This function is called automatically at the beginning of program after
231 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
232 * @note In the default implementation, SysTick timer is the source of time base.
233 * It is used to generate interrupts at regular time intervals.
234 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
235 * The the SysTick interrupt must have higher priority (numerically lower)
236 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
237 * The function is declared as __weak to be overwritten in case of other
238 * implementation in user file.
239 * @param TickPriority: Tick interrupt priority.
242 __weak HAL_StatusTypeDef
HAL_InitTick(uint32_t TickPriority
)
244 /*Configure the SysTick to have interrupt in 1ms time basis*/
245 HAL_SYSTICK_Config(SystemCoreClock
/1000);
247 /*Configure the SysTick IRQ priority */
248 HAL_NVIC_SetPriority(SysTick_IRQn
, TickPriority
,0);
250 /* Return function status */
258 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
259 * @brief HAL Control functions
262 ===============================================================================
263 ##### HAL Control functions #####
264 ===============================================================================
265 [..] This section provides functions allowing to:
266 (+) Provide a tick value in millisecond
267 (+) Provide a blocking delay in millisecond
268 (+) Suspend the time base source interrupt
269 (+) Resume the time base source interrupt
270 (+) Get the HAL API driver version
271 (+) Get the device identifier
272 (+) Get the device revision identifier
273 (+) Enable/Disable Debug module during SLEEP mode
274 (+) Enable/Disable Debug module during STOP mode
275 (+) Enable/Disable Debug module during STANDBY mode
282 * @brief This function is called to increment a global variable "uwTick"
283 * used as application time base.
284 * @note In the default implementation, this variable is incremented each 1ms
286 * @note This function is declared as __weak to be overwritten in case of other
287 * implementations in user file.
290 __weak
void HAL_IncTick(void)
296 * @brief Provides a tick value in millisecond.
297 * @note This function is declared as __weak to be overwritten in case of other
298 * implementations in user file.
301 __weak
uint32_t HAL_GetTick(void)
307 * @brief This function provides accurate delay (in milliseconds) based
308 * on variable incremented.
309 * @note In the default implementation , SysTick timer is the source of time base.
310 * It is used to generate interrupts at regular time intervals where uwTick
312 * @note This function is declared as __weak to be overwritten in case of other
313 * implementations in user file.
314 * @param Delay: specifies the delay time length, in milliseconds.
317 __weak
void HAL_Delay(__IO
uint32_t Delay
)
319 uint32_t tickstart
= 0;
320 tickstart
= HAL_GetTick();
321 while((HAL_GetTick() - tickstart
) < Delay
)
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 SysTick interrupt will be disabled and so Tick increment
332 * @note This function is declared as __weak to be overwritten in case of other
333 * implementations in user file.
336 __weak
void HAL_SuspendTick(void)
338 /* Disable SysTick Interrupt */
339 SysTick
->CTRL
&= ~SysTick_CTRL_TICKINT_Msk
;
343 * @brief Resume Tick increment.
344 * @note In the default implementation , SysTick timer is the source of time base. It is
345 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
346 * is called, the SysTick interrupt will be enabled and so Tick increment
348 * @note This function is declared as __weak to be overwritten in case of other
349 * implementations in user file.
352 __weak
void HAL_ResumeTick(void)
354 /* Enable SysTick Interrupt */
355 SysTick
->CTRL
|= SysTick_CTRL_TICKINT_Msk
;
359 * @brief Returns the HAL revision
360 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
362 uint32_t HAL_GetHalVersion(void)
364 return __STM32F7xx_HAL_VERSION
;
368 * @brief Returns the device revision identifier.
369 * @retval Device revision identifier
371 uint32_t HAL_GetREVID(void)
373 return((DBGMCU
->IDCODE
) >> 16U);
377 * @brief Returns the device identifier.
378 * @retval Device identifier
380 uint32_t HAL_GetDEVID(void)
382 return((DBGMCU
->IDCODE
) & IDCODE_DEVID_MASK
);
386 * @brief Enable the Debug Module during SLEEP mode
389 void HAL_DBGMCU_EnableDBGSleepMode(void)
391 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_SLEEP
);
395 * @brief Disable the Debug Module during SLEEP mode
398 void HAL_DBGMCU_DisableDBGSleepMode(void)
400 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_SLEEP
);
404 * @brief Enable the Debug Module during STOP mode
407 void HAL_DBGMCU_EnableDBGStopMode(void)
409 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STOP
);
413 * @brief Disable the Debug Module during STOP mode
416 void HAL_DBGMCU_DisableDBGStopMode(void)
418 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STOP
);
422 * @brief Enable the Debug Module during STANDBY mode
425 void HAL_DBGMCU_EnableDBGStandbyMode(void)
427 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STANDBY
);
431 * @brief Disable the Debug Module during STANDBY mode
434 void HAL_DBGMCU_DisableDBGStandbyMode(void)
436 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STANDBY
);
440 * @brief Enables the I/O Compensation Cell.
441 * @note The I/O compensation cell can be used only when the device supply
442 * voltage ranges from 2.4 to 3.6 V.
445 void HAL_EnableCompensationCell(void)
447 SYSCFG
->CMPCR
|= SYSCFG_CMPCR_CMP_PD
;
451 * @brief Power-down the I/O Compensation Cell.
452 * @note The I/O compensation cell can be used only when the device supply
453 * voltage ranges from 2.4 to 3.6 V.
456 void HAL_DisableCompensationCell(void)
458 SYSCFG
->CMPCR
&= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD
);
462 * @brief Enables the FMC Memory Mapping Swapping.
464 * @note SDRAM is accessible at 0x60000000
465 * and NOR/RAM is accessible at 0xC0000000
469 void HAL_EnableFMCMemorySwapping(void)
471 SYSCFG
->MEMRMP
|= SYSCFG_MEMRMP_SWP_FMC_0
;
475 * @brief Disables the FMC Memory Mapping Swapping
477 * @note SDRAM is accessible at 0xC0000000 (default mapping)
478 * and NOR/RAM is accessible at 0x60000000 (default mapping)
482 void HAL_DisableFMCMemorySwapping(void)
485 SYSCFG
->MEMRMP
&= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC
);
488 #if defined (STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
490 * @brief Enable the Internal FLASH Bank Swapping.
492 * @note This function can be used only for STM32F77xx/STM32F76xx devices.
494 * @note Flash Bank2 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
495 * and Flash Bank1 mapped at 0x08100000 (AXI) (aliased at 0x00300000 (TCM))
499 void HAL_EnableMemorySwappingBank(void)
501 SET_BIT(SYSCFG
->MEMRMP
, SYSCFG_MEMRMP_SWP_FB
);
505 * @brief Disable the Internal FLASH Bank Swapping.
507 * @note This function can be used only for STM32F77xx/STM32F76xx devices.
509 * @note The default state : Flash Bank1 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
510 * and Flash Bank2 mapped at 0x08100000 (AXI)( aliased at 0x00300000 (TCM))
514 void HAL_DisableMemorySwappingBank(void)
516 CLEAR_BIT(SYSCFG
->MEMRMP
, SYSCFG_MEMRMP_SWP_FB
);
518 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
536 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/