xref: /btstack/port/stm32-f4discovery-cc256x/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c (revision 225f4ba4fe806afeda1ee8519bb5f4a8ce540af2)
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal.c
4   * @author  MCD Application Team
5   * @brief   HAL module driver.
6   *          This is the common part of the HAL initialization
7   *
8   @verbatim
9   ==============================================================================
10                      ##### How to use this driver #####
11   ==============================================================================
12     [..]
13     The common HAL driver contains a set of generic and common APIs that can be
14     used by the PPP peripheral drivers and the user to start using the HAL.
15     [..]
16     The HAL contains two APIs' categories:
17          (+) Common HAL APIs
18          (+) Services HAL APIs
19 
20   @endverbatim
21   ******************************************************************************
22   * @attention
23   *
24   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
25   * All rights reserved.</center></h2>
26   *
27   * This software component is licensed by ST under BSD 3-Clause license,
28   * the "License"; You may not use this file except in compliance with the
29   * License. You may obtain a copy of the License at:
30   *                        opensource.org/licenses/BSD-3-Clause
31   *
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32f4xx_hal.h"
37 
38 /** @addtogroup STM32F4xx_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 STM32F4xx HAL Driver version number V1.7.5
54   */
55 #define __STM32F4xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
56 #define __STM32F4xx_HAL_VERSION_SUB1   (0x07U) /*!< [23:16] sub1 version */
57 #define __STM32F4xx_HAL_VERSION_SUB2   (0x05U) /*!< [15:8]  sub2 version */
58 #define __STM32F4xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
59 #define __STM32F4xx_HAL_VERSION         ((__STM32F4xx_HAL_VERSION_MAIN << 24U)\
60                                         |(__STM32F4xx_HAL_VERSION_SUB1 << 16U)\
61                                         |(__STM32F4xx_HAL_VERSION_SUB2 << 8U )\
62                                         |(__STM32F4xx_HAL_VERSION_RC))
63 
64 #define IDCODE_DEVID_MASK    0x00000FFFU
65 
66 /* ------------ RCC registers bit address in the alias region ----------- */
67 #define SYSCFG_OFFSET             (SYSCFG_BASE - PERIPH_BASE)
68 /* ---  MEMRMP Register ---*/
69 /* Alias word address of UFB_MODE bit */
70 #define MEMRMP_OFFSET             SYSCFG_OFFSET
71 #define UFB_MODE_BIT_NUMBER       SYSCFG_MEMRMP_UFB_MODE_Pos
72 #define UFB_MODE_BB               (uint32_t)(PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (UFB_MODE_BIT_NUMBER * 4U))
73 
74 /* ---  CMPCR Register ---*/
75 /* Alias word address of CMP_PD bit */
76 #define CMPCR_OFFSET              (SYSCFG_OFFSET + 0x20U)
77 #define CMP_PD_BIT_NUMBER         SYSCFG_CMPCR_CMP_PD_Pos
78 #define CMPCR_CMP_PD_BB           (uint32_t)(PERIPH_BB_BASE + (CMPCR_OFFSET * 32U) + (CMP_PD_BIT_NUMBER * 4U))
79 
80 /* ---  MCHDLYCR Register ---*/
81 /* Alias word address of BSCKSEL bit */
82 #define MCHDLYCR_OFFSET            (SYSCFG_OFFSET + 0x30U)
83 #define BSCKSEL_BIT_NUMBER         SYSCFG_MCHDLYCR_BSCKSEL_Pos
84 #define MCHDLYCR_BSCKSEL_BB        (uint32_t)(PERIPH_BB_BASE + (MCHDLYCR_OFFSET * 32U) + (BSCKSEL_BIT_NUMBER * 4U))
85 /**
86   * @}
87   */
88 
89 /* Private macro -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 /** @addtogroup HAL_Private_Variables
92   * @{
93   */
94 __IO uint32_t uwTick;
95 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
96 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
97 /**
98   * @}
99   */
100 /* Private function prototypes -----------------------------------------------*/
101 /* Private functions ---------------------------------------------------------*/
102 
103 /** @defgroup HAL_Exported_Functions HAL Exported Functions
104   * @{
105   */
106 
107 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
108  *  @brief    Initialization and de-initialization functions
109  *
110 @verbatim
111  ===============================================================================
112               ##### Initialization and Configuration functions #####
113  ===============================================================================
114     [..]  This section provides functions allowing to:
115       (+) Initializes the Flash interface the NVIC allocation and initial clock
116           configuration. It initializes the systick also when timeout is needed
117           and the backup domain when enabled.
118       (+) De-Initializes common part of the HAL.
119       (+) Configure the time base source to have 1ms time base with a dedicated
120           Tick interrupt priority.
121         (++) SysTick timer is used by default as source of time base, but user
122              can eventually implement his proper time base source (a general purpose
123              timer for example or other time source), keeping in mind that Time base
124              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
125              handled in milliseconds basis.
126         (++) Time base configuration function (HAL_InitTick ()) is called automatically
127              at the beginning of the program after reset by HAL_Init() or at any time
128              when clock is configured, by HAL_RCC_ClockConfig().
129         (++) Source of time base is configured  to generate interrupts at regular
130              time intervals. Care must be taken if HAL_Delay() is called from a
131              peripheral ISR process, the Tick interrupt line must have higher priority
132             (numerically lower) than the peripheral interrupt. Otherwise the caller
133             ISR process will be blocked.
134        (++) functions affecting time base configurations are declared as __weak
135              to make  override possible  in case of other  implementations in user file.
136 @endverbatim
137   * @{
138   */
139 
140 /**
141   * @brief  This function is used to initialize the HAL Library; it must be the first
142   *         instruction to be executed in the main program (before to call any other
143   *         HAL function), it performs the following:
144   *           Configure the Flash prefetch, instruction and Data caches.
145   *           Configures the SysTick to generate an interrupt each 1 millisecond,
146   *           which is clocked by the HSI (at this stage, the clock is not yet
147   *           configured and thus the system is running from the internal HSI at 16 MHz).
148   *           Set NVIC Group Priority to 4.
149   *           Calls the HAL_MspInit() callback function defined in user file
150   *           "stm32f4xx_hal_msp.c" to do the global low level hardware initialization
151   *
152   * @note   SysTick is used as time base for the HAL_Delay() function, the application
153   *         need to ensure that the SysTick time base is always set to 1 millisecond
154   *         to have correct HAL operation.
155   * @retval HAL status
156   */
HAL_Init(void)157 HAL_StatusTypeDef HAL_Init(void)
158 {
159   /* Configure Flash prefetch, Instruction cache, Data cache */
160 #if (INSTRUCTION_CACHE_ENABLE != 0U)
161   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
162 #endif /* INSTRUCTION_CACHE_ENABLE */
163 
164 #if (DATA_CACHE_ENABLE != 0U)
165   __HAL_FLASH_DATA_CACHE_ENABLE();
166 #endif /* DATA_CACHE_ENABLE */
167 
168 #if (PREFETCH_ENABLE != 0U)
169   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
170 #endif /* PREFETCH_ENABLE */
171 
172   /* Set Interrupt Group Priority */
173   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
174 
175   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
176   HAL_InitTick(TICK_INT_PRIORITY);
177 
178   /* Init the low level hardware */
179   HAL_MspInit();
180 
181   /* Return function status */
182   return HAL_OK;
183 }
184 
185 /**
186   * @brief  This function de-Initializes common part of the HAL and stops the systick.
187   *         This function is optional.
188   * @retval HAL status
189   */
HAL_DeInit(void)190 HAL_StatusTypeDef HAL_DeInit(void)
191 {
192   /* Reset of all peripherals */
193   __HAL_RCC_APB1_FORCE_RESET();
194   __HAL_RCC_APB1_RELEASE_RESET();
195 
196   __HAL_RCC_APB2_FORCE_RESET();
197   __HAL_RCC_APB2_RELEASE_RESET();
198 
199   __HAL_RCC_AHB1_FORCE_RESET();
200   __HAL_RCC_AHB1_RELEASE_RESET();
201 
202   __HAL_RCC_AHB2_FORCE_RESET();
203   __HAL_RCC_AHB2_RELEASE_RESET();
204 
205   __HAL_RCC_AHB3_FORCE_RESET();
206   __HAL_RCC_AHB3_RELEASE_RESET();
207 
208   /* De-Init the low level hardware */
209   HAL_MspDeInit();
210 
211   /* Return function status */
212   return HAL_OK;
213 }
214 
215 /**
216   * @brief  Initialize the MSP.
217   * @retval None
218   */
HAL_MspInit(void)219 __weak void HAL_MspInit(void)
220 {
221   /* NOTE : This function should not be modified, when the callback is needed,
222             the HAL_MspInit could be implemented in the user file
223    */
224 }
225 
226 /**
227   * @brief  DeInitializes the MSP.
228   * @retval None
229   */
HAL_MspDeInit(void)230 __weak void HAL_MspDeInit(void)
231 {
232   /* NOTE : This function should not be modified, when the callback is needed,
233             the HAL_MspDeInit could be implemented in the user file
234    */
235 }
236 
237 /**
238   * @brief This function configures the source of the time base.
239   *        The time source is configured  to have 1ms time base with a dedicated
240   *        Tick interrupt priority.
241   * @note This function is called  automatically at the beginning of program after
242   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
243   * @note In the default implementation, SysTick timer is the source of time base.
244   *       It is used to generate interrupts at regular time intervals.
245   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
246   *       The SysTick interrupt must have higher priority (numerically lower)
247   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
248   *       The function is declared as __weak  to be overwritten  in case of other
249   *       implementation  in user file.
250   * @param TickPriority Tick interrupt priority.
251   * @retval HAL status
252   */
HAL_InitTick(uint32_t TickPriority)253 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
254 {
255   /* Configure the SysTick to have interrupt in 1ms time basis*/
256   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
257   {
258     return HAL_ERROR;
259   }
260 
261   /* Configure the SysTick IRQ priority */
262   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
263   {
264     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
265     uwTickPrio = TickPriority;
266   }
267   else
268   {
269     return HAL_ERROR;
270   }
271 
272   /* Return function status */
273   return HAL_OK;
274 }
275 
276 /**
277   * @}
278   */
279 
280 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
281  *  @brief    HAL Control functions
282  *
283 @verbatim
284  ===============================================================================
285                       ##### HAL Control functions #####
286  ===============================================================================
287     [..]  This section provides functions allowing to:
288       (+) Provide a tick value in millisecond
289       (+) Provide a blocking delay in millisecond
290       (+) Suspend the time base source interrupt
291       (+) Resume the time base source interrupt
292       (+) Get the HAL API driver version
293       (+) Get the device identifier
294       (+) Get the device revision identifier
295       (+) Enable/Disable Debug module during SLEEP mode
296       (+) Enable/Disable Debug module during STOP mode
297       (+) Enable/Disable Debug module during STANDBY mode
298 
299 @endverbatim
300   * @{
301   */
302 
303 /**
304   * @brief This function is called to increment  a global variable "uwTick"
305   *        used as application time base.
306   * @note In the default implementation, this variable is incremented each 1ms
307   *       in SysTick ISR.
308  * @note This function is declared as __weak to be overwritten in case of other
309   *      implementations in user file.
310   * @retval None
311   */
HAL_IncTick(void)312 __weak void HAL_IncTick(void)
313 {
314   uwTick += uwTickFreq;
315 }
316 
317 /**
318   * @brief Provides a tick value in millisecond.
319   * @note This function is declared as __weak to be overwritten in case of other
320   *       implementations in user file.
321   * @retval tick value
322   */
HAL_GetTick(void)323 __weak uint32_t HAL_GetTick(void)
324 {
325   return uwTick;
326 }
327 
328 /**
329   * @brief This function returns a tick priority.
330   * @retval tick priority
331   */
HAL_GetTickPrio(void)332 uint32_t HAL_GetTickPrio(void)
333 {
334   return uwTickPrio;
335 }
336 
337 /**
338   * @brief Set new tick Freq.
339   * @retval Status
340   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)341 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
342 {
343   HAL_StatusTypeDef status  = HAL_OK;
344   assert_param(IS_TICKFREQ(Freq));
345 
346   if (uwTickFreq != Freq)
347   {
348     uwTickFreq = Freq;
349 
350     /* Apply the new tick Freq  */
351     status = HAL_InitTick(uwTickPrio);
352   }
353 
354   return status;
355 }
356 
357 /**
358   * @brief Return tick frequency.
359   * @retval tick period in Hz
360   */
HAL_GetTickFreq(void)361 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
362 {
363   return uwTickFreq;
364 }
365 
366 /**
367   * @brief This function provides minimum delay (in milliseconds) based
368   *        on variable incremented.
369   * @note In the default implementation , SysTick timer is the source of time base.
370   *       It is used to generate interrupts at regular time intervals where uwTick
371   *       is incremented.
372   * @note This function is declared as __weak to be overwritten in case of other
373   *       implementations in user file.
374   * @param Delay specifies the delay time length, in milliseconds.
375   * @retval None
376   */
HAL_Delay(uint32_t Delay)377 __weak void HAL_Delay(uint32_t Delay)
378 {
379   uint32_t tickstart = HAL_GetTick();
380   uint32_t wait = Delay;
381 
382   /* Add a freq to guarantee minimum wait */
383   if (wait < HAL_MAX_DELAY)
384   {
385     wait += (uint32_t)(uwTickFreq);
386   }
387 
388   while((HAL_GetTick() - tickstart) < wait)
389   {
390   }
391 }
392 
393 /**
394   * @brief Suspend Tick increment.
395   * @note In the default implementation , SysTick timer is the source of time base. It is
396   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
397   *       is called, the SysTick interrupt will be disabled and so Tick increment
398   *       is suspended.
399   * @note This function is declared as __weak to be overwritten in case of other
400   *       implementations in user file.
401   * @retval None
402   */
HAL_SuspendTick(void)403 __weak void HAL_SuspendTick(void)
404 {
405   /* Disable SysTick Interrupt */
406   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
407 }
408 
409 /**
410   * @brief Resume Tick increment.
411   * @note In the default implementation , SysTick timer is the source of time base. It is
412   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
413   *       is called, the SysTick interrupt will be enabled and so Tick increment
414   *       is resumed.
415   * @note This function is declared as __weak to be overwritten in case of other
416   *       implementations in user file.
417   * @retval None
418   */
HAL_ResumeTick(void)419 __weak void HAL_ResumeTick(void)
420 {
421   /* Enable SysTick Interrupt */
422   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
423 }
424 
425 /**
426   * @brief  Returns the HAL revision
427   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
428   */
HAL_GetHalVersion(void)429 uint32_t HAL_GetHalVersion(void)
430 {
431   return __STM32F4xx_HAL_VERSION;
432 }
433 
434 /**
435   * @brief  Returns the device revision identifier.
436   * @retval Device revision identifier
437   */
HAL_GetREVID(void)438 uint32_t HAL_GetREVID(void)
439 {
440   return((DBGMCU->IDCODE) >> 16U);
441 }
442 
443 /**
444   * @brief  Returns the device identifier.
445   * @retval Device identifier
446   */
HAL_GetDEVID(void)447 uint32_t HAL_GetDEVID(void)
448 {
449   return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
450 }
451 
452 /**
453   * @brief  Enable the Debug Module during SLEEP mode
454   * @retval None
455   */
HAL_DBGMCU_EnableDBGSleepMode(void)456 void HAL_DBGMCU_EnableDBGSleepMode(void)
457 {
458   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
459 }
460 
461 /**
462   * @brief  Disable the Debug Module during SLEEP mode
463   * @retval None
464   */
HAL_DBGMCU_DisableDBGSleepMode(void)465 void HAL_DBGMCU_DisableDBGSleepMode(void)
466 {
467   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
468 }
469 
470 /**
471   * @brief  Enable the Debug Module during STOP mode
472   * @retval None
473   */
HAL_DBGMCU_EnableDBGStopMode(void)474 void HAL_DBGMCU_EnableDBGStopMode(void)
475 {
476   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
477 }
478 
479 /**
480   * @brief  Disable the Debug Module during STOP mode
481   * @retval None
482   */
HAL_DBGMCU_DisableDBGStopMode(void)483 void HAL_DBGMCU_DisableDBGStopMode(void)
484 {
485   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
486 }
487 
488 /**
489   * @brief  Enable the Debug Module during STANDBY mode
490   * @retval None
491   */
HAL_DBGMCU_EnableDBGStandbyMode(void)492 void HAL_DBGMCU_EnableDBGStandbyMode(void)
493 {
494   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
495 }
496 
497 /**
498   * @brief  Disable the Debug Module during STANDBY mode
499   * @retval None
500   */
HAL_DBGMCU_DisableDBGStandbyMode(void)501 void HAL_DBGMCU_DisableDBGStandbyMode(void)
502 {
503   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
504 }
505 
506 /**
507   * @brief  Enables the I/O Compensation Cell.
508   * @note   The I/O compensation cell can be used only when the device supply
509   *         voltage ranges from 2.4 to 3.6 V.
510   * @retval None
511   */
HAL_EnableCompensationCell(void)512 void HAL_EnableCompensationCell(void)
513 {
514   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
515 }
516 
517 /**
518   * @brief  Power-down the I/O Compensation Cell.
519   * @note   The I/O compensation cell can be used only when the device supply
520   *         voltage ranges from 2.4 to 3.6 V.
521   * @retval None
522   */
HAL_DisableCompensationCell(void)523 void HAL_DisableCompensationCell(void)
524 {
525   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
526 }
527 
528 /**
529   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
530   * @retval Device identifier
531   */
HAL_GetUIDw0(void)532 uint32_t HAL_GetUIDw0(void)
533 {
534   return (READ_REG(*((uint32_t *)UID_BASE)));
535 }
536 
537 /**
538   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
539   * @retval Device identifier
540   */
HAL_GetUIDw1(void)541 uint32_t HAL_GetUIDw1(void)
542 {
543   return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
544 }
545 
546 /**
547   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
548   * @retval Device identifier
549   */
HAL_GetUIDw2(void)550 uint32_t HAL_GetUIDw2(void)
551 {
552   return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
553 }
554 
555 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) ||\
556     defined(STM32F469xx) || defined(STM32F479xx)
557 /**
558   * @brief  Enables the Internal FLASH Bank Swapping.
559   *
560   * @note   This function can be used only for STM32F42xxx/43xxx devices.
561   *
562   * @note   Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
563   *         and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
564   *
565   * @retval None
566   */
HAL_EnableMemorySwappingBank(void)567 void HAL_EnableMemorySwappingBank(void)
568 {
569   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
570 }
571 
572 /**
573   * @brief  Disables the Internal FLASH Bank Swapping.
574   *
575   * @note   This function can be used only for STM32F42xxx/43xxx devices.
576   *
577   * @note   The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x00000000)
578   *         and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
579   *
580   * @retval None
581   */
HAL_DisableMemorySwappingBank(void)582 void HAL_DisableMemorySwappingBank(void)
583 {
584   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
585 }
586 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
587 /**
588   * @}
589   */
590 
591 /**
592   * @}
593   */
594 
595 /**
596   * @}
597   */
598 
599 /**
600   * @}
601   */
602 
603 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
604