Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f1xx_hal.c Source File

stm32f1xx_hal.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal.c
00004   * @author  MCD Application Team
00005   * @version V1.0.4
00006   * @date    29-April-2016
00007   * @brief   HAL module driver.
00008   *          This is the common part of the HAL initialization
00009   *
00010   @verbatim
00011   ==============================================================================
00012                      ##### How to use this driver #####
00013   ==============================================================================
00014     [..]
00015     The common HAL driver contains a set of generic and common APIs that can be
00016     used by the PPP peripheral drivers and the user to start using the HAL. 
00017     [..]
00018     The HAL contains two APIs' categories: 
00019          (+) Common HAL APIs
00020          (+) Services HAL APIs
00021 
00022   @endverbatim
00023   ******************************************************************************
00024   * @attention
00025   *
00026   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00027   *
00028   * Redistribution and use in source and binary forms, with or without modification,
00029   * are permitted provided that the following conditions are met:
00030   *   1. Redistributions of source code must retain the above copyright notice,
00031   *      this list of conditions and the following disclaimer.
00032   *   2. Redistributions in binary form must reproduce the above copyright notice,
00033   *      this list of conditions and the following disclaimer in the documentation
00034   *      and/or other materials provided with the distribution.
00035   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00036   *      may be used to endorse or promote products derived from this software
00037   *      without specific prior written permission.
00038   *
00039   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00040   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00041   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00042   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00043   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00044   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00045   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00046   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00047   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00048   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049   *
00050   ******************************************************************************
00051   */
00052 
00053 /* Includes ------------------------------------------------------------------*/
00054 #include "stm32f1xx_hal.h"
00055 
00056 /** @addtogroup STM32F1xx_HAL_Driver
00057   * @{
00058   */
00059 
00060 /** @defgroup HAL HAL
00061   * @brief HAL module driver.
00062   * @{
00063   */
00064 
00065 #ifdef HAL_MODULE_ENABLED
00066 
00067 /* Private typedef -----------------------------------------------------------*/
00068 /* Private define ------------------------------------------------------------*/
00069 
00070 /** @defgroup HAL_Private_Constants HAL Private Constants
00071   * @{
00072   */
00073 
00074 /**
00075  * @brief STM32F1xx HAL Driver version number
00076    */
00077 #define __STM32F1xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00078 #define __STM32F1xx_HAL_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00079 #define __STM32F1xx_HAL_VERSION_SUB2   (0x04) /*!< [15:8]  sub2 version */
00080 #define __STM32F1xx_HAL_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00081 #define __STM32F1xx_HAL_VERSION         ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
00082                                         |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
00083                                         |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
00084                                         |(__STM32F1xx_HAL_VERSION_RC))
00085 
00086 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)
00087 
00088 /**
00089   * @}
00090   */
00091 
00092 /* Private macro -------------------------------------------------------------*/
00093 /* Private variables ---------------------------------------------------------*/
00094 
00095 /** @defgroup HAL_Private_Variables HAL Private Variables
00096   * @{
00097   */
00098 
00099 static __IO uint32_t uwTick;
00100 
00101 /**
00102   * @}
00103   */
00104 
00105 /* Private function prototypes -----------------------------------------------*/
00106 /* Exported functions ---------------------------------------------------------*/
00107 
00108 /** @defgroup HAL_Exported_Functions HAL Exported Functions
00109   * @{
00110   */
00111 
00112 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions 
00113  *  @brief    Initialization and de-initialization functions
00114  *
00115 @verbatim
00116  ===============================================================================
00117               ##### Initialization and de-initialization functions #####
00118  ===============================================================================
00119    [..]  This section provides functions allowing to:
00120       (+) Initializes the Flash interface, the NVIC allocation and initial clock 
00121           configuration. It initializes the source of time base also when timeout 
00122           is needed and the backup domain when enabled.
00123       (+) de-Initializes common part of the HAL.
00124       (+) Configure The time base source to have 1ms time base with a dedicated 
00125           Tick interrupt priority. 
00126         (++) Systick timer is used by default as source of time base, but user 
00127              can eventually implement his proper time base source (a general purpose 
00128              timer for example or other time source), keeping in mind that Time base 
00129              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
00130              handled in milliseconds basis.
00131         (++) Time base configuration function (HAL_InitTick ()) is called automatically 
00132              at the beginning of the program after reset by HAL_Init() or at any time 
00133              when clock is configured, by HAL_RCC_ClockConfig(). 
00134         (++) Source of time base is configured  to generate interrupts at regular 
00135              time intervals. Care must be taken if HAL_Delay() is called from a 
00136              peripheral ISR process, the Tick interrupt line must have higher priority 
00137             (numerically lower) than the peripheral interrupt. Otherwise the caller 
00138             ISR process will be blocked. 
00139        (++) functions affecting time base configurations are declared as __Weak  
00140              to make  override possible  in case of other  implementations in user file.
00141  
00142 @endverbatim
00143   * @{
00144   */
00145 
00146 /**
00147   * @brief This function configures the Flash prefetch, 
00148   *        Configures time base source, NVIC and Low level hardware
00149   * @note This function is called at the beginning of program after reset and before 
00150   *       the clock configuration
00151   * @note The time base configuration is based on MSI clock when exiting from Reset.
00152   *       Once done, time base tick start incrementing.
00153   *        In the default implementation,Systick is used as source of time base.
00154   *       The tick variable is incremented each 1ms in its ISR.
00155   * @retval HAL status
00156   */
00157 HAL_StatusTypeDef HAL_Init(void)
00158 {
00159   /* Configure Flash prefetch */
00160 #if (PREFETCH_ENABLE != 0)
00161 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
00162     defined(STM32F102x6) || defined(STM32F102xB) || \
00163     defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
00164     defined(STM32F105xC) || defined(STM32F107xC)
00165 
00166   /* Prefetch buffer is not available on value line devices */
00167   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
00168 #endif
00169 #endif /* PREFETCH_ENABLE */
00170 
00171   /* Set Interrupt Group Priority */
00172   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
00173 
00174   /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
00175   HAL_InitTick(TICK_INT_PRIORITY);
00176 
00177   /* Init the low level hardware */
00178   HAL_MspInit();
00179 
00180   /* Return function status */
00181   return HAL_OK;
00182 }
00183 
00184 /**
00185   * @brief This function de-Initializes common part of the HAL and stops the source
00186   *        of time base.
00187   * @note This function is optional.
00188   * @retval HAL status
00189   */
00190 HAL_StatusTypeDef HAL_DeInit(void)
00191 {
00192   /* Reset of all peripherals */
00193   __HAL_RCC_APB1_FORCE_RESET();
00194   __HAL_RCC_APB1_RELEASE_RESET();
00195 
00196   __HAL_RCC_APB2_FORCE_RESET();
00197   __HAL_RCC_APB2_RELEASE_RESET();
00198 
00199 #if defined(STM32F105xC) || defined(STM32F107xC)
00200   __HAL_RCC_AHB_FORCE_RESET();
00201   __HAL_RCC_AHB_RELEASE_RESET();
00202 #endif
00203   
00204   /* De-Init the low level hardware */
00205   HAL_MspDeInit();
00206     
00207   /* Return function status */
00208   return HAL_OK;
00209 }
00210 
00211 /**
00212   * @brief  Initializes the MSP.
00213   * @retval None
00214   */
00215 __weak void HAL_MspInit(void)
00216 {
00217   /* NOTE : This function Should not be modified, when the callback is needed,
00218             the HAL_MspInit could be implemented in the user file
00219    */
00220 }
00221 
00222 /**
00223   * @brief  DeInitializes the MSP.
00224   * @retval None
00225   */
00226 __weak void HAL_MspDeInit(void)
00227 {
00228   /* NOTE : This function Should not be modified, when the callback is needed,
00229             the HAL_MspDeInit could be implemented in the user file
00230    */
00231 }
00232 
00233 /**
00234   * @brief This function configures the source of the time base. 
00235   *        The time source is configured  to have 1ms time base with a dedicated 
00236   *        Tick interrupt priority.
00237   * @note This function is called  automatically at the beginning of program after
00238   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig(). 
00239   * @note In the default implementation, SysTick timer is the source of time base. 
00240   *       It is used to generate interrupts at regular time intervals. 
00241   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process, 
00242   *       The the SysTick interrupt must have higher priority (numerically lower) 
00243   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
00244   *       The function is declared as __Weak  to be overwritten  in case of other
00245   *       implementation  in user file.
00246   * @param TickPriority: Tick interrupt priority.
00247   * @retval HAL status
00248   */
00249 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
00250 {
00251   /*Configure the SysTick to have interrupt in 1ms time basis*/
00252   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
00253 
00254   /*Configure the SysTick IRQ priority */
00255   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
00256 
00257    /* Return function status */
00258   return HAL_OK;
00259 }
00260 
00261 /**
00262   * @}
00263   */
00264 
00265 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions 
00266  *  @brief    HAL Control functions
00267  *
00268 @verbatim
00269  ===============================================================================
00270                       ##### HAL Control functions #####
00271  ===============================================================================
00272     [..]  This section provides functions allowing to:
00273       (+) Provide a tick value in millisecond
00274       (+) Provide a blocking delay in millisecond
00275       (+) Suspend the time base source interrupt
00276       (+) Resume the time base source interrupt
00277       (+) Get the HAL API driver version
00278       (+) Get the device identifier
00279       (+) Get the device revision identifier
00280       (+) Enable/Disable Debug module during Sleep mode
00281       (+) Enable/Disable Debug module during STOP mode
00282       (+) Enable/Disable Debug module during STANDBY mode
00283       
00284 @endverbatim
00285   * @{
00286   */
00287 
00288 /**
00289   * @brief This function is called to increment  a global variable "uwTick"
00290   *        used as application time base.
00291   * @note In the default implementation, this variable is incremented each 1ms
00292   *       in Systick ISR.
00293   * @note This function is declared as __weak to be overwritten in case of other 
00294   *      implementations in user file.
00295   * @retval None
00296   */
00297 __weak void HAL_IncTick(void)
00298 {
00299   uwTick++;
00300 }
00301 
00302 /**
00303   * @brief Provides a tick value in millisecond.
00304   * @note   This function is declared as __weak  to be overwritten  in case of other 
00305   *       implementations in user file.
00306   * @retval tick value
00307   */
00308 __weak uint32_t HAL_GetTick(void)
00309 {
00310   return uwTick;
00311 }
00312 
00313 /**
00314   * @brief This function provides accurate delay (in milliseconds) based 
00315   *        on variable incremented.
00316   * @note In the default implementation , SysTick timer is the source of time base.
00317   *       It is used to generate interrupts at regular time intervals where uwTick
00318   *       is incremented.
00319   * @note ThiS function is declared as __weak to be overwritten in case of other
00320   *       implementations in user file.
00321   * @param Delay: specifies the delay time length, in milliseconds.
00322   * @retval None
00323   */
00324 __weak void HAL_Delay(__IO uint32_t Delay)
00325 {
00326   uint32_t tickstart = 0;
00327   tickstart = HAL_GetTick();
00328   while((HAL_GetTick() - tickstart) < Delay)
00329   {
00330   }
00331 }
00332 
00333 /**
00334   * @brief Suspend Tick increment.
00335   * @note In the default implementation , SysTick timer is the source of time base. It is
00336   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
00337   *       is called, the the SysTick interrupt will be disabled and so Tick increment 
00338   *       is suspended.
00339   * @note This function is declared as __weak to be overwritten in case of other
00340   *       implementations in user file.
00341   * @retval None
00342   */
00343 __weak void HAL_SuspendTick(void)
00344 {
00345   /* Disable SysTick Interrupt */
00346   CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
00347 }
00348 
00349 /**
00350   * @brief Resume Tick increment.
00351   * @note In the default implementation , SysTick timer is the source of time base. It is
00352   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
00353   *       is called, the the SysTick interrupt will be enabled and so Tick increment 
00354   *       is resumed.
00355   * @note This function is declared as __weak  to be overwritten  in case of other
00356   *       implementations in user file.
00357   * @retval None
00358   */
00359 __weak void HAL_ResumeTick(void)
00360 {
00361   /* Enable SysTick Interrupt */
00362   SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
00363 }
00364 
00365 /**
00366   * @brief  This method returns the HAL revision
00367   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00368   */
00369 uint32_t HAL_GetHalVersion(void)
00370 {
00371  return __STM32F1xx_HAL_VERSION;
00372 }
00373 
00374 /**
00375   * @brief Returns the device revision identifier.
00376   * Note: On devices STM32F10xx8 and STM32F10xxB,
00377   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00378   *                  STM32F101xF/G and STM32F103xF/G
00379   *                  STM32F10xx4 and STM32F10xx6
00380   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00381   *       debug mode (not accessible by the user software in normal mode).
00382   *       Refer to errata sheet of these devices for more details.
00383   * @retval Device revision identifier
00384   */
00385 uint32_t HAL_GetREVID(void)
00386 {
00387   return((DBGMCU->IDCODE) >> POSITION_VAL(DBGMCU_IDCODE_REV_ID));
00388 }
00389 
00390 /**
00391   * @brief  Returns the device identifier.
00392   * Note: On devices STM32F10xx8 and STM32F10xxB,
00393   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00394   *                  STM32F101xF/G and STM32F103xF/G
00395   *                  STM32F10xx4 and STM32F10xx6
00396   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00397   *       debug mode (not accessible by the user software in normal mode).
00398   *       Refer to errata sheet of these devices for more details.
00399   * @retval Device identifier
00400   */
00401 uint32_t HAL_GetDEVID(void)
00402 {
00403    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
00404 }
00405 
00406 /**
00407   * @brief  Enable the Debug Module during SLEEP mode
00408   * @retval None
00409   */
00410 void HAL_DBGMCU_EnableDBGSleepMode(void)
00411 {
00412   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00413 }
00414 
00415 /**
00416   * @brief  Disable the Debug Module during SLEEP mode
00417   * Note: On devices STM32F10xx8 and STM32F10xxB,
00418   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00419   *                  STM32F101xF/G and STM32F103xF/G
00420   *                  STM32F10xx4 and STM32F10xx6
00421   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00422   *       debug mode (not accessible by the user software in normal mode).
00423   *       Refer to errata sheet of these devices for more details.
00424   * @retval None
00425   */
00426 void HAL_DBGMCU_DisableDBGSleepMode(void)
00427 {
00428   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00429 }
00430 
00431 /**
00432   * @brief  Enable the Debug Module during STOP mode
00433   * Note: On devices STM32F10xx8 and STM32F10xxB,
00434   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00435   *                  STM32F101xF/G and STM32F103xF/G
00436   *                  STM32F10xx4 and STM32F10xx6
00437   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00438   *       debug mode (not accessible by the user software in normal mode).
00439   *       Refer to errata sheet of these devices for more details.
00440   * Note: On all STM32F1 devices:
00441   *       If the system tick timer interrupt is enabled during the Stop mode 
00442   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
00443   *       the system from Stop mode.
00444   *       Workaround: To debug the Stop mode, disable the system tick timer 
00445   *       interrupt.
00446   *       Refer to errata sheet of these devices for more details.
00447   * Note: On all STM32F1 devices:
00448   *       If the system tick timer interrupt is enabled during the Stop mode  
00449   *       debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
00450   *       the system from Stop mode.
00451   *       Workaround: To debug the Stop mode, disable the system tick timer
00452   *       interrupt.
00453   *       Refer to errata sheet of these devices for more details.
00454   * @retval None
00455   */
00456 void HAL_DBGMCU_EnableDBGStopMode(void)
00457 {
00458   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00459 }
00460 
00461 /**
00462   * @brief  Disable the Debug Module during STOP mode
00463   * Note: On devices STM32F10xx8 and STM32F10xxB,
00464   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00465   *                  STM32F101xF/G and STM32F103xF/G
00466   *                  STM32F10xx4 and STM32F10xx6
00467   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00468   *       debug mode (not accessible by the user software in normal mode).
00469   *       Refer to errata sheet of these devices for more details.
00470   * @retval None
00471   */
00472 void HAL_DBGMCU_DisableDBGStopMode(void)
00473 {
00474   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00475 }
00476 
00477 /**
00478   * @brief  Enable the Debug Module during STANDBY mode
00479   * Note: On devices STM32F10xx8 and STM32F10xxB,
00480   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00481   *                  STM32F101xF/G and STM32F103xF/G
00482   *                  STM32F10xx4 and STM32F10xx6
00483   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00484   *       debug mode (not accessible by the user software in normal mode).
00485   *       Refer to errata sheet of these devices for more details.
00486   * @retval None
00487   */
00488 void HAL_DBGMCU_EnableDBGStandbyMode(void)
00489 {
00490   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00491 }
00492 
00493 /**
00494   * @brief  Disable the Debug Module during STANDBY mode
00495   * Note: On devices STM32F10xx8 and STM32F10xxB,
00496   *                  STM32F101xC/D/E and STM32F103xC/D/E,
00497   *                  STM32F101xF/G and STM32F103xF/G
00498   *                  STM32F10xx4 and STM32F10xx6
00499   *       Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in 
00500   *       debug mode (not accessible by the user software in normal mode).
00501   *       Refer to errata sheet of these devices for more details.
00502   * @retval None
00503   */
00504 void HAL_DBGMCU_DisableDBGStandbyMode(void)
00505 {
00506   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00507 }
00508 
00509 /**
00510   * @}
00511   */
00512 
00513 /**
00514   * @}
00515   */
00516 
00517 #endif /* HAL_MODULE_ENABLED */
00518 /**
00519   * @}
00520   */
00521 
00522 /**
00523   * @}
00524   */
00525 
00526 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/