comparison Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c @ 160:e3ca52b8e7fa

Merge with FlipDisplay
author heinrichsweikamp
date Thu, 07 Mar 2019 15:06:43 +0100
parents c78bcbd5deda
children
comparison
equal deleted inserted replaced
80:cc2bb7bb8456 160:e3ca52b8e7fa
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_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 "stm32f4xx_hal.h"
53
54 /** @addtogroup STM32F4xx_HAL_Driver
55 * @{
56 */
57
58 /** @defgroup HAL HAL
59 * @brief HAL module driver.
60 * @{
61 */
62
63 /* Private typedef -----------------------------------------------------------*/
64 /* Private define ------------------------------------------------------------*/
65 /** @addtogroup HAL_Private_Constants
66 * @{
67 */
68 /**
69 * @brief STM32F4xx HAL Driver version number V1.7.4
70 */
71 #define __STM32F4xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
72 #define __STM32F4xx_HAL_VERSION_SUB1 (0x07U) /*!< [23:16] sub1 version */
73 #define __STM32F4xx_HAL_VERSION_SUB2 (0x04U) /*!< [15:8] sub2 version */
74 #define __STM32F4xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
75 #define __STM32F4xx_HAL_VERSION ((__STM32F4xx_HAL_VERSION_MAIN << 24U)\
76 |(__STM32F4xx_HAL_VERSION_SUB1 << 16U)\
77 |(__STM32F4xx_HAL_VERSION_SUB2 << 8U )\
78 |(__STM32F4xx_HAL_VERSION_RC))
79
80 #define IDCODE_DEVID_MASK 0x00000FFFU
81
82 /* ------------ RCC registers bit address in the alias region ----------- */
83 #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
84 /* --- MEMRMP Register ---*/
85 /* Alias word address of UFB_MODE bit */
86 #define MEMRMP_OFFSET SYSCFG_OFFSET
87 #define UFB_MODE_BIT_NUMBER SYSCFG_MEMRMP_UFB_MODE_Pos
88 #define UFB_MODE_BB (uint32_t)(PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (UFB_MODE_BIT_NUMBER * 4U))
89
90 /* --- CMPCR Register ---*/
91 /* Alias word address of CMP_PD bit */
92 #define CMPCR_OFFSET (SYSCFG_OFFSET + 0x20U)
93 #define CMP_PD_BIT_NUMBER SYSCFG_CMPCR_CMP_PD_Pos
94 #define CMPCR_CMP_PD_BB (uint32_t)(PERIPH_BB_BASE + (CMPCR_OFFSET * 32U) + (CMP_PD_BIT_NUMBER * 4U))
95
96 /* --- MCHDLYCR Register ---*/
97 /* Alias word address of BSCKSEL bit */
98 #define MCHDLYCR_OFFSET (SYSCFG_OFFSET + 0x30U)
99 #define BSCKSEL_BIT_NUMBER SYSCFG_MCHDLYCR_BSCKSEL_Pos
100 #define MCHDLYCR_BSCKSEL_BB (uint32_t)(PERIPH_BB_BASE + (MCHDLYCR_OFFSET * 32U) + (BSCKSEL_BIT_NUMBER * 4U))
101 /**
102 * @}
103 */
104
105 /* Private macro -------------------------------------------------------------*/
106 /* Private variables ---------------------------------------------------------*/
107 /** @addtogroup HAL_Private_Variables
108 * @{
109 */
110 __IO uint32_t uwTick;
111 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
112 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
113 /**
114 * @}
115 */
116 /* Private function prototypes -----------------------------------------------*/
117 /* Private functions ---------------------------------------------------------*/
118
119 /** @defgroup HAL_Exported_Functions HAL Exported Functions
120 * @{
121 */
122
123 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
124 * @brief Initialization and de-initialization functions
125 *
126 @verbatim
127 ===============================================================================
128 ##### Initialization and Configuration functions #####
129 ===============================================================================
130 [..] This section provides functions allowing to:
131 (+) Initializes the Flash interface the NVIC allocation and initial clock
132 configuration. It initializes the systick also when timeout is needed
133 and the backup domain when enabled.
134 (+) De-Initializes common part of the HAL.
135 (+) Configure the time base source to have 1ms time base with a dedicated
136 Tick interrupt priority.
137 (++) SysTick timer is used by default as source of time base, but user
138 can eventually implement his proper time base source (a general purpose
139 timer for example or other time source), keeping in mind that Time base
140 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
141 handled in milliseconds basis.
142 (++) Time base configuration function (HAL_InitTick ()) is called automatically
143 at the beginning of the program after reset by HAL_Init() or at any time
144 when clock is configured, by HAL_RCC_ClockConfig().
145 (++) Source of time base is configured to generate interrupts at regular
146 time intervals. Care must be taken if HAL_Delay() is called from a
147 peripheral ISR process, the Tick interrupt line must have higher priority
148 (numerically lower) than the peripheral interrupt. Otherwise the caller
149 ISR process will be blocked.
150 (++) functions affecting time base configurations are declared as __weak
151 to make override possible in case of other implementations in user file.
152 @endverbatim
153 * @{
154 */
155
156 /**
157 * @brief This function is used to initialize the HAL Library; it must be the first
158 * instruction to be executed in the main program (before to call any other
159 * HAL function), it performs the following:
160 * Configure the Flash prefetch, instruction and Data caches.
161 * Configures the SysTick to generate an interrupt each 1 millisecond,
162 * which is clocked by the HSI (at this stage, the clock is not yet
163 * configured and thus the system is running from the internal HSI at 16 MHz).
164 * Set NVIC Group Priority to 4.
165 * Calls the HAL_MspInit() callback function defined in user file
166 * "stm32f4xx_hal_msp.c" to do the global low level hardware initialization
167 *
168 * @note SysTick is used as time base for the HAL_Delay() function, the application
169 * need to ensure that the SysTick time base is always set to 1 millisecond
170 * to have correct HAL operation.
171 * @retval HAL status
172 */
173 HAL_StatusTypeDef HAL_Init(void)
174 {
175 /* Configure Flash prefetch, Instruction cache, Data cache */
176 #if (INSTRUCTION_CACHE_ENABLE != 0U)
177 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
178 #endif /* INSTRUCTION_CACHE_ENABLE */
179
180 #if (DATA_CACHE_ENABLE != 0U)
181 __HAL_FLASH_DATA_CACHE_ENABLE();
182 #endif /* DATA_CACHE_ENABLE */
183
184 #if (PREFETCH_ENABLE != 0U)
185 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
186 #endif /* PREFETCH_ENABLE */
187
188 /* Set Interrupt Group Priority */
189 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
190
191 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
192 HAL_InitTick(TICK_INT_PRIORITY);
193
194 /* Init the low level hardware */
195 HAL_MspInit();
196
197 /* Return function status */
198 return HAL_OK;
199 }
200
201 /**
202 * @brief This function de-Initializes common part of the HAL and stops the systick.
203 * This function is optional.
204 * @retval HAL status
205 */
206 HAL_StatusTypeDef HAL_DeInit(void)
207 {
208 /* Reset of all peripherals */
209 __HAL_RCC_APB1_FORCE_RESET();
210 __HAL_RCC_APB1_RELEASE_RESET();
211
212 __HAL_RCC_APB2_FORCE_RESET();
213 __HAL_RCC_APB2_RELEASE_RESET();
214
215 __HAL_RCC_AHB1_FORCE_RESET();
216 __HAL_RCC_AHB1_RELEASE_RESET();
217
218 __HAL_RCC_AHB2_FORCE_RESET();
219 __HAL_RCC_AHB2_RELEASE_RESET();
220
221 __HAL_RCC_AHB3_FORCE_RESET();
222 __HAL_RCC_AHB3_RELEASE_RESET();
223
224 /* De-Init the low level hardware */
225 HAL_MspDeInit();
226
227 /* Return function status */
228 return HAL_OK;
229 }
230
231 /**
232 * @brief Initialize the MSP.
233 * @retval None
234 */
235 __weak void HAL_MspInit(void)
236 {
237 /* NOTE : This function should not be modified, when the callback is needed,
238 the HAL_MspInit could be implemented in the user file
239 */
240 }
241
242 /**
243 * @brief DeInitializes the MSP.
244 * @retval None
245 */
246 __weak void HAL_MspDeInit(void)
247 {
248 /* NOTE : This function should not be modified, when the callback is needed,
249 the HAL_MspDeInit could be implemented in the user file
250 */
251 }
252
253 /**
254 * @brief This function configures the source of the time base.
255 * The time source is configured to have 1ms time base with a dedicated
256 * Tick interrupt priority.
257 * @note This function is called automatically at the beginning of program after
258 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
259 * @note In the default implementation, SysTick timer is the source of time base.
260 * It is used to generate interrupts at regular time intervals.
261 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
262 * The SysTick interrupt must have higher priority (numerically lower)
263 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
264 * The function is declared as __weak to be overwritten in case of other
265 * implementation in user file.
266 * @param TickPriority Tick interrupt priority.
267 * @retval HAL status
268 */
269 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
270 {
271 /* Configure the SysTick to have interrupt in 1ms time basis*/
272 if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
273 {
274 return HAL_ERROR;
275 }
276
277 /* Configure the SysTick IRQ priority */
278 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
279 {
280 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
281 uwTickPrio = TickPriority;
282 }
283 else
284 {
285 return HAL_ERROR;
286 }
287
288 /* Return function status */
289 return HAL_OK;
290 }
291
292 /**
293 * @}
294 */
295
296 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
297 * @brief HAL Control functions
298 *
299 @verbatim
300 ===============================================================================
301 ##### HAL Control functions #####
302 ===============================================================================
303 [..] This section provides functions allowing to:
304 (+) Provide a tick value in millisecond
305 (+) Provide a blocking delay in millisecond
306 (+) Suspend the time base source interrupt
307 (+) Resume the time base source interrupt
308 (+) Get the HAL API driver version
309 (+) Get the device identifier
310 (+) Get the device revision identifier
311 (+) Enable/Disable Debug module during SLEEP mode
312 (+) Enable/Disable Debug module during STOP mode
313 (+) Enable/Disable Debug module during STANDBY mode
314
315 @endverbatim
316 * @{
317 */
318
319 /**
320 * @brief This function is called to increment a global variable "uwTick"
321 * used as application time base.
322 * @note In the default implementation, this variable is incremented each 1ms
323 * in SysTick ISR.
324 * @note This function is declared as __weak to be overwritten in case of other
325 * implementations in user file.
326 * @retval None
327 */
328 __weak void HAL_IncTick(void)
329 {
330 uwTick += uwTickFreq;
331 }
332
333 /**
334 * @brief Provides a tick value in millisecond.
335 * @note This function is declared as __weak to be overwritten in case of other
336 * implementations in user file.
337 * @retval tick value
338 */
339 __weak uint32_t HAL_GetTick(void)
340 {
341 return uwTick;
342 }
343
344 /**
345 * @brief This function returns a tick priority.
346 * @retval tick priority
347 */
348 uint32_t HAL_GetTickPrio(void)
349 {
350 return uwTickPrio;
351 }
352
353 /**
354 * @brief Set new tick Freq.
355 * @retval Status
356 */
357 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
358 {
359 HAL_StatusTypeDef status = HAL_OK;
360 assert_param(IS_TICKFREQ(Freq));
361
362 if (uwTickFreq != Freq)
363 {
364 uwTickFreq = Freq;
365
366 /* Apply the new tick Freq */
367 status = HAL_InitTick(uwTickPrio);
368 }
369
370 return status;
371 }
372
373 /**
374 * @brief Return tick frequency.
375 * @retval tick period in Hz
376 */
377 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
378 {
379 return uwTickFreq;
380 }
381
382 /**
383 * @brief This function provides minimum delay (in milliseconds) based
384 * on variable incremented.
385 * @note In the default implementation , SysTick timer is the source of time base.
386 * It is used to generate interrupts at regular time intervals where uwTick
387 * is incremented.
388 * @note This function is declared as __weak to be overwritten in case of other
389 * implementations in user file.
390 * @param Delay specifies the delay time length, in milliseconds.
391 * @retval None
392 */
393 __weak void HAL_Delay(uint32_t Delay)
394 {
395 uint32_t tickstart = HAL_GetTick();
396 uint32_t wait = Delay;
397
398 /* Add a freq to guarantee minimum wait */
399 if (wait < HAL_MAX_DELAY)
400 {
401 wait += (uint32_t)(uwTickFreq);
402 }
403
404 while((HAL_GetTick() - tickstart) < wait)
405 {
406 }
407 }
408
409 /**
410 * @brief Suspend Tick increment.
411 * @note In the default implementation , SysTick timer is the source of time base. It is
412 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
413 * is called, the SysTick interrupt will be disabled and so Tick increment
414 * is suspended.
415 * @note This function is declared as __weak to be overwritten in case of other
416 * implementations in user file.
417 * @retval None
418 */
419 __weak void HAL_SuspendTick(void)
420 {
421 /* Disable SysTick Interrupt */
422 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
423 }
424
425 /**
426 * @brief Resume Tick increment.
427 * @note In the default implementation , SysTick timer is the source of time base. It is
428 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
429 * is called, the SysTick interrupt will be enabled and so Tick increment
430 * is resumed.
431 * @note This function is declared as __weak to be overwritten in case of other
432 * implementations in user file.
433 * @retval None
434 */
435 __weak void HAL_ResumeTick(void)
436 {
437 /* Enable SysTick Interrupt */
438 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
439 }
440
441 /**
442 * @brief Returns the HAL revision
443 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
444 */
445 uint32_t HAL_GetHalVersion(void)
446 {
447 return __STM32F4xx_HAL_VERSION;
448 }
449
450 /**
451 * @brief Returns the device revision identifier.
452 * @retval Device revision identifier
453 */
454 uint32_t HAL_GetREVID(void)
455 {
456 return((DBGMCU->IDCODE) >> 16U);
457 }
458
459 /**
460 * @brief Returns the device identifier.
461 * @retval Device identifier
462 */
463 uint32_t HAL_GetDEVID(void)
464 {
465 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
466 }
467
468 /**
469 * @brief Enable the Debug Module during SLEEP mode
470 * @retval None
471 */
472 void HAL_DBGMCU_EnableDBGSleepMode(void)
473 {
474 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
475 }
476
477 /**
478 * @brief Disable the Debug Module during SLEEP mode
479 * @retval None
480 */
481 void HAL_DBGMCU_DisableDBGSleepMode(void)
482 {
483 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
484 }
485
486 /**
487 * @brief Enable the Debug Module during STOP mode
488 * @retval None
489 */
490 void HAL_DBGMCU_EnableDBGStopMode(void)
491 {
492 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
493 }
494
495 /**
496 * @brief Disable the Debug Module during STOP mode
497 * @retval None
498 */
499 void HAL_DBGMCU_DisableDBGStopMode(void)
500 {
501 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
502 }
503
504 /**
505 * @brief Enable the Debug Module during STANDBY mode
506 * @retval None
507 */
508 void HAL_DBGMCU_EnableDBGStandbyMode(void)
509 {
510 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
511 }
512
513 /**
514 * @brief Disable the Debug Module during STANDBY mode
515 * @retval None
516 */
517 void HAL_DBGMCU_DisableDBGStandbyMode(void)
518 {
519 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
520 }
521
522 /**
523 * @brief Enables the I/O Compensation Cell.
524 * @note The I/O compensation cell can be used only when the device supply
525 * voltage ranges from 2.4 to 3.6 V.
526 * @retval None
527 */
528 void HAL_EnableCompensationCell(void)
529 {
530 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
531 }
532
533 /**
534 * @brief Power-down the I/O Compensation Cell.
535 * @note The I/O compensation cell can be used only when the device supply
536 * voltage ranges from 2.4 to 3.6 V.
537 * @retval None
538 */
539 void HAL_DisableCompensationCell(void)
540 {
541 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
542 }
543
544 /**
545 * @brief Return the unique device identifier (UID based on 96 bits)
546 * @param UID pointer to 3 words array.
547 * @retval Device identifier
548 */
549 void HAL_GetUID(uint32_t *UID)
550 {
551 UID[0] = (uint32_t)(READ_REG(*((uint32_t *)UID_BASE)));
552 UID[1] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
553 UID[2] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
554 }
555
556 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) ||\
557 defined(STM32F469xx) || defined(STM32F479xx)
558 /**
559 * @brief Enables the Internal FLASH Bank Swapping.
560 *
561 * @note This function can be used only for STM32F42xxx/43xxx devices.
562 *
563 * @note Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
564 * and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
565 *
566 * @retval None
567 */
568 void HAL_EnableMemorySwappingBank(void)
569 {
570 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
571 }
572
573 /**
574 * @brief Disables the Internal FLASH Bank Swapping.
575 *
576 * @note This function can be used only for STM32F42xxx/43xxx devices.
577 *
578 * @note The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x00000000)
579 * and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
580 *
581 * @retval None
582 */
583 void HAL_DisableMemorySwappingBank(void)
584 {
585 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
586 }
587 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
588 /**
589 * @}
590 */
591
592 /**
593 * @}
594 */
595
596 /**
597 * @}
598 */
599
600 /**
601 * @}
602 */
603
604 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/