STM32F479xx HAL User Manual
stm32f4xx_hal.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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) 2017 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 "stm32f4xx_hal.h"
00037 
00038 /** @addtogroup STM32F4xx_HAL_Driver
00039   * @{
00040   */
00041 
00042 /** @defgroup HAL HAL
00043   * @brief HAL module driver.
00044   * @{
00045   */
00046 
00047 /* Private typedef -----------------------------------------------------------*/
00048 /* Private define ------------------------------------------------------------*/
00049 /** @addtogroup HAL_Private_Constants
00050   * @{
00051   */
00052 /**
00053   * @brief STM32F4xx HAL Driver version number V1.7.11
00054   */
00055 #define __STM32F4xx_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
00056 #define __STM32F4xx_HAL_VERSION_SUB1   (0x07U) /*!< [23:16] sub1 version */
00057 #define __STM32F4xx_HAL_VERSION_SUB2   (0x0BU) /*!< [15:8]  sub2 version */
00058 #define __STM32F4xx_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */ 
00059 #define __STM32F4xx_HAL_VERSION         ((__STM32F4xx_HAL_VERSION_MAIN << 24U)\
00060                                         |(__STM32F4xx_HAL_VERSION_SUB1 << 16U)\
00061                                         |(__STM32F4xx_HAL_VERSION_SUB2 << 8U )\
00062                                         |(__STM32F4xx_HAL_VERSION_RC))
00063                                         
00064 #define IDCODE_DEVID_MASK    0x00000FFFU
00065 
00066 /* ------------ RCC registers bit address in the alias region ----------- */
00067 #define SYSCFG_OFFSET             (SYSCFG_BASE - PERIPH_BASE)
00068 /* ---  MEMRMP Register ---*/ 
00069 /* Alias word address of UFB_MODE bit */ 
00070 #define MEMRMP_OFFSET             SYSCFG_OFFSET 
00071 #define UFB_MODE_BIT_NUMBER       SYSCFG_MEMRMP_UFB_MODE_Pos
00072 #define UFB_MODE_BB               (uint32_t)(PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (UFB_MODE_BIT_NUMBER * 4U)) 
00073 
00074 /* ---  CMPCR Register ---*/ 
00075 /* Alias word address of CMP_PD bit */ 
00076 #define CMPCR_OFFSET              (SYSCFG_OFFSET + 0x20U) 
00077 #define CMP_PD_BIT_NUMBER         SYSCFG_CMPCR_CMP_PD_Pos
00078 #define CMPCR_CMP_PD_BB           (uint32_t)(PERIPH_BB_BASE + (CMPCR_OFFSET * 32U) + (CMP_PD_BIT_NUMBER * 4U))
00079 
00080 /* ---  MCHDLYCR Register ---*/ 
00081 /* Alias word address of BSCKSEL bit */ 
00082 #define MCHDLYCR_OFFSET            (SYSCFG_OFFSET + 0x30U) 
00083 #define BSCKSEL_BIT_NUMBER         SYSCFG_MCHDLYCR_BSCKSEL_Pos
00084 #define MCHDLYCR_BSCKSEL_BB        (uint32_t)(PERIPH_BB_BASE + (MCHDLYCR_OFFSET * 32U) + (BSCKSEL_BIT_NUMBER * 4U))
00085 /**
00086   * @}
00087   */
00088 
00089 /* Private macro -------------------------------------------------------------*/
00090 /* Private variables ---------------------------------------------------------*/
00091 /** @addtogroup HAL_Private_Variables
00092   * @{
00093   */
00094 __IO uint32_t uwTick;
00095 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
00096 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
00097 /**
00098   * @}
00099   */
00100 /* Private function prototypes -----------------------------------------------*/
00101 /* Private functions ---------------------------------------------------------*/
00102 
00103 /** @defgroup HAL_Exported_Functions HAL Exported Functions
00104   * @{
00105   */
00106 
00107 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions 
00108  *  @brief    Initialization and de-initialization functions
00109  *
00110 @verbatim    
00111  ===============================================================================
00112               ##### Initialization and Configuration functions #####
00113  ===============================================================================
00114     [..]  This section provides functions allowing to:
00115       (+) Initializes the Flash interface the NVIC allocation and initial clock 
00116           configuration. It initializes the systick also when timeout is needed 
00117           and the backup domain when enabled.
00118       (+) De-Initializes common part of the HAL.
00119       (+) Configure the time base source to have 1ms time base with a dedicated 
00120           Tick interrupt priority. 
00121         (++) SysTick timer is used by default as source of time base, but user
00122              can eventually implement his proper time base source (a general purpose 
00123              timer for example or other time source), keeping in mind that Time base 
00124              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
00125              handled in milliseconds basis.
00126         (++) Time base configuration function (HAL_InitTick ()) is called automatically 
00127              at the beginning of the program after reset by HAL_Init() or at any time 
00128              when clock is configured, by HAL_RCC_ClockConfig(). 
00129         (++) Source of time base is configured  to generate interrupts at regular 
00130              time intervals. Care must be taken if HAL_Delay() is called from a 
00131              peripheral ISR process, the Tick interrupt line must have higher priority 
00132             (numerically lower) than the peripheral interrupt. Otherwise the caller 
00133             ISR process will be blocked. 
00134        (++) functions affecting time base configurations are declared as __weak  
00135              to make  override possible  in case of other  implementations in user file.
00136 @endverbatim
00137   * @{
00138   */
00139 
00140 /**
00141   * @brief  This function is used to initialize the HAL Library; it must be the first 
00142   *         instruction to be executed in the main program (before to call any other
00143   *         HAL function), it performs the following:
00144   *           Configure the Flash prefetch, instruction and Data caches.
00145   *           Configures the SysTick to generate an interrupt each 1 millisecond,
00146   *           which is clocked by the HSI (at this stage, the clock is not yet
00147   *           configured and thus the system is running from the internal HSI at 16 MHz).
00148   *           Set NVIC Group Priority to 4.
00149   *           Calls the HAL_MspInit() callback function defined in user file 
00150   *           "stm32f4xx_hal_msp.c" to do the global low level hardware initialization 
00151   *            
00152   * @note   SysTick is used as time base for the HAL_Delay() function, the application
00153   *         need to ensure that the SysTick time base is always set to 1 millisecond
00154   *         to have correct HAL operation.
00155   * @retval HAL status
00156   */
00157 HAL_StatusTypeDef HAL_Init(void)
00158 {
00159   /* Configure Flash prefetch, Instruction cache, Data cache */ 
00160 #if (INSTRUCTION_CACHE_ENABLE != 0U)
00161   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
00162 #endif /* INSTRUCTION_CACHE_ENABLE */
00163 
00164 #if (DATA_CACHE_ENABLE != 0U)
00165   __HAL_FLASH_DATA_CACHE_ENABLE();
00166 #endif /* DATA_CACHE_ENABLE */
00167 
00168 #if (PREFETCH_ENABLE != 0U)
00169   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
00170 #endif /* PREFETCH_ENABLE */
00171 
00172   /* Set Interrupt Group Priority */
00173   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
00174 
00175   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
00176   HAL_InitTick(TICK_INT_PRIORITY);
00177 
00178   /* Init the low level hardware */
00179   HAL_MspInit();
00180 
00181   /* Return function status */
00182   return HAL_OK;
00183 }
00184 
00185 /**
00186   * @brief  This function de-Initializes common part of the HAL and stops the systick.
00187   *         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   __HAL_RCC_AHB1_FORCE_RESET();
00200   __HAL_RCC_AHB1_RELEASE_RESET();
00201 
00202   __HAL_RCC_AHB2_FORCE_RESET();
00203   __HAL_RCC_AHB2_RELEASE_RESET();
00204 
00205   __HAL_RCC_AHB3_FORCE_RESET();
00206   __HAL_RCC_AHB3_RELEASE_RESET();
00207 
00208   /* De-Init the low level hardware */
00209   HAL_MspDeInit();
00210     
00211   /* Return function status */
00212   return HAL_OK;
00213 }
00214 
00215 /**
00216   * @brief  Initialize the MSP.
00217   * @retval None
00218   */
00219 __weak void HAL_MspInit(void)
00220 {
00221   /* NOTE : This function should not be modified, when the callback is needed,
00222             the HAL_MspInit could be implemented in the user file
00223    */
00224 }
00225 
00226 /**
00227   * @brief  DeInitializes the MSP.
00228   * @retval None
00229   */
00230 __weak void HAL_MspDeInit(void)
00231 {
00232   /* NOTE : This function should not be modified, when the callback is needed,
00233             the HAL_MspDeInit could be implemented in the user file
00234    */ 
00235 }
00236 
00237 /**
00238   * @brief This function configures the source of the time base.
00239   *        The time source is configured  to have 1ms time base with a dedicated 
00240   *        Tick interrupt priority.
00241   * @note This function is called  automatically at the beginning of program after
00242   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
00243   * @note In the default implementation, SysTick timer is the source of time base. 
00244   *       It is used to generate interrupts at regular time intervals. 
00245   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process, 
00246   *       The SysTick interrupt must have higher priority (numerically lower)
00247   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
00248   *       The function is declared as __weak  to be overwritten  in case of other
00249   *       implementation  in user file.
00250   * @param TickPriority Tick interrupt priority.
00251   * @retval HAL status
00252   */
00253 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
00254 {
00255   /* Configure the SysTick to have interrupt in 1ms time basis*/
00256   if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
00257   {
00258     return HAL_ERROR;
00259   }
00260 
00261   /* Configure the SysTick IRQ priority */
00262   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
00263   {
00264     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
00265     uwTickPrio = TickPriority;
00266   }
00267   else
00268   {
00269     return HAL_ERROR;
00270   }
00271 
00272   /* Return function status */
00273   return HAL_OK;
00274 }
00275 
00276 /**
00277   * @}
00278   */
00279 
00280 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions 
00281  *  @brief    HAL Control functions
00282  *
00283 @verbatim
00284  ===============================================================================
00285                       ##### HAL Control functions #####
00286  ===============================================================================
00287     [..]  This section provides functions allowing to:
00288       (+) Provide a tick value in millisecond
00289       (+) Provide a blocking delay in millisecond
00290       (+) Suspend the time base source interrupt
00291       (+) Resume the time base source interrupt
00292       (+) Get the HAL API driver version
00293       (+) Get the device identifier
00294       (+) Get the device revision identifier
00295       (+) Enable/Disable Debug module during SLEEP mode
00296       (+) Enable/Disable Debug module during STOP mode
00297       (+) Enable/Disable Debug module during STANDBY mode
00298 
00299 @endverbatim
00300   * @{
00301   */
00302 
00303 /**
00304   * @brief This function is called to increment  a global variable "uwTick"
00305   *        used as application time base.
00306   * @note In the default implementation, this variable is incremented each 1ms
00307   *       in SysTick ISR.
00308  * @note This function is declared as __weak to be overwritten in case of other 
00309   *      implementations in user file.
00310   * @retval None
00311   */
00312 __weak void HAL_IncTick(void)
00313 {
00314   uwTick += uwTickFreq;
00315 }
00316 
00317 /**
00318   * @brief Provides a tick value in millisecond.
00319   * @note This function is declared as __weak to be overwritten in case of other 
00320   *       implementations in user file.
00321   * @retval tick value
00322   */
00323 __weak uint32_t HAL_GetTick(void)
00324 {
00325   return uwTick;
00326 }
00327 
00328 /**
00329   * @brief This function returns a tick priority.
00330   * @retval tick priority
00331   */
00332 uint32_t HAL_GetTickPrio(void)
00333 {
00334   return uwTickPrio;
00335 }
00336 
00337 /**
00338   * @brief Set new tick Freq.
00339   * @retval Status
00340   */
00341 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
00342 {
00343   HAL_StatusTypeDef status  = HAL_OK;
00344   HAL_TickFreqTypeDef prevTickFreq;
00345 
00346   assert_param(IS_TICKFREQ(Freq));
00347 
00348   if (uwTickFreq != Freq)
00349   {
00350     /* Back up uwTickFreq frequency */
00351     prevTickFreq = uwTickFreq;
00352 
00353     /* Update uwTickFreq global variable used by HAL_InitTick() */
00354     uwTickFreq = Freq;
00355 
00356     /* Apply the new tick Freq  */
00357     status = HAL_InitTick(uwTickPrio);
00358 
00359     if (status != HAL_OK)
00360     {
00361       /* Restore previous tick frequency */
00362       uwTickFreq = prevTickFreq;
00363     }
00364   }
00365 
00366   return status;
00367 }
00368 
00369 /**
00370   * @brief Return tick frequency.
00371   * @retval tick period in Hz
00372   */
00373 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
00374 {
00375   return uwTickFreq;
00376 }
00377 
00378 /**
00379   * @brief This function provides minimum delay (in milliseconds) based 
00380   *        on variable incremented.
00381   * @note In the default implementation , SysTick timer is the source of time base.
00382   *       It is used to generate interrupts at regular time intervals where uwTick
00383   *       is incremented.
00384   * @note This function is declared as __weak to be overwritten in case of other
00385   *       implementations in user file.
00386   * @param Delay specifies the delay time length, in milliseconds.
00387   * @retval None
00388   */
00389 __weak void HAL_Delay(uint32_t Delay)
00390 {
00391   uint32_t tickstart = HAL_GetTick();
00392   uint32_t wait = Delay;
00393 
00394   /* Add a freq to guarantee minimum wait */
00395   if (wait < HAL_MAX_DELAY)
00396   {
00397     wait += (uint32_t)(uwTickFreq);
00398   }
00399 
00400   while((HAL_GetTick() - tickstart) < wait)
00401   {
00402   }
00403 }
00404 
00405 /**
00406   * @brief Suspend Tick increment.
00407   * @note In the default implementation , SysTick timer is the source of time base. It is
00408   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
00409   *       is called, the SysTick interrupt will be disabled and so Tick increment 
00410   *       is suspended.
00411   * @note This function is declared as __weak to be overwritten in case of other
00412   *       implementations in user file.
00413   * @retval None
00414   */
00415 __weak void HAL_SuspendTick(void)
00416 {
00417   /* Disable SysTick Interrupt */
00418   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
00419 }
00420 
00421 /**
00422   * @brief Resume Tick increment.
00423   * @note In the default implementation , SysTick timer is the source of time base. It is
00424   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
00425   *       is called, the SysTick interrupt will be enabled and so Tick increment 
00426   *       is resumed.
00427   * @note This function is declared as __weak to be overwritten in case of other
00428   *       implementations in user file.
00429   * @retval None
00430   */
00431 __weak void HAL_ResumeTick(void)
00432 {
00433   /* Enable SysTick Interrupt */
00434   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
00435 }
00436 
00437 /**
00438   * @brief  Returns the HAL revision
00439   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
00440   */
00441 uint32_t HAL_GetHalVersion(void)
00442 {
00443   return __STM32F4xx_HAL_VERSION;
00444 }
00445 
00446 /**
00447   * @brief  Returns the device revision identifier.
00448   * @retval Device revision identifier
00449   */
00450 uint32_t HAL_GetREVID(void)
00451 {
00452   return((DBGMCU->IDCODE) >> 16U);
00453 }
00454 
00455 /**
00456   * @brief  Returns the device identifier.
00457   * @retval Device identifier
00458   */
00459 uint32_t HAL_GetDEVID(void)
00460 {
00461   return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
00462 }
00463 
00464 /**
00465   * @brief  Enable the Debug Module during SLEEP mode
00466   * @retval None
00467   */
00468 void HAL_DBGMCU_EnableDBGSleepMode(void)
00469 {
00470   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00471 }
00472 
00473 /**
00474   * @brief  Disable the Debug Module during SLEEP mode
00475   * @retval None
00476   */
00477 void HAL_DBGMCU_DisableDBGSleepMode(void)
00478 {
00479   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
00480 }
00481 
00482 /**
00483   * @brief  Enable the Debug Module during STOP mode
00484   * @retval None
00485   */
00486 void HAL_DBGMCU_EnableDBGStopMode(void)
00487 {
00488   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00489 }
00490 
00491 /**
00492   * @brief  Disable the Debug Module during STOP mode
00493   * @retval None
00494   */
00495 void HAL_DBGMCU_DisableDBGStopMode(void)
00496 {
00497   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
00498 }
00499 
00500 /**
00501   * @brief  Enable the Debug Module during STANDBY mode
00502   * @retval None
00503   */
00504 void HAL_DBGMCU_EnableDBGStandbyMode(void)
00505 {
00506   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00507 }
00508 
00509 /**
00510   * @brief  Disable the Debug Module during STANDBY mode
00511   * @retval None
00512   */
00513 void HAL_DBGMCU_DisableDBGStandbyMode(void)
00514 {
00515   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
00516 }
00517 
00518 /**
00519   * @brief  Enables the I/O Compensation Cell.
00520   * @note   The I/O compensation cell can be used only when the device supply
00521   *         voltage ranges from 2.4 to 3.6 V.  
00522   * @retval None
00523   */
00524 void HAL_EnableCompensationCell(void)
00525 {
00526   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
00527 }
00528 
00529 /**
00530   * @brief  Power-down the I/O Compensation Cell.
00531   * @note   The I/O compensation cell can be used only when the device supply
00532   *         voltage ranges from 2.4 to 3.6 V.  
00533   * @retval None
00534   */
00535 void HAL_DisableCompensationCell(void)
00536 {
00537   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
00538 }
00539 
00540 /**
00541   * @brief  Returns first word of the unique device identifier (UID based on 96 bits)
00542   * @retval Device identifier
00543   */
00544 uint32_t HAL_GetUIDw0(void)
00545 {
00546   return (READ_REG(*((uint32_t *)UID_BASE)));
00547 }
00548 
00549 /**
00550   * @brief  Returns second word of the unique device identifier (UID based on 96 bits)
00551   * @retval Device identifier
00552   */
00553 uint32_t HAL_GetUIDw1(void)
00554 {
00555   return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
00556 }
00557 
00558 /**
00559   * @brief  Returns third word of the unique device identifier (UID based on 96 bits)
00560   * @retval Device identifier
00561   */
00562 uint32_t HAL_GetUIDw2(void)
00563 {
00564   return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
00565 }
00566 
00567 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) ||\
00568     defined(STM32F469xx) || defined(STM32F479xx)
00569 /**
00570   * @brief  Enables the Internal FLASH Bank Swapping.
00571   *   
00572   * @note   This function can be used only for STM32F42xxx/43xxx/469xx/479xx devices. 
00573   *
00574   * @note   Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000) 
00575   *         and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)   
00576   *
00577   * @retval None
00578   */
00579 void HAL_EnableMemorySwappingBank(void)
00580 {
00581   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
00582 }
00583 
00584 /**
00585   * @brief  Disables the Internal FLASH Bank Swapping.
00586   *   
00587   * @note   This function can be used only for STM32F42xxx/43xxx/469xx/479xx devices. 
00588   *
00589   * @note   The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x00000000) 
00590   *         and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000) 
00591   *           
00592   * @retval None
00593   */
00594 void HAL_DisableMemorySwappingBank(void)
00595 {
00596   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
00597 }
00598 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
00599 /**
00600   * @}
00601   */
00602 
00603 /**
00604   * @}
00605   */
00606 
00607 /**
00608   * @}
00609   */
00610 
00611 /**
00612   * @}
00613   */
00614 
00615 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/