stm32f7xx_hal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal.c
  4. * @author MCD Application Team
  5. * @brief HAL module driver.
  6. * This is the common part of the HAL initialization
  7. *
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * Copyright (c) 2017 STMicroelectronics.
  12. * All rights reserved.
  13. *
  14. * This software is licensed under terms that can be found in the LICENSE file
  15. * in the root directory of this software component.
  16. * If no LICENSE file comes with this software, it is provided AS-IS.
  17. *
  18. ******************************************************************************
  19. @verbatim
  20. ==============================================================================
  21. ##### How to use this driver #####
  22. ==============================================================================
  23. [..]
  24. The common HAL driver contains a set of generic and common APIs that can be
  25. used by the PPP peripheral drivers and the user to start using the HAL.
  26. [..]
  27. The HAL contains two APIs' categories:
  28. (+) Common HAL APIs
  29. (+) Services HAL APIs
  30. @endverbatim
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f7xx_hal.h"
  35. /** @addtogroup STM32F7xx_HAL_Driver
  36. * @{
  37. */
  38. /** @defgroup HAL HAL
  39. * @brief HAL module driver.
  40. * @{
  41. */
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /** @addtogroup HAL_Private_Constants
  45. * @{
  46. */
  47. /**
  48. * @brief STM32F7xx HAL Driver version number V1.2.10
  49. */
  50. #define __STM32F7xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
  51. #define __STM32F7xx_HAL_VERSION_SUB1 (0x02) /*!< [23:16] sub1 version */
  52. #define __STM32F7xx_HAL_VERSION_SUB2 (0x0A) /*!< [15:8] sub2 version */
  53. #define __STM32F7xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
  54. #define __STM32F7xx_HAL_VERSION ((__STM32F7xx_HAL_VERSION_MAIN << 24)\
  55. |(__STM32F7xx_HAL_VERSION_SUB1 << 16)\
  56. |(__STM32F7xx_HAL_VERSION_SUB2 << 8 )\
  57. |(__STM32F7xx_HAL_VERSION_RC))
  58. #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
  59. /**
  60. * @}
  61. */
  62. /* Private macro -------------------------------------------------------------*/
  63. /* Exported variables ---------------------------------------------------------*/
  64. /** @addtogroup HAL_Exported_Variables
  65. * @{
  66. */
  67. __IO uint32_t uwTick;
  68. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
  69. HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  70. /**
  71. * @}
  72. */
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Private functions ---------------------------------------------------------*/
  75. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  76. * @{
  77. */
  78. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  79. * @brief Initialization and de-initialization functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. ##### Initialization and Configuration functions #####
  84. ===============================================================================
  85. [..] This section provides functions allowing to:
  86. (+) Initializes the Flash interface the NVIC allocation and initial clock
  87. configuration. It initializes the systick also when timeout is needed
  88. and the backup domain when enabled.
  89. (+) De-Initializes common part of the HAL.
  90. (+) Configure the time base source to have 1ms time base with a dedicated
  91. Tick interrupt priority.
  92. (++) SysTick timer is used by default as source of time base, but user
  93. can eventually implement his proper time base source (a general purpose
  94. timer for example or other time source), keeping in mind that Time base
  95. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  96. handled in milliseconds basis.
  97. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  98. at the beginning of the program after reset by HAL_Init() or at any time
  99. when clock is configured, by HAL_RCC_ClockConfig().
  100. (++) Source of time base is configured to generate interrupts at regular
  101. time intervals. Care must be taken if HAL_Delay() is called from a
  102. peripheral ISR process, the Tick interrupt line must have higher priority
  103. (numerically lower) than the peripheral interrupt. Otherwise the caller
  104. ISR process will be blocked.
  105. (++) functions affecting time base configurations are declared as __weak
  106. to make override possible in case of other implementations in user file.
  107. @endverbatim
  108. * @{
  109. */
  110. /**
  111. * @brief This function is used to initialize the HAL Library; it must be the first
  112. * instruction to be executed in the main program (before to call any other
  113. * HAL function), it performs the following:
  114. * Configure the Flash prefetch, and instruction cache through ART accelerator.
  115. * Configures the SysTick to generate an interrupt each 1 millisecond,
  116. * which is clocked by the HSI (at this stage, the clock is not yet
  117. * configured and thus the system is running from the internal HSI at 16 MHz).
  118. * Set NVIC Group Priority to 4.
  119. * Calls the HAL_MspInit() callback function defined in user file
  120. * "stm32f7xx_hal_msp.c" to do the global low level hardware initialization
  121. *
  122. * @note SysTick is used as time base for the HAL_Delay() function, the application
  123. * need to ensure that the SysTick time base is always set to 1 millisecond
  124. * to have correct HAL operation.
  125. * @retval HAL status
  126. */
  127. HAL_StatusTypeDef HAL_Init(void)
  128. {
  129. /* Configure Instruction cache through ART accelerator */
  130. #if (ART_ACCLERATOR_ENABLE != 0)
  131. __HAL_FLASH_ART_ENABLE();
  132. #endif /* ART_ACCLERATOR_ENABLE */
  133. /* Configure Flash prefetch */
  134. #if (PREFETCH_ENABLE != 0U)
  135. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  136. #endif /* PREFETCH_ENABLE */
  137. /* Set Interrupt Group Priority */
  138. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  139. /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  140. HAL_InitTick(TICK_INT_PRIORITY);
  141. /* Init the low level hardware */
  142. HAL_MspInit();
  143. /* Return function status */
  144. return HAL_OK;
  145. }
  146. /**
  147. * @brief This function de-Initializes common part of the HAL and stops the systick.
  148. * This function is optional.
  149. * @retval HAL status
  150. */
  151. HAL_StatusTypeDef HAL_DeInit(void)
  152. {
  153. /* Reset of all peripherals */
  154. __HAL_RCC_APB1_FORCE_RESET();
  155. __HAL_RCC_APB1_RELEASE_RESET();
  156. __HAL_RCC_APB2_FORCE_RESET();
  157. __HAL_RCC_APB2_RELEASE_RESET();
  158. __HAL_RCC_AHB1_FORCE_RESET();
  159. __HAL_RCC_AHB1_RELEASE_RESET();
  160. __HAL_RCC_AHB2_FORCE_RESET();
  161. __HAL_RCC_AHB2_RELEASE_RESET();
  162. __HAL_RCC_AHB3_FORCE_RESET();
  163. __HAL_RCC_AHB3_RELEASE_RESET();
  164. /* De-Init the low level hardware */
  165. HAL_MspDeInit();
  166. /* Return function status */
  167. return HAL_OK;
  168. }
  169. /**
  170. * @brief Initialize the MSP.
  171. * @retval None
  172. */
  173. __weak void HAL_MspInit(void)
  174. {
  175. /* NOTE : This function should not be modified, when the callback is needed,
  176. the HAL_MspInit could be implemented in the user file
  177. */
  178. }
  179. /**
  180. * @brief DeInitializes the MSP.
  181. * @retval None
  182. */
  183. __weak void HAL_MspDeInit(void)
  184. {
  185. /* NOTE : This function should not be modified, when the callback is needed,
  186. the HAL_MspDeInit could be implemented in the user file
  187. */
  188. }
  189. /**
  190. * @brief This function configures the source of the time base.
  191. * The time source is configured to have 1ms time base with a dedicated
  192. * Tick interrupt priority.
  193. * @note This function is called automatically at the beginning of program after
  194. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  195. * @note In the default implementation, SysTick timer is the source of time base.
  196. * It is used to generate interrupts at regular time intervals.
  197. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  198. * The SysTick interrupt must have higher priority (numerically lower)
  199. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  200. * The function is declared as __weak to be overwritten in case of other
  201. * implementation in user file.
  202. * @param TickPriority Tick interrupt priority.
  203. * @retval HAL status
  204. */
  205. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  206. {
  207. /* Configure the SysTick to have interrupt in 1ms time basis*/
  208. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
  209. {
  210. return HAL_ERROR;
  211. }
  212. /* Configure the SysTick IRQ priority */
  213. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  214. {
  215. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  216. uwTickPrio = TickPriority;
  217. }
  218. else
  219. {
  220. return HAL_ERROR;
  221. }
  222. /* Return function status */
  223. return HAL_OK;
  224. }
  225. /**
  226. * @}
  227. */
  228. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  229. * @brief HAL Control functions
  230. *
  231. @verbatim
  232. ===============================================================================
  233. ##### HAL Control functions #####
  234. ===============================================================================
  235. [..] This section provides functions allowing to:
  236. (+) Provide a tick value in millisecond
  237. (+) Provide a blocking delay in millisecond
  238. (+) Suspend the time base source interrupt
  239. (+) Resume the time base source interrupt
  240. (+) Get the HAL API driver version
  241. (+) Get the device identifier
  242. (+) Get the device revision identifier
  243. (+) Enable/Disable Debug module during SLEEP mode
  244. (+) Enable/Disable Debug module during STOP mode
  245. (+) Enable/Disable Debug module during STANDBY mode
  246. @endverbatim
  247. * @{
  248. */
  249. /**
  250. * @brief This function is called to increment a global variable "uwTick"
  251. * used as application time base.
  252. * @note In the default implementation, this variable is incremented each 1ms
  253. * in SysTick ISR.
  254. * @note This function is declared as __weak to be overwritten in case of other
  255. * implementations in user file.
  256. * @retval None
  257. */
  258. __weak void HAL_IncTick(void)
  259. {
  260. uwTick += uwTickFreq;
  261. }
  262. /**
  263. * @brief Provides a tick value in millisecond.
  264. * @note This function is declared as __weak to be overwritten in case of other
  265. * implementations in user file.
  266. * @retval tick value
  267. */
  268. __weak uint32_t HAL_GetTick(void)
  269. {
  270. return uwTick;
  271. }
  272. /**
  273. * @brief This function returns a tick priority.
  274. * @retval tick priority
  275. */
  276. uint32_t HAL_GetTickPrio(void)
  277. {
  278. return uwTickPrio;
  279. }
  280. /**
  281. * @brief Set new tick Freq.
  282. * @retval Status
  283. */
  284. HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
  285. {
  286. HAL_StatusTypeDef status = HAL_OK;
  287. HAL_TickFreqTypeDef prevTickFreq;
  288. assert_param(IS_TICKFREQ(Freq));
  289. if (uwTickFreq != Freq)
  290. {
  291. /* Back up uwTickFreq frequency */
  292. prevTickFreq = uwTickFreq;
  293. /* Update uwTickFreq global variable used by HAL_InitTick() */
  294. uwTickFreq = Freq;
  295. /* Apply the new tick Freq */
  296. status = HAL_InitTick(uwTickPrio);
  297. if (status != HAL_OK)
  298. {
  299. /* Restore previous tick frequency */
  300. uwTickFreq = prevTickFreq;
  301. }
  302. }
  303. return status;
  304. }
  305. /**
  306. * @brief Return tick frequency.
  307. * @retval tick period in Hz
  308. */
  309. HAL_TickFreqTypeDef HAL_GetTickFreq(void)
  310. {
  311. return uwTickFreq;
  312. }
  313. /**
  314. * @brief This function provides minimum delay (in milliseconds) based
  315. * on variable incremented.
  316. * @note In the default implementation , SysTick timer is the source of time base.
  317. * It is used to generate interrupts at regular time intervals where uwTick
  318. * is incremented.
  319. * @note This function is declared as __weak to be overwritten in case of other
  320. * implementations in user file.
  321. * @param Delay specifies the delay time length, in milliseconds.
  322. * @retval None
  323. */
  324. __weak void HAL_Delay(uint32_t Delay)
  325. {
  326. uint32_t tickstart = HAL_GetTick();
  327. uint32_t wait = Delay;
  328. /* Add a freq to guarantee minimum wait */
  329. if (wait < HAL_MAX_DELAY)
  330. {
  331. wait += (uint32_t)(uwTickFreq);
  332. }
  333. while ((HAL_GetTick() - tickstart) < wait)
  334. {
  335. }
  336. }
  337. /**
  338. * @brief Suspend Tick increment.
  339. * @note In the default implementation , SysTick timer is the source of time base. It is
  340. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  341. * is called, the SysTick interrupt will be disabled and so Tick increment
  342. * is suspended.
  343. * @note This function is declared as __weak to be overwritten in case of other
  344. * implementations in user file.
  345. * @retval None
  346. */
  347. __weak void HAL_SuspendTick(void)
  348. {
  349. /* Disable SysTick Interrupt */
  350. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  351. }
  352. /**
  353. * @brief Resume Tick increment.
  354. * @note In the default implementation , SysTick timer is the source of time base. It is
  355. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  356. * is called, the SysTick interrupt will be enabled and so Tick increment
  357. * is resumed.
  358. * @note This function is declared as __weak to be overwritten in case of other
  359. * implementations in user file.
  360. * @retval None
  361. */
  362. __weak void HAL_ResumeTick(void)
  363. {
  364. /* Enable SysTick Interrupt */
  365. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  366. }
  367. /**
  368. * @brief Returns the HAL revision
  369. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  370. */
  371. uint32_t HAL_GetHalVersion(void)
  372. {
  373. return __STM32F7xx_HAL_VERSION;
  374. }
  375. /**
  376. * @brief Returns the device revision identifier.
  377. * @retval Device revision identifier
  378. */
  379. uint32_t HAL_GetREVID(void)
  380. {
  381. return((DBGMCU->IDCODE) >> 16U);
  382. }
  383. /**
  384. * @brief Returns the device identifier.
  385. * @retval Device identifier
  386. */
  387. uint32_t HAL_GetDEVID(void)
  388. {
  389. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  390. }
  391. /**
  392. * @brief Returns first word of the unique device identifier (UID based on 96 bits)
  393. * @retval Device identifier
  394. */
  395. uint32_t HAL_GetUIDw0(void)
  396. {
  397. return(READ_REG(*((uint32_t *)UID_BASE)));
  398. }
  399. /**
  400. * @brief Returns second word of the unique device identifier (UID based on 96 bits)
  401. * @retval Device identifier
  402. */
  403. uint32_t HAL_GetUIDw1(void)
  404. {
  405. return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  406. }
  407. /**
  408. * @brief Returns third word of the unique device identifier (UID based on 96 bits)
  409. * @retval Device identifier
  410. */
  411. uint32_t HAL_GetUIDw2(void)
  412. {
  413. return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  414. }
  415. /**
  416. * @brief Enable the Debug Module during SLEEP mode
  417. * @retval None
  418. */
  419. void HAL_DBGMCU_EnableDBGSleepMode(void)
  420. {
  421. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  422. }
  423. /**
  424. * @brief Disable the Debug Module during SLEEP mode
  425. * @retval None
  426. */
  427. void HAL_DBGMCU_DisableDBGSleepMode(void)
  428. {
  429. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  430. }
  431. /**
  432. * @brief Enable the Debug Module during STOP mode
  433. * @retval None
  434. */
  435. void HAL_DBGMCU_EnableDBGStopMode(void)
  436. {
  437. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  438. }
  439. /**
  440. * @brief Disable the Debug Module during STOP mode
  441. * @retval None
  442. */
  443. void HAL_DBGMCU_DisableDBGStopMode(void)
  444. {
  445. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  446. }
  447. /**
  448. * @brief Enable the Debug Module during STANDBY mode
  449. * @retval None
  450. */
  451. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  452. {
  453. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  454. }
  455. /**
  456. * @brief Disable the Debug Module during STANDBY mode
  457. * @retval None
  458. */
  459. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  460. {
  461. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  462. }
  463. /**
  464. * @brief Enables the I/O Compensation Cell.
  465. * @note The I/O compensation cell can be used only when the device supply
  466. * voltage ranges from 2.4 to 3.6 V.
  467. * @retval None
  468. */
  469. void HAL_EnableCompensationCell(void)
  470. {
  471. SYSCFG->CMPCR |= SYSCFG_CMPCR_CMP_PD;
  472. }
  473. /**
  474. * @brief Power-down 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
  478. */
  479. void HAL_DisableCompensationCell(void)
  480. {
  481. SYSCFG->CMPCR &= (uint32_t)~((uint32_t)SYSCFG_CMPCR_CMP_PD);
  482. }
  483. /**
  484. * @brief Enables the FMC Memory Mapping Swapping.
  485. *
  486. * @note SDRAM is accessible at 0x60000000
  487. * and NOR/RAM is accessible at 0xC0000000
  488. *
  489. * @retval None
  490. */
  491. void HAL_EnableFMCMemorySwapping(void)
  492. {
  493. SYSCFG->MEMRMP |= SYSCFG_MEMRMP_SWP_FMC_0;
  494. }
  495. /**
  496. * @brief Disables the FMC Memory Mapping Swapping
  497. *
  498. * @note SDRAM is accessible at 0xC0000000 (default mapping)
  499. * and NOR/RAM is accessible at 0x60000000 (default mapping)
  500. *
  501. * @retval None
  502. */
  503. void HAL_DisableFMCMemorySwapping(void)
  504. {
  505. SYSCFG->MEMRMP &= (uint32_t)~((uint32_t)SYSCFG_MEMRMP_SWP_FMC);
  506. }
  507. #if defined (STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
  508. /**
  509. * @brief Enable the Internal FLASH Bank Swapping.
  510. *
  511. * @note This function can be used only for STM32F77xx/STM32F76xx devices.
  512. *
  513. * @note Flash Bank2 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
  514. * and Flash Bank1 mapped at 0x08100000 (AXI) (aliased at 0x00300000 (TCM))
  515. *
  516. * @retval None
  517. */
  518. void HAL_EnableMemorySwappingBank(void)
  519. {
  520. SET_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
  521. }
  522. /**
  523. * @brief Disable the Internal FLASH Bank Swapping.
  524. *
  525. * @note This function can be used only for STM32F77xx/STM32F76xx devices.
  526. *
  527. * @note The default state : Flash Bank1 mapped at 0x08000000 (AXI) (aliased at 0x00200000 (TCM))
  528. * and Flash Bank2 mapped at 0x08100000 (AXI)( aliased at 0x00300000 (TCM))
  529. *
  530. * @retval None
  531. */
  532. void HAL_DisableMemorySwappingBank(void)
  533. {
  534. CLEAR_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_SWP_FB);
  535. }
  536. #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
  537. /**
  538. * @}
  539. */
  540. /**
  541. * @}
  542. */
  543. /**
  544. * @}
  545. */
  546. /**
  547. * @}
  548. */