1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_hal.c
4   * @author  MCD Application Team
5   * @version V1.0.1
6   * @date    25-June-2015
7   * @brief   HAL module driver.
8   *          This is the common part of the HAL initialization
9   *
10   @verbatim
11   ==============================================================================
12                      ##### How to use this driver #####
13   ==============================================================================
14     [..]
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.
17     [..]
18     The HAL contains two APIs' categories:
19          (+) Common HAL APIs
20          (+) Services HAL APIs
21 
22   @endverbatim
23   ******************************************************************************
24   * @attention
25   *
26   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
27   *
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.
38   *
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.
49   *
50   ******************************************************************************
51   */
52 
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32f7xx_hal.h"
55 
56 /** @addtogroup STM32F7xx_HAL_Driver
57   * @{
58   */
59 
60 /** @defgroup HAL HAL
61   * @brief HAL module driver.
62   * @{
63   */
64 
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /** @addtogroup HAL_Private_Constants
68   * @{
69   */
70 /**
71  * @brief STM32F7xx HAL Driver version number V1.0.1
72    */
73 #define __STM32F7xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
74 #define __STM32F7xx_HAL_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
75 #define __STM32F7xx_HAL_VERSION_SUB2   (0x01) /*!< [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))
81 
82 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)
83 /**
84   * @}
85   */
86 
87 /* Private macro -------------------------------------------------------------*/
88 /* Private variables ---------------------------------------------------------*/
89 /** @addtogroup HAL_Private_Variables
90   * @{
91   */
92 static __IO uint32_t uwTick;
93 /**
94   * @}
95   */
96 
97 /* Private function prototypes -----------------------------------------------*/
98 /* Private functions ---------------------------------------------------------*/
99 
100 /** @defgroup HAL_Exported_Functions HAL Exported Functions
101   * @{
102   */
103 
104 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
105  *  @brief    Initialization and de-initialization functions
106  *
107 @verbatim
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.
133 @endverbatim
134   * @{
135   */
136 
137 /**
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
148   *
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.
152   * @retval HAL status
153   */
HAL_Init(void)154 HAL_StatusTypeDef HAL_Init(void)
155 {
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 */
160 
161     /* Set Interrupt Group Priority */
162     HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
163 
164     /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
165     HAL_InitTick(TICK_INT_PRIORITY);
166 
167     /* Init the low level hardware */
168     HAL_MspInit();
169 
170     /* Return function status */
171     return HAL_OK;
172 }
173 
174 /**
175   * @brief  This function de-Initializes common part of the HAL and stops the systick.
176   *         This function is optional.
177   * @retval HAL status
178   */
HAL_DeInit(void)179 HAL_StatusTypeDef HAL_DeInit(void)
180 {
181     /* Reset of all peripherals */
182     __HAL_RCC_APB1_FORCE_RESET();
183     __HAL_RCC_APB1_RELEASE_RESET();
184 
185     __HAL_RCC_APB2_FORCE_RESET();
186     __HAL_RCC_APB2_RELEASE_RESET();
187 
188     __HAL_RCC_AHB1_FORCE_RESET();
189     __HAL_RCC_AHB1_RELEASE_RESET();
190 
191     __HAL_RCC_AHB2_FORCE_RESET();
192     __HAL_RCC_AHB2_RELEASE_RESET();
193 
194     __HAL_RCC_AHB3_FORCE_RESET();
195     __HAL_RCC_AHB3_RELEASE_RESET();
196 
197     /* De-Init the low level hardware */
198     HAL_MspDeInit();
199 
200     /* Return function status */
201     return HAL_OK;
202 }
203 
204 /**
205   * @brief  Initializes the MSP.
206   * @retval None
207   */
HAL_MspInit(void)208 __weak void HAL_MspInit(void)
209 {
210     /* NOTE : This function Should not be modified, when the callback is needed,
211               the HAL_MspInit could be implemented in the user file
212      */
213 }
214 
215 /**
216   * @brief  DeInitializes the MSP.
217   * @retval None
218   */
HAL_MspDeInit(void)219 __weak void HAL_MspDeInit(void)
220 {
221     /* NOTE : This function Should not be modified, when the callback is needed,
222               the HAL_MspDeInit could be implemented in the user file
223      */
224 }
225 
226 /**
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.
240   * @retval HAL status
241   */
242 #if 0 // disabled for LK
243 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
244 {
245     /*Configure the SysTick to have interrupt in 1ms time basis*/
246     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
247 
248     /*Configure the SysTick IRQ priority */
249     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
250 
251     /* Return function status */
252     return HAL_OK;
253 }
254 #endif
255 
256 /**
257   * @}
258   */
259 
260 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
261  *  @brief    HAL Control functions
262  *
263 @verbatim
264  ===============================================================================
265                       ##### HAL Control functions #####
266  ===============================================================================
267     [..]  This section provides functions allowing to:
268       (+) Provide a tick value in millisecond
269       (+) Provide a blocking delay in millisecond
270       (+) Suspend the time base source interrupt
271       (+) Resume the time base source interrupt
272       (+) Get the HAL API driver version
273       (+) Get the device identifier
274       (+) Get the device revision identifier
275       (+) Enable/Disable Debug module during SLEEP mode
276       (+) Enable/Disable Debug module during STOP mode
277       (+) Enable/Disable Debug module during STANDBY mode
278 
279 @endverbatim
280   * @{
281   */
282 
283 #if 0 // disabled for LK
284 /**
285   * @brief This function is called to increment  a global variable "uwTick"
286   *        used as application time base.
287   * @note In the default implementation, this variable is incremented each 1ms
288   *       in Systick ISR.
289  * @note This function is declared as __weak to be overwritten in case of other
290   *      implementations in user file.
291   * @retval None
292   */
293 __weak void HAL_IncTick(void)
294 {
295     uwTick++;
296 }
297 
298 /**
299   * @brief Provides a tick value in millisecond.
300   * @note This function is declared as __weak to be overwritten in case of other
301   *       implementations in user file.
302   * @retval tick value
303   */
304 __weak uint32_t HAL_GetTick(void)
305 {
306     return uwTick;
307 }
308 
309 /**
310   * @brief This function provides accurate delay (in milliseconds) based
311   *        on variable incremented.
312   * @note In the default implementation , SysTick timer is the source of time base.
313   *       It is used to generate interrupts at regular time intervals where uwTick
314   *       is incremented.
315   * @note ThiS function is declared as __weak to be overwritten in case of other
316   *       implementations in user file.
317   * @param Delay: specifies the delay time length, in milliseconds.
318   * @retval None
319   */
320 __weak void HAL_Delay(__IO uint32_t Delay)
321 {
322     uint32_t tickstart = 0;
323     tickstart = HAL_GetTick();
324     while ((HAL_GetTick() - tickstart) < Delay) {
325     }
326 }
327 
328 /**
329   * @brief Suspend Tick increment.
330   * @note In the default implementation , SysTick timer is the source of time base. It is
331   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
332   *       is called, the the SysTick interrupt will be disabled and so Tick increment
333   *       is suspended.
334   * @note This function is declared as __weak to be overwritten in case of other
335   *       implementations in user file.
336   * @retval None
337   */
338 __weak void HAL_SuspendTick(void)
339 {
340     /* Disable SysTick Interrupt */
341     SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
342 }
343 
344 /**
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   * @note This function is declared as __weak to be overwritten in case of other
351   *       implementations in user file.
352   * @retval None
353   */
354 __weak void HAL_ResumeTick(void)
355 {
356     /* Enable SysTick Interrupt */
357     SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
358 }
359 #endif // LK
360 
361 /**
362   * @brief  Returns the HAL revision
363   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
364   */
HAL_GetHalVersion(void)365 uint32_t HAL_GetHalVersion(void)
366 {
367     return __STM32F7xx_HAL_VERSION;
368 }
369 
370 /**
371   * @brief  Returns the device revision identifier.
372   * @retval Device revision identifier
373   */
HAL_GetREVID(void)374 uint32_t HAL_GetREVID(void)
375 {
376     return ((DBGMCU->IDCODE) >> 16);
377 }
378 
379 /**
380   * @brief  Returns the device identifier.
381   * @retval Device identifier
382   */
HAL_GetDEVID(void)383 uint32_t HAL_GetDEVID(void)
384 {
385     return ((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
386 }
387 
388 /**
389   * @brief  Enable the Debug Module during SLEEP mode
390   * @retval None
391   */
HAL_DBGMCU_EnableDBGSleepMode(void)392 void HAL_DBGMCU_EnableDBGSleepMode(void)
393 {
394     SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
395 }
396 
397 /**
398   * @brief  Disable the Debug Module during SLEEP mode
399   * @retval None
400   */
HAL_DBGMCU_DisableDBGSleepMode(void)401 void HAL_DBGMCU_DisableDBGSleepMode(void)
402 {
403     CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
404 }
405 
406 /**
407   * @brief  Enable the Debug Module during STOP mode
408   * @retval None
409   */
HAL_DBGMCU_EnableDBGStopMode(void)410 void HAL_DBGMCU_EnableDBGStopMode(void)
411 {
412     SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
413 }
414 
415 /**
416   * @brief  Disable the Debug Module during STOP mode
417   * @retval None
418   */
HAL_DBGMCU_DisableDBGStopMode(void)419 void HAL_DBGMCU_DisableDBGStopMode(void)
420 {
421     CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
422 }
423 
424 /**
425   * @brief  Enable the Debug Module during STANDBY mode
426   * @retval None
427   */
HAL_DBGMCU_EnableDBGStandbyMode(void)428 void HAL_DBGMCU_EnableDBGStandbyMode(void)
429 {
430     SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
431 }
432 
433 /**
434   * @brief  Disable the Debug Module during STANDBY mode
435   * @retval None
436   */
HAL_DBGMCU_DisableDBGStandbyMode(void)437 void HAL_DBGMCU_DisableDBGStandbyMode(void)
438 {
439     CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
440 }
441 
442 /**
443   * @brief  Enables the I/O Compensation Cell.
444   * @note   The I/O compensation cell can be used only when the device supply
445   *         voltage ranges from 2.4 to 3.6 V.
446   * @retval None
447   */
HAL_EnableCompensationCell(void)448 void HAL_EnableCompensationCell(void)
449 {
450     SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;
451 }
452 
453 /**
454   * @brief  Power-down the I/O Compensation Cell.
455   * @note   The I/O compensation cell can be used only when the device supply
456   *         voltage ranges from 2.4 to 3.6 V.
457   * @retval None
458   */
HAL_DisableCompensationCell(void)459 void HAL_DisableCompensationCell(void)
460 {
461     SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);
462 }
463 
464 /**
465   * @brief  Enables the FMC Memory Mapping Swapping.
466   *
467   * @note   SDRAM is accessible at 0x60000000
468   *         and NOR/RAM is accessible at 0xC0000000
469   *
470   * @retval None
471   */
HAL_EnableFMCMemorySwapping(void)472 void HAL_EnableFMCMemorySwapping(void)
473 {
474     SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;
475 }
476 
477 /**
478   * @brief  Disables the FMC Memory Mapping Swapping
479   *
480   * @note   SDRAM is accessible at 0xC0000000 (default mapping)
481   *         and NOR/RAM is accessible at 0x60000000 (default mapping)
482   *
483   * @retval None
484   */
HAL_DisableFMCMemorySwapping(void)485 void HAL_DisableFMCMemorySwapping(void)
486 {
487 
488     SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);
489 }
490 
491 /**
492   * @}
493   */
494 
495 /**
496   * @}
497   */
498 
499 /**
500   * @}
501   */
502 
503 /**
504   * @}
505   */
506 
507 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
508