stm32f4xx_hal.c 19 KB

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