1 /**
2 ******************************************************************************
3 * @file stm32l0xx_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) 2016 STMicroelectronics</center></h2>
25 *
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 ******************************************************************************
49 */
50
51 /* Includes ------------------------------------------------------------------*/
52 #include "stm32l0xx_hal.h"
53
54 /** @addtogroup STM32L0xx_HAL_Driver
55 * @{
56 */
57
58 #ifdef HAL_MODULE_ENABLED
59
60 /** @addtogroup HAL
61 * @brief HAL module driver.
62 * @{
63 */
64
65 /** @addtogroup HAL_Exported_Constants
66 * @{
67 */
68
69
70 /** @defgroup SysTick System Tick
71 * @{
72 */
73 /**
74 * @brief uwTick_variable uwTick variable
75 */
76 __IO uint32_t uwTick;
77
78 /**
79 * @}
80 */
81
82 /** @defgroup HAL_Version HAL Version
83 * @{
84 */
85
86 /**
87 * @brief STM32L0xx HAL Driver version number
88 */
89 #define __STM32L0xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
90 #define __STM32L0xx_HAL_VERSION_SUB1 (0x08U) /*!< [23:16] sub1 version */
91 #define __STM32L0xx_HAL_VERSION_SUB2 (0x02U) /*!< [15:8] sub2 version */
92 #define __STM32L0xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
93 #define __STM32L0xx_HAL_VERSION ((__STM32L0xx_HAL_VERSION_MAIN << 24U)\
94 |(__STM32L0xx_HAL_VERSION_SUB1 << 16U)\
95 |(__STM32L0xx_HAL_VERSION_SUB2 << 8U )\
96 |(__STM32L0xx_HAL_VERSION_RC))
97
98 #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFFU)
99
100 /**
101 * @}
102 */
103
104 /**
105 * @}
106 */
107 /** @defgroup HAL_Private HAL Private
108 * @{
109 */
110
111
112 /**
113 * @}
114 */
115
116 /** @addtogroup HAL_Exported_Functions HAL Exported Functions
117 * @{
118 */
119
120 /** @addtogroup HAL_Exported_Functions_Group1
121 * @brief Initialization and de-initialization functions
122 *
123 @verbatim
124 ===============================================================================
125 ##### Initialization and de-initialization functions #####
126 ===============================================================================
127 [..] This section provides functions allowing to:
128 (+) Initializes the Flash interface, the NVIC allocation and initial clock
129 configuration. It initializes the source of time base also when timeout
130 is needed and the backup domain when enabled.
131 (+) de-Initializes common part of the HAL.
132 (+) Configure The time base source to have 1ms time base with a dedicated
133 Tick interrupt priority.
134 (++) Systick timer is used by default as source of time base, but user
135 can eventually implement his proper time base source (a general purpose
136 timer for example or other time source), keeping in mind that Time base
137 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
138 handled in milliseconds basis.
139 (++) Time base configuration function (HAL_InitTick ()) is called automatically
140 at the beginning of the program after reset by HAL_Init() or at any time
141 when clock is configured, by HAL_RCC_ClockConfig().
142 (++) Source of time base is configured to generate interrupts at regular
143 time intervals. Care must be taken if HAL_Delay() is called from a
144 peripheral ISR process, the Tick interrupt line must have higher priority
145 (numerically lower) than the peripheral interrupt. Otherwise the caller
146 ISR process will be blocked.
147 (++) functions affecting time base configurations are declared as __weak
148 to make override possible in case of other implementations in user file.
149
150 @endverbatim
151 * @{
152 */
153
154 /**
155 * @brief This function configures the Flash prefetch, Flash preread and Buffer cache,
156 * Configures time base source, NVIC and Low level hardware
157 * @note This function is called at the beginning of program after reset and before
158 * the clock configuration
159 * @note The time base configuration is based on MSI clock when exiting from Reset.
160 * Once done, time base tick start incrementing.
161 * In the default implementation,Systick is used as source of time base.
162 * the tick variable is incremented each 1ms in its ISR.
163 * @retval HAL status
164 */
HAL_Init(void)165 HAL_StatusTypeDef HAL_Init(void)
166 {
167 /* Configure Buffer cache, Flash prefetch, Flash preread */
168 #if (BUFFER_CACHE_DISABLE != 0)
169 __HAL_FLASH_BUFFER_CACHE_DISABLE();
170 #endif /* BUFFER_CACHE_DISABLE */
171
172 #if (PREREAD_ENABLE != 0)
173 __HAL_FLASH_PREREAD_BUFFER_ENABLE();
174 #endif /* PREREAD_ENABLE */
175
176 #if (PREFETCH_ENABLE != 0)
177 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
178 #endif /* PREFETCH_ENABLE */
179
180 /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
181
182 HAL_InitTick(TICK_INT_PRIORITY);
183
184 /* Init the low level hardware */
185 HAL_MspInit();
186
187 /* Return function status */
188 return HAL_OK;
189 }
190
191 /**
192 * @brief This function de-Initializes common part of the HAL and stops the source
193 * of time base.
194 * @note This function is optional.
195 * @retval HAL status
196 */
HAL_DeInit(void)197 HAL_StatusTypeDef HAL_DeInit(void)
198 {
199 /* Reset of all peripherals */
200 __HAL_RCC_APB1_FORCE_RESET();
201 __HAL_RCC_APB1_RELEASE_RESET();
202
203 __HAL_RCC_APB2_FORCE_RESET();
204 __HAL_RCC_APB2_RELEASE_RESET();
205
206 __HAL_RCC_AHB_FORCE_RESET();
207 __HAL_RCC_AHB_RELEASE_RESET();
208
209 __HAL_RCC_IOP_FORCE_RESET();
210 __HAL_RCC_IOP_RELEASE_RESET();
211
212 /* De-Init the low level hardware */
213 HAL_MspDeInit();
214
215 /* Return function status */
216 return HAL_OK;
217 }
218
219 /**
220 * @brief Initializes the MSP.
221 * @retval None
222 */
HAL_MspInit(void)223 __weak void HAL_MspInit(void)
224 {
225 /* NOTE : This function Should not be modified, when the callback is needed,
226 the HAL_MspInit could be implemented in the user file
227 */
228 }
229
230 /**
231 * @brief DeInitializes the MSP.
232 * @retval None
233 */
HAL_MspDeInit(void)234 __weak void HAL_MspDeInit(void)
235 {
236 /* NOTE : This function Should not be modified, when the callback is needed,
237 the HAL_MspDeInit could be implemented in the user file
238 */
239 }
240
241
242 /**
243 * @brief This function configures the source of the time base.
244 * The time source is configured to have 1ms time base with a dedicated
245 * Tick interrupt priority.
246 * @note This function is called automatically at the beginning of program after
247 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
248 * @note In the default implementation, SysTick timer is the source of time base.
249 * It is used to generate interrupts at regular time intervals.
250 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
251 * The the SysTick interrupt must have higher priority (numerically lower)
252 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
253 * The function is declared as __Weak to be overwritten in case of other
254 * implementation in user file.
255 * @param TickPriority: Tick interrupt priority.
256 * @retval HAL status
257 */
HAL_InitTick(uint32_t TickPriority)258 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
259 {
260 /*Configure the SysTick to have interrupt in 1ms time basis*/
261 HAL_SYSTICK_Config(SystemCoreClock/1000U);
262
263 /*Configure the SysTick IRQ priority */
264 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
265
266 /* Return function status */
267 return HAL_OK;
268 }
269
270 /**
271 * @}
272 */
273
274 /** @addtogroup HAL_Exported_Functions_Group2
275 * @brief Peripheral Control functions
276 *
277 @verbatim
278 ===============================================================================
279 ##### HAL Control functions #####
280 ===============================================================================
281 [..] This section provides functions allowing to:
282 (+) Provide a tick value in millisecond
283 (+) Provide a blocking delay in millisecond
284 (+) Suspend the time base source interrupt
285 (+) Resume the time base source interrupt
286 (+) Get the HAL API driver version
287 (+) Get the device identifier
288 (+) Get the device revision identifier
289 (+) Configure low power mode behavior when the MCU is in Debug mode
290 (+) Manage the VEREFINT feature (activation, lock, output selection)
291
292 @endverbatim
293 * @{
294 */
295
296 /**
297 * @brief This function is called to increment a global variable "uwTick"
298 * used as application time base.
299 * @note In the default implementation, this variable is incremented each 1ms
300 * in Systick ISR.
301 * @note This function is declared as __weak to be overwritten in case of other
302 * implementations in user file.
303 * @retval None
304 */
HAL_IncTick(void)305 __weak void HAL_IncTick(void)
306 {
307 uwTick++;
308 }
309
310 /**
311 * @brief Provides a tick value in millisecond.
312 * @note This function is declared as __weak to be overwritten in case of other
313 * implementations in user file.
314 * @retval tick value
315 */
HAL_GetTick(void)316 __weak uint32_t HAL_GetTick(void)
317 {
318 return uwTick;
319 }
320
321 /**
322 * @brief This function provides accurate delay (in ms) based on a variable incremented.
323 * @note In the default implementation , SysTick timer is the source of time base.
324 * It is used to generate interrupts at regular time intervals where uwTick
325 * is incremented.
326 * @note ThiS function is declared as __weak to be overwritten in case of other
327 * implementations in user file.
328 * @param Delay: specifies the delay time length, in milliseconds.
329 * @retval None
330 */
HAL_Delay(__IO uint32_t Delay)331 __weak void HAL_Delay(__IO uint32_t Delay)
332 {
333 uint32_t tickstart = 0U;
334 tickstart = HAL_GetTick();
335 while((HAL_GetTick() - tickstart) < Delay)
336 {
337 }
338 }
339
340 /**
341 * @brief Suspends the Tick increment.
342 * @note In the default implementation , SysTick timer is the source of time base. It is
343 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
344 * is called, the the SysTick interrupt will be disabled and so Tick increment
345 * is suspended.
346 * @note This function is declared as __weak to be overwritten in case of other
347 * implementations in user file.
348 * @retval None
349 */
HAL_SuspendTick(void)350 __weak void HAL_SuspendTick(void)
351 {
352 /* Disable SysTick Interrupt */
353 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
354 }
355
356 /**
357 * @brief Resumes the Tick increment.
358 * @note In the default implementation , SysTick timer is the source of time base. It is
359 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
360 * is called, the the SysTick interrupt will be enabled and so Tick increment
361 * is resumed.
362 * @note This function is declared as __weak to be overwritten in case of other
363 * implementations in user file.
364 * @retval None
365 */
HAL_ResumeTick(void)366 __weak void HAL_ResumeTick(void)
367 {
368 /* Enable SysTick Interrupt */
369 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
370 }
371
372 /**
373 * @brief Returns the HAL revision
374 * @retval version: 0xXYZR (8bits for each decimal, R for RC)
375 */
HAL_GetHalVersion(void)376 uint32_t HAL_GetHalVersion(void)
377 {
378 return __STM32L0xx_HAL_VERSION;
379 }
380
381 /**
382 * @brief Returns the device revision identifier.
383 * @retval Device revision identifier
384 */
HAL_GetREVID(void)385 uint32_t HAL_GetREVID(void)
386 {
387 return((DBGMCU->IDCODE) >> 16U);
388 }
389
390 /**
391 * @brief Returns the device identifier.
392 * @retval Device identifier
393 */
HAL_GetDEVID(void)394 uint32_t HAL_GetDEVID(void)
395 {
396 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
397 }
398
399 /**
400 * @brief Enables the Debug Module during SLEEP mode
401 * @retval None
402 */
HAL_DBGMCU_EnableDBGSleepMode(void)403 void HAL_DBGMCU_EnableDBGSleepMode(void)
404 {
405 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
406 }
407
408 /**
409 * @brief Disables the Debug Module during SLEEP mode
410 * @retval None
411 */
HAL_DBGMCU_DisableDBGSleepMode(void)412 void HAL_DBGMCU_DisableDBGSleepMode(void)
413 {
414 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
415 }
416
417 /**
418 * @brief Enables the Debug Module during STOP mode
419 * @retval None
420 */
HAL_DBGMCU_EnableDBGStopMode(void)421 void HAL_DBGMCU_EnableDBGStopMode(void)
422 {
423 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
424 }
425
426 /**
427 * @brief Disables the Debug Module during STOP mode
428 * @retval None
429 */
HAL_DBGMCU_DisableDBGStopMode(void)430 void HAL_DBGMCU_DisableDBGStopMode(void)
431 {
432 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
433 }
434
435 /**
436 * @brief Enables the Debug Module during STANDBY mode
437 * @retval None
438 */
HAL_DBGMCU_EnableDBGStandbyMode(void)439 void HAL_DBGMCU_EnableDBGStandbyMode(void)
440 {
441 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
442 }
443
444 /**
445 * @brief Disables the Debug Module during STANDBY mode
446 * @retval None
447 */
HAL_DBGMCU_DisableDBGStandbyMode(void)448 void HAL_DBGMCU_DisableDBGStandbyMode(void)
449 {
450 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
451 }
452
453 /**
454 * @brief Enable low power mode behavior when the MCU is in Debug mode.
455 * @param Periph: specifies the low power mode.
456 * This parameter can be any combination of the following values:
457 * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
458 * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
459 * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
460 * @retval None
461 */
HAL_DBGMCU_DBG_EnableLowPowerConfig(uint32_t Periph)462 void HAL_DBGMCU_DBG_EnableLowPowerConfig(uint32_t Periph)
463 {
464 /* Check the parameters */
465 assert_param(IS_DBGMCU_PERIPH(Periph));
466
467 DBGMCU->CR |= Periph;
468
469 }
470 /**
471 * @brief Disable low power mode behavior when the MCU is in Debug mode.
472 * @param Periph: specifies the low power mode.
473 * This parameter can be any combination of the following values:
474 * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
475 * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
476 * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
477 * @retval None
478 */
HAL_DBGMCU_DBG_DisableLowPowerConfig(uint32_t Periph)479 void HAL_DBGMCU_DBG_DisableLowPowerConfig(uint32_t Periph)
480 {
481 /* Check the parameters */
482 assert_param(IS_DBGMCU_PERIPH(Periph));
483 {
484 DBGMCU->CR &= ~Periph;
485 }
486 }
487
488 /**
489 * @brief Returns the boot mode as configured by user.
490 * @retval The boot mode as configured by user. The returned value can be one
491 * of the following values:
492 * - 0x00000000 : Boot is configured in Main Flash memory
493 * - 0x00000100 : Boot is configured in System Flash memory
494 * - 0x00000300 : Boot is configured in Embedded SRAM memory
495 */
HAL_SYSCFG_GetBootMode(void)496 uint32_t HAL_SYSCFG_GetBootMode(void)
497 {
498 return (SYSCFG->CFGR1 & SYSCFG_CFGR1_BOOT_MODE);
499 }
500
501 /**
502 * @brief Selects the output of internal reference voltage (VREFINT).
503 * The VREFINT output can be routed to(PB0) or
504 * (PB1) or both.
505 * @param SYSCFG_Vrefint_OUTPUT: new state of the Vrefint output.
506 * This parameter can be one of the following values:
507 * @arg SYSCFG_VREFINT_OUT_NONE
508 * @arg SYSCFG_VREFINT_OUT_PB0
509 * @arg SYSCFG_VREFINT_OUT_PB1
510 * @arg SYSCFG_VREFINT_OUT_PB0_PB1
511 * @retval None
512 */
HAL_SYSCFG_VREFINT_OutputSelect(uint32_t SYSCFG_Vrefint_OUTPUT)513 void HAL_SYSCFG_VREFINT_OutputSelect(uint32_t SYSCFG_Vrefint_OUTPUT)
514 {
515 /* Check the parameters */
516 assert_param(IS_SYSCFG_VREFINT_OUT_SELECT(SYSCFG_Vrefint_OUTPUT));
517
518 /* Set the output Vrefint pin */
519 SYSCFG->CFGR3 &= ~(SYSCFG_CFGR3_VREF_OUT);
520 SYSCFG->CFGR3 |= (uint32_t)(SYSCFG_Vrefint_OUTPUT);
521 }
522
523 /**
524 * @brief Lock the SYSCFG VREF register values
525 * @retval None
526 */
HAL_SYSCFG_Enable_Lock_VREFINT(void)527 void HAL_SYSCFG_Enable_Lock_VREFINT(void)
528 {
529 /* Enable the LOCK by setting REF_LOCK bit in the CFGR3 register */
530 SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
531 }
532
533 /**
534 * @brief Unlock the overall SYSCFG VREF register values
535 * @retval None
536 */
HAL_SYSCFG_Disable_Lock_VREFINT(void)537 void HAL_SYSCFG_Disable_Lock_VREFINT(void)
538 {
539 /* Disable the LOCK by setting REF_LOCK bit in the CFGR3 register */
540 CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
541 }
542
543 /**
544 * @}
545 */
546
547 /**
548 * @}
549 */
550
551 /**
552 * @}
553 */
554 #endif /* HAL_MODULE_ENABLED */
555 /**
556 * @}
557 */
558
559 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
560
561