2 ******************************************************************************
3 * @file stm32f1xx_hal.c
4 * @author MCD Application Team
7 * @brief HAL module driver.
8 * This is the common part of the HAL initialization
11 ==============================================================================
12 ##### How to use this driver #####
13 ==============================================================================
15 The common HAL driver contains a set of generic and common APIs that can be
16 used by the PPP peripheral drivers and the user to start using the HAL.
18 The HAL contains two APIs' categories:
23 ******************************************************************************
26 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
28 * Redistribution and use in source and binary forms, with or without modification,
29 * are permitted provided that the following conditions are met:
30 * 1. Redistributions of source code must retain the above copyright notice,
31 * this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 * 3. Neither the name of STMicroelectronics nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 ******************************************************************************
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32f1xx_hal.h"
56 /** @addtogroup STM32F1xx_HAL_Driver
61 * @brief HAL module driver.
65 #ifdef HAL_MODULE_ENABLED
67 /* Private typedef -----------------------------------------------------------*/
68 /* Private define ------------------------------------------------------------*/
70 /** @defgroup HAL_Private_Constants HAL Private Constants
74 * @brief STM32F1xx HAL Driver version number V1.1.1
76 #define __STM32F1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
77 #define __STM32F1xx_HAL_VERSION_SUB1 (0x01U) /*!< [23:16] sub1 version */
78 #define __STM32F1xx_HAL_VERSION_SUB2 (0x01U) /*!< [15:8] sub2 version */
79 #define __STM32F1xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
80 #define __STM32F1xx_HAL_VERSION ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
81 |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
82 |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
83 |(__STM32F1xx_HAL_VERSION_RC))
85 #define IDCODE_DEVID_MASK 0x00000FFFU
91 /* Private macro -------------------------------------------------------------*/
92 /* Private variables ---------------------------------------------------------*/
94 /** @defgroup HAL_Private_Variables HAL Private Variables
101 /* Private function prototypes -----------------------------------------------*/
102 /* Exported functions ---------------------------------------------------------*/
104 /** @defgroup HAL_Exported_Functions HAL Exported Functions
108 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
109 * @brief Initialization and de-initialization functions
112 ===============================================================================
113 ##### Initialization and de-initialization functions #####
114 ===============================================================================
115 [..] This section provides functions allowing to:
116 (+) Initializes the Flash interface, the NVIC allocation and initial clock
117 configuration. It initializes the source of time base also when timeout
118 is needed and the backup domain when enabled.
119 (+) de-Initializes common part of the HAL.
120 (+) Configure The time base source to have 1ms time base with a dedicated
121 Tick interrupt priority.
122 (++) Systick timer is used by default as source of time base, but user
123 can eventually implement his proper time base source (a general purpose
124 timer for example or other time source), keeping in mind that Time base
125 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
126 handled in milliseconds basis.
127 (++) Time base configuration function (HAL_InitTick ()) is called automatically
128 at the beginning of the program after reset by HAL_Init() or at any time
129 when clock is configured, by HAL_RCC_ClockConfig().
130 (++) Source of time base is configured to generate interrupts at regular
131 time intervals. Care must be taken if HAL_Delay() is called from a
132 peripheral ISR process, the Tick interrupt line must have higher priority
133 (numerically lower) than the peripheral interrupt. Otherwise the caller
134 ISR process will be blocked.
135 (++) functions affecting time base configurations are declared as __weak
136 to make override possible in case of other implementations in user file.
142 * @brief This function is used to initialize the HAL Library; it must be the first
143 * instruction to be executed in the main program (before to call any other
144 * HAL function), it performs the following:
145 * Configure the Flash prefetch.
146 * Configures the SysTick to generate an interrupt each 1 millisecond,
147 * which is clocked by the HSI (at this stage, the clock is not yet
148 * configured and thus the system is running from the internal HSI at 16 MHz).
149 * Set NVIC Group Priority to 4.
150 * Calls the HAL_MspInit() callback function defined in user file
151 * "stm32f1xx_hal_msp.c" to do the global low level hardware initialization
153 * @note SysTick is used as time base for the HAL_Delay() function, the application
154 * need to ensure that the SysTick time base is always set to 1 millisecond
155 * to have correct HAL operation.
158 HAL_StatusTypeDef
HAL_Init(void)
160 /* Configure Flash prefetch */
161 #if (PREFETCH_ENABLE != 0)
162 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
163 defined(STM32F102x6) || defined(STM32F102xB) || \
164 defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
165 defined(STM32F105xC) || defined(STM32F107xC)
167 /* Prefetch buffer is not available on value line devices */
168 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
170 #endif /* PREFETCH_ENABLE */
172 /* Set Interrupt Group Priority */
173 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4
);
175 /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
176 HAL_InitTick(TICK_INT_PRIORITY
);
178 /* Init the low level hardware */
181 /* Return function status */
186 * @brief This function de-Initializes common part of the HAL and stops the source
188 * @note This function is optional.
191 HAL_StatusTypeDef
HAL_DeInit(void)
193 /* Reset of all peripherals */
194 __HAL_RCC_APB1_FORCE_RESET();
195 __HAL_RCC_APB1_RELEASE_RESET();
197 __HAL_RCC_APB2_FORCE_RESET();
198 __HAL_RCC_APB2_RELEASE_RESET();
200 #if defined(STM32F105xC) || defined(STM32F107xC)
201 __HAL_RCC_AHB_FORCE_RESET();
202 __HAL_RCC_AHB_RELEASE_RESET();
205 /* De-Init the low level hardware */
208 /* Return function status */
213 * @brief Initializes the MSP.
216 __weak
void HAL_MspInit(void)
218 /* NOTE : This function Should not be modified, when the callback is needed,
219 the HAL_MspInit could be implemented in the user file
224 * @brief DeInitializes the MSP.
227 __weak
void HAL_MspDeInit(void)
229 /* NOTE : This function Should not be modified, when the callback is needed,
230 the HAL_MspDeInit could be implemented in the user file
235 * @brief This function configures the source of the time base.
236 * The time source is configured to have 1ms time base with a dedicated
237 * Tick interrupt priority.
238 * @note This function is called automatically at the beginning of program after
239 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
240 * @note In the default implementation, SysTick timer is the source of time base.
241 * It is used to generate interrupts at regular time intervals.
242 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
243 * The the SysTick interrupt must have higher priority (numerically lower)
244 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
245 * The function is declared as __weak to be overwritten in case of other
246 * implementation in user file.
247 * @param TickPriority: Tick interrupt priority.
250 __weak HAL_StatusTypeDef
HAL_InitTick(uint32_t TickPriority
)
252 /*Configure the SysTick to have interrupt in 1ms time basis*/
253 HAL_SYSTICK_Config(SystemCoreClock
/1000U);
255 /*Configure the SysTick IRQ priority */
256 HAL_NVIC_SetPriority(SysTick_IRQn
, TickPriority
,0U);
258 /* Return function status */
266 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
267 * @brief HAL Control functions
270 ===============================================================================
271 ##### HAL Control functions #####
272 ===============================================================================
273 [..] This section provides functions allowing to:
274 (+) Provide a tick value in millisecond
275 (+) Provide a blocking delay in millisecond
276 (+) Suspend the time base source interrupt
277 (+) Resume the time base source interrupt
278 (+) Get the HAL API driver version
279 (+) Get the device identifier
280 (+) Get the device revision identifier
281 (+) Enable/Disable Debug module during SLEEP mode
282 (+) Enable/Disable Debug module during STOP mode
283 (+) Enable/Disable Debug module during STANDBY mode
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
294 * @note This function is declared as __weak to be overwritten in case of other
295 * implementations in user file.
298 __weak
void HAL_IncTick(void)
304 * @brief Provides 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.
309 __weak
uint32_t HAL_GetTick(void)
315 * @brief This function provides minimum delay (in milliseconds) based
316 * on variable incremented.
317 * @note In the default implementation , SysTick timer is the source of time base.
318 * It is used to generate interrupts at regular time intervals where uwTick
320 * @note This function is declared as __weak to be overwritten in case of other
321 * implementations in user file.
322 * @param Delay: specifies the delay time length, in milliseconds.
325 __weak
void HAL_Delay(__IO
uint32_t Delay
)
327 uint32_t tickstart
= HAL_GetTick();
328 uint32_t wait
= Delay
;
330 /* Add a period to guarantee minimum wait */
331 if (wait
< HAL_MAX_DELAY
)
336 while((HAL_GetTick() - tickstart
) < wait
)
342 * @brief Suspend Tick increment.
343 * @note In the default implementation , SysTick timer is the source of time base. It is
344 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
345 * is called, the SysTick interrupt will be disabled and so Tick increment
347 * @note This function is declared as __weak to be overwritten in case of other
348 * implementations in user file.
351 __weak
void HAL_SuspendTick(void)
353 /* Disable SysTick Interrupt */
354 CLEAR_BIT(SysTick
->CTRL
,SysTick_CTRL_TICKINT_Msk
);
358 * @brief Resume Tick increment.
359 * @note In the default implementation , SysTick timer is the source of time base. It is
360 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
361 * is called, the SysTick interrupt will be enabled and so Tick increment
363 * @note This function is declared as __weak to be overwritten in case of other
364 * implementations in user file.
367 __weak
void HAL_ResumeTick(void)
369 /* Enable SysTick Interrupt */
370 SET_BIT(SysTick
->CTRL
,SysTick_CTRL_TICKINT_Msk
);
374 * @brief Returns the HAL revision
375 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
377 uint32_t HAL_GetHalVersion(void)
379 return __STM32F1xx_HAL_VERSION
;
383 * @brief Returns the device revision identifier.
384 * Note: On devices STM32F10xx8 and STM32F10xxB,
385 * STM32F101xC/D/E and STM32F103xC/D/E,
386 * STM32F101xF/G and STM32F103xF/G
387 * STM32F10xx4 and STM32F10xx6
388 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
389 * debug mode (not accessible by the user software in normal mode).
390 * Refer to errata sheet of these devices for more details.
391 * @retval Device revision identifier
393 uint32_t HAL_GetREVID(void)
395 return((DBGMCU
->IDCODE
) >> DBGMCU_IDCODE_REV_ID_Pos
);
399 * @brief Returns the device identifier.
400 * Note: On devices STM32F10xx8 and STM32F10xxB,
401 * STM32F101xC/D/E and STM32F103xC/D/E,
402 * STM32F101xF/G and STM32F103xF/G
403 * STM32F10xx4 and STM32F10xx6
404 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
405 * debug mode (not accessible by the user software in normal mode).
406 * Refer to errata sheet of these devices for more details.
407 * @retval Device identifier
409 uint32_t HAL_GetDEVID(void)
411 return((DBGMCU
->IDCODE
) & IDCODE_DEVID_MASK
);
415 * @brief Enable the Debug Module during SLEEP mode
418 void HAL_DBGMCU_EnableDBGSleepMode(void)
420 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_SLEEP
);
424 * @brief Disable the Debug Module during SLEEP mode
425 * Note: On devices STM32F10xx8 and STM32F10xxB,
426 * STM32F101xC/D/E and STM32F103xC/D/E,
427 * STM32F101xF/G and STM32F103xF/G
428 * STM32F10xx4 and STM32F10xx6
429 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
430 * debug mode (not accessible by the user software in normal mode).
431 * Refer to errata sheet of these devices for more details.
434 void HAL_DBGMCU_DisableDBGSleepMode(void)
436 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_SLEEP
);
440 * @brief Enable the Debug Module during STOP mode
441 * Note: On devices STM32F10xx8 and STM32F10xxB,
442 * STM32F101xC/D/E and STM32F103xC/D/E,
443 * STM32F101xF/G and STM32F103xF/G
444 * STM32F10xx4 and STM32F10xx6
445 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
446 * debug mode (not accessible by the user software in normal mode).
447 * Refer to errata sheet of these devices for more details.
448 * Note: On all STM32F1 devices:
449 * If the system tick timer interrupt is enabled during the Stop mode
450 * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
451 * the system from Stop mode.
452 * Workaround: To debug the Stop mode, disable the system tick timer
454 * Refer to errata sheet of these devices for more details.
455 * Note: On all STM32F1 devices:
456 * If the system tick timer interrupt is enabled during the Stop mode
457 * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
458 * the system from Stop mode.
459 * Workaround: To debug the Stop mode, disable the system tick timer
461 * Refer to errata sheet of these devices for more details.
464 void HAL_DBGMCU_EnableDBGStopMode(void)
466 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STOP
);
470 * @brief Disable the Debug Module during STOP mode
471 * Note: On devices STM32F10xx8 and STM32F10xxB,
472 * STM32F101xC/D/E and STM32F103xC/D/E,
473 * STM32F101xF/G and STM32F103xF/G
474 * STM32F10xx4 and STM32F10xx6
475 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
476 * debug mode (not accessible by the user software in normal mode).
477 * Refer to errata sheet of these devices for more details.
480 void HAL_DBGMCU_DisableDBGStopMode(void)
482 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STOP
);
486 * @brief Enable the Debug Module during STANDBY mode
487 * Note: On devices STM32F10xx8 and STM32F10xxB,
488 * STM32F101xC/D/E and STM32F103xC/D/E,
489 * STM32F101xF/G and STM32F103xF/G
490 * STM32F10xx4 and STM32F10xx6
491 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
492 * debug mode (not accessible by the user software in normal mode).
493 * Refer to errata sheet of these devices for more details.
496 void HAL_DBGMCU_EnableDBGStandbyMode(void)
498 SET_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STANDBY
);
502 * @brief Disable the Debug Module during STANDBY mode
503 * Note: On devices STM32F10xx8 and STM32F10xxB,
504 * STM32F101xC/D/E and STM32F103xC/D/E,
505 * STM32F101xF/G and STM32F103xF/G
506 * STM32F10xx4 and STM32F10xx6
507 * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
508 * debug mode (not accessible by the user software in normal mode).
509 * Refer to errata sheet of these devices for more details.
512 void HAL_DBGMCU_DisableDBGStandbyMode(void)
514 CLEAR_BIT(DBGMCU
->CR
, DBGMCU_CR_DBG_STANDBY
);
518 * @brief Return the unique device identifier (UID based on 96 bits)
519 * @param UID: pointer to 3 words array.
520 * @retval Device identifier
522 void HAL_GetUID(uint32_t *UID
)
524 UID
[0] = (uint32_t)(READ_REG(*((uint32_t *)UID_BASE
)));
525 UID
[1] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE
+ 4U))));
526 UID
[2] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE
+ 8U))));
537 #endif /* HAL_MODULE_ENABLED */
546 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/