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 ******************************************************************************
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 "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
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 (0x07U) /*!< [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 frequency.
341 * Value of @ref HAL_TickFreqTypeDef.
342 */
HAL_GetTickFreq(void)343 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
344 {
345 return uwTickFreq;
346 }
347
348 /**
349 * @brief This function provides accurate delay (in milliseconds) based
350 * on variable incremented.
351 * @note In the default implementation , SysTick timer is the source of time base.
352 * It is used to generate interrupts at regular time intervals where uwTick
353 * is incremented.
354 * @note ThiS function is declared as __weak to be overwritten in case of other
355 * implementations in user file.
356 * @param Delay specifies the delay time length, in milliseconds.
357 * @retval None
358 */
HAL_Delay(uint32_t Delay)359 __weak void HAL_Delay(uint32_t Delay)
360 {
361 uint32_t tickstart = HAL_GetTick();
362 uint32_t wait = Delay;
363
364 /* Add a freq to guarantee minimum wait */
365 if (wait < HAL_MAX_DELAY)
366 {
367 wait += (uint32_t)(uwTickFreq);
368 }
369
370 while((HAL_GetTick() - tickstart) < wait)
371 {
372 }
373 }
374
375 /**
376 * @brief Suspend Tick increment.
377 * @note In the default implementation , SysTick timer is the source of time base. It is
378 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
379 * is called, the the SysTick interrupt will be disabled and so Tick increment
380 * is suspended.
381 * @note This function is declared as __weak to be overwritten in case of other
382 * implementations in user file.
383 * @retval None
384 */
HAL_SuspendTick(void)385 __weak void HAL_SuspendTick(void)
386
387 {
388 /* Disable SysTick Interrupt */
389 CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
390 }
391
392 /**
393 * @brief Resume Tick increment.
394 * @note In the default implementation , SysTick timer is the source of time base. It is
395 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
396 * is called, the the SysTick interrupt will be enabled and so Tick increment
397 * is resumed.
398 * @note This function is declared as __weak to be overwritten in case of other
399 * implementations in user file.
400 * @retval None
401 */
HAL_ResumeTick(void)402 __weak void HAL_ResumeTick(void)
403 {
404 /* Enable SysTick Interrupt */
405 SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
406 }
407
408 /**
409 * @brief This method returns the HAL revision
410 * @retval version 0xXYZR (8bits for each decimal, R for RC)
411 */
HAL_GetHalVersion(void)412 uint32_t HAL_GetHalVersion(void)
413 {
414 return __STM32F0xx_HAL_VERSION;
415 }
416
417 /**
418 * @brief Returns the device revision identifier.
419 * @retval Device revision identifier
420 */
HAL_GetREVID(void)421 uint32_t HAL_GetREVID(void)
422 {
423 return((DBGMCU->IDCODE) >> 16U);
424 }
425
426 /**
427 * @brief Returns the device identifier.
428 * @retval Device identifier
429 */
HAL_GetDEVID(void)430 uint32_t HAL_GetDEVID(void)
431 {
432 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
433 }
434
435 /**
436 * @brief Returns first word of the unique device identifier (UID based on 96 bits)
437 * @retval Device identifier
438 */
HAL_GetUIDw0(void)439 uint32_t HAL_GetUIDw0(void)
440 {
441 return(READ_REG(*((uint32_t *)UID_BASE)));
442 }
443
444 /**
445 * @brief Returns second word of the unique device identifier (UID based on 96 bits)
446 * @retval Device identifier
447 */
HAL_GetUIDw1(void)448 uint32_t HAL_GetUIDw1(void)
449 {
450 return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
451 }
452
453 /**
454 * @brief Returns third word of the unique device identifier (UID based on 96 bits)
455 * @retval Device identifier
456 */
HAL_GetUIDw2(void)457 uint32_t HAL_GetUIDw2(void)
458 {
459 return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
460 }
461
462 /**
463 * @brief Enable the Debug Module during STOP mode
464 * @retval None
465 */
HAL_DBGMCU_EnableDBGStopMode(void)466 void HAL_DBGMCU_EnableDBGStopMode(void)
467 {
468 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
469 }
470
471 /**
472 * @brief Disable the Debug Module during STOP mode
473 * @retval None
474 */
HAL_DBGMCU_DisableDBGStopMode(void)475 void HAL_DBGMCU_DisableDBGStopMode(void)
476 {
477 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
478 }
479
480 /**
481 * @brief Enable the Debug Module during STANDBY mode
482 * @retval None
483 */
HAL_DBGMCU_EnableDBGStandbyMode(void)484 void HAL_DBGMCU_EnableDBGStandbyMode(void)
485 {
486 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
487 }
488
489 /**
490 * @brief Disable the Debug Module during STANDBY mode
491 * @retval None
492 */
HAL_DBGMCU_DisableDBGStandbyMode(void)493 void HAL_DBGMCU_DisableDBGStandbyMode(void)
494 {
495 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
496 }
497
498 /**
499 * @}
500 */
501
502 /**
503 * @}
504 */
505
506 #endif /* HAL_MODULE_ENABLED */
507 /**
508 * @}
509 */
510
511 /**
512 * @}
513 */
514
515