]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   HAL module driver.\r
8   *          This is the common part of the HAL initialization\r
9   *\r
10   @verbatim\r
11   ==============================================================================\r
12                      ##### How to use this driver #####\r
13   ==============================================================================\r
14     [..]\r
15     The common HAL driver contains a set of generic and common APIs that can be\r
16     used by the PPP peripheral drivers and the user to start using the HAL. \r
17     [..]\r
18     The HAL contains two APIs' categories: \r
19          (+) Common HAL APIs\r
20          (+) Services HAL APIs\r
21 \r
22   @endverbatim\r
23   ******************************************************************************\r
24   * @attention\r
25   *\r
26   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
27   *\r
28   * Redistribution and use in source and binary forms, with or without modification,\r
29   * are permitted provided that the following conditions are met:\r
30   *   1. Redistributions of source code must retain the above copyright notice,\r
31   *      this list of conditions and the following disclaimer.\r
32   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
33   *      this list of conditions and the following disclaimer in the documentation\r
34   *      and/or other materials provided with the distribution.\r
35   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
36   *      may be used to endorse or promote products derived from this software\r
37   *      without specific prior written permission.\r
38   *\r
39   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
40   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
41   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
42   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
43   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
44   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
45   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
46   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
47   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
48   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
49   *\r
50   ******************************************************************************\r
51   */ \r
52 \r
53 /* Includes ------------------------------------------------------------------*/\r
54 #include "stm32f7xx_hal.h"\r
55 \r
56 /** @addtogroup STM32F7xx_HAL_Driver\r
57   * @{\r
58   */\r
59 \r
60 /** @defgroup HAL HAL\r
61   * @brief HAL module driver.\r
62   * @{\r
63   */\r
64 \r
65 /* Private typedef -----------------------------------------------------------*/\r
66 /* Private define ------------------------------------------------------------*/\r
67 /**\r
68  * @brief STM32F7xx HAL Driver version number V1.0.0RC1\r
69    */\r
70 #define __STM32F7xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */\r
71 #define __STM32F7xx_HAL_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */\r
72 #define __STM32F7xx_HAL_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */\r
73 #define __STM32F7xx_HAL_VERSION_RC     (0x01) /*!< [7:0]  release candidate */ \r
74 #define __STM32F7xx_HAL_VERSION         ((__STM32F7xx_HAL_VERSION_MAIN << 24)\\r
75                                         |(__STM32F7xx_HAL_VERSION_SUB1 << 16)\\r
76                                         |(__STM32F7xx_HAL_VERSION_SUB2 << 8 )\\r
77                                         |(__STM32F7xx_HAL_VERSION_RC))\r
78                                         \r
79 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)\r
80 \r
81 /* Private macro -------------------------------------------------------------*/\r
82 /* Private variables ---------------------------------------------------------*/\r
83 static __IO uint32_t uwTick;\r
84 \r
85 /* Private function prototypes -----------------------------------------------*/\r
86 /* Private functions ---------------------------------------------------------*/\r
87 \r
88 /** @defgroup HAL_Private_Functions HAL Private Functions\r
89   * @{\r
90   */\r
91 \r
92 /** @defgroup HAL_Group1 Initialization and de-initialization Functions \r
93  *  @brief    Initialization and de-initialization functions\r
94  *\r
95 @verbatim    \r
96  ===============================================================================\r
97               ##### Initialization and de-initialization functions #####\r
98  ===============================================================================\r
99     [..]  This section provides functions allowing to:\r
100       (+) Initializes the Flash interface the NVIC allocation and initial clock \r
101           configuration. It initializes the systick also when timeout is needed \r
102           and the backup domain when enabled.\r
103       (+) de-Initializes common part of the HAL\r
104       (+) Configure The time base source to have 1ms time base with a dedicated \r
105           Tick interrupt priority. \r
106         (++) Systick timer is used by default as source of time base, but user \r
107              can eventually implement his proper time base source (a general purpose \r
108              timer for example or other time source), keeping in mind that Time base \r
109              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and \r
110              handled in milliseconds basis.\r
111         (++) Time base configuration function (HAL_InitTick ()) is called automatically \r
112              at the beginning of the program after reset by HAL_Init() or at any time \r
113              when clock is configured, by HAL_RCC_ClockConfig(). \r
114         (++) Source of time base is configured  to generate interrupts at regular \r
115              time intervals. Care must be taken if HAL_Delay() is called from a \r
116              peripheral ISR process, the Tick interrupt line must have higher priority \r
117             (numerically lower) than the peripheral interrupt. Otherwise the caller \r
118             ISR process will be blocked. \r
119        (++) functions affecting time base configurations are declared as __weak  \r
120              to make  override possible  in case of other  implementations in user file.\r
121 @endverbatim\r
122   * @{\r
123   */\r
124 \r
125 /**\r
126   * @brief  This function is used to initialize the HAL Library; it must be the first \r
127   *         instruction to be executed in the main program (before to call any other\r
128   *         HAL function), it performs the following:\r
129   *           Configure the Flash prefetch, and instruction cache through ART accelerator.\r
130   *           Configures the SysTick to generate an interrupt each 1 millisecond,\r
131   *           which is clocked by the HSI (at this stage, the clock is not yet\r
132   *           configured and thus the system is running from the internal HSI at 16 MHz).\r
133   *           Set NVIC Group Priority to 4.\r
134   *           Calls the HAL_MspInit() callback function defined in user file \r
135   *           "stm32f7xx_hal_msp.c" to do the global low level hardware initialization \r
136   *            \r
137   * @note   SysTick is used as time base for the HAL_Delay() function, the application\r
138   *         need to ensure that the SysTick time base is always set to 1 millisecond\r
139   *         to have correct HAL operation.\r
140   * @retval HAL status\r
141   */\r
142 HAL_StatusTypeDef HAL_Init(void)\r
143 {\r
144   /* Configure Flash prefetch and Instruction cache through ART accelerator */ \r
145 #if (ART_ACCLERATOR_ENABLE != 0)\r
146    __HAL_FLASH_ART_ENABLE();\r
147 #endif /* ART_ACCLERATOR_ENABLE */\r
148 \r
149   /* Set Interrupt Group Priority */\r
150   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);\r
151 \r
152   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */\r
153   HAL_InitTick(TICK_INT_PRIORITY);\r
154   \r
155   /* Init the low level hardware */\r
156   HAL_MspInit();\r
157   \r
158   /* Return function status */\r
159   return HAL_OK;\r
160 }\r
161 \r
162 /**\r
163   * @brief  This function de-Initializes common part of the HAL and stops the systick.\r
164   *         This function is optional.   \r
165   * @retval HAL status\r
166   */\r
167 HAL_StatusTypeDef HAL_DeInit(void)\r
168 {\r
169   /* Reset of all peripherals */\r
170   __HAL_RCC_APB1_FORCE_RESET();\r
171   __HAL_RCC_APB1_RELEASE_RESET();\r
172 \r
173   __HAL_RCC_APB2_FORCE_RESET();\r
174   __HAL_RCC_APB2_RELEASE_RESET();\r
175 \r
176   __HAL_RCC_AHB1_FORCE_RESET();\r
177   __HAL_RCC_AHB1_RELEASE_RESET();\r
178 \r
179   __HAL_RCC_AHB2_FORCE_RESET();\r
180   __HAL_RCC_AHB2_RELEASE_RESET();\r
181 \r
182   __HAL_RCC_AHB3_FORCE_RESET();\r
183   __HAL_RCC_AHB3_RELEASE_RESET();\r
184 \r
185   /* De-Init the low level hardware */\r
186   HAL_MspDeInit();\r
187     \r
188   /* Return function status */\r
189   return HAL_OK;\r
190 }\r
191 \r
192 /**\r
193   * @brief  Initializes the MSP.\r
194   * @retval None\r
195   */\r
196 __weak void HAL_MspInit(void)\r
197 {\r
198   /* NOTE : This function Should not be modified, when the callback is needed,\r
199             the HAL_MspInit could be implemented in the user file\r
200    */\r
201 }\r
202 \r
203 /**\r
204   * @brief  DeInitializes the MSP.  \r
205   * @retval None\r
206   */\r
207 __weak void HAL_MspDeInit(void)\r
208 {\r
209   /* NOTE : This function Should not be modified, when the callback is needed,\r
210             the HAL_MspDeInit could be implemented in the user file\r
211    */ \r
212 }\r
213 \r
214 /**\r
215   * @brief This function configures the source of the time base.\r
216   *        The time source is configured  to have 1ms time base with a dedicated \r
217   *        Tick interrupt priority.\r
218   * @note This function is called  automatically at the beginning of program after\r
219   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().\r
220   * @note In the default implementation, SysTick timer is the source of time base. \r
221   *       It is used to generate interrupts at regular time intervals. \r
222   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process, \r
223   *       The the SysTick interrupt must have higher priority (numerically lower) \r
224   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.\r
225   *       The function is declared as __weak  to be overwritten  in case of other\r
226   *       implementation  in user file.\r
227   * @param TickPriority: Tick interrupt priority.\r
228   * @retval HAL status\r
229   */\r
230 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)\r
231 {\r
232   /*Configure the SysTick to have interrupt in 1ms time basis*/\r
233   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);\r
234 \r
235   /*Configure the SysTick IRQ priority */\r
236   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);\r
237 \r
238   /* Return function status */\r
239   return HAL_OK;\r
240 }\r
241 \r
242 /**\r
243   * @}\r
244   */\r
245 \r
246 /** @defgroup HAL_Group2 HAL Control functions \r
247  *  @brief    HAL Control functions\r
248  *\r
249 @verbatim\r
250  ===============================================================================\r
251                       ##### HAL Control functions #####\r
252  ===============================================================================\r
253     [..]  This section provides functions allowing to:\r
254       (+) Provide a tick value in millisecond\r
255       (+) Provide a blocking delay in millisecond\r
256       (+) Suspend the time base source interrupt\r
257       (+) Resume the time base source interrupt\r
258       (+) Get the HAL API driver version\r
259       (+) Get the device identifier\r
260       (+) Get the device revision identifier\r
261       (+) Enable/Disable Debug module during SLEEP mode\r
262       (+) Enable/Disable Debug module during STOP mode\r
263       (+) Enable/Disable Debug module during STANDBY mode\r
264 \r
265 @endverbatim\r
266   * @{\r
267   */\r
268 \r
269 /**\r
270   * @brief This function is called to increment  a global variable "uwTick"\r
271   *        used as application time base.\r
272   * @note In the default implementation, this variable is incremented each 1ms\r
273   *       in Systick ISR.\r
274  * @note This function is declared as __weak to be overwritten in case of other \r
275   *      implementations in user file.\r
276   * @retval None\r
277   */\r
278 __weak void HAL_IncTick(void)\r
279 {\r
280   uwTick++;\r
281 }\r
282 \r
283 /**\r
284   * @brief Provides a tick value in millisecond.\r
285   * @note This function is declared as __weak to be overwritten in case of other \r
286   *       implementations in user file.\r
287   * @retval tick value\r
288   */\r
289 __weak uint32_t HAL_GetTick(void)\r
290 {\r
291   return uwTick;\r
292 }\r
293 \r
294 /**\r
295   * @brief This function provides accurate delay (in milliseconds) based \r
296   *        on variable incremented.\r
297   * @note In the default implementation , SysTick timer is the source of time base.\r
298   *       It is used to generate interrupts at regular time intervals where uwTick\r
299   *       is incremented.\r
300   * @note ThiS function is declared as __weak to be overwritten in case of other\r
301   *       implementations in user file.\r
302   * @param Delay: specifies the delay time length, in milliseconds.\r
303   * @retval None\r
304   */\r
305 __weak void HAL_Delay(__IO uint32_t Delay)\r
306 {\r
307   uint32_t tickstart = 0;\r
308   tickstart = HAL_GetTick();\r
309   while((HAL_GetTick() - tickstart) < Delay)\r
310   {\r
311   }\r
312 }\r
313 \r
314 /**\r
315   * @brief Suspend Tick increment.\r
316   * @note In the default implementation , SysTick timer is the source of time base. It is\r
317   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()\r
318   *       is called, the the SysTick interrupt will be disabled and so Tick increment \r
319   *       is suspended.\r
320   * @note This function is declared as __weak to be overwritten in case of other\r
321   *       implementations in user file.\r
322   * @retval None\r
323   */\r
324 __weak void HAL_SuspendTick(void)\r
325 {\r
326   /* Disable SysTick Interrupt */\r
327   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;\r
328 }\r
329 \r
330 /**\r
331   * @brief Resume Tick increment.\r
332   * @note In the default implementation , SysTick timer is the source of time base. It is\r
333   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()\r
334   *       is called, the the SysTick interrupt will be enabled and so Tick increment \r
335   *       is resumed.\r
336   * @note This function is declared as __weak to be overwritten in case of other\r
337   *       implementations in user file.\r
338   * @retval None\r
339   */\r
340 __weak void HAL_ResumeTick(void)\r
341 {\r
342   /* Enable SysTick Interrupt */\r
343   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;\r
344 }\r
345 \r
346 /**\r
347   * @brief  Returns the HAL revision\r
348   * @retval version : 0xXYZR (8bits for each decimal, R for RC)\r
349   */\r
350 uint32_t HAL_GetHalVersion(void)\r
351 {\r
352  return __STM32F7xx_HAL_VERSION;\r
353 }\r
354 \r
355 /**\r
356   * @brief  Returns the device revision identifier.\r
357   * @retval Device revision identifier\r
358   */\r
359 uint32_t HAL_GetREVID(void)\r
360 {\r
361    return((DBGMCU->IDCODE) >> 16);\r
362 }\r
363 \r
364 /**\r
365   * @brief  Returns the device identifier.\r
366   * @retval Device identifier\r
367   */\r
368 uint32_t HAL_GetDEVID(void)\r
369 {\r
370    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);\r
371 }\r
372 \r
373 /**\r
374   * @brief  Enable the Debug Module during SLEEP mode\r
375   * @retval None\r
376   */\r
377 void HAL_DBGMCU_EnableDBGSleepMode(void)\r
378 {\r
379   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);\r
380 }\r
381 \r
382 /**\r
383   * @brief  Disable the Debug Module during SLEEP mode\r
384   * @retval None\r
385   */\r
386 void HAL_DBGMCU_DisableDBGSleepMode(void)\r
387 {\r
388   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);\r
389 }\r
390 \r
391 /**\r
392   * @brief  Enable the Debug Module during STOP mode\r
393   * @retval None\r
394   */\r
395 void HAL_DBGMCU_EnableDBGStopMode(void)\r
396 {\r
397   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);\r
398 }\r
399 \r
400 /**\r
401   * @brief  Disable the Debug Module during STOP mode\r
402   * @retval None\r
403   */\r
404 void HAL_DBGMCU_DisableDBGStopMode(void)\r
405 {\r
406   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);\r
407 }\r
408 \r
409 /**\r
410   * @brief  Enable the Debug Module during STANDBY mode\r
411   * @retval None\r
412   */\r
413 void HAL_DBGMCU_EnableDBGStandbyMode(void)\r
414 {\r
415   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);\r
416 }\r
417 \r
418 /**\r
419   * @brief  Disable the Debug Module during STANDBY mode\r
420   * @retval None\r
421   */\r
422 void HAL_DBGMCU_DisableDBGStandbyMode(void)\r
423 {\r
424   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);\r
425 }\r
426 \r
427 /**\r
428   * @brief  Enables the I/O Compensation Cell.\r
429   * @note   The I/O compensation cell can be used only when the device supply\r
430   *         voltage ranges from 2.4 to 3.6 V.  \r
431   * @retval None\r
432   */\r
433 void HAL_EnableCompensationCell(void)\r
434 {\r
435   SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;\r
436 }\r
437 \r
438 /**\r
439   * @brief  Power-down the I/O Compensation Cell.\r
440   * @note   The I/O compensation cell can be used only when the device supply\r
441   *         voltage ranges from 2.4 to 3.6 V.  \r
442   * @retval None\r
443   */\r
444 void HAL_DisableCompensationCell(void)\r
445 {\r
446   SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);\r
447 }\r
448 \r
449 /**\r
450   * @brief  Enables the FMC Memory Mapping Swapping.\r
451   *   \r
452   * @note   SDRAM is accessible at 0x60000000 \r
453   *         and NOR/RAM is accessible at 0xC0000000   \r
454   *\r
455   * @retval None\r
456   */\r
457 void HAL_EnableFMCMemorySwapping(void)\r
458 {\r
459   SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;\r
460 }\r
461 \r
462 /**\r
463   * @brief  Disables the FMC Memory Mapping Swapping\r
464   *   \r
465   * @note   SDRAM is accessible at 0xC0000000 (default mapping)  \r
466   *         and NOR/RAM is accessible at 0x60000000 (default mapping)    \r
467   *           \r
468   * @retval None\r
469   */\r
470 void HAL_DisableFMCMemorySwapping(void)\r
471 {\r
472 \r
473   SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);\r
474 }\r
475 \r
476 /**\r
477   * @}\r
478   */\r
479 \r
480 /**\r
481   * @}\r
482   */\r
483 \r
484 /**\r
485   * @}\r
486   */\r
487 \r
488 /**\r
489   * @}\r
490   */\r
491 \r
492 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r