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
7   *
8   ******************************************************************************
9   * @attention
10   *
11   * Copyright (c) 2017 STMicroelectronics.
12   * All rights reserved.
13   *
14   * This software is licensed under terms that can be found in the LICENSE file
15   * in the root directory of this software component.
16   * If no LICENSE file comes with this software, it is provided AS-IS.
17   *
18   ******************************************************************************
19   @verbatim
20   ==============================================================================
21                      ##### How to use this driver #####
22   ==============================================================================
23     [..]
24     The common HAL driver contains a set of generic and common APIs that can be
25     used by the PPP peripheral drivers and the user to start using the HAL.
26     [..]
27     The HAL contains two APIs' categories:
28          (+) Common HAL APIs
29          (+) Services HAL APIs
30 
31   @endverbatim
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32f7xx_hal.h"
37 
38 /** @addtogroup STM32F7xx_HAL_Driver
39   * @{
40   */
41 
42 /** @defgroup HAL HAL
43   * @brief HAL module driver.
44   * @{
45   */
46 
47 /* Private typedef -----------------------------------------------------------*/
48 /* Private define ------------------------------------------------------------*/
49 /** @addtogroup HAL_Private_Constants
50   * @{
51   */
52 /**
53  * @brief STM32F7xx HAL Driver version number V1.3.1
54    */
55 #define __STM32F7xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
56 #define __STM32F7xx_HAL_VERSION_SUB1   (0x03) /*!< [23:16] sub1 version */
57 #define __STM32F7xx_HAL_VERSION_SUB2   (0x01) /*!< [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))
63 
64 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)
65 /**
66   * @}
67   */
68 
69 /* Private macro -------------------------------------------------------------*/
70 /* Exported variables ---------------------------------------------------------*/
71 /** @addtogroup HAL_Exported_Variables
72   * @{
73   */
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   * @}
79   */
80 
81 /* Private function prototypes -----------------------------------------------*/
82 /* Private functions ---------------------------------------------------------*/
83 
84 /** @defgroup HAL_Exported_Functions HAL Exported Functions
85   * @{
86   */
87 
88 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
89  *  @brief    Initialization and de-initialization functions
90  *
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   * @{
119   */
120 
121 /**
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
132   *
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
137   */
HAL_Init(void)138 HAL_StatusTypeDef HAL_Init(void)
139 {
140   /* Configure Instruction cache through ART accelerator */
141 #if (ART_ACCELERATOR_ENABLE != 0)
142   __HAL_FLASH_ART_ENABLE();
143 #endif /* ART_ACCELERATOR_ENABLE */
144 
145   /* Configure Flash prefetch */
146 #if (PREFETCH_ENABLE != 0U)
147   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
148 #endif /* PREFETCH_ENABLE */
149 
150   /* Set Interrupt Group Priority */
151   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
152 
153   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
154   HAL_InitTick(TICK_INT_PRIORITY);
155 
156   /* Init the low level hardware */
157   HAL_MspInit();
158 
159   /* Return function status */
160   return HAL_OK;
161 }
162 
163 /**
164   * @brief  This function de-Initializes common part of the HAL and stops the systick.
165   *         This function is optional.
166   * @retval HAL status
167   */
HAL_DeInit(void)168 HAL_StatusTypeDef HAL_DeInit(void)
169 {
170   /* Reset of all peripherals */
171   __HAL_RCC_APB1_FORCE_RESET();
172   __HAL_RCC_APB1_RELEASE_RESET();
173 
174   __HAL_RCC_APB2_FORCE_RESET();
175   __HAL_RCC_APB2_RELEASE_RESET();
176 
177   __HAL_RCC_AHB1_FORCE_RESET();
178   __HAL_RCC_AHB1_RELEASE_RESET();
179 
180   __HAL_RCC_AHB2_FORCE_RESET();
181   __HAL_RCC_AHB2_RELEASE_RESET();
182 
183   __HAL_RCC_AHB3_FORCE_RESET();
184   __HAL_RCC_AHB3_RELEASE_RESET();
185 
186   /* De-Init the low level hardware */
187   HAL_MspDeInit();
188 
189   /* Return function status */
190   return HAL_OK;
191 }
192 
193 /**
194   * @brief  Initialize the MSP.
195   * @retval None
196   */
HAL_MspInit(void)197 __weak void HAL_MspInit(void)
198 {
199   /* NOTE : This function should not be modified, when the callback is needed,
200             the HAL_MspInit could be implemented in the user file
201    */
202 }
203 
204 /**
205   * @brief  DeInitializes the MSP.
206   * @retval None
207   */
HAL_MspDeInit(void)208 __weak void HAL_MspDeInit(void)
209 {
210   /* NOTE : This function should not be modified, when the callback is needed,
211             the HAL_MspDeInit could be implemented in the user file
212    */
213 }
214 
215 /**
216   * @brief This function configures the source of the time base.
217   *        The time source is configured  to have 1ms time base with a dedicated
218   *        Tick interrupt priority.
219   * @note This function is called  automatically at the beginning of program after
220   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
221   * @note In the default implementation, SysTick timer is the source of time base.
222   *       It is used to generate interrupts at regular time intervals.
223   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
224   *       The SysTick interrupt must have higher priority (numerically lower)
225   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
226   *       The function is declared as __weak  to be overwritten  in case of other
227   *       implementation  in user file.
228   * @param TickPriority Tick interrupt priority.
229   * @retval HAL status
230   */
HAL_InitTick(uint32_t TickPriority)231 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
232 {
233   /* Configure the SysTick to have interrupt in 1ms time basis*/
234   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
235   {
236     return HAL_ERROR;
237   }
238 
239   /* Configure the SysTick IRQ priority */
240   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
241   {
242     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
243     uwTickPrio = TickPriority;
244   }
245   else
246   {
247     return HAL_ERROR;
248   }
249 
250   /* Return function status */
251   return HAL_OK;
252 }
253 
254 /**
255   * @}
256   */
257 
258 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
259  *  @brief    HAL Control functions
260  *
261 @verbatim
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
276 
277 @endverbatim
278   * @{
279   */
280 
281 /**
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
285   *       in SysTick ISR.
286  * @note This function is declared as __weak to be overwritten in case of other
287   *      implementations in user file.
288   * @retval None
289   */
HAL_IncTick(void)290 __weak void HAL_IncTick(void)
291 {
292   uwTick += uwTickFreq;
293 }
294 
295 /**
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.
299   * @retval tick value
300   */
HAL_GetTick(void)301 __weak uint32_t HAL_GetTick(void)
302 {
303   return uwTick;
304 }
305 
306 /**
307   * @brief This function returns a tick priority.
308   * @retval tick priority
309   */
HAL_GetTickPrio(void)310 uint32_t HAL_GetTickPrio(void)
311 {
312   return uwTickPrio;
313 }
314 
315 /**
316   * @brief Set new tick Freq.
317   * @retval Status
318   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)319 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
320 {
321   HAL_StatusTypeDef status  = HAL_OK;
322   HAL_TickFreqTypeDef prevTickFreq;
323 
324   assert_param(IS_TICKFREQ(Freq));
325 
326   if (uwTickFreq != Freq)
327   {
328     /* Back up uwTickFreq frequency */
329     prevTickFreq = uwTickFreq;
330 
331     /* Update uwTickFreq global variable used by HAL_InitTick() */
332     uwTickFreq = Freq;
333 
334     /* Apply the new tick Freq  */
335     status = HAL_InitTick(uwTickPrio);
336 
337     if (status != HAL_OK)
338     {
339       /* Restore previous tick frequency */
340       uwTickFreq = prevTickFreq;
341     }
342   }
343 
344   return status;
345 }
346 
347 /**
348   * @brief Return tick frequency.
349   * @retval Tick frequency.
350   *         Value of @ref HAL_TickFreqTypeDef.
351   */
HAL_GetTickFreq(void)352 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
353 {
354   return uwTickFreq;
355 }
356 
357 /**
358   * @brief This function provides minimum delay (in milliseconds) based
359   *        on variable incremented.
360   * @note In the default implementation , SysTick timer is the source of time base.
361   *       It is used to generate interrupts at regular time intervals where uwTick
362   *       is incremented.
363   * @note This function is declared as __weak to be overwritten in case of other
364   *       implementations in user file.
365   * @param Delay  specifies the delay time length, in milliseconds.
366   * @retval None
367   */
HAL_Delay(uint32_t Delay)368 __weak void HAL_Delay(uint32_t Delay)
369 {
370   uint32_t tickstart = HAL_GetTick();
371   uint32_t wait = Delay;
372 
373   /* Add a freq to guarantee minimum wait */
374   if (wait < HAL_MAX_DELAY)
375   {
376     wait += (uint32_t)(uwTickFreq);
377   }
378 
379   while ((HAL_GetTick() - tickstart) < wait)
380   {
381   }
382 }
383 
384 /**
385   * @brief Suspend Tick increment.
386   * @note In the default implementation , SysTick timer is the source of time base. It is
387   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
388   *       is called, the SysTick interrupt will be disabled and so Tick increment
389   *       is suspended.
390   * @note This function is declared as __weak to be overwritten in case of other
391   *       implementations in user file.
392   * @retval None
393   */
HAL_SuspendTick(void)394 __weak void HAL_SuspendTick(void)
395 {
396   /* Disable SysTick Interrupt */
397   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
398 }
399 
400 /**
401   * @brief Resume Tick increment.
402   * @note In the default implementation , SysTick timer is the source of time base. It is
403   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
404   *       is called, the SysTick interrupt will be enabled and so Tick increment
405   *       is resumed.
406   * @note This function is declared as __weak to be overwritten in case of other
407   *       implementations in user file.
408   * @retval None
409   */
HAL_ResumeTick(void)410 __weak void HAL_ResumeTick(void)
411 {
412   /* Enable SysTick Interrupt */
413   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
414 }
415 
416 /**
417   * @brief  Returns the HAL revision
418   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
419   */
HAL_GetHalVersion(void)420 uint32_t HAL_GetHalVersion(void)
421 {
422   return __STM32F7xx_HAL_VERSION;
423 }
424 
425 /**
426   * @brief  Returns the device revision identifier.
427   * @retval Device revision identifier
428   */
HAL_GetREVID(void)429 uint32_t HAL_GetREVID(void)
430 {
431    return((DBGMCU->IDCODE) >> 16U);
432 }
433 
434 /**
435   * @brief  Returns the device identifier.
436   * @retval Device identifier
437   */
HAL_GetDEVID(void)438 uint32_t HAL_GetDEVID(void)
439 {
440    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
441 }
442 
443 /**
444   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
445   * @retval Device identifier
446   */
HAL_GetUIDw0(void)447 uint32_t HAL_GetUIDw0(void)
448 {
449   return(READ_REG(*((uint32_t *)UID_BASE)));
450 }
451 
452 /**
453   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
454   * @retval Device identifier
455   */
HAL_GetUIDw1(void)456 uint32_t HAL_GetUIDw1(void)
457 {
458   return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
459 }
460 
461 /**
462   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
463   * @retval Device identifier
464   */
HAL_GetUIDw2(void)465 uint32_t HAL_GetUIDw2(void)
466 {
467   return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
468 }
469 
470 /**
471   * @brief  Enable the Debug Module during SLEEP mode
472   * @retval None
473   */
HAL_DBGMCU_EnableDBGSleepMode(void)474 void HAL_DBGMCU_EnableDBGSleepMode(void)
475 {
476   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
477 }
478 
479 /**
480   * @brief  Disable the Debug Module during SLEEP mode
481   * @retval None
482   */
HAL_DBGMCU_DisableDBGSleepMode(void)483 void HAL_DBGMCU_DisableDBGSleepMode(void)
484 {
485   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
486 }
487 
488 /**
489   * @brief  Enable the Debug Module during STOP mode
490   * @retval None
491   */
HAL_DBGMCU_EnableDBGStopMode(void)492 void HAL_DBGMCU_EnableDBGStopMode(void)
493 {
494   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
495 }
496 
497 /**
498   * @brief  Disable the Debug Module during STOP mode
499   * @retval None
500   */
HAL_DBGMCU_DisableDBGStopMode(void)501 void HAL_DBGMCU_DisableDBGStopMode(void)
502 {
503   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
504 }
505 
506 /**
507   * @brief  Enable the Debug Module during STANDBY mode
508   * @retval None
509   */
HAL_DBGMCU_EnableDBGStandbyMode(void)510 void HAL_DBGMCU_EnableDBGStandbyMode(void)
511 {
512   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
513 }
514 
515 /**
516   * @brief  Disable the Debug Module during STANDBY mode
517   * @retval None
518   */
HAL_DBGMCU_DisableDBGStandbyMode(void)519 void HAL_DBGMCU_DisableDBGStandbyMode(void)
520 {
521   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
522 }
523 
524 /**
525   * @brief  Enables the I/O Compensation Cell.
526   * @note   The I/O compensation cell can be used only when the device supply
527   *         voltage ranges from 2.4 to 3.6 V.
528   * @retval None
529   */
HAL_EnableCompensationCell(void)530 void HAL_EnableCompensationCell(void)
531 {
532   SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;
533 }
534 
535 /**
536   * @brief  Power-down the I/O Compensation Cell.
537   * @note   The I/O compensation cell can be used only when the device supply
538   *         voltage ranges from 2.4 to 3.6 V.
539   * @retval None
540   */
HAL_DisableCompensationCell(void)541 void HAL_DisableCompensationCell(void)
542 {
543   SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);
544 }
545 
546 /**
547   * @brief  Enables the FMC Memory Mapping Swapping.
548   *
549   * @note   SDRAM is accessible at 0x60000000
550   *         and NOR/RAM is accessible at 0xC0000000
551   *
552   * @retval None
553   */
HAL_EnableFMCMemorySwapping(void)554 void HAL_EnableFMCMemorySwapping(void)
555 {
556   SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;
557 }
558 
559 /**
560   * @brief  Disables the FMC Memory Mapping Swapping
561   *
562   * @note   SDRAM is accessible at 0xC0000000 (default mapping)
563   *         and NOR/RAM is accessible at 0x60000000 (default mapping)
564   *
565   * @retval None
566   */
HAL_DisableFMCMemorySwapping(void)567 void HAL_DisableFMCMemorySwapping(void)
568 {
569   SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);
570 }
571 
572 #if defined (STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
573 /**
574 * @brief  Enable the Internal FLASH Bank Swapping.
575 *
576 * @note   This function can be used only for STM32F77xx/STM32F76xx devices.
577 *
578 * @note   Flash Bank2 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
579 *         and Flash Bank1 mapped at 0x08100000 (AXI) (aliased at 0x00300000 (TCM))
580 *
581 * @retval None
582 */
HAL_EnableMemorySwappingBank(void)583 void HAL_EnableMemorySwappingBank(void)
584 {
585   SET_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
586 }
587 
588 /**
589 * @brief  Disable the Internal FLASH Bank Swapping.
590 *
591 * @note   This function can be used only for STM32F77xx/STM32F76xx devices.
592 *
593 * @note   The default state : Flash Bank1 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
594 *         and Flash Bank2 mapped at 0x08100000 (AXI)( aliased at 0x00300000 (TCM))
595 *
596 * @retval None
597 */
HAL_DisableMemorySwappingBank(void)598 void HAL_DisableMemorySwappingBank(void)
599 {
600   CLEAR_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
601 }
602 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
603 
604 /**
605   * @}
606   */
607 
608 /**
609   * @}
610   */
611 
612 /**
613   * @}
614   */
615 
616 /**
617   * @}
618   */
619 
620