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>&copy; COPYRIGHT(c) 2017 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 "stm32l1xx_hal.h"
53 
54 /** @addtogroup STM32L1xx_HAL_Driver
55   * @{
56   */
57 
58 /** @defgroup HAL HAL
59   * @brief HAL module driver.
60   * @{
61   */
62 
63 #ifdef HAL_MODULE_ENABLED
64 
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 
68 /** @defgroup HAL_Private_Defines HAL Private Defines
69   * @{
70   */
71 
72 /**
73  * @brief STM32L1xx HAL Driver version number
74    */
75 #define __STM32L1xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
76 #define __STM32L1xx_HAL_VERSION_SUB1   (0x03) /*!< [23:16] sub1 version */
77 #define __STM32L1xx_HAL_VERSION_SUB2   (0x01) /*!< [15:8]  sub2 version */
78 #define __STM32L1xx_HAL_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
79 #define __STM32L1xx_HAL_VERSION         ((__STM32L1xx_HAL_VERSION_MAIN << 24)\
80                                         |(__STM32L1xx_HAL_VERSION_SUB1 << 16)\
81                                         |(__STM32L1xx_HAL_VERSION_SUB2 << 8 )\
82                                         |(__STM32L1xx_HAL_VERSION_RC))
83 
84 #define IDCODE_DEVID_MASK    (0x00000FFFU)
85 
86 /**
87   * @}
88   */
89 
90 /* Private macro -------------------------------------------------------------*/
91 /* Private variables ---------------------------------------------------------*/
92 
93 /** @defgroup HAL_Private_Variables HAL Private Variables
94   * @{
95   */
96 
97 __IO uint32_t uwTick;
98 
99 /**
100   * @}
101   */
102 
103 /* Private function prototypes -----------------------------------------------*/
104 /* Private functions ---------------------------------------------------------*/
105 
106 /** @defgroup HAL_Exported_Functions HAL Exported Functions
107   * @{
108   */
109 
110 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
111  *  @brief    Initialization and de-initialization functions
112  *
113 @verbatim
114  ===============================================================================
115               ##### Initialization and de-initialization functions #####
116  ===============================================================================
117    [..]  This section provides functions allowing to:
118       (+) Initializes the Flash interface, the NVIC allocation and initial clock
119           configuration. It initializes the source of time base also when timeout
120           is needed and the backup domain when enabled.
121       (+) de-Initializes common part of the HAL.
122       (+) Configure The time base source to have 1ms time base with a dedicated
123           Tick interrupt priority.
124         (++) Systick timer is used by default as source of time base, but user
125              can eventually implement his proper time base source (a general purpose
126              timer for example or other time source), keeping in mind that Time base
127              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
128              handled in milliseconds basis.
129         (++) Time base configuration function (HAL_InitTick ()) is called automatically
130              at the beginning of the program after reset by HAL_Init() or at any time
131              when clock is configured, by HAL_RCC_ClockConfig().
132         (++) Source of time base is configured  to generate interrupts at regular
133              time intervals. Care must be taken if HAL_Delay() is called from a
134              peripheral ISR process, the Tick interrupt line must have higher priority
135             (numerically lower) than the peripheral interrupt. Otherwise the caller
136             ISR process will be blocked.
137        (++) functions affecting time base configurations are declared as __Weak
138              to make  override possible  in case of other  implementations in user file.
139 
140 @endverbatim
141   * @{
142   */
143 
144 /**
145   * @brief This function configures the Flash prefetch,
146   *        Configures time base source, NVIC and Low level hardware
147   * @note This function is called at the beginning of program after reset and before
148   *       the clock configuration
149   * @note The time base configuration is based on MSI clock when exiting from Reset.
150   *       Once done, time base tick start incrementing.
151   *        In the default implementation,Systick is used as source of time base.
152   *        the tick variable is incremented each 1ms in its ISR.
153   * @retval HAL status
154   */
HAL_Init(void)155 HAL_StatusTypeDef HAL_Init(void)
156 {
157   /* Configure Flash prefetch */
158 #if (PREFETCH_ENABLE != 0)
159   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
160 #endif /* PREFETCH_ENABLE */
161 
162   /* Set Interrupt Group Priority */
163   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
164 
165   /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
166   HAL_InitTick(TICK_INT_PRIORITY);
167 
168   /* Init the low level hardware */
169   HAL_MspInit();
170 
171   /* Return function status */
172   return HAL_OK;
173 }
174 
175 /**
176   * @brief This function de-Initializes common part of the HAL and stops the source
177   *        of time base.
178   * @note This function is optional.
179   * @retval HAL status
180   */
HAL_DeInit(void)181 HAL_StatusTypeDef HAL_DeInit(void)
182 {
183   /* Reset of all peripherals */
184   __HAL_RCC_APB1_FORCE_RESET();
185   __HAL_RCC_APB1_RELEASE_RESET();
186 
187   __HAL_RCC_APB2_FORCE_RESET();
188   __HAL_RCC_APB2_RELEASE_RESET();
189 
190   __HAL_RCC_AHB_FORCE_RESET();
191   __HAL_RCC_AHB_RELEASE_RESET();
192 
193   /* De-Init the low level hardware */
194   HAL_MspDeInit();
195 
196   /* Return function status */
197   return HAL_OK;
198 }
199 
200 /**
201   * @brief  Initializes the MSP.
202   * @retval None
203   */
HAL_MspInit(void)204 __weak void HAL_MspInit(void)
205 {
206   /* NOTE : This function Should not be modified, when the callback is needed,
207             the HAL_MspInit could be implemented in the user file
208    */
209 }
210 
211 /**
212   * @brief  DeInitializes the MSP.
213   * @retval None
214   */
HAL_MspDeInit(void)215 __weak void HAL_MspDeInit(void)
216 {
217   /* NOTE : This function Should not be modified, when the callback is needed,
218             the HAL_MspDeInit could be implemented in the user file
219    */
220 }
221 
222 /**
223   * @brief This function configures the source of the time base.
224   *        The time source is configured  to have 1ms time base with a dedicated
225   *        Tick interrupt priority.
226   * @note This function is called  automatically at the beginning of program after
227   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
228   * @note In the default implementation, SysTick timer is the source of time base.
229   *       It is used to generate interrupts at regular time intervals.
230   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
231   *       The the SysTick interrupt must have higher priority (numerically lower)
232   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
233   *       The function is declared as __Weak  to be overwritten  in case of other
234   *       implementation  in user file.
235   * @param TickPriority: Tick interrupt priority.
236   * @retval HAL status
237   */
HAL_InitTick(uint32_t TickPriority)238 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
239 {
240   /*Configure the SysTick to have interrupt in 1ms time basis*/
241   HAL_SYSTICK_Config(SystemCoreClock /1000);
242 
243   /*Configure the SysTick IRQ priority */
244   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
245 
246    /* Return function status */
247   return HAL_OK;
248 }
249 
250 /**
251   * @}
252   */
253 
254 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
255  *  @brief    HAL Control functions
256  *
257 @verbatim
258  ===============================================================================
259                       ##### HAL Control functions #####
260  ===============================================================================
261     [..]  This section provides functions allowing to:
262       (+) Provide a tick value in millisecond
263       (+) Provide a blocking delay in millisecond
264       (+) Suspend the time base source interrupt
265       (+) Resume the time base source interrupt
266       (+) Get the HAL API driver version
267       (+) Get the device identifier
268       (+) Get the device revision identifier
269       (+) Enable/Disable Debug module during Sleep mode
270       (+) Enable/Disable Debug module during STOP mode
271       (+) Enable/Disable Debug module during STANDBY mode
272 
273 @endverbatim
274   * @{
275   */
276 
277 /**
278   * @brief This function is called to increment  a global variable "uwTick"
279   *        used as application time base.
280   * @note In the default implementation, this variable is incremented each 1ms
281   *       in Systick ISR.
282  * @note This function is declared as __weak to be overwritten in case of other
283   *      implementations in user file.
284   * @retval None
285   */
HAL_IncTick(void)286 __weak void HAL_IncTick(void)
287 {
288   uwTick++;
289 }
290 
291 /**
292   * @brief Provides a tick value in millisecond.
293   * @note This function is declared as __weak to be overwritten in case of other
294   *       implementations in user file.
295   * @retval tick value
296   */
HAL_GetTick(void)297 __weak uint32_t HAL_GetTick(void)
298 {
299   return uwTick;
300 }
301 
302 /**
303   * @brief This function provides accurate delay (in milliseconds) based
304   *        on variable incremented.
305   * @note In the default implementation , SysTick timer is the source of time base.
306   *       It is used to generate interrupts at regular time intervals where uwTick
307   *       is incremented.
308   * @note ThiS function is declared as __weak to be overwritten in case of other
309   *       implementations in user file.
310   * @param Delay: specifies the delay time length, in milliseconds.
311   * @retval None
312   */
HAL_Delay(__IO uint32_t Delay)313 __weak void HAL_Delay(__IO uint32_t Delay)
314 {
315   uint32_t tickstart = 0;
316   tickstart = HAL_GetTick();
317   while((HAL_GetTick() - tickstart) < Delay)
318   {
319   }
320 }
321 
322 /**
323   * @brief Suspend Tick increment.
324   * @note In the default implementation , SysTick timer is the source of time base. It is
325   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
326   *       is called, the the SysTick interrupt will be disabled and so Tick increment
327   *       is suspended.
328   * @note This function is declared as __weak to be overwritten in case of other
329   *       implementations in user file.
330   * @retval None
331   */
HAL_SuspendTick(void)332 __weak void HAL_SuspendTick(void)
333 {
334   /* Disable SysTick Interrupt */
335   CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
336 }
337 
338 /**
339   * @brief Resume Tick increment.
340   * @note In the default implementation , SysTick timer is the source of time base. It is
341   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
342   *       is called, the the SysTick interrupt will be enabled and so Tick increment
343   *       is resumed.
344   * @note This function is declared as __weak to be overwritten in case of other
345   *       implementations in user file.
346   * @retval None
347   */
HAL_ResumeTick(void)348 __weak void HAL_ResumeTick(void)
349 {
350   /* Enable SysTick Interrupt */
351   SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
352 }
353 
354 /**
355   * @brief Returns the HAL revision
356   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
357   */
HAL_GetHalVersion(void)358 uint32_t HAL_GetHalVersion(void)
359 {
360  return __STM32L1xx_HAL_VERSION;
361 }
362 
363 /**
364   * @brief Returns the device revision identifier.
365   * @retval Device revision identifier
366   */
HAL_GetREVID(void)367 uint32_t HAL_GetREVID(void)
368 {
369   return((DBGMCU->IDCODE) >> 16);
370 }
371 
372 /**
373   * @brief  Returns the device identifier.
374   * @retval Device identifier
375   */
HAL_GetDEVID(void)376 uint32_t HAL_GetDEVID(void)
377 {
378    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
379 }
380 
381 /**
382   * @brief  Enable the Debug Module during SLEEP mode
383   * @retval None
384   */
HAL_DBGMCU_EnableDBGSleepMode(void)385 void HAL_DBGMCU_EnableDBGSleepMode(void)
386 {
387   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
388 }
389 
390 /**
391   * @brief  Disable the Debug Module during SLEEP mode
392   * @retval None
393   */
HAL_DBGMCU_DisableDBGSleepMode(void)394 void HAL_DBGMCU_DisableDBGSleepMode(void)
395 {
396   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
397 }
398 
399 /**
400   * @brief  Enable the Debug Module during STOP mode
401   * @retval None
402   */
HAL_DBGMCU_EnableDBGStopMode(void)403 void HAL_DBGMCU_EnableDBGStopMode(void)
404 {
405   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
406 }
407 
408 /**
409   * @brief  Disable the Debug Module during STOP mode
410   * @retval None
411   */
HAL_DBGMCU_DisableDBGStopMode(void)412 void HAL_DBGMCU_DisableDBGStopMode(void)
413 {
414   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
415 }
416 
417 /**
418   * @brief  Enable the Debug Module during STANDBY mode
419   * @retval None
420   */
HAL_DBGMCU_EnableDBGStandbyMode(void)421 void HAL_DBGMCU_EnableDBGStandbyMode(void)
422 {
423   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
424 }
425 
426 /**
427   * @brief  Disable the Debug Module during STANDBY mode
428   * @retval None
429   */
HAL_DBGMCU_DisableDBGStandbyMode(void)430 void HAL_DBGMCU_DisableDBGStandbyMode(void)
431 {
432   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
433 }
434 
435 /**
436   * @}
437   */
438 
439 /**
440   * @}
441   */
442 
443 #endif /* HAL_MODULE_ENABLED */
444 /**
445   * @}
446   */
447 
448 /**
449   * @}
450   */
451 
452 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
453