1 /**
2 ******************************************************************************
3 * @file stm32f0xx_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 (+) HAL Initialization and de-initialization functions
18 (+) HAL Control functions
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 "stm32f0xx_hal.h"
53
54 /** @addtogroup STM32F0xx_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 /** @defgroup HAL_Private_Constants HAL Private Constants
68 * @{
69 */
70 /**
71 * @brief STM32F0xx HAL Driver version number V1.7.2
72 */
73 #define __STM32F0xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
74 #define __STM32F0xx_HAL_VERSION_SUB1 (0x07) /*!< [23:16] sub1 version */
75 #define __STM32F0xx_HAL_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */
76 #define __STM32F0xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
77 #define __STM32F0xx_HAL_VERSION ((__STM32F0xx_HAL_VERSION_MAIN << 24U)\
78 |(__STM32F0xx_HAL_VERSION_SUB1 << 16U)\
79 |(__STM32F0xx_HAL_VERSION_SUB2 << 8U )\
80 |(__STM32F0xx_HAL_VERSION_RC))
81
82 #define IDCODE_DEVID_MASK (0x00000FFFU)
83 /**
84 * @}
85 */
86
87 /* Private macro -------------------------------------------------------------*/
88 /** @defgroup HAL_Private_Macros HAL Private Macros
89 * @{
90 */
91 /**
92 * @}
93 */
94
95 /* Private variables ---------------------------------------------------------*/
96 /** @defgroup HAL_Private_Variables HAL Private Variables
97 * @{
98 */
99 __IO uint32_t uwTick;
100 /**
101 * @}
102 */
103 /* Private function prototypes -----------------------------------------------*/
104 /* Exported 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 HSI 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 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
163
164 HAL_InitTick(TICK_INT_PRIORITY);
165
166 /* Init the low level hardware */
167 HAL_MspInit();
168
169 /* Return function status */
170 return HAL_OK;
171 }
172
173 /**
174 * @brief This function de-Initializes common part of the HAL and stops the source
175 * of time base.
176 * @note This function is optional.
177 * @retval HAL status
178 */
HAL_DeInit(void)179 HAL_StatusTypeDef HAL_DeInit(void)
180 {
181 /* Reset of all peripherals */
182 __HAL_RCC_APB1_FORCE_RESET();
183 __HAL_RCC_APB1_RELEASE_RESET();
184
185 __HAL_RCC_APB2_FORCE_RESET();
186 __HAL_RCC_APB2_RELEASE_RESET();
187
188 __HAL_RCC_AHB_FORCE_RESET();
189 __HAL_RCC_AHB_RELEASE_RESET();
190
191 /* De-Init the low level hardware */
192 HAL_MspDeInit();
193
194 /* Return function status */
195 return HAL_OK;
196 }
197
198 /**
199 * @brief Initializes the MSP.
200 * @retval None
201 */
HAL_MspInit(void)202 __weak void HAL_MspInit(void)
203 {
204 /* NOTE : This function Should not be modified, when the callback is needed,
205 the HAL_MspInit could be implemented in the user file
206 */
207 }
208
209 /**
210 * @brief DeInitializes the MSP.
211 * @retval None
212 */
HAL_MspDeInit(void)213 __weak void HAL_MspDeInit(void)
214 {
215 /* NOTE : This function Should not be modified, when the callback is needed,
216 the HAL_MspDeInit could be implemented in the user file
217 */
218 }
219
220 /**
221 * @brief This function configures the source of the time base.
222 * The time source is configured to have 1ms time base with a dedicated
223 * Tick interrupt priority.
224 * @note This function is called automatically at the beginning of program after
225 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
226 * @note In the default implementation, SysTick timer is the source of time base.
227 * It is used to generate interrupts at regular time intervals.
228 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
229 * The the SysTick interrupt must have higher priority (numerically lower)
230 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
231 * The function is declared as __Weak to be overwritten in case of other
232 * implementation in user file.
233 * @param TickPriority Tick interrupt priority.
234 * @retval HAL status
235 */
HAL_InitTick(uint32_t TickPriority)236 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
237 {
238 /*Configure the SysTick to have interrupt in 1ms time basis*/
239 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000U);
240
241 /*Configure the SysTick IRQ priority */
242 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
243
244 /* Return function status */
245 return HAL_OK;
246 }
247
248 /**
249 * @}
250 */
251
252 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
253 * @brief HAL Control functions
254 *
255 @verbatim
256 ===============================================================================
257 ##### HAL Control functions #####
258 ===============================================================================
259 [..] This section provides functions allowing to:
260 (+) Provide a tick value in millisecond
261 (+) Provide a blocking delay in millisecond
262 (+) Suspend the time base source interrupt
263 (+) Resume the time base source interrupt
264 (+) Get the HAL API driver version
265 (+) Get the device identifier
266 (+) Get the device revision identifier
267 (+) Enable/Disable Debug module during Sleep mode
268 (+) Enable/Disable Debug module during STOP mode
269 (+) Enable/Disable Debug module during STANDBY mode
270
271 @endverbatim
272 * @{
273 */
274
275 /**
276 * @brief This function is called to increment a global variable "uwTick"
277 * used as application time base.
278 * @note In the default implementation, this variable is incremented each 1ms
279 * in Systick ISR.
280 * @note This function is declared as __weak to be overwritten in case of other
281 * implementations in user file.
282 * @retval None
283 */
HAL_IncTick(void)284 __weak void HAL_IncTick(void)
285 {
286 uwTick++;
287 }
288
289 /**
290 * @brief Provides a tick value in millisecond.
291 * @note This function is declared as __weak to be overwritten in case of other
292 * implementations in user file.
293 * @retval tick value
294 */
HAL_GetTick(void)295 __weak uint32_t HAL_GetTick(void)
296 {
297 return uwTick;
298 }
299
300 /**
301 * @brief This function provides accurate delay (in milliseconds) based
302 * on variable incremented.
303 * @note In the default implementation , SysTick timer is the source of time base.
304 * It is used to generate interrupts at regular time intervals where uwTick
305 * is incremented.
306 * @note ThiS function is declared as __weak to be overwritten in case of other
307 * implementations in user file.
308 * @param Delay specifies the delay time length, in milliseconds.
309 * @retval None
310 */
HAL_Delay(__IO uint32_t Delay)311 __weak void HAL_Delay(__IO uint32_t Delay)
312 {
313 uint32_t tickstart = HAL_GetTick();
314 uint32_t wait = Delay;
315
316 /* Add a period to guarantee minimum wait */
317 if (wait < HAL_MAX_DELAY)
318 {
319 wait++;
320 }
321
322 while((HAL_GetTick() - tickstart) < wait)
323 {
324 }
325 }
326
327 /**
328 * @brief Suspend Tick increment.
329 * @note In the default implementation , SysTick timer is the source of time base. It is
330 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
331 * is called, the the SysTick interrupt will be disabled and so Tick increment
332 * is suspended.
333 * @note This function is declared as __weak to be overwritten in case of other
334 * implementations in user file.
335 * @retval None
336 */
HAL_SuspendTick(void)337 __weak void HAL_SuspendTick(void)
338
339 {
340 /* Disable SysTick Interrupt */
341 CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
342 }
343
344 /**
345 * @brief Resume Tick increment.
346 * @note In the default implementation , SysTick timer is the source of time base. It is
347 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
348 * is called, the the SysTick interrupt will be enabled and so Tick increment
349 * is resumed.
350 * @note This function is declared as __weak to be overwritten in case of other
351 * implementations in user file.
352 * @retval None
353 */
HAL_ResumeTick(void)354 __weak void HAL_ResumeTick(void)
355 {
356 /* Enable SysTick Interrupt */
357 SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
358 }
359
360 /**
361 * @brief This method returns the HAL revision
362 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
363 */
HAL_GetHalVersion(void)364 uint32_t HAL_GetHalVersion(void)
365 {
366 return __STM32F0xx_HAL_VERSION;
367 }
368
369 /**
370 * @brief Returns the device revision identifier.
371 * @retval Device revision identifier
372 */
HAL_GetREVID(void)373 uint32_t HAL_GetREVID(void)
374 {
375 return((DBGMCU->IDCODE) >> 16U);
376 }
377
378 /**
379 * @brief Returns the device identifier.
380 * @retval Device identifier
381 */
HAL_GetDEVID(void)382 uint32_t HAL_GetDEVID(void)
383 {
384 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
385 }
386
387 /**
388 * @brief Returns first word of the unique device identifier (UID based on 96 bits)
389 * @retval Device identifier
390 */
HAL_GetUIDw0(void)391 uint32_t HAL_GetUIDw0(void)
392 {
393 return(READ_REG(*((uint32_t *)UID_BASE)));
394 }
395
396 /**
397 * @brief Returns second word of the unique device identifier (UID based on 96 bits)
398 * @retval Device identifier
399 */
HAL_GetUIDw1(void)400 uint32_t HAL_GetUIDw1(void)
401 {
402 return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
403 }
404
405 /**
406 * @brief Returns third word of the unique device identifier (UID based on 96 bits)
407 * @retval Device identifier
408 */
HAL_GetUIDw2(void)409 uint32_t HAL_GetUIDw2(void)
410 {
411 return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
412 }
413
414 /**
415 * @brief Enable the Debug Module during STOP mode
416 * @retval None
417 */
HAL_DBGMCU_EnableDBGStopMode(void)418 void HAL_DBGMCU_EnableDBGStopMode(void)
419 {
420 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
421 }
422
423 /**
424 * @brief Disable the Debug Module during STOP mode
425 * @retval None
426 */
HAL_DBGMCU_DisableDBGStopMode(void)427 void HAL_DBGMCU_DisableDBGStopMode(void)
428 {
429 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
430 }
431
432 /**
433 * @brief Enable the Debug Module during STANDBY mode
434 * @retval None
435 */
HAL_DBGMCU_EnableDBGStandbyMode(void)436 void HAL_DBGMCU_EnableDBGStandbyMode(void)
437 {
438 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
439 }
440
441 /**
442 * @brief Disable the Debug Module during STANDBY mode
443 * @retval None
444 */
HAL_DBGMCU_DisableDBGStandbyMode(void)445 void HAL_DBGMCU_DisableDBGStandbyMode(void)
446 {
447 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
448 }
449
450 /**
451 * @}
452 */
453
454 /**
455 * @}
456 */
457
458 #endif /* HAL_MODULE_ENABLED */
459 /**
460 * @}
461 */
462
463 /**
464 * @}
465 */
466
467 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
468