]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[max/tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F4 / stm32f4xx_hal.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    19-June-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          (+) Common HAL APIs
20          (+) Services HAL APIs
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 "stm32f4xx_hal.h"
55
56 /** @addtogroup STM32F4xx_HAL_Driver
57   * @{
58   */
59
60 /** @defgroup HAL 
61   * @brief HAL module driver.
62   * @{
63   */
64
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /**
68  * @brief STM32F4xx HAL Driver version number V1.1.0
69    */
70 #define __STM32F4xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
71 #define __STM32F4xx_HAL_VERSION_SUB1   (0x01) /*!< [23:16] sub1 version */
72 #define __STM32F4xx_HAL_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
73 #define __STM32F4xx_HAL_VERSION_RC     (0x00) /*!< [7:0]  release candidate */ 
74 #define __STM32F4xx_HAL_VERSION         ((__STM32F4xx_HAL_VERSION_MAIN << 24)\
75                                         |(__STM32F4xx_HAL_VERSION_SUB1 << 16)\
76                                         |(__STM32F4xx_HAL_VERSION_SUB2 << 8 )\
77                                         |(__STM32F4xx_HAL_VERSION_RC))
78                                         
79 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)
80
81 /* ------------ RCC registers bit address in the alias region ----------- */
82 #define SYSCFG_OFFSET             (SYSCFG_BASE - PERIPH_BASE)
83 /* ---  MEMRMP Register ---*/ 
84 /* Alias word address of UFB_MODE bit */ 
85 #define MEMRMP_OFFSET             SYSCFG_OFFSET 
86 #define UFB_MODE_BitNumber        ((uint8_t)0x8) 
87 #define UFB_MODE_BB               (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32) + (UFB_MODE_BitNumber * 4)) 
88
89 /* ---  CMPCR Register ---*/ 
90 /* Alias word address of CMP_PD bit */ 
91 #define CMPCR_OFFSET              (SYSCFG_OFFSET + 0x20) 
92 #define CMP_PD_BitNumber          ((uint8_t)0x00) 
93 #define CMPCR_CMP_PD_BB           (PERIPH_BB_BASE + (CMPCR_OFFSET * 32) + (CMP_PD_BitNumber * 4))
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 static __IO uint32_t uwTick;
97
98 /* Private function prototypes -----------------------------------------------*/
99 /* Private functions ---------------------------------------------------------*/
100
101 /** @defgroup HAL_Private_Functions
102   * @{
103   */
104
105 /** @defgroup HAL_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 systick also when timeout is needed 
115           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 @endverbatim
135   * @{
136   */
137
138 /**
139   * @brief  This function is used to initialize the HAL Library; it must be the first 
140   *         instruction to be executed in the main program (before to call any other
141   *         HAL function), it performs the following:
142   *           Configure the Flash prefetch, instruction and Data caches.
143   *           Configures the SysTick to generate an interrupt each 1 millisecond,
144   *           which is clocked by the HSI (at this stage, the clock is not yet
145   *           configured and thus the system is running from the internal HSI at 16 MHz).
146   *           Set NVIC Group Priority to 4.
147   *           Calls the HAL_MspInit() callback function defined in user file 
148   *           "stm32f4xx_hal_msp.c" to do the global low level hardware initialization 
149   *            
150   * @note   SysTick is used as time base for the HAL_Delay() function, the application
151   *         need to ensure that the SysTick time base is always set to 1 millisecond
152   *         to have correct HAL operation.
153   * @param  None
154   * @retval HAL status
155   */
156 HAL_StatusTypeDef HAL_Init(void)
157 {
158   /* Configure Flash prefetch, Instruction cache, Data cache */ 
159 #if (INSTRUCTION_CACHE_ENABLE != 0)
160    __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
161 #endif /* INSTRUCTION_CACHE_ENABLE */
162
163 #if (DATA_CACHE_ENABLE != 0)
164    __HAL_FLASH_DATA_CACHE_ENABLE();
165 #endif /* DATA_CACHE_ENABLE */
166
167 #if (PREFETCH_ENABLE != 0)
168   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
169 #endif /* PREFETCH_ENABLE */
170
171   /* Set Interrupt Group Priority */
172   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
173
174   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
175   HAL_InitTick(TICK_INT_PRIORITY);
176   
177   /* Init the low level hardware */
178   HAL_MspInit();
179   
180   /* Return function status */
181   return HAL_OK;
182 }
183
184 /**
185   * @brief  This function de-Initializes common part of the HAL and stops the systick.
186   *         This function is optional.   
187   * @param  None
188   * @retval HAL status
189   */
190 HAL_StatusTypeDef HAL_DeInit(void)
191 {
192   /* Reset of all peripherals */
193   __APB1_FORCE_RESET();
194   __APB1_RELEASE_RESET();
195
196   __APB2_FORCE_RESET();
197   __APB2_RELEASE_RESET();
198
199   __AHB1_FORCE_RESET();
200   __AHB1_RELEASE_RESET();
201
202   __AHB2_FORCE_RESET();
203   __AHB2_RELEASE_RESET();
204
205   __AHB3_FORCE_RESET();
206   __AHB3_RELEASE_RESET();
207
208   /* De-Init the low level hardware */
209   HAL_MspDeInit();
210     
211   /* Return function status */
212   return HAL_OK;
213 }
214
215 /**
216   * @brief  Initializes the MSP.
217   * @param  None
218   * @retval None
219   */
220 __weak void HAL_MspInit(void)
221 {
222   /* NOTE : This function Should not be modified, when the callback is needed,
223             the HAL_MspInit could be implemented in the user file
224    */
225 }
226
227 /**
228   * @brief  DeInitializes the MSP.
229   * @param  None  
230   * @retval None
231   */
232 __weak void HAL_MspDeInit(void)
233 {
234   /* NOTE : This function Should not be modified, when the callback is needed,
235             the HAL_MspDeInit could be implemented in the user file
236    */ 
237 }
238
239 /**
240   * @brief This function configures the source of the time base.
241   *        The time source is configured  to have 1ms time base with a dedicated 
242   *        Tick interrupt priority.
243   * @note This function is called  automatically at the beginning of program after
244   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
245   * @note In the default implementation, SysTick timer is the source of time base. 
246   *       It is used to generate interrupts at regular time intervals. 
247   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process, 
248   *       The the SysTick interrupt must have higher priority (numerically lower) 
249   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
250   *       The function is declared as __weak  to be overwritten  in case of other
251   *       implementation  in user file.
252   * @param TickPriority: Tick interrupt priority.
253   * @retval HAL status
254   */
255 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
256 {
257   /*Configure the SysTick to have interrupt in 1ms time basis*/
258   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
259
260   /*Configure the SysTick IRQ priority */
261   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
262
263   /* Return function status */
264   return HAL_OK;
265 }
266
267 /**
268   * @}
269   */
270
271 /** @defgroup HAL_Group2 HAL Control functions 
272  *  @brief    HAL Control functions
273  *
274 @verbatim
275  ===============================================================================
276                       ##### HAL Control functions #####
277  ===============================================================================
278     [..]  This section provides functions allowing to:
279       (+) Provide a tick value in millisecond
280       (+) Provide a blocking delay in millisecond
281       (+) Suspend the time base source interrupt
282       (+) Resume the time base source interrupt
283       (+) Get the HAL API driver version
284       (+) Get the device identifier
285       (+) Get the device revision identifier
286       (+) Enable/Disable Debug module during SLEEP mode
287       (+) Enable/Disable Debug module during STOP mode
288       (+) Enable/Disable Debug module during STANDBY mode
289
290 @endverbatim
291   * @{
292   */
293
294 /**
295   * @brief This function is called to increment  a global variable "uwTick"
296   *        used as application time base.
297   * @note In the default implementation, this variable is incremented each 1ms
298   *       in Systick ISR.
299  * @note This function is declared as __weak to be overwritten in case of other 
300   *      implementations in user file.
301   * @param None
302   * @retval None
303   */
304 __weak void HAL_IncTick(void)
305 {
306   uwTick++;
307 }
308
309 /**
310   * @brief Provides a tick value in millisecond.
311   * @note This function is declared as __weak to be overwritten in case of other 
312   *       implementations in user file.
313   * @param None
314   * @retval tick value
315   */
316 __weak uint32_t HAL_GetTick(void)
317 {
318   return uwTick;
319 }
320
321 /**
322   * @brief This function provides accurate delay (in milliseconds) based 
323   *        on variable incremented.
324   * @note In the default implementation , SysTick timer is the source of time base.
325   *       It is used to generate interrupts at regular time intervals where uwTick
326   *       is incremented.
327   * @note ThiS function is declared as __weak to be overwritten in case of other
328   *       implementations in user file.
329   * @param Delay: specifies the delay time length, in milliseconds.
330   * @retval None
331   */
332 __weak void HAL_Delay(__IO uint32_t Delay)
333 {
334   uint32_t tickstart = 0;
335   tickstart = HAL_GetTick();
336   while((HAL_GetTick() - tickstart) < Delay)
337   {
338   }
339 }
340
341 /**
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 the SysTick interrupt will be disabled and so Tick increment 
346   *       is suspended.
347   * @note This function is declared as __weak to be overwritten in case of other
348   *       implementations in user file.
349   * @param None
350   * @retval None
351   */
352 __weak void HAL_SuspendTick(void)
353 {
354   /* Disable SysTick Interrupt */
355   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
356 }
357
358 /**
359   * @brief Resume Tick increment.
360   * @note In the default implementation , SysTick timer is the source of time base. It is
361   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
362   *       is called, the the SysTick interrupt will be enabled and so Tick increment 
363   *       is resumed.
364   * @note This function is declared as __weak to be overwritten in case of other
365   *       implementations in user file.
366   * @param None
367   * @retval None
368   */
369 __weak void HAL_ResumeTick(void)
370 {
371   /* Enable SysTick Interrupt */
372   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
373 }
374
375 /**
376   * @brief  Returns the HAL revision
377   * @param  None
378   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
379   */
380 uint32_t HAL_GetHalVersion(void)
381 {
382  return __STM32F4xx_HAL_VERSION;
383 }
384
385 /**
386   * @brief  Returns the device revision identifier.
387   * @param  None
388   * @retval Device revision identifier
389   */
390 uint32_t HAL_GetREVID(void)
391 {
392    return((DBGMCU->IDCODE) >> 16);
393 }
394
395 /**
396   * @brief  Returns the device identifier.
397   * @param  None
398   * @retval Device identifier
399   */
400 uint32_t HAL_GetDEVID(void)
401 {
402    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
403 }
404
405 /**
406   * @brief  Enable the Debug Module during SLEEP mode
407   * @param  None
408   * @retval None
409   */
410 void HAL_EnableDBGSleepMode(void)
411 {
412   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
413 }
414
415 /**
416   * @brief  Disable the Debug Module during SLEEP mode
417   * @param  None
418   * @retval None
419   */
420 void HAL_DisableDBGSleepMode(void)
421 {
422   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
423 }
424
425 /**
426   * @brief  Enable the Debug Module during STOP mode
427   * @param  None
428   * @retval None
429   */
430 void HAL_EnableDBGStopMode(void)
431 {
432   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
433 }
434
435 /**
436   * @brief  Disable the Debug Module during STOP mode
437   * @param  None
438   * @retval None
439   */
440 void HAL_DisableDBGStopMode(void)
441 {
442   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
443 }
444
445 /**
446   * @brief  Enable the Debug Module during STANDBY mode
447   * @param  None
448   * @retval None
449   */
450 void HAL_EnableDBGStandbyMode(void)
451 {
452   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
453 }
454
455 /**
456   * @brief  Disable the Debug Module during STANDBY mode
457   * @param  None
458   * @retval None
459   */
460 void HAL_DisableDBGStandbyMode(void)
461 {
462   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
463 }
464
465 /**
466   * @brief  Enables the I/O Compensation Cell.
467   * @note   The I/O compensation cell can be used only when the device supply
468   *         voltage ranges from 2.4 to 3.6 V.  
469   * @retval None
470   */
471 void HAL_EnableCompensationCell(void)
472 {
473   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
474 }
475
476 /**
477   * @brief  Power-down the I/O Compensation Cell.
478   * @note   The I/O compensation cell can be used only when the device supply
479   *         voltage ranges from 2.4 to 3.6 V.  
480   * @retval None
481   */
482 void HAL_DisableCompensationCell(void)
483 {
484   *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
485 }
486
487 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx)
488 /**
489   * @brief  Enables the Internal FLASH Bank Swapping.
490   *   
491   * @note   This function can be used only for STM32F42xxx/43xxx devices. 
492   *
493   * @note   Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000) 
494   *         and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)   
495   *
496   * @retval None
497   */
498 void HAL_EnableMemorySwappingBank(void)
499 {
500   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
501 }
502
503 /**
504   * @brief  Disables the Internal FLASH Bank Swapping.
505   *   
506   * @note   This function can be used only for STM32F42xxx/43xxx devices. 
507   *
508   * @note   The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000) 
509   *         and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000) 
510   *           
511   * @retval None
512   */
513 void HAL_DisableMemorySwappingBank(void)
514 {
515
516   *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
517 }
518 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
519
520 /**
521   * @}
522   */
523
524 /**
525   * @}
526   */
527
528 /**
529   * @}
530   */
531
532 /**
533   * @}
534   */
535
536 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/