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