1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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) 2016 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          (+) HAL Initialization and de-initialization functions
29          (+) HAL Control functions
30 
31   @endverbatim
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32f3xx_hal.h"
37 
38 /** @addtogroup STM32F3xx_HAL_Driver
39   * @{
40   */
41 
42 /** @defgroup HAL HAL
43   * @brief HAL module driver.
44   * @{
45   */
46 
47 #ifdef HAL_MODULE_ENABLED
48 
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /** @defgroup HAL_Private Constants
52   * @{
53   */
54 /**
55  * @brief STM32F3xx HAL Driver version number V1.5.7
56    */
57 #define __STM32F3xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
58 #define __STM32F3xx_HAL_VERSION_SUB1   (0x05U) /*!< [23:16] sub1 version */
59 #define __STM32F3xx_HAL_VERSION_SUB2   (0x07U) /*!< [15:8]  sub2 version */
60 #define __STM32F3xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
61 #define __STM32F3xx_HAL_VERSION         ((__STM32F3xx_HAL_VERSION_MAIN << 24U)\
62                                         |(__STM32F3xx_HAL_VERSION_SUB1 << 16U)\
63                                         |(__STM32F3xx_HAL_VERSION_SUB2 << 8U )\
64                                         |(__STM32F3xx_HAL_VERSION_RC))
65 
66 #define IDCODE_DEVID_MASK    (0x00000FFFU)
67 /**
68   * @}
69   */
70 
71 /* Private macro -------------------------------------------------------------*/
72 /* Exported variables --------------------------------------------------------*/
73 /** @defgroup HAL_Exported_Variables HAL Exported Variables
74   * @{
75   */
76 __IO uint32_t uwTick;
77 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
78 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
79 /**
80   * @}
81   */
82 /* Private function prototypes -----------------------------------------------*/
83 /* Exported functions ---------------------------------------------------------*/
84 
85 /** @defgroup HAL_Exported_Functions HAL Exported Functions
86   * @{
87   */
88 
89 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
90  *  @brief    Initialization and de-initialization functions
91  *
92 @verbatim
93  ===============================================================================
94               ##### Initialization and de-initialization functions #####
95  ===============================================================================
96     [..]  This section provides functions allowing to:
97       (+) Initializes the Flash interface, the NVIC allocation and initial clock
98           configuration. It initializes the systick also when timeout is needed
99           and the backup domain when enabled.
100       (+) de-Initializes common part of the HAL.
101       (+) Configure The time base source to have 1ms time base with a dedicated
102           Tick interrupt priority.
103         (++) SysTick timer is used by default as source of time base, but user
104              can eventually implement his proper time base source (a general purpose
105              timer for example or other time source), keeping in mind that Time base
106              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
107              handled in milliseconds basis.
108         (++) Time base configuration function (HAL_InitTick ()) is called automatically
109              at the beginning of the program after reset by HAL_Init() or at any time
110              when clock is configured, by HAL_RCC_ClockConfig().
111         (++) Source of time base is configured  to generate interrupts at regular
112              time intervals. Care must be taken if HAL_Delay() is called from a
113              peripheral ISR process, the Tick interrupt line must have higher priority
114             (numerically lower) than the peripheral interrupt. Otherwise the caller
115             ISR process will be blocked.
116        (++) functions affecting time base configurations are declared as __Weak
117              to make  override possible  in case of other  implementations in user file.
118 
119 @endverbatim
120   * @{
121   */
122 
123 /**
124   * @brief  This function configures the Flash prefetch,
125   *         Configures time base source, NVIC and Low level hardware
126   * @note   This function is called at the beginning of program after reset and before
127   *         the clock configuration
128   *
129   * @note   The Systick configuration is based on HSI clock, as HSI is the clock
130   *         used after a system Reset and the NVIC configuration is set to Priority group 4
131   *
132   * @note   The time base configuration is based on MSI clock when exiting from Reset.
133   *         Once done, time base tick start incrementing.
134   *         In the default implementation,Systick is used as source of time base.
135   *       The tick variable is incremented each 1ms in its ISR.
136   * @retval HAL status
137   */
HAL_Init(void)138 HAL_StatusTypeDef HAL_Init(void)
139 {
140   /* Configure Flash prefetch */
141 #if (PREFETCH_ENABLE != 0U)
142   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
143 #endif /* PREFETCH_ENABLE */
144 
145   /* Set Interrupt Group Priority */
146   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
147 
148   /* Enable systick and configure 1ms tick (default clock after Reset is HSI) */
149   HAL_InitTick(TICK_INT_PRIORITY);
150 
151   /* Init the low level hardware */
152   HAL_MspInit();
153 
154   /* Return function status */
155   return HAL_OK;
156 }
157 
158 /**
159   * @brief  This function de-Initializes common part of the HAL and stops the systick.
160   * @note This function is optional.
161   * @retval HAL status
162   */
HAL_DeInit(void)163 HAL_StatusTypeDef HAL_DeInit(void)
164 {
165   /* Reset of all peripherals */
166   __HAL_RCC_APB1_FORCE_RESET();
167   __HAL_RCC_APB1_RELEASE_RESET();
168 
169   __HAL_RCC_APB2_FORCE_RESET();
170   __HAL_RCC_APB2_RELEASE_RESET();
171 
172   __HAL_RCC_AHB_FORCE_RESET();
173   __HAL_RCC_AHB_RELEASE_RESET();
174 
175   /* De-Init the low level hardware */
176   HAL_MspDeInit();
177 
178   /* Return function status */
179   return HAL_OK;
180 }
181 
182 /**
183   * @brief  Initialize the MSP.
184   * @retval None
185   */
HAL_MspInit(void)186 __weak void HAL_MspInit(void)
187 {
188   /* NOTE : This function should not be modified, when the callback is needed,
189             the HAL_MspInit could be implemented in the user file
190    */
191 }
192 
193 /**
194   * @brief  DeInitialize the MSP.
195   * @retval None
196   */
HAL_MspDeInit(void)197 __weak void HAL_MspDeInit(void)
198 {
199   /* NOTE : This function should not be modified, when the callback is needed,
200             the HAL_MspDeInit could be implemented in the user file
201    */
202 }
203 
204 /**
205   * @brief  This function configures the source of the time base.
206   *         The time source is configured  to have 1ms time base with a dedicated
207   *         Tick interrupt priority.
208   * @note   This function is called  automatically at the beginning of program after
209   *         reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
210   * @note   In the default implementation , SysTick timer is the source of time base.
211   *         It is used to generate interrupts at regular time intervals.
212   *         Care must be taken if HAL_Delay() is called from a peripheral ISR process,
213   *         The SysTick interrupt must have higher priority (numerically lower)
214   *         than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
215   *         The function is declared as __Weak  to be overwritten  in case of other
216   *         implementation  in user file.
217   * @param TickPriority Tick interrupt priority.
218   * @retval HAL status
219   */
HAL_InitTick(uint32_t TickPriority)220 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
221 {
222   /* Configure the SysTick to have interrupt in 1ms time basis*/
223   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
224   {
225     return HAL_ERROR;
226   }
227 
228   /* Configure the SysTick IRQ priority */
229   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
230   {
231     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
232     uwTickPrio = TickPriority;
233   }
234   else
235   {
236     return HAL_ERROR;
237   }
238    /* Return function status */
239   return HAL_OK;
240 }
241 
242 /**
243   * @}
244   */
245 
246 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
247  *  @brief    HAL Control functions
248  *
249 @verbatim
250  ===============================================================================
251                       ##### HAL Control functions #####
252  ===============================================================================
253     [..]  This section provides functions allowing to:
254       (+) Provide a tick value in millisecond
255       (+) Provide a blocking delay in millisecond
256       (+) Suspend the time base source interrupt
257       (+) Resume the time base source interrupt
258       (+) Get the HAL API driver version
259       (+) Get the device identifier
260       (+) Get the device revision identifier
261       (+) Enable/Disable Debug module during Sleep mode
262       (+) Enable/Disable Debug module during STOP mode
263       (+) Enable/Disable Debug module during STANDBY mode
264 
265 @endverbatim
266   * @{
267   */
268 
269 /**
270   * @brief  This function is called to increment  a global variable "uwTick"
271   *         used as application time base.
272   * @note In the default implementation, this variable is incremented each 1ms
273   *         in SysTick ISR.
274   * @note This function is declared as __weak to be overwritten in case of other
275   *         implementations  in user file.
276   * @retval None
277   */
HAL_IncTick(void)278 __weak void HAL_IncTick(void)
279 {
280   uwTick += uwTickFreq;
281 }
282 
283 /**
284   * @brief  Povides a tick value in millisecond.
285   * @note   The function is declared as __Weak  to be overwritten  in case of other
286   *         implementations  in user file.
287   * @retval tick value
288   */
HAL_GetTick(void)289 __weak uint32_t HAL_GetTick(void)
290 {
291   return uwTick;
292 }
293 
294 /**
295   * @brief This function returns a tick priority.
296   * @retval tick priority
297   */
HAL_GetTickPrio(void)298 uint32_t HAL_GetTickPrio(void)
299 {
300   return uwTickPrio;
301 }
302 
303 /**
304   * @brief Set new tick Freq.
305   * @retval status
306   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)307 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
308 {
309   HAL_StatusTypeDef status  = HAL_OK;
310   HAL_TickFreqTypeDef prevTickFreq;
311 
312   assert_param(IS_TICKFREQ(Freq));
313 
314   if (uwTickFreq != Freq)
315   {
316     /* Back up uwTickFreq frequency */
317     prevTickFreq = uwTickFreq;
318 
319     /* Update uwTickFreq global variable used by HAL_InitTick() */
320     uwTickFreq = Freq;
321 
322     /* Apply the new tick Freq */
323     status = HAL_InitTick(uwTickPrio);
324 
325     if (status != HAL_OK)
326     {
327       /* Restore previous tick frequency */
328       uwTickFreq = prevTickFreq;
329     }
330   }
331 
332   return status;
333 }
334 
335 /**
336   * @brief Return tick frequency.
337   * @retval Tick frequency.
338   *         Value of @ref HAL_TickFreqTypeDef.
339   */
HAL_GetTickFreq(void)340 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
341 {
342   return uwTickFreq;
343 }
344 
345 /**
346   * @brief  This function provides accurate delay (in milliseconds) based
347   *         on variable incremented.
348   * @note   In the default implementation , SysTick timer is the source of time base.
349   *         It is used to generate interrupts at regular time intervals where uwTick
350   *         is incremented.
351   *         The function is declared as __Weak  to be overwritten  in case of other
352   *         implementations  in user file.
353   * @param  Delay specifies the delay time length, in milliseconds.
354   * @retval None
355   */
HAL_Delay(uint32_t Delay)356 __weak void HAL_Delay(uint32_t Delay)
357 {
358   uint32_t tickstart = HAL_GetTick();
359   uint32_t wait = Delay;
360 
361   /* Add freq to guarantee minimum wait */
362   if (wait < HAL_MAX_DELAY)
363   {
364     wait += (uint32_t)(uwTickFreq);
365   }
366 
367   while((HAL_GetTick() - tickstart) < wait)
368   {
369   }
370 }
371 
372 /**
373   * @brief  Suspend Tick increment.
374   * @note   In the default implementation , SysTick timer is the source of time base. It is
375   *         used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
376   *         is called, the the SysTick interrupt will be disabled and so Tick increment
377   *         is suspended.
378   * @note This function is declared as __weak to be overwritten in case of other
379   *         implementations  in user file.
380   * @retval None
381   */
HAL_SuspendTick(void)382 __weak void HAL_SuspendTick(void)
383 
384 {
385   /* Disable SysTick Interrupt */
386   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
387 
388 }
389 
390 /**
391   * @brief  Resume Tick increment.
392   * @note   In the default implementation , SysTick timer is the source of time base. It is
393   *         used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
394   *         is called, the the SysTick interrupt will be enabled and so Tick increment
395   *         is resumed.
396   *         The function is declared as __Weak  to be overwritten  in case of other
397   *         implementations  in user file.
398   * @retval None
399   */
HAL_ResumeTick(void)400 __weak void HAL_ResumeTick(void)
401 {
402   /* Enable SysTick Interrupt */
403   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
404 
405 }
406 
407 /**
408   * @brief  This function returns the HAL revision
409   * @retval version 0xXYZR (8bits for each decimal, R for RC)
410   */
HAL_GetHalVersion(void)411 uint32_t HAL_GetHalVersion(void)
412 {
413  return __STM32F3xx_HAL_VERSION;
414 }
415 
416 /**
417   * @brief  Returns the device revision identifier.
418   * @retval Device revision identifier
419   */
HAL_GetREVID(void)420 uint32_t HAL_GetREVID(void)
421 {
422   return((DBGMCU->IDCODE) >> 16U);
423 }
424 
425 /**
426   * @brief  Returns the device identifier.
427   * @retval Device identifier
428   */
HAL_GetDEVID(void)429 uint32_t HAL_GetDEVID(void)
430 {
431   return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
432 }
433 
434 /**
435   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
436   * @retval Device identifier
437   */
HAL_GetUIDw0(void)438 uint32_t HAL_GetUIDw0(void)
439 {
440    return(READ_REG(*((uint32_t *)UID_BASE)));
441 }
442 
443 /**
444   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
445   * @retval Device identifier
446   */
HAL_GetUIDw1(void)447 uint32_t HAL_GetUIDw1(void)
448 {
449    return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
450 }
451 
452 /**
453   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
454   * @retval Device identifier
455   */
HAL_GetUIDw2(void)456 uint32_t HAL_GetUIDw2(void)
457 {
458    return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
459 }
460 
461 /**
462   * @brief  Enable the Debug Module during SLEEP mode
463   * @retval None
464   */
HAL_DBGMCU_EnableDBGSleepMode(void)465 void HAL_DBGMCU_EnableDBGSleepMode(void)
466 {
467   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
468 }
469 
470 /**
471   * @brief  Disable the Debug Module during SLEEP mode
472   * @retval None
473   */
HAL_DBGMCU_DisableDBGSleepMode(void)474 void HAL_DBGMCU_DisableDBGSleepMode(void)
475 {
476   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
477 }
478 
479 /**
480   * @brief  Enable the Debug Module during STOP mode
481   * @retval None
482   */
HAL_DBGMCU_EnableDBGStopMode(void)483 void HAL_DBGMCU_EnableDBGStopMode(void)
484 {
485   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
486 }
487 
488 /**
489   * @brief  Disable the Debug Module during STOP mode
490   * @retval None
491   */
HAL_DBGMCU_DisableDBGStopMode(void)492 void HAL_DBGMCU_DisableDBGStopMode(void)
493 {
494   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
495 }
496 
497 /**
498   * @brief  Enable the Debug Module during STANDBY mode
499   * @retval None
500   */
HAL_DBGMCU_EnableDBGStandbyMode(void)501 void HAL_DBGMCU_EnableDBGStandbyMode(void)
502 {
503   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
504 }
505 
506 /**
507   * @brief  Disable the Debug Module during STANDBY mode
508   * @retval None
509   */
HAL_DBGMCU_DisableDBGStandbyMode(void)510 void HAL_DBGMCU_DisableDBGStandbyMode(void)
511 {
512   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
513 }
514 
515 /**
516   * @}
517   */
518 
519 /**
520   * @}
521   */
522 
523 #endif /* HAL_MODULE_ENABLED */
524 /**
525   * @}
526   */
527 
528 /**
529   * @}
530   */
531 
532