FIX: Flash page size check is STM (or clone) specific (#14130)
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal.c
blob8e741e4f88c5e8701c08af7a1fa6436c71da2df5
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief HAL module driver.
8 * This is the common part of the HAL initialization
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
22 @endverbatim
23 ******************************************************************************
24 * @attention
26 * <h2><center>&copy; COPYRIGHT(c) 2017 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 ******************************************************************************
51 */
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32f4xx_hal.h"
56 /** @addtogroup STM32F4xx_HAL_Driver
57 * @{
60 /** @defgroup HAL HAL
61 * @brief HAL module driver.
62 * @{
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /** @addtogroup HAL_Private_Constants
68 * @{
70 /**
71 * @brief STM32F4xx HAL Driver version number V1.7.1
73 #define __STM32F4xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
74 #define __STM32F4xx_HAL_VERSION_SUB1 (0x07U) /*!< [23:16] sub1 version */
75 #define __STM32F4xx_HAL_VERSION_SUB2 (0x01U) /*!< [15:8] sub2 version */
76 #define __STM32F4xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
77 #define __STM32F4xx_HAL_VERSION ((__STM32F4xx_HAL_VERSION_MAIN << 24U)\
78 |(__STM32F4xx_HAL_VERSION_SUB1 << 16U)\
79 |(__STM32F4xx_HAL_VERSION_SUB2 << 8U )\
80 |(__STM32F4xx_HAL_VERSION_RC))
82 #define IDCODE_DEVID_MASK 0x00000FFFU
84 /* ------------ RCC registers bit address in the alias region ----------- */
85 #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
86 /* --- MEMRMP Register ---*/
87 /* Alias word address of UFB_MODE bit */
88 #define MEMRMP_OFFSET SYSCFG_OFFSET
89 #define UFB_MODE_BIT_NUMBER POSITION_VAL(SYSCFG_MEMRMP_UFB_MODE)
90 #define UFB_MODE_BB (uint32_t)(PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (UFB_MODE_BIT_NUMBER * 4U))
92 /* --- CMPCR Register ---*/
93 /* Alias word address of CMP_PD bit */
94 #define CMPCR_OFFSET (SYSCFG_OFFSET + 0x20U)
95 #define CMP_PD_BIT_NUMBER POSITION_VAL(SYSCFG_CMPCR_CMP_PD)
96 #define CMPCR_CMP_PD_BB (uint32_t)(PERIPH_BB_BASE + (CMPCR_OFFSET * 32U) + (CMP_PD_BIT_NUMBER * 4U))
98 /* --- MCHDLYCR Register ---*/
99 /* Alias word address of BSCKSEL bit */
100 #define MCHDLYCR_OFFSET (SYSCFG_OFFSET + 0x30U)
101 #define BSCKSEL_BIT_NUMBER POSITION_VAL(SYSCFG_MCHDLYCR_BSCKSEL)
102 #define MCHDLYCR_BSCKSEL_BB (uint32_t)(PERIPH_BB_BASE + (MCHDLYCR_OFFSET * 32U) + (BSCKSEL_BIT_NUMBER * 4U))
104 * @}
107 /* Private macro -------------------------------------------------------------*/
108 /* Private variables ---------------------------------------------------------*/
109 /** @addtogroup HAL_Private_Variables
110 * @{
112 __IO uint32_t uwTick;
114 * @}
116 /* Private function prototypes -----------------------------------------------*/
117 /* Private functions ---------------------------------------------------------*/
119 /** @defgroup HAL_Exported_Functions HAL Exported Functions
120 * @{
123 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
124 * @brief Initialization and de-initialization functions
126 @verbatim
127 ===============================================================================
128 ##### Initialization and de-initialization 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 * @{
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
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
173 HAL_StatusTypeDef HAL_Init(void)
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 */
180 #if (DATA_CACHE_ENABLE != 0U)
181 __HAL_FLASH_DATA_CACHE_ENABLE();
182 #endif /* DATA_CACHE_ENABLE */
184 #if (PREFETCH_ENABLE != 0U)
185 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
186 #endif /* PREFETCH_ENABLE */
188 /* Set Interrupt Group Priority */
189 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
191 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
192 HAL_InitTick(TICK_INT_PRIORITY);
194 /* Init the low level hardware */
195 HAL_MspInit();
197 /* Return function status */
198 return HAL_OK;
202 * @brief This function de-Initializes common part of the HAL and stops the systick.
203 * This function is optional.
204 * @retval HAL status
206 HAL_StatusTypeDef HAL_DeInit(void)
208 /* Reset of all peripherals */
209 __HAL_RCC_APB1_FORCE_RESET();
210 __HAL_RCC_APB1_RELEASE_RESET();
212 __HAL_RCC_APB2_FORCE_RESET();
213 __HAL_RCC_APB2_RELEASE_RESET();
215 __HAL_RCC_AHB1_FORCE_RESET();
216 __HAL_RCC_AHB1_RELEASE_RESET();
218 __HAL_RCC_AHB2_FORCE_RESET();
219 __HAL_RCC_AHB2_RELEASE_RESET();
221 __HAL_RCC_AHB3_FORCE_RESET();
222 __HAL_RCC_AHB3_RELEASE_RESET();
224 /* De-Init the low level hardware */
225 HAL_MspDeInit();
227 /* Return function status */
228 return HAL_OK;
232 * @brief Initializes the MSP.
233 * @retval None
235 __weak void HAL_MspInit(void)
237 /* NOTE : This function Should not be modified, when the callback is needed,
238 the HAL_MspInit could be implemented in the user file
243 * @brief DeInitializes the MSP.
244 * @retval None
246 __weak void HAL_MspDeInit(void)
248 /* NOTE : This function Should not be modified, when the callback is needed,
249 the HAL_MspDeInit could be implemented in the user file
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 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
269 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
271 /*Configure the SysTick to have interrupt in 1ms time basis*/
272 HAL_SYSTICK_Config(SystemCoreClock/1000U);
274 /*Configure the SysTick IRQ priority */
275 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
277 /* Return function status */
278 return HAL_OK;
282 * @}
285 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
286 * @brief HAL Control functions
288 @verbatim
289 ===============================================================================
290 ##### HAL Control functions #####
291 ===============================================================================
292 [..] This section provides functions allowing to:
293 (+) Provide a tick value in millisecond
294 (+) Provide a blocking delay in millisecond
295 (+) Suspend the time base source interrupt
296 (+) Resume the time base source interrupt
297 (+) Get the HAL API driver version
298 (+) Get the device identifier
299 (+) Get the device revision identifier
300 (+) Enable/Disable Debug module during SLEEP mode
301 (+) Enable/Disable Debug module during STOP mode
302 (+) Enable/Disable Debug module during STANDBY mode
304 @endverbatim
305 * @{
309 * @brief This function is called to increment a global variable "uwTick"
310 * used as application time base.
311 * @note In the default implementation, this variable is incremented each 1ms
312 * in Systick ISR.
313 * @note This function is declared as __weak to be overwritten in case of other
314 * implementations in user file.
315 * @retval None
317 __weak void HAL_IncTick(void)
319 uwTick++;
323 * @brief Provides a tick value in millisecond.
324 * @note This function is declared as __weak to be overwritten in case of other
325 * implementations in user file.
326 * @retval tick value
328 __weak uint32_t HAL_GetTick(void)
330 return uwTick;
334 * @brief This function provides minimum delay (in milliseconds) based
335 * on variable incremented.
336 * @note In the default implementation , SysTick timer is the source of time base.
337 * It is used to generate interrupts at regular time intervals where uwTick
338 * is incremented.
339 * @note This function is declared as __weak to be overwritten in case of other
340 * implementations in user file.
341 * @param Delay: specifies the delay time length, in milliseconds.
342 * @retval None
344 __weak void HAL_Delay(__IO uint32_t Delay)
346 uint32_t tickstart = HAL_GetTick();
347 uint32_t wait = Delay;
349 /* Add a period to guarantee minimum wait */
350 if (wait < HAL_MAX_DELAY)
352 wait++;
355 while((HAL_GetTick() - tickstart) < wait)
361 * @brief Suspend Tick increment.
362 * @note In the default implementation , SysTick timer is the source of time base. It is
363 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
364 * is called, the SysTick interrupt will be disabled and so Tick increment
365 * is suspended.
366 * @note This function is declared as __weak to be overwritten in case of other
367 * implementations in user file.
368 * @retval None
370 __weak void HAL_SuspendTick(void)
372 /* Disable SysTick Interrupt */
373 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
377 * @brief Resume Tick increment.
378 * @note In the default implementation , SysTick timer is the source of time base. It is
379 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
380 * is called, the SysTick interrupt will be enabled and so Tick increment
381 * is resumed.
382 * @note This function is declared as __weak to be overwritten in case of other
383 * implementations in user file.
384 * @retval None
386 __weak void HAL_ResumeTick(void)
388 /* Enable SysTick Interrupt */
389 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
393 * @brief Returns the HAL revision
394 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
396 uint32_t HAL_GetHalVersion(void)
398 return __STM32F4xx_HAL_VERSION;
402 * @brief Returns the device revision identifier.
403 * @retval Device revision identifier
405 uint32_t HAL_GetREVID(void)
407 return((DBGMCU->IDCODE) >> 16U);
411 * @brief Returns the device identifier.
412 * @retval Device identifier
414 uint32_t HAL_GetDEVID(void)
416 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
420 * @brief Enable the Debug Module during SLEEP mode
421 * @retval None
423 void HAL_DBGMCU_EnableDBGSleepMode(void)
425 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
429 * @brief Disable the Debug Module during SLEEP mode
430 * @retval None
432 void HAL_DBGMCU_DisableDBGSleepMode(void)
434 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
438 * @brief Enable the Debug Module during STOP mode
439 * @retval None
441 void HAL_DBGMCU_EnableDBGStopMode(void)
443 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
447 * @brief Disable the Debug Module during STOP mode
448 * @retval None
450 void HAL_DBGMCU_DisableDBGStopMode(void)
452 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
456 * @brief Enable the Debug Module during STANDBY mode
457 * @retval None
459 void HAL_DBGMCU_EnableDBGStandbyMode(void)
461 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
465 * @brief Disable the Debug Module during STANDBY mode
466 * @retval None
468 void HAL_DBGMCU_DisableDBGStandbyMode(void)
470 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
474 * @brief Enables the I/O Compensation Cell.
475 * @note The I/O compensation cell can be used only when the device supply
476 * voltage ranges from 2.4 to 3.6 V.
477 * @retval None
479 void HAL_EnableCompensationCell(void)
481 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
485 * @brief Power-down the I/O Compensation Cell.
486 * @note The I/O compensation cell can be used only when the device supply
487 * voltage ranges from 2.4 to 3.6 V.
488 * @retval None
490 void HAL_DisableCompensationCell(void)
492 *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
496 * @brief Return the unique device identifier (UID based on 96 bits)
497 * @param UID: pointer to 3 words array.
498 * @retval Device identifier
500 void HAL_GetUID(uint32_t *UID)
502 UID[0] = (uint32_t)(READ_REG(*((uint32_t *)UID_BASE)));
503 UID[1] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
504 UID[2] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
507 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) ||\
508 defined(STM32F469xx) || defined(STM32F479xx)
510 * @brief Enables the Internal FLASH Bank Swapping.
512 * @note This function can be used only for STM32F42xxx/43xxx devices.
514 * @note Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
515 * and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
517 * @retval None
519 void HAL_EnableMemorySwappingBank(void)
521 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
525 * @brief Disables the Internal FLASH Bank Swapping.
527 * @note This function can be used only for STM32F42xxx/43xxx devices.
529 * @note The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x00000000)
530 * and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
532 * @retval None
534 void HAL_DisableMemorySwappingBank(void)
537 *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
539 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
541 * @}
545 * @}
549 * @}
553 * @}
556 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/