1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_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) 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 "stm32f1xx_hal.h"
37 
38 /** @addtogroup STM32F1xx_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 
52 /** @defgroup HAL_Private_Constants HAL Private Constants
53   * @{
54   */
55 /**
56  * @brief STM32F1xx HAL Driver version number V1.1.8
57    */
58 #define __STM32F1xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
59 #define __STM32F1xx_HAL_VERSION_SUB1   (0x01U) /*!< [23:16] sub1 version */
60 #define __STM32F1xx_HAL_VERSION_SUB2   (0x08U) /*!< [15:8]  sub2 version */
61 #define __STM32F1xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
62 #define __STM32F1xx_HAL_VERSION         ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
63                                         |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
64                                         |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
65                                         |(__STM32F1xx_HAL_VERSION_RC))
66 
67 #define IDCODE_DEVID_MASK    0x00000FFFU
68 
69 /**
70   * @}
71   */
72 
73 /* Private macro -------------------------------------------------------------*/
74 /* Private variables ---------------------------------------------------------*/
75 
76 /** @defgroup HAL_Private_Variables HAL Private Variables
77   * @{
78   */
79 __IO uint32_t uwTick;
80 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
81 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
82 /**
83   * @}
84   */
85 /* Private function prototypes -----------------------------------------------*/
86 /* Exported functions ---------------------------------------------------------*/
87 
88 /** @defgroup HAL_Exported_Functions HAL Exported Functions
89   * @{
90   */
91 
92 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
93  *  @brief    Initialization and de-initialization functions
94  *
95 @verbatim
96  ===============================================================================
97               ##### Initialization and de-initialization functions #####
98  ===============================================================================
99    [..]  This section provides functions allowing to:
100       (+) Initializes the Flash interface, the NVIC allocation and initial clock
101           configuration. It initializes the systick also when timeout is needed
102           and the backup domain when enabled.
103       (+) de-Initializes common part of the HAL.
104       (+) Configure The time base source to have 1ms time base with a dedicated
105           Tick interrupt priority.
106         (++) SysTick timer is used by default as source of time base, but user
107              can eventually implement his proper time base source (a general purpose
108              timer for example or other time source), keeping in mind that Time base
109              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
110              handled in milliseconds basis.
111         (++) Time base configuration function (HAL_InitTick ()) is called automatically
112              at the beginning of the program after reset by HAL_Init() or at any time
113              when clock is configured, by HAL_RCC_ClockConfig().
114         (++) Source of time base is configured  to generate interrupts at regular
115              time intervals. Care must be taken if HAL_Delay() is called from a
116              peripheral ISR process, the Tick interrupt line must have higher priority
117             (numerically lower) than the peripheral interrupt. Otherwise the caller
118             ISR process will be blocked.
119        (++) functions affecting time base configurations are declared as __weak
120              to make  override possible  in case of other  implementations in user file.
121 @endverbatim
122   * @{
123   */
124 
125 /**
126   * @brief  This function is used to initialize the HAL Library; it must be the first
127   *         instruction to be executed in the main program (before to call any other
128   *         HAL function), it performs the following:
129   *           Configure the Flash prefetch.
130   *           Configures the SysTick to generate an interrupt each 1 millisecond,
131   *           which is clocked by the HSI (at this stage, the clock is not yet
132   *           configured and thus the system is running from the internal HSI at 16 MHz).
133   *           Set NVIC Group Priority to 4.
134   *           Calls the HAL_MspInit() callback function defined in user file
135   *           "stm32f1xx_hal_msp.c" to do the global low level hardware initialization
136   *
137   * @note   SysTick is used as time base for the HAL_Delay() function, the application
138   *         need to ensure that the SysTick time base is always set to 1 millisecond
139   *         to have correct HAL operation.
140   * @retval HAL status
141   */
HAL_Init(void)142 HAL_StatusTypeDef HAL_Init(void)
143 {
144   /* Configure Flash prefetch */
145 #if (PREFETCH_ENABLE != 0)
146 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
147     defined(STM32F102x6) || defined(STM32F102xB) || \
148     defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
149     defined(STM32F105xC) || defined(STM32F107xC)
150 
151   /* Prefetch buffer is not available on value line devices */
152   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
153 #endif
154 #endif /* PREFETCH_ENABLE */
155 
156   /* Set Interrupt Group Priority */
157   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
158 
159   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
160   HAL_InitTick(TICK_INT_PRIORITY);
161 
162   /* Init the low level hardware */
163   HAL_MspInit();
164 
165   /* Return function status */
166   return HAL_OK;
167 }
168 
169 /**
170   * @brief This function de-Initializes common part of the HAL and stops the systick.
171   *        of time base.
172   * @note This function is optional.
173   * @retval HAL status
174   */
HAL_DeInit(void)175 HAL_StatusTypeDef HAL_DeInit(void)
176 {
177   /* Reset of all peripherals */
178   __HAL_RCC_APB1_FORCE_RESET();
179   __HAL_RCC_APB1_RELEASE_RESET();
180 
181   __HAL_RCC_APB2_FORCE_RESET();
182   __HAL_RCC_APB2_RELEASE_RESET();
183 
184 #if defined(STM32F105xC) || defined(STM32F107xC)
185   __HAL_RCC_AHB_FORCE_RESET();
186   __HAL_RCC_AHB_RELEASE_RESET();
187 #endif
188 
189   /* De-Init the low level hardware */
190   HAL_MspDeInit();
191 
192   /* Return function status */
193   return HAL_OK;
194 }
195 
196 /**
197   * @brief  Initialize the MSP.
198   * @retval None
199   */
HAL_MspInit(void)200 __weak void HAL_MspInit(void)
201 {
202   /* NOTE : This function should not be modified, when the callback is needed,
203             the HAL_MspInit could be implemented in the user file
204    */
205 }
206 
207 /**
208   * @brief  DeInitializes the MSP.
209   * @retval None
210   */
HAL_MspDeInit(void)211 __weak void HAL_MspDeInit(void)
212 {
213   /* NOTE : This function should not be modified, when the callback is needed,
214             the HAL_MspDeInit could be implemented in the user file
215    */
216 }
217 
218 /**
219   * @brief This function configures the source of the time base.
220   *        The time source is configured  to have 1ms time base with a dedicated
221   *        Tick interrupt priority.
222   * @note This function is called  automatically at the beginning of program after
223   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
224   * @note In the default implementation, SysTick timer is the source of time base.
225   *       It is used to generate interrupts at regular time intervals.
226   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
227   *       The SysTick interrupt must have higher priority (numerically lower)
228   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
229   *       The function is declared as __weak  to be overwritten  in case of other
230   *       implementation  in user file.
231   * @param TickPriority Tick interrupt priority.
232   * @retval HAL status
233   */
HAL_InitTick(uint32_t TickPriority)234 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
235 {
236   /* Configure the SysTick to have interrupt in 1ms time basis*/
237   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
238   {
239     return HAL_ERROR;
240   }
241 
242   /* Configure the SysTick IRQ priority */
243   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
244   {
245     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
246     uwTickPrio = TickPriority;
247   }
248   else
249   {
250     return HAL_ERROR;
251   }
252 
253   /* Return function status */
254   return HAL_OK;
255 }
256 
257 /**
258   * @}
259   */
260 
261 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
262   *  @brief    HAL Control functions
263   *
264 @verbatim
265  ===============================================================================
266                       ##### HAL Control functions #####
267  ===============================================================================
268     [..]  This section provides functions allowing to:
269       (+) Provide a tick value in millisecond
270       (+) Provide a blocking delay in millisecond
271       (+) Suspend the time base source interrupt
272       (+) Resume the time base source interrupt
273       (+) Get the HAL API driver version
274       (+) Get the device identifier
275       (+) Get the device revision identifier
276       (+) Enable/Disable Debug module during SLEEP mode
277       (+) Enable/Disable Debug module during STOP mode
278       (+) Enable/Disable Debug module during STANDBY mode
279 
280 @endverbatim
281   * @{
282   */
283 
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   */
HAL_IncTick(void)293 __weak void HAL_IncTick(void)
294 {
295   uwTick += uwTickFreq;
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   */
HAL_GetTick(void)304 __weak uint32_t HAL_GetTick(void)
305 {
306   return uwTick;
307 }
308 
309 /**
310   * @brief This function returns a tick priority.
311   * @retval tick priority
312   */
HAL_GetTickPrio(void)313 uint32_t HAL_GetTickPrio(void)
314 {
315   return uwTickPrio;
316 }
317 
318 /**
319   * @brief Set new tick Freq.
320   * @retval status
321   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)322 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
323 {
324   HAL_StatusTypeDef status  = HAL_OK;
325   HAL_TickFreqTypeDef prevTickFreq;
326 
327   assert_param(IS_TICKFREQ(Freq));
328 
329   if (uwTickFreq != Freq)
330   {
331     /* Back up uwTickFreq frequency */
332     prevTickFreq = uwTickFreq;
333 
334     /* Update uwTickFreq global variable used by HAL_InitTick() */
335     uwTickFreq = Freq;
336 
337     /* Apply the new tick Freq  */
338     status = HAL_InitTick(uwTickPrio);
339 
340     if (status != HAL_OK)
341     {
342       /* Restore previous tick frequency */
343       uwTickFreq = prevTickFreq;
344     }
345   }
346 
347   return status;
348 }
349 
350 /**
351   * @brief Return tick frequency.
352   * @retval tick period in Hz
353   */
HAL_GetTickFreq(void)354 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
355 {
356   return uwTickFreq;
357 }
358 
359 /**
360   * @brief This function provides minimum delay (in milliseconds) based
361   *        on variable incremented.
362   * @note In the default implementation , SysTick timer is the source of time base.
363   *       It is used to generate interrupts at regular time intervals where uwTick
364   *       is incremented.
365   * @note This function is declared as __weak to be overwritten in case of other
366   *       implementations in user file.
367   * @param Delay specifies the delay time length, in milliseconds.
368   * @retval None
369   */
HAL_Delay(uint32_t Delay)370 __weak void HAL_Delay(uint32_t Delay)
371 {
372   uint32_t tickstart = HAL_GetTick();
373   uint32_t wait = Delay;
374 
375   /* Add a freq to guarantee minimum wait */
376   if (wait < HAL_MAX_DELAY)
377   {
378     wait += (uint32_t)(uwTickFreq);
379   }
380 
381   while ((HAL_GetTick() - tickstart) < wait)
382   {
383   }
384 }
385 
386 /**
387   * @brief Suspend Tick increment.
388   * @note In the default implementation , SysTick timer is the source of time base. It is
389   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
390   *       is called, the SysTick interrupt will be disabled and so Tick increment
391   *       is suspended.
392   * @note This function is declared as __weak to be overwritten in case of other
393   *       implementations in user file.
394   * @retval None
395   */
HAL_SuspendTick(void)396 __weak void HAL_SuspendTick(void)
397 {
398   /* Disable SysTick Interrupt */
399   CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
400 }
401 
402 /**
403   * @brief Resume Tick increment.
404   * @note In the default implementation , SysTick timer is the source of time base. It is
405   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
406   *       is called, the SysTick interrupt will be enabled and so Tick increment
407   *       is resumed.
408   * @note This function is declared as __weak to be overwritten in case of other
409   *       implementations in user file.
410   * @retval None
411   */
HAL_ResumeTick(void)412 __weak void HAL_ResumeTick(void)
413 {
414   /* Enable SysTick Interrupt */
415   SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
416 }
417 
418 /**
419   * @brief  Returns the HAL revision
420   * @retval version 0xXYZR (8bits for each decimal, R for RC)
421   */
HAL_GetHalVersion(void)422 uint32_t HAL_GetHalVersion(void)
423 {
424   return __STM32F1xx_HAL_VERSION;
425 }
426 
427 /**
428   * @brief Returns the device revision identifier.
429   * Note: On devices STM32F10xx8 and STM32F10xxB,
430   *                  STM32F101xC/D/E and STM32F103xC/D/E,
431   *                  STM32F101xF/G and STM32F103xF/G
432   *                  STM32F10xx4 and STM32F10xx6
433   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
434   *       debug mode (not accessible by the user software in normal mode).
435   *       Refer to errata sheet of these devices for more details.
436   * @retval Device revision identifier
437   */
HAL_GetREVID(void)438 uint32_t HAL_GetREVID(void)
439 {
440   return ((DBGMCU->IDCODE) >> DBGMCU_IDCODE_REV_ID_Pos);
441 }
442 
443 /**
444   * @brief  Returns the device identifier.
445   * Note: On devices STM32F10xx8 and STM32F10xxB,
446   *                  STM32F101xC/D/E and STM32F103xC/D/E,
447   *                  STM32F101xF/G and STM32F103xF/G
448   *                  STM32F10xx4 and STM32F10xx6
449   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
450   *       debug mode (not accessible by the user software in normal mode).
451   *       Refer to errata sheet of these devices for more details.
452   * @retval Device identifier
453   */
HAL_GetDEVID(void)454 uint32_t HAL_GetDEVID(void)
455 {
456   return ((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
457 }
458 
459 /**
460   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
461   * @retval Device identifier
462   */
HAL_GetUIDw0(void)463 uint32_t HAL_GetUIDw0(void)
464 {
465    return(READ_REG(*((uint32_t *)UID_BASE)));
466 }
467 
468 /**
469   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
470   * @retval Device identifier
471   */
HAL_GetUIDw1(void)472 uint32_t HAL_GetUIDw1(void)
473 {
474    return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
475 }
476 
477 /**
478   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
479   * @retval Device identifier
480   */
HAL_GetUIDw2(void)481 uint32_t HAL_GetUIDw2(void)
482 {
483    return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
484 }
485 
486 /**
487   * @brief  Enable the Debug Module during SLEEP mode
488   * @retval None
489   */
HAL_DBGMCU_EnableDBGSleepMode(void)490 void HAL_DBGMCU_EnableDBGSleepMode(void)
491 {
492   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
493 }
494 
495 /**
496   * @brief  Disable the Debug Module during SLEEP mode
497   * Note: On devices STM32F10xx8 and STM32F10xxB,
498   *                  STM32F101xC/D/E and STM32F103xC/D/E,
499   *                  STM32F101xF/G and STM32F103xF/G
500   *                  STM32F10xx4 and STM32F10xx6
501   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
502   *       debug mode (not accessible by the user software in normal mode).
503   *       Refer to errata sheet of these devices for more details.
504   * @retval None
505   */
HAL_DBGMCU_DisableDBGSleepMode(void)506 void HAL_DBGMCU_DisableDBGSleepMode(void)
507 {
508   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
509 }
510 
511 /**
512   * @brief  Enable the Debug Module during STOP mode
513   * Note: On devices STM32F10xx8 and STM32F10xxB,
514   *                  STM32F101xC/D/E and STM32F103xC/D/E,
515   *                  STM32F101xF/G and STM32F103xF/G
516   *                  STM32F10xx4 and STM32F10xx6
517   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
518   *       debug mode (not accessible by the user software in normal mode).
519   *       Refer to errata sheet of these devices for more details.
520   * Note: On all STM32F1 devices:
521   *       If the system tick timer interrupt is enabled during the Stop mode
522   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
523   *       the system from Stop mode.
524   *       Workaround: To debug the Stop mode, disable the system tick timer
525   *       interrupt.
526   *       Refer to errata sheet of these devices for more details.
527   * Note: On all STM32F1 devices:
528   *       If the system tick timer interrupt is enabled during the Stop mode
529   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
530   *       the system from Stop mode.
531   *       Workaround: To debug the Stop mode, disable the system tick timer
532   *       interrupt.
533   *       Refer to errata sheet of these devices for more details.
534   * @retval None
535   */
HAL_DBGMCU_EnableDBGStopMode(void)536 void HAL_DBGMCU_EnableDBGStopMode(void)
537 {
538   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
539 }
540 
541 /**
542   * @brief  Disable the Debug Module during STOP mode
543   * Note: On devices STM32F10xx8 and STM32F10xxB,
544   *                  STM32F101xC/D/E and STM32F103xC/D/E,
545   *                  STM32F101xF/G and STM32F103xF/G
546   *                  STM32F10xx4 and STM32F10xx6
547   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
548   *       debug mode (not accessible by the user software in normal mode).
549   *       Refer to errata sheet of these devices for more details.
550   * @retval None
551   */
HAL_DBGMCU_DisableDBGStopMode(void)552 void HAL_DBGMCU_DisableDBGStopMode(void)
553 {
554   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
555 }
556 
557 /**
558   * @brief  Enable the Debug Module during STANDBY mode
559   * Note: On devices STM32F10xx8 and STM32F10xxB,
560   *                  STM32F101xC/D/E and STM32F103xC/D/E,
561   *                  STM32F101xF/G and STM32F103xF/G
562   *                  STM32F10xx4 and STM32F10xx6
563   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
564   *       debug mode (not accessible by the user software in normal mode).
565   *       Refer to errata sheet of these devices for more details.
566   * @retval None
567   */
HAL_DBGMCU_EnableDBGStandbyMode(void)568 void HAL_DBGMCU_EnableDBGStandbyMode(void)
569 {
570   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
571 }
572 
573 /**
574   * @brief  Disable the Debug Module during STANDBY mode
575   * Note: On devices STM32F10xx8 and STM32F10xxB,
576   *                  STM32F101xC/D/E and STM32F103xC/D/E,
577   *                  STM32F101xF/G and STM32F103xF/G
578   *                  STM32F10xx4 and STM32F10xx6
579   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
580   *       debug mode (not accessible by the user software in normal mode).
581   *       Refer to errata sheet of these devices for more details.
582   * @retval None
583   */
HAL_DBGMCU_DisableDBGStandbyMode(void)584 void HAL_DBGMCU_DisableDBGStandbyMode(void)
585 {
586   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
587 }
588 
589 /**
590   * @}
591   */
592 
593 /**
594   * @}
595   */
596 
597 #endif /* HAL_MODULE_ENABLED */
598 /**
599   * @}
600   */
601 
602 /**
603   * @}
604   */
605 
606 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
607