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