2 ******************************************************************************
\r
3 * @file stm32l1xx_hal.c
\r
4 * @author MCD Application Team
\r
5 * @brief HAL module driver.
\r
6 * This is the common part of the HAL initialization
\r
9 ==============================================================================
\r
10 ##### How to use this driver #####
\r
11 ==============================================================================
\r
13 The common HAL driver contains a set of generic and common APIs that can be
\r
14 used by the PPP peripheral drivers and the user to start using the HAL.
\r
16 The HAL contains two APIs categories:
\r
18 (+) Services HAL APIs
\r
21 ******************************************************************************
\r
24 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
25 * All rights reserved.</center></h2>
\r
27 * This software component is licensed by ST under BSD 3-Clause license,
\r
28 * the "License"; You may not use this file except in compliance with the
\r
29 * License. You may obtain a copy of the License at:
\r
30 * opensource.org/licenses/BSD-3-Clause
\r
32 ******************************************************************************
\r
35 /* Includes ------------------------------------------------------------------*/
\r
36 #include "stm32l1xx_hal.h"
\r
38 /** @addtogroup STM32L1xx_HAL_Driver
\r
42 /** @defgroup HAL HAL
\r
43 * @brief HAL module driver.
\r
47 #ifdef HAL_MODULE_ENABLED
\r
49 /* Private typedef -----------------------------------------------------------*/
\r
50 /* Private define ------------------------------------------------------------*/
\r
52 /** @defgroup HAL_Private_Defines HAL Private Defines
\r
57 * @brief STM32L1xx HAL Driver version number
\r
59 #define __STM32L1xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
\r
60 #define __STM32L1xx_HAL_VERSION_SUB1 (0x04) /*!< [23:16] sub1 version */
\r
61 #define __STM32L1xx_HAL_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
\r
62 #define __STM32L1xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
\r
63 #define __STM32L1xx_HAL_VERSION ((__STM32L1xx_HAL_VERSION_MAIN << 24)\
\r
64 |(__STM32L1xx_HAL_VERSION_SUB1 << 16)\
\r
65 |(__STM32L1xx_HAL_VERSION_SUB2 << 8 )\
\r
66 |(__STM32L1xx_HAL_VERSION_RC))
\r
68 #define IDCODE_DEVID_MASK (0x00000FFFU)
\r
74 /* Private macro -------------------------------------------------------------*/
\r
75 /* Private variables ---------------------------------------------------------*/
\r
76 /* Private function prototypes -----------------------------------------------*/
\r
77 /* Private functions ---------------------------------------------------------*/
\r
79 /* Exported variables --------------------------------------------------------*/
\r
80 /** @addtogroup HAL_Exported_Variables
\r
83 __IO uint32_t uwTick;
\r
84 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
\r
85 uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
\r
90 /* Exported functions --------------------------------------------------------*/
\r
91 /** @defgroup HAL_Exported_Functions HAL Exported Functions
\r
95 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
\r
96 * @brief Initialization and de-initialization functions
\r
99 ===============================================================================
\r
100 ##### Initialization and de-initialization functions #####
\r
101 ===============================================================================
\r
102 [..] This section provides functions allowing to:
\r
103 (+) Initialize the Flash interface, the NVIC allocation and initial clock
\r
104 configuration. It initializes the source of time base also when timeout
\r
105 is needed and the backup domain when enabled.
\r
106 (+) De-initialize common part of the HAL.
\r
107 (+) Configure the time base source to have 1ms time base with a dedicated
\r
108 Tick interrupt priority.
\r
109 (++) SysTick timer is used by default as source of time base, but user
\r
110 can eventually implement his proper time base source (a general purpose
\r
111 timer for example or other time source), keeping in mind that Time base
\r
112 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
\r
113 handled in milliseconds basis.
\r
114 (++) Time base configuration function (HAL_InitTick ()) is called automatically
\r
115 at the beginning of the program after reset by HAL_Init() or at any time
\r
116 when clock is configured, by HAL_RCC_ClockConfig().
\r
117 (++) Source of time base is configured to generate interrupts at regular
\r
118 time intervals. Care must be taken if HAL_Delay() is called from a
\r
119 peripheral ISR process, the Tick interrupt line must have higher priority
\r
120 (numerically lower) than the peripheral interrupt. Otherwise the caller
\r
121 ISR process will be blocked.
\r
122 (++) functions affecting time base configurations are declared as __weak
\r
123 to make override possible in case of other implementations in user file.
\r
130 * @brief This function configures the Flash prefetch,
\r
131 * configures time base source, NVIC and Low level hardware
\r
132 * @note This function is called at the beginning of program after reset and before
\r
133 * the clock configuration
\r
134 * @note The time base configuration is based on MSI clock when exiting from Reset.
\r
135 * Once done, time base tick start incrementing.
\r
136 * In the default implementation,Systick is used as source of time base.
\r
137 * the tick variable is incremented each 1ms in its ISR.
\r
138 * @retval HAL status
\r
140 HAL_StatusTypeDef HAL_Init(void)
\r
142 HAL_StatusTypeDef status = HAL_OK;
\r
144 /* Configure Flash prefetch */
\r
145 #if (PREFETCH_ENABLE != 0)
\r
146 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
\r
147 #endif /* PREFETCH_ENABLE */
\r
149 /* Set Interrupt Group Priority */
\r
150 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
\r
152 /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
\r
153 if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
\r
155 status = HAL_ERROR;
\r
159 /* Init the low level hardware */
\r
163 /* Return function status */
\r
168 * @brief This function de-initializes common part of the HAL and stops the source
\r
170 * @note This function is optional.
\r
171 * @retval HAL status
\r
173 HAL_StatusTypeDef HAL_DeInit(void)
\r
175 /* Reset of all peripherals */
\r
176 __HAL_RCC_APB1_FORCE_RESET();
\r
177 __HAL_RCC_APB1_RELEASE_RESET();
\r
179 __HAL_RCC_APB2_FORCE_RESET();
\r
180 __HAL_RCC_APB2_RELEASE_RESET();
\r
182 __HAL_RCC_AHB_FORCE_RESET();
\r
183 __HAL_RCC_AHB_RELEASE_RESET();
\r
185 /* De-Init the low level hardware */
\r
188 /* Return function status */
\r
193 * @brief Initialize the MSP.
\r
196 __weak void HAL_MspInit(void)
\r
198 /* NOTE : This function should not be modified, when the callback is needed,
\r
199 the HAL_MspInit could be implemented in the user file
\r
204 * @brief DeInitialize the MSP.
\r
207 __weak void HAL_MspDeInit(void)
\r
209 /* NOTE : This function should not be modified, when the callback is needed,
\r
210 the HAL_MspDeInit could be implemented in the user file
\r
215 * @brief This function configures the source of the time base:
\r
216 * The time source is configured to have 1ms time base with a dedicated
\r
217 * Tick interrupt priority.
\r
218 * @note This function is called automatically at the beginning of program after
\r
219 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
\r
220 * @note In the default implementation, SysTick timer is the source of time base.
\r
221 * It is used to generate interrupts at regular time intervals.
\r
222 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
\r
223 * The SysTick interrupt must have higher priority (numerically lower)
\r
224 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
\r
225 * The function is declared as __weak to be overwritten in case of other
\r
226 * implementation in user file.
\r
227 * @param TickPriority Tick interrupt priority.
\r
228 * @retval HAL status
\r
230 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
\r
232 HAL_StatusTypeDef status = HAL_OK;
\r
234 if (uwTickFreq != 0U)
\r
236 /*Configure the SysTick to have interrupt in 1ms time basis*/
\r
237 if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) == 0U)
\r
239 /* Configure the SysTick IRQ priority */
\r
240 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
\r
242 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
\r
243 uwTickPrio = TickPriority;
\r
247 status = HAL_ERROR;
\r
252 status = HAL_ERROR;
\r
257 status = HAL_ERROR;
\r
260 /* Return function status */
\r
268 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
\r
269 * @brief HAL Control functions
\r
272 ===============================================================================
\r
273 ##### HAL Control functions #####
\r
274 ===============================================================================
\r
275 [..] This section provides functions allowing to:
\r
276 (+) Provide a tick value in millisecond
\r
277 (+) Provide a blocking delay in millisecond
\r
278 (+) Suspend the time base source interrupt
\r
279 (+) Resume the time base source interrupt
\r
280 (+) Get the HAL API driver version
\r
281 (+) Get the device identifier
\r
282 (+) Get the device revision identifier
\r
283 (+) Get the unique device identifier
\r
290 * @brief This function is called to increment a global variable "uwTick"
\r
291 * used as application time base.
\r
292 * @note In the default implementation, this variable is incremented each 1ms
\r
294 * @note This function is declared as __weak to be overwritten in case of other
\r
295 * implementations in user file.
\r
298 __weak void HAL_IncTick(void)
\r
300 uwTick += uwTickFreq;
\r
304 * @brief Provide a tick value in millisecond.
\r
305 * @note This function is declared as __weak to be overwritten in case of other
\r
306 * implementations in user file.
\r
307 * @retval tick value
\r
309 __weak uint32_t HAL_GetTick(void)
\r
315 * @brief This function returns a tick priority.
\r
316 * @retval tick priority
\r
318 uint32_t HAL_GetTickPrio(void)
\r
324 * @brief Set new tick Freq.
\r
325 * @param Freq tick frequency
\r
326 * @retval HAL status
\r
328 HAL_StatusTypeDef HAL_SetTickFreq(uint32_t Freq)
\r
330 HAL_StatusTypeDef status = HAL_OK;
\r
331 assert_param(IS_TICKFREQ(Freq));
\r
333 if (uwTickFreq != Freq)
\r
335 /* Apply the new tick Freq */
\r
336 status = HAL_InitTick(uwTickPrio);
\r
337 if (status == HAL_OK)
\r
347 * @brief Return tick frequency.
\r
348 * @retval tick period in Hz
\r
350 uint32_t HAL_GetTickFreq(void)
\r
356 * @brief This function provides minimum delay (in milliseconds) based
\r
357 * on variable incremented.
\r
358 * @note In the default implementation , SysTick timer is the source of time base.
\r
359 * It is used to generate interrupts at regular time intervals where uwTick
\r
361 * @note This function is declared as __weak to be overwritten in case of other
\r
362 * implementations in user file.
\r
363 * @param Delay specifies the delay time length, in milliseconds.
\r
366 __weak void HAL_Delay(uint32_t Delay)
\r
368 uint32_t tickstart = HAL_GetTick();
\r
369 uint32_t wait = Delay;
\r
371 /* Add a period to guaranty minimum wait */
\r
372 if (wait < HAL_MAX_DELAY)
\r
374 wait += (uint32_t)(uwTickFreq);
\r
377 while((HAL_GetTick() - tickstart) < wait)
\r
383 * @brief Suspend the Tick increment.
\r
384 * @note In the default implementation , SysTick timer is the source of time base. It is
\r
385 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
\r
386 * is called, the SysTick interrupt will be disabled and so Tick increment
\r
388 * @note This function is declared as __weak to be overwritten in case of other
\r
389 * implementations in user file.
\r
392 __weak void HAL_SuspendTick(void)
\r
394 /* Disable SysTick Interrupt */
\r
395 CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
\r
399 * @brief Resume the Tick increment.
\r
400 * @note In the default implementation , SysTick timer is the source of time base. It is
\r
401 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
\r
402 * is called, the SysTick interrupt will be enabled and so Tick increment
\r
404 * @note This function is declared as __weak to be overwritten in case of other
\r
405 * implementations in user file.
\r
408 __weak void HAL_ResumeTick(void)
\r
410 /* Enable SysTick Interrupt */
\r
411 SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
\r
415 * @brief Return the HAL revision
\r
416 * @retval version: 0xXYZR (8bits for each decimal, R for RC)
\r
418 uint32_t HAL_GetHalVersion(void)
\r
420 return __STM32L1xx_HAL_VERSION;
\r
424 * @brief Return the device revision identifier.
\r
425 * @retval Device revision identifier
\r
427 uint32_t HAL_GetREVID(void)
\r
429 return((DBGMCU->IDCODE) >> 16U);
\r
433 * @brief Return the device identifier.
\r
434 * @retval Device identifier
\r
436 uint32_t HAL_GetDEVID(void)
\r
438 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
\r
442 * @brief Return the first word of the unique device identifier (UID based on 96 bits)
\r
443 * @retval Device identifier 31:0 bits
\r
445 uint32_t HAL_GetUIDw0(void)
\r
447 return(READ_REG(*((uint32_t *)UID_BASE)));
\r
451 * @brief Return the second word of the unique device identifier (UID based on 96 bits)
\r
452 * @retval Device identifier 63:32 bits
\r
454 uint32_t HAL_GetUIDw1(void)
\r
456 return(READ_REG(*((uint32_t *)(UID_BASE + 0x4U))));
\r
460 * @brief Return the third word of the unique device identifier (UID based on 96 bits)
\r
461 * @retval Device identifier 95:64 bits
\r
463 uint32_t HAL_GetUIDw2(void)
\r
465 return(READ_REG(*((uint32_t *)(UID_BASE + 0x14U))));
\r
472 /** @defgroup HAL_Exported_Functions_Group3 DBGMCU Peripheral Control functions
\r
473 * @brief DBGMCU Peripheral Control functions
\r
476 ===============================================================================
\r
477 ##### DBGMCU Peripheral Control functions #####
\r
478 ===============================================================================
\r
479 [..] This section provides functions allowing to:
\r
480 (+) Enable/Disable Debug module during SLEEP mode
\r
481 (+) Enable/Disable Debug module during STOP mode
\r
482 (+) Enable/Disable Debug module during STANDBY mode
\r
489 * @brief Enable the Debug Module during SLEEP mode
\r
492 void HAL_DBGMCU_EnableDBGSleepMode(void)
\r
494 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
\r
498 * @brief Disable the Debug Module during SLEEP mode
\r
501 void HAL_DBGMCU_DisableDBGSleepMode(void)
\r
503 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
\r
507 * @brief Enable the Debug Module during STOP mode
\r
510 void HAL_DBGMCU_EnableDBGStopMode(void)
\r
512 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
\r
516 * @brief Disable the Debug Module during STOP mode
\r
519 void HAL_DBGMCU_DisableDBGStopMode(void)
\r
521 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
\r
525 * @brief Enable the Debug Module during STANDBY mode
\r
528 void HAL_DBGMCU_EnableDBGStandbyMode(void)
\r
530 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
\r
534 * @brief Disable the Debug Module during STANDBY mode
\r
537 void HAL_DBGMCU_DisableDBGStandbyMode(void)
\r
539 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
\r
550 #endif /* HAL_MODULE_ENABLED */
\r
559 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r