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.0
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   (0x00) /*!< [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 period in Hz
350   */
HAL_GetTickFreq(void)351 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
352 {
353   return uwTickFreq;
354 }
355 
356 /**
357   * @brief This function provides minimum delay (in milliseconds) based
358   *        on variable incremented.
359   * @note In the default implementation , SysTick timer is the source of time base.
360   *       It is used to generate interrupts at regular time intervals where uwTick
361   *       is incremented.
362   * @note This function is declared as __weak to be overwritten in case of other
363   *       implementations in user file.
364   * @param Delay  specifies the delay time length, in milliseconds.
365   * @retval None
366   */
HAL_Delay(uint32_t Delay)367 __weak void HAL_Delay(uint32_t Delay)
368 {
369   uint32_t tickstart = HAL_GetTick();
370   uint32_t wait = Delay;
371 
372   /* Add a freq to guarantee minimum wait */
373   if (wait < HAL_MAX_DELAY)
374   {
375     wait += (uint32_t)(uwTickFreq);
376   }
377 
378   while ((HAL_GetTick() - tickstart) < wait)
379   {
380   }
381 }
382 
383 /**
384   * @brief Suspend Tick increment.
385   * @note In the default implementation , SysTick timer is the source of time base. It is
386   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
387   *       is called, the SysTick interrupt will be disabled and so Tick increment
388   *       is suspended.
389   * @note This function is declared as __weak to be overwritten in case of other
390   *       implementations in user file.
391   * @retval None
392   */
HAL_SuspendTick(void)393 __weak void HAL_SuspendTick(void)
394 {
395   /* Disable SysTick Interrupt */
396   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
397 }
398 
399 /**
400   * @brief Resume Tick increment.
401   * @note In the default implementation , SysTick timer is the source of time base. It is
402   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
403   *       is called, the SysTick interrupt will be enabled and so Tick increment
404   *       is resumed.
405   * @note This function is declared as __weak to be overwritten in case of other
406   *       implementations in user file.
407   * @retval None
408   */
HAL_ResumeTick(void)409 __weak void HAL_ResumeTick(void)
410 {
411   /* Enable SysTick Interrupt */
412   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
413 }
414 
415 /**
416   * @brief  Returns the HAL revision
417   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
418   */
HAL_GetHalVersion(void)419 uint32_t HAL_GetHalVersion(void)
420 {
421   return __STM32F7xx_HAL_VERSION;
422 }
423 
424 /**
425   * @brief  Returns the device revision identifier.
426   * @retval Device revision identifier
427   */
HAL_GetREVID(void)428 uint32_t HAL_GetREVID(void)
429 {
430    return((DBGMCU->IDCODE) >> 16U);
431 }
432 
433 /**
434   * @brief  Returns the device identifier.
435   * @retval Device identifier
436   */
HAL_GetDEVID(void)437 uint32_t HAL_GetDEVID(void)
438 {
439    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
440 }
441 
442 /**
443   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
444   * @retval Device identifier
445   */
HAL_GetUIDw0(void)446 uint32_t HAL_GetUIDw0(void)
447 {
448   return(READ_REG(*((uint32_t *)UID_BASE)));
449 }
450 
451 /**
452   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
453   * @retval Device identifier
454   */
HAL_GetUIDw1(void)455 uint32_t HAL_GetUIDw1(void)
456 {
457   return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
458 }
459 
460 /**
461   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
462   * @retval Device identifier
463   */
HAL_GetUIDw2(void)464 uint32_t HAL_GetUIDw2(void)
465 {
466   return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
467 }
468 
469 /**
470   * @brief  Enable the Debug Module during SLEEP mode
471   * @retval None
472   */
HAL_DBGMCU_EnableDBGSleepMode(void)473 void HAL_DBGMCU_EnableDBGSleepMode(void)
474 {
475   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
476 }
477 
478 /**
479   * @brief  Disable the Debug Module during SLEEP mode
480   * @retval None
481   */
HAL_DBGMCU_DisableDBGSleepMode(void)482 void HAL_DBGMCU_DisableDBGSleepMode(void)
483 {
484   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
485 }
486 
487 /**
488   * @brief  Enable the Debug Module during STOP mode
489   * @retval None
490   */
HAL_DBGMCU_EnableDBGStopMode(void)491 void HAL_DBGMCU_EnableDBGStopMode(void)
492 {
493   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
494 }
495 
496 /**
497   * @brief  Disable the Debug Module during STOP mode
498   * @retval None
499   */
HAL_DBGMCU_DisableDBGStopMode(void)500 void HAL_DBGMCU_DisableDBGStopMode(void)
501 {
502   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
503 }
504 
505 /**
506   * @brief  Enable the Debug Module during STANDBY mode
507   * @retval None
508   */
HAL_DBGMCU_EnableDBGStandbyMode(void)509 void HAL_DBGMCU_EnableDBGStandbyMode(void)
510 {
511   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
512 }
513 
514 /**
515   * @brief  Disable the Debug Module during STANDBY mode
516   * @retval None
517   */
HAL_DBGMCU_DisableDBGStandbyMode(void)518 void HAL_DBGMCU_DisableDBGStandbyMode(void)
519 {
520   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
521 }
522 
523 /**
524   * @brief  Enables the I/O Compensation Cell.
525   * @note   The I/O compensation cell can be used only when the device supply
526   *         voltage ranges from 2.4 to 3.6 V.
527   * @retval None
528   */
HAL_EnableCompensationCell(void)529 void HAL_EnableCompensationCell(void)
530 {
531   SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;
532 }
533 
534 /**
535   * @brief  Power-down the I/O Compensation Cell.
536   * @note   The I/O compensation cell can be used only when the device supply
537   *         voltage ranges from 2.4 to 3.6 V.
538   * @retval None
539   */
HAL_DisableCompensationCell(void)540 void HAL_DisableCompensationCell(void)
541 {
542   SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);
543 }
544 
545 /**
546   * @brief  Enables the FMC Memory Mapping Swapping.
547   *
548   * @note   SDRAM is accessible at 0x60000000
549   *         and NOR/RAM is accessible at 0xC0000000
550   *
551   * @retval None
552   */
HAL_EnableFMCMemorySwapping(void)553 void HAL_EnableFMCMemorySwapping(void)
554 {
555   SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;
556 }
557 
558 /**
559   * @brief  Disables the FMC Memory Mapping Swapping
560   *
561   * @note   SDRAM is accessible at 0xC0000000 (default mapping)
562   *         and NOR/RAM is accessible at 0x60000000 (default mapping)
563   *
564   * @retval None
565   */
HAL_DisableFMCMemorySwapping(void)566 void HAL_DisableFMCMemorySwapping(void)
567 {
568   SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);
569 }
570 
571 #if defined (STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
572 /**
573 * @brief  Enable the Internal FLASH Bank Swapping.
574 *
575 * @note   This function can be used only for STM32F77xx/STM32F76xx devices.
576 *
577 * @note   Flash Bank2 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
578 *         and Flash Bank1 mapped at 0x08100000 (AXI) (aliased at 0x00300000 (TCM))
579 *
580 * @retval None
581 */
HAL_EnableMemorySwappingBank(void)582 void HAL_EnableMemorySwappingBank(void)
583 {
584   SET_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
585 }
586 
587 /**
588 * @brief  Disable the Internal FLASH Bank Swapping.
589 *
590 * @note   This function can be used only for STM32F77xx/STM32F76xx devices.
591 *
592 * @note   The default state : Flash Bank1 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
593 *         and Flash Bank2 mapped at 0x08100000 (AXI)( aliased at 0x00300000 (TCM))
594 *
595 * @retval None
596 */
HAL_DisableMemorySwappingBank(void)597 void HAL_DisableMemorySwappingBank(void)
598 {
599   CLEAR_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
600 }
601 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
602 
603 /**
604   * @}
605   */
606 
607 /**
608   * @}
609   */
610 
611 /**
612   * @}
613   */
614 
615 /**
616   * @}
617   */
618 
619