STM32F103xB HAL User Manual
stm32f1xx_hal.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal.c
00004   * @author  MCD Application Team
00005   * @brief   HAL module driver.
00006   *          This is the common part of the HAL initialization
00007   *
00008   @verbatim
00009   ==============================================================================
00010                      ##### How to use this driver #####
00011   ==============================================================================
00012     [..]
00013     The common HAL driver contains a set of generic and common APIs that can be
00014     used by the PPP peripheral drivers and the user to start using the HAL.
00015     [..]
00016     The HAL contains two APIs' categories:
00017          (+) Common HAL APIs
00018          (+) Services HAL APIs
00019 
00020   @endverbatim
00021   ******************************************************************************
00022   * @attention
00023   *
00024   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00025   * All rights reserved.</center></h2>
00026   *
00027   * This software component is licensed by ST under BSD 3-Clause license,
00028   * the "License"; You may not use this file except in compliance with the
00029   * License. You may obtain a copy of the License at:
00030   *                        opensource.org/licenses/BSD-3-Clause
00031   *
00032   ******************************************************************************
00033   */
00034 
00035 /* Includes ------------------------------------------------------------------*/
00036 #include "stm32f1xx_hal.h"
00037 
00038 /** @addtogroup STM32F1xx_HAL_Driver
00039   * @{
00040   */
00041 
00042 /** @defgroup HAL HAL
00043   * @brief HAL module driver.
00044   * @{
00045   */
00046 
00047 #ifdef HAL_MODULE_ENABLED
00048 
00049 /* Private typedef -----------------------------------------------------------*/
00050 /* Private define ------------------------------------------------------------*/
00051 
00052 /** @defgroup HAL_Private_Constants HAL Private Constants
00053   * @{
00054   */
00055 /**
00056  * @brief STM32F1xx HAL Driver version number V1.1.7
00057    */
00058 #define __STM32F1xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
00059 #define __STM32F1xx_HAL_VERSION_SUB1   (0x01U) /*!< [23:16] sub1 version */
00060 #define __STM32F1xx_HAL_VERSION_SUB2   (0x07U) /*!< [15:8]  sub2 version */
00061 #define __STM32F1xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
00062 #define __STM32F1xx_HAL_VERSION         ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
00063                                         |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
00064                                         |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
00065                                         |(__STM32F1xx_HAL_VERSION_RC))
00066 
00067 #define IDCODE_DEVID_MASK    0x00000FFFU
00068 
00069 /**
00070   * @}
00071   */
00072 
00073 /* Private macro -------------------------------------------------------------*/
00074 /* Private variables ---------------------------------------------------------*/
00075 
00076 /** @defgroup HAL_Private_Variables HAL Private Variables
00077   * @{
00078   */
00079 __IO uint32_t uwTick;
00080 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
00081 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
00082 /**
00083   * @}
00084   */
00085 /* Private function prototypes -----------------------------------------------*/
00086 /* Exported functions ---------------------------------------------------------*/
00087 
00088 /** @defgroup HAL_Exported_Functions HAL Exported Functions
00089   * @{
00090   */
00091 
00092 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
00093  *  @brief    Initialization and de-initialization functions
00094  *
00095 @verbatim
00096  ===============================================================================
00097               ##### Initialization and de-initialization functions #####
00098  ===============================================================================
00099    [..]  This section provides functions allowing to:
00100       (+) Initializes the Flash interface, the NVIC allocation and initial clock
00101           configuration. It initializes the systick also when timeout is needed
00102           and the backup domain when enabled.
00103       (+) de-Initializes common part of the HAL.
00104       (+) Configure The time base source to have 1ms time base with a dedicated
00105           Tick interrupt priority.
00106         (++) SysTick timer is used by default as source of time base, but user
00107              can eventually implement his proper time base source (a general purpose
00108              timer for example or other time source), keeping in mind that Time base
00109              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
00110              handled in milliseconds basis.
00111         (++) Time base configuration function (HAL_InitTick ()) is called automatically
00112              at the beginning of the program after reset by HAL_Init() or at any time
00113              when clock is configured, by HAL_RCC_ClockConfig().
00114         (++) Source of time base is configured  to generate interrupts at regular
00115              time intervals. Care must be taken if HAL_Delay() is called from a
00116              peripheral ISR process, the Tick interrupt line must have higher priority
00117             (numerically lower) than the peripheral interrupt. Otherwise the caller
00118             ISR process will be blocked.
00119        (++) functions affecting time base configurations are declared as __weak
00120              to make  override possible  in case of other  implementations in user file.
00121 @endverbatim
00122   * @{
00123   */
00124 
00125 /**
00126   * @brief  This function is used to initialize the HAL Library; it must be the first
00127   *         instruction to be executed in the main program (before to call any other
00128   *         HAL function), it performs the following:
00129   *           Configure the Flash prefetch.
00130   *           Configures the SysTick to generate an interrupt each 1 millisecond,
00131   *           which is clocked by the HSI (at this stage, the clock is not yet
00132   *           configured and thus the system is running from the internal HSI at 16 MHz).
00133   *           Set NVIC Group Priority to 4.
00134   *           Calls the HAL_MspInit() callback function defined in user file
00135   *           "stm32f1xx_hal_msp.c" to do the global low level hardware initialization
00136   *
00137   * @note   SysTick is used as time base for the HAL_Delay() function, the application
00138   *         need to ensure that the SysTick time base is always set to 1 millisecond
00139   *         to have correct HAL operation.
00140   * @retval HAL status
00141   */
00142 HAL_StatusTypeDef HAL_Init(void)
00143 {
00144   /* Configure Flash prefetch */
00145 #if (PREFETCH_ENABLE != 0)
00146 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
00147     defined(STM32F102x6) || defined(STM32F102xB) || \
00148     defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
00149     defined(STM32F105xC) || defined(STM32F107xC)
00150 
00151   /* Prefetch buffer is not available on value line devices */
00152   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
00153 #endif
00154 #endif /* PREFETCH_ENABLE */
00155 
00156   /* Set Interrupt Group Priority */
00157   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
00158 
00159   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
00160   HAL_InitTick(TICK_INT_PRIORITY);
00161 
00162   /* Init the low level hardware */
00163   HAL_MspInit();
00164 
00165   /* Return function status */
00166   return HAL_OK;
00167 }
00168 
00169 /**
00170   * @brief This function de-Initializes common part of the HAL and stops the systick.
00171   *        of time base.
00172   * @note This function is optional.
00173   * @retval HAL status
00174   */
00175 HAL_StatusTypeDef HAL_DeInit(void)
00176 {
00177   /* Reset of all peripherals */
00178   __HAL_RCC_APB1_FORCE_RESET();
00179   __HAL_RCC_APB1_RELEASE_RESET();
00180 
00181   __HAL_RCC_APB2_FORCE_RESET();
00182   __HAL_RCC_APB2_RELEASE_RESET();
00183 
00184 #if defined(STM32F105xC) || defined(STM32F107xC)
00185   __HAL_RCC_AHB_FORCE_RESET();
00186   __HAL_RCC_AHB_RELEASE_RESET();
00187 #endif
00188 
00189   /* De-Init the low level hardware */
00190   HAL_MspDeInit();
00191 
00192   /* Return function status */
00193   return HAL_OK;
00194 }
00195 
00196 /**
00197   * @brief  Initialize the MSP.
00198   * @retval None
00199   */
00200 __weak void HAL_MspInit(void)
00201 {
00202   /* NOTE : This function should not be modified, when the callback is needed,
00203             the HAL_MspInit could be implemented in the user file
00204    */
00205 }
00206 
00207 /**
00208   * @brief  DeInitializes the MSP.
00209   * @retval None
00210   */
00211 __weak void HAL_MspDeInit(void)
00212 {
00213   /* NOTE : This function should not be modified, when the callback is needed,
00214             the HAL_MspDeInit could be implemented in the user file
00215    */
00216 }
00217 
00218 /**
00219   * @brief This function configures the source of the time base.
00220   *        The time source is configured  to have 1ms time base with a dedicated
00221   *        Tick interrupt priority.
00222   * @note This function is called  automatically at the beginning of program after
00223   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
00224   * @note In the default implementation, SysTick timer is the source of time base.
00225   *       It is used to generate interrupts at regular time intervals.
00226   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
00227   *       The SysTick interrupt must have higher priority (numerically lower)
00228   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
00229   *       The function is declared as __weak  to be overwritten  in case of other
00230   *       implementation  in user file.
00231   * @param TickPriority Tick interrupt priority.
00232   * @retval HAL status
00233   */
00234 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
00235 {
00236   /* Configure the SysTick to have interrupt in 1ms time basis*/
00237   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
00238   {
00239     return HAL_ERROR;
00240   }
00241 
00242   /* Configure the SysTick IRQ priority */
00243   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
00244   {
00245     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
00246     uwTickPrio = TickPriority;
00247   }
00248   else
00249   {
00250     return HAL_ERROR;
00251   }
00252 
00253   /* Return function status */
00254   return HAL_OK;
00255 }
00256 
00257 /**
00258   * @}
00259   */
00260 
00261 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
00262   *  @brief    HAL Control functions
00263   *
00264 @verbatim
00265  ===============================================================================
00266                       ##### HAL Control functions #####
00267  ===============================================================================
00268     [..]  This section provides functions allowing to:
00269       (+) Provide a tick value in millisecond
00270       (+) Provide a blocking delay in millisecond
00271       (+) Suspend the time base source interrupt
00272       (+) Resume the time base source interrupt
00273       (+) Get the HAL API driver version
00274       (+) Get the device identifier
00275       (+) Get the device revision identifier
00276       (+) Enable/Disable Debug module during SLEEP mode
00277       (+) Enable/Disable Debug module during STOP mode
00278       (+) Enable/Disable Debug module during STANDBY mode
00279 
00280 @endverbatim
00281   * @{
00282   */
00283 
00284 /**
00285   * @brief This function is called to increment  a global variable "uwTick"
00286   *        used as application time base.
00287   * @note In the default implementation, this variable is incremented each 1ms
00288   *       in SysTick ISR.
00289   * @note This function is declared as __weak to be overwritten in case of other
00290   *      implementations in user file.
00291   * @retval None
00292   */
00293 __weak void HAL_IncTick(void)
00294 {
00295   uwTick += uwTickFreq;
00296 }
00297 
00298 /**
00299   * @brief Provides a tick value in millisecond.
00300   * @note  This function is declared as __weak to be overwritten in case of other
00301   *       implementations in user file.
00302   * @retval tick value
00303   */
00304 __weak uint32_t HAL_GetTick(void)
00305 {
00306   return uwTick;
00307 }
00308 
00309 /**
00310   * @brief This function returns a tick priority.
00311   * @retval tick priority
00312   */
00313 uint32_t HAL_GetTickPrio(void)
00314 {
00315   return uwTickPrio;
00316 }
00317 
00318 /**
00319   * @brief Set new tick Freq.
00320   * @retval status
00321   */
00322 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
00323 {
00324   HAL_StatusTypeDef status  = HAL_OK;
00325   HAL_TickFreqTypeDef prevTickFreq;
00326 
00327   assert_param(IS_TICKFREQ(Freq));
00328 
00329   if (uwTickFreq != Freq)
00330   {
00331     /* Back up uwTickFreq frequency */
00332     prevTickFreq = uwTickFreq;
00333 
00334     /* Update uwTickFreq global variable used by HAL_InitTick() */
00335     uwTickFreq = Freq;
00336 
00337     /* Apply the new tick Freq  */
00338     status = HAL_InitTick(uwTickPrio);
00339 
00340     if (status != HAL_OK)
00341     {
00342       /* Restore previous tick frequency */
00343       uwTickFreq = prevTickFreq;
00344     }
00345   }
00346 
00347   return status;
00348 }
00349 
00350 /**
00351   * @brief Return tick frequency.
00352   * @retval tick period in Hz
00353   */
00354 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
00355 {
00356   return uwTickFreq;
00357 }
00358 
00359 /**
00360   * @brief This function provides minimum delay (in milliseconds) based
00361   *        on variable incremented.
00362   * @note In the default implementation , SysTick timer is the source of time base.
00363   *       It is used to generate interrupts at regular time intervals where uwTick
00364   *       is incremented.
00365   * @note This function is declared as __weak to be overwritten in case of other
00366   *       implementations in user file.
00367   * @param Delay specifies the delay time length, in milliseconds.
00368   * @retval None
00369   */
00370 __weak void HAL_Delay(uint32_t Delay)
00371 {
00372   uint32_t tickstart = HAL_GetTick();
00373   uint32_t wait = Delay;
00374 
00375   /* Add a freq to guarantee minimum wait */
00376   if (wait < HAL_MAX_DELAY)
00377   {
00378     wait += (uint32_t)(uwTickFreq);
00379   }
00380 
00381   while ((HAL_GetTick() - tickstart) < wait)
00382   {
00383   }
00384 }
00385 
00386 /**
00387   * @brief Suspend Tick increment.
00388   * @note In the default implementation , SysTick timer is the source of time base. It is
00389   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
00390   *       is called, the SysTick interrupt will be disabled and so Tick increment
00391   *       is suspended.
00392   * @note This function is declared as __weak to be overwritten in case of other
00393   *       implementations in user file.
00394   * @retval None
00395   */
00396 __weak void HAL_SuspendTick(void)
00397 {
00398   /* Disable SysTick Interrupt */
00399   CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
00400 }
00401 
00402 /**
00403   * @brief Resume Tick increment.
00404   * @note In the default implementation , SysTick timer is the source of time base. It is
00405   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
00406   *       is called, the SysTick interrupt will be enabled and so Tick increment
00407   *       is resumed.
00408   * @note This function is declared as __weak to be overwritten in case of other
00409   *       implementations in user file.
00410   * @retval None
00411   */
00412 __weak void HAL_ResumeTick(void)
00413 {
00414   /* Enable SysTick Interrupt */
00415   SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
00416 }
00417 
00418 /**
00419   * @brief  Returns the HAL revision
00420   * @retval version 0xXYZR (8bits for each decimal, R for RC)
00421   */
00422 uint32_t HAL_GetHalVersion(void)
00423 {
00424   return __STM32F1xx_HAL_VERSION;
00425 }
00426 
00427 /**
00428   * @brief Returns the device revision identifier.
00429   * Note: On devices STM32F10xx8 and STM32F10xxB,
00430   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00431   *                  STM32F101xF/G and STM32F103xF/G
00432   *                  STM32F10xx4 and STM32F10xx6
00433   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00434   *       debug mode (not accessible by the user software in normal mode).
00435   *       Refer to errata sheet of these devices for more details.
00436   * @retval Device revision identifier
00437   */
00438 uint32_t HAL_GetREVID(void)
00439 {
00440   return ((DBGMCU->IDCODE) >> DBGMCU_IDCODE_REV_ID_Pos);
00441 }
00442 
00443 /**
00444   * @brief  Returns the device identifier.
00445   * Note: On devices STM32F10xx8 and STM32F10xxB,
00446   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00447   *                  STM32F101xF/G and STM32F103xF/G
00448   *                  STM32F10xx4 and STM32F10xx6
00449   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00450   *       debug mode (not accessible by the user software in normal mode).
00451   *       Refer to errata sheet of these devices for more details.
00452   * @retval Device identifier
00453   */
00454 uint32_t HAL_GetDEVID(void)
00455 {
00456   return ((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
00457 }
00458 
00459 /**
00460   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
00461   * @retval Device identifier
00462   */
00463 uint32_t HAL_GetUIDw0(void)
00464 {
00465    return(READ_REG(*((uint32_t *)UID_BASE)));
00466 }
00467 
00468 /**
00469   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
00470   * @retval Device identifier
00471   */
00472 uint32_t HAL_GetUIDw1(void)
00473 {
00474    return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
00475 }
00476 
00477 /**
00478   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
00479   * @retval Device identifier
00480   */
00481 uint32_t HAL_GetUIDw2(void)
00482 {
00483    return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
00484 }
00485 
00486 /**
00487   * @brief  Enable the Debug Module during SLEEP mode
00488   * @retval None
00489   */
00490 void HAL_DBGMCU_EnableDBGSleepMode(void)
00491 {
00492   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00493 }
00494 
00495 /**
00496   * @brief  Disable the Debug Module during SLEEP mode
00497   * Note: On devices STM32F10xx8 and STM32F10xxB,
00498   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00499   *                  STM32F101xF/G and STM32F103xF/G
00500   *                  STM32F10xx4 and STM32F10xx6
00501   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00502   *       debug mode (not accessible by the user software in normal mode).
00503   *       Refer to errata sheet of these devices for more details.
00504   * @retval None
00505   */
00506 void HAL_DBGMCU_DisableDBGSleepMode(void)
00507 {
00508   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00509 }
00510 
00511 /**
00512   * @brief  Enable the Debug Module during STOP mode
00513   * Note: On devices STM32F10xx8 and STM32F10xxB,
00514   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00515   *                  STM32F101xF/G and STM32F103xF/G
00516   *                  STM32F10xx4 and STM32F10xx6
00517   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00518   *       debug mode (not accessible by the user software in normal mode).
00519   *       Refer to errata sheet of these devices for more details.
00520   * Note: On all STM32F1 devices:
00521   *       If the system tick timer interrupt is enabled during the Stop mode
00522   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
00523   *       the system from Stop mode.
00524   *       Workaround: To debug the Stop mode, disable the system tick timer
00525   *       interrupt.
00526   *       Refer to errata sheet of these devices for more details.
00527   * Note: On all STM32F1 devices:
00528   *       If the system tick timer interrupt is enabled during the Stop mode
00529   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
00530   *       the system from Stop mode.
00531   *       Workaround: To debug the Stop mode, disable the system tick timer
00532   *       interrupt.
00533   *       Refer to errata sheet of these devices for more details.
00534   * @retval None
00535   */
00536 void HAL_DBGMCU_EnableDBGStopMode(void)
00537 {
00538   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00539 }
00540 
00541 /**
00542   * @brief  Disable the Debug Module during STOP mode
00543   * Note: On devices STM32F10xx8 and STM32F10xxB,
00544   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00545   *                  STM32F101xF/G and STM32F103xF/G
00546   *                  STM32F10xx4 and STM32F10xx6
00547   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00548   *       debug mode (not accessible by the user software in normal mode).
00549   *       Refer to errata sheet of these devices for more details.
00550   * @retval None
00551   */
00552 void HAL_DBGMCU_DisableDBGStopMode(void)
00553 {
00554   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00555 }
00556 
00557 /**
00558   * @brief  Enable the Debug Module during STANDBY mode
00559   * Note: On devices STM32F10xx8 and STM32F10xxB,
00560   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00561   *                  STM32F101xF/G and STM32F103xF/G
00562   *                  STM32F10xx4 and STM32F10xx6
00563   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00564   *       debug mode (not accessible by the user software in normal mode).
00565   *       Refer to errata sheet of these devices for more details.
00566   * @retval None
00567   */
00568 void HAL_DBGMCU_EnableDBGStandbyMode(void)
00569 {
00570   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00571 }
00572 
00573 /**
00574   * @brief  Disable the Debug Module during STANDBY mode
00575   * Note: On devices STM32F10xx8 and STM32F10xxB,
00576   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00577   *                  STM32F101xF/G and STM32F103xF/G
00578   *                  STM32F10xx4 and STM32F10xx6
00579   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
00580   *       debug mode (not accessible by the user software in normal mode).
00581   *       Refer to errata sheet of these devices for more details.
00582   * @retval None
00583   */
00584 void HAL_DBGMCU_DisableDBGStandbyMode(void)
00585 {
00586   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00587 }
00588 
00589 /**
00590   * @}
00591   */
00592 
00593 /**
00594   * @}
00595   */
00596 
00597 #endif /* HAL_MODULE_ENABLED */
00598 /**
00599   * @}
00600   */
00601 
00602 /**
00603   * @}
00604   */
00605 
00606 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/