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 ******************************************************************************
9 * @attention
10 *
11 * Copyright (c) 2017 STMicroelectronics.
12 * All rights reserved.
13 *
14 * This software is licensed under terms that can be found in the LICENSE file
15 * in the root directory of this software component.
16 * If no LICENSE file comes with this software, it is provided AS-IS.
17 *
18 ******************************************************************************
19 @verbatim
20 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
23 [..]
24 The common HAL driver contains a set of generic and common APIs that can be
25 used by the PPP peripheral drivers and the user to start using the HAL.
26 [..]
27 The HAL contains two APIs categories:
28 (+) Common HAL APIs
29 (+) Services HAL APIs
30
31 @endverbatim
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
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 (0x05) /*!< [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 frequency.
359 * Value of @ref HAL_TickFreqTypeDef.
360 */
HAL_GetTickFreq(void)361 uint32_t HAL_GetTickFreq(void)
362 {
363 return uwTickFreq;
364 }
365
366 /**
367 * @brief This function provides minimum delay (in milliseconds) based
368 * on variable incremented.
369 * @note In the default implementation , SysTick timer is the source of time base.
370 * It is used to generate interrupts at regular time intervals where uwTick
371 * is incremented.
372 * @note This function is declared as __weak to be overwritten in case of other
373 * implementations in user file.
374 * @param Delay specifies the delay time length, in milliseconds.
375 * @retval None
376 */
HAL_Delay(uint32_t Delay)377 __weak void HAL_Delay(uint32_t Delay)
378 {
379 uint32_t tickstart = HAL_GetTick();
380 uint32_t wait = Delay;
381
382 /* Add a period to guaranty minimum wait */
383 if (wait < HAL_MAX_DELAY)
384 {
385 wait += (uint32_t)(uwTickFreq);
386 }
387
388 while((HAL_GetTick() - tickstart) < wait)
389 {
390 }
391 }
392
393 /**
394 * @brief Suspend the Tick increment.
395 * @note In the default implementation , SysTick timer is the source of time base. It is
396 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
397 * is called, the SysTick interrupt will be disabled and so Tick increment
398 * is suspended.
399 * @note This function is declared as __weak to be overwritten in case of other
400 * implementations in user file.
401 * @retval None
402 */
HAL_SuspendTick(void)403 __weak void HAL_SuspendTick(void)
404 {
405 /* Disable SysTick Interrupt */
406 CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
407 }
408
409 /**
410 * @brief Resume the Tick increment.
411 * @note In the default implementation , SysTick timer is the source of time base. It is
412 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
413 * is called, the SysTick interrupt will be enabled and so Tick increment
414 * is resumed.
415 * @note This function is declared as __weak to be overwritten in case of other
416 * implementations in user file.
417 * @retval None
418 */
HAL_ResumeTick(void)419 __weak void HAL_ResumeTick(void)
420 {
421 /* Enable SysTick Interrupt */
422 SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
423 }
424
425 /**
426 * @brief Return the HAL revision
427 * @retval version: 0xXYZR (8bits for each decimal, R for RC)
428 */
HAL_GetHalVersion(void)429 uint32_t HAL_GetHalVersion(void)
430 {
431 return __STM32L1xx_HAL_VERSION;
432 }
433
434 /**
435 * @brief Return the device revision identifier.
436 * @retval Device revision identifier
437 */
HAL_GetREVID(void)438 uint32_t HAL_GetREVID(void)
439 {
440 return((DBGMCU->IDCODE) >> 16U);
441 }
442
443 /**
444 * @brief Return the device identifier.
445 * @retval Device identifier
446 */
HAL_GetDEVID(void)447 uint32_t HAL_GetDEVID(void)
448 {
449 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
450 }
451
452 /**
453 * @brief Return the first word of the unique device identifier (UID based on 96 bits)
454 * @retval Device identifier 31:0 bits
455 */
HAL_GetUIDw0(void)456 uint32_t HAL_GetUIDw0(void)
457 {
458 return(READ_REG(*((uint32_t *)UID_BASE)));
459 }
460
461 /**
462 * @brief Return the second word of the unique device identifier (UID based on 96 bits)
463 * @retval Device identifier 63:32 bits
464 */
HAL_GetUIDw1(void)465 uint32_t HAL_GetUIDw1(void)
466 {
467 return(READ_REG(*((uint32_t *)(UID_BASE + 0x4U))));
468 }
469
470 /**
471 * @brief Return the third word of the unique device identifier (UID based on 96 bits)
472 * @retval Device identifier 95:64 bits
473 */
HAL_GetUIDw2(void)474 uint32_t HAL_GetUIDw2(void)
475 {
476 return(READ_REG(*((uint32_t *)(UID_BASE + 0x14U))));
477 }
478
479 /**
480 * @}
481 */
482
483 /** @defgroup HAL_Exported_Functions_Group3 DBGMCU Peripheral Control functions
484 * @brief DBGMCU Peripheral Control functions
485 *
486 @verbatim
487 ===============================================================================
488 ##### DBGMCU Peripheral Control functions #####
489 ===============================================================================
490 [..] This section provides functions allowing to:
491 (+) Enable/Disable Debug module during SLEEP mode
492 (+) Enable/Disable Debug module during STOP mode
493 (+) Enable/Disable Debug module during STANDBY mode
494
495 @endverbatim
496 * @{
497 */
498
499 /**
500 * @brief Enable the Debug Module during SLEEP mode
501 * @retval None
502 */
HAL_DBGMCU_EnableDBGSleepMode(void)503 void HAL_DBGMCU_EnableDBGSleepMode(void)
504 {
505 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
506 }
507
508 /**
509 * @brief Disable the Debug Module during SLEEP mode
510 * @retval None
511 */
HAL_DBGMCU_DisableDBGSleepMode(void)512 void HAL_DBGMCU_DisableDBGSleepMode(void)
513 {
514 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
515 }
516
517 /**
518 * @brief Enable the Debug Module during STOP mode
519 * @retval None
520 */
HAL_DBGMCU_EnableDBGStopMode(void)521 void HAL_DBGMCU_EnableDBGStopMode(void)
522 {
523 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
524 }
525
526 /**
527 * @brief Disable the Debug Module during STOP mode
528 * @retval None
529 */
HAL_DBGMCU_DisableDBGStopMode(void)530 void HAL_DBGMCU_DisableDBGStopMode(void)
531 {
532 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
533 }
534
535 /**
536 * @brief Enable the Debug Module during STANDBY mode
537 * @retval None
538 */
HAL_DBGMCU_EnableDBGStandbyMode(void)539 void HAL_DBGMCU_EnableDBGStandbyMode(void)
540 {
541 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
542 }
543
544 /**
545 * @brief Disable the Debug Module during STANDBY mode
546 * @retval None
547 */
HAL_DBGMCU_DisableDBGStandbyMode(void)548 void HAL_DBGMCU_DisableDBGStandbyMode(void)
549 {
550 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
551 }
552
553 /**
554 * @}
555 */
556
557 /**
558 * @}
559 */
560
561 #endif /* HAL_MODULE_ENABLED */
562 /**
563 * @}
564 */
565
566 /**
567 * @}
568 */
569
570