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