stm32f4xx_hal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief HAL module driver.
  8. * This is the common part of the HAL initialization
  9. *
  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
  21. @endverbatim
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "stm32f4xx_hal.h"
  53. /** @addtogroup STM32F4xx_HAL_Driver
  54. * @{
  55. */
  56. /** @defgroup HAL HAL
  57. * @brief HAL module driver.
  58. * @{
  59. */
  60. /* Private typedef -----------------------------------------------------------*/
  61. /* Private define ------------------------------------------------------------*/
  62. /** @addtogroup HAL_Private_Constants
  63. * @{
  64. */
  65. /**
  66. * @brief STM32F4xx HAL Driver version number V1.4.3
  67. */
  68. #define __STM32F4xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
  69. #define __STM32F4xx_HAL_VERSION_SUB1 (0x04) /*!< [23:16] sub1 version */
  70. #define __STM32F4xx_HAL_VERSION_SUB2 (0x03) /*!< [15:8] sub2 version */
  71. #define __STM32F4xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
  72. #define __STM32F4xx_HAL_VERSION ((__STM32F4xx_HAL_VERSION_MAIN << 24)\
  73. |(__STM32F4xx_HAL_VERSION_SUB1 << 16)\
  74. |(__STM32F4xx_HAL_VERSION_SUB2 << 8 )\
  75. |(__STM32F4xx_HAL_VERSION_RC))
  76. #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
  77. /* ------------ RCC registers bit address in the alias region ----------- */
  78. #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
  79. /* --- MEMRMP Register ---*/
  80. /* Alias word address of UFB_MODE bit */
  81. #define MEMRMP_OFFSET SYSCFG_OFFSET
  82. #define UFB_MODE_BIT_NUMBER POSITION_VAL(SYSCFG_MEMRMP_UFB_MODE)
  83. #define UFB_MODE_BB (uint32_t)(PERIPH_BB_BASE + (MEMRMP_OFFSET * 32) + (UFB_MODE_BIT_NUMBER * 4))
  84. /* --- CMPCR Register ---*/
  85. /* Alias word address of CMP_PD bit */
  86. #define CMPCR_OFFSET (SYSCFG_OFFSET + 0x20)
  87. #define CMP_PD_BIT_NUMBER POSITION_VAL(SYSCFG_CMPCR_CMP_PD)
  88. #define CMPCR_CMP_PD_BB (uint32_t)(PERIPH_BB_BASE + (CMPCR_OFFSET * 32) + (CMP_PD_BIT_NUMBER * 4))
  89. /**
  90. * @}
  91. */
  92. /* Private macro -------------------------------------------------------------*/
  93. /* Private variables ---------------------------------------------------------*/
  94. /** @addtogroup HAL_Private_Variables
  95. * @{
  96. */
  97. static __IO uint32_t uwTick;
  98. /**
  99. * @}
  100. */
  101. /* Private function prototypes -----------------------------------------------*/
  102. /* Private functions ---------------------------------------------------------*/
  103. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  104. * @{
  105. */
  106. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  107. * @brief Initialization and de-initialization functions
  108. *
  109. @verbatim
  110. ===============================================================================
  111. ##### Initialization and de-initialization functions #####
  112. ===============================================================================
  113. [..] This section provides functions allowing to:
  114. (+) Initializes the Flash interface the NVIC allocation and initial clock
  115. configuration. It initializes the systick also when timeout is needed
  116. and the backup domain when enabled.
  117. (+) de-Initializes common part of the HAL
  118. (+) Configure The time base source to have 1ms time base with a dedicated
  119. Tick interrupt priority.
  120. (++) Systick timer is used by default as source of time base, but user
  121. can eventually implement his proper time base source (a general purpose
  122. timer for example or other time source), keeping in mind that Time base
  123. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  124. handled in milliseconds basis.
  125. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  126. at the beginning of the program after reset by HAL_Init() or at any time
  127. when clock is configured, by HAL_RCC_ClockConfig().
  128. (++) Source of time base is configured to generate interrupts at regular
  129. time intervals. Care must be taken if HAL_Delay() is called from a
  130. peripheral ISR process, the Tick interrupt line must have higher priority
  131. (numerically lower) than the peripheral interrupt. Otherwise the caller
  132. ISR process will be blocked.
  133. (++) functions affecting time base configurations are declared as __weak
  134. to make override possible in case of other implementations in user file.
  135. @endverbatim
  136. * @{
  137. */
  138. /**
  139. * @brief This function is used to initialize the HAL Library; it must be the first
  140. * instruction to be executed in the main program (before to call any other
  141. * HAL function), it performs the following:
  142. * Configure the Flash prefetch, instruction and Data caches.
  143. * Configures the SysTick to generate an interrupt each 1 millisecond,
  144. * which is clocked by the HSI (at this stage, the clock is not yet
  145. * configured and thus the system is running from the internal HSI at 16 MHz).
  146. * Set NVIC Group Priority to 4.
  147. * Calls the HAL_MspInit() callback function defined in user file
  148. * "stm32f4xx_hal_msp.c" to do the global low level hardware initialization
  149. *
  150. * @note SysTick is used as time base for the HAL_Delay() function, the application
  151. * need to ensure that the SysTick time base is always set to 1 millisecond
  152. * to have correct HAL operation.
  153. * @retval HAL status
  154. */
  155. HAL_StatusTypeDef HAL_Init(void)
  156. {
  157. /* Configure Flash prefetch, Instruction cache, Data cache */
  158. #if (INSTRUCTION_CACHE_ENABLE != 0)
  159. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  160. #endif /* INSTRUCTION_CACHE_ENABLE */
  161. #if (DATA_CACHE_ENABLE != 0)
  162. __HAL_FLASH_DATA_CACHE_ENABLE();
  163. #endif /* DATA_CACHE_ENABLE */
  164. #if (PREFETCH_ENABLE != 0)
  165. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  166. #endif /* PREFETCH_ENABLE */
  167. /* Set Interrupt Group Priority */
  168. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  169. /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  170. HAL_InitTick(TICK_INT_PRIORITY);
  171. /* Init the low level hardware */
  172. HAL_MspInit();
  173. /* Return function status */
  174. return HAL_OK;
  175. }
  176. /**
  177. * @brief This function de-Initializes common part of the HAL and stops the systick.
  178. * This function is optional.
  179. * @retval HAL status
  180. */
  181. HAL_StatusTypeDef HAL_DeInit(void)
  182. {
  183. /* Reset of all peripherals */
  184. __HAL_RCC_APB1_FORCE_RESET();
  185. __HAL_RCC_APB1_RELEASE_RESET();
  186. __HAL_RCC_APB2_FORCE_RESET();
  187. __HAL_RCC_APB2_RELEASE_RESET();
  188. __HAL_RCC_AHB1_FORCE_RESET();
  189. __HAL_RCC_AHB1_RELEASE_RESET();
  190. __HAL_RCC_AHB2_FORCE_RESET();
  191. __HAL_RCC_AHB2_RELEASE_RESET();
  192. __HAL_RCC_AHB3_FORCE_RESET();
  193. __HAL_RCC_AHB3_RELEASE_RESET();
  194. /* De-Init the low level hardware */
  195. HAL_MspDeInit();
  196. /* Return function status */
  197. return HAL_OK;
  198. }
  199. /**
  200. * @brief Initializes the MSP.
  201. * @retval None
  202. */
  203. __weak void HAL_MspInit(void)
  204. {
  205. /* NOTE : This function Should not be modified, when the callback is needed,
  206. the HAL_MspInit could be implemented in the user file
  207. */
  208. }
  209. /**
  210. * @brief DeInitializes the MSP.
  211. * @retval None
  212. */
  213. __weak void HAL_MspDeInit(void)
  214. {
  215. /* NOTE : This function Should not be modified, when the callback is needed,
  216. the HAL_MspDeInit could be implemented in the user file
  217. */
  218. }
  219. /**
  220. * @brief This function configures the source of the time base.
  221. * The time source is configured to have 1ms time base with a dedicated
  222. * Tick interrupt priority.
  223. * @note This function is called automatically at the beginning of program after
  224. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  225. * @note In the default implementation, SysTick timer is the source of time base.
  226. * It is used to generate interrupts at regular time intervals.
  227. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  228. * The the SysTick interrupt must have higher priority (numerically lower)
  229. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  230. * The function is declared as __weak to be overwritten in case of other
  231. * implementation in user file.
  232. * @param TickPriority: Tick interrupt priority.
  233. * @retval HAL status
  234. */
  235. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  236. {
  237. /*Configure the SysTick to have interrupt in 1ms time basis*/
  238. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  239. /*Configure the SysTick IRQ priority */
  240. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  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++;
  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 provides accurate delay (in milliseconds) based
  293. * on variable incremented.
  294. * @note In the default implementation , SysTick timer is the source of time base.
  295. * It is used to generate interrupts at regular time intervals where uwTick
  296. * is incremented.
  297. * @note This function is declared as __weak to be overwritten in case of other
  298. * implementations in user file.
  299. * @param Delay: specifies the delay time length, in milliseconds.
  300. * @retval None
  301. */
  302. __weak void HAL_Delay(__IO uint32_t Delay)
  303. {
  304. uint32_t tickstart = 0;
  305. tickstart = HAL_GetTick();
  306. while((HAL_GetTick() - tickstart) < Delay)
  307. {
  308. }
  309. }
  310. /**
  311. * @brief Suspend Tick increment.
  312. * @note In the default implementation , SysTick timer is the source of time base. It is
  313. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  314. * is called, the SysTick interrupt will be disabled and so Tick increment
  315. * is suspended.
  316. * @note This function is declared as __weak to be overwritten in case of other
  317. * implementations in user file.
  318. * @retval None
  319. */
  320. __weak void HAL_SuspendTick(void)
  321. {
  322. /* Disable SysTick Interrupt */
  323. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  324. }
  325. /**
  326. * @brief Resume Tick increment.
  327. * @note In the default implementation , SysTick timer is the source of time base. It is
  328. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  329. * is called, the SysTick interrupt will be enabled and so Tick increment
  330. * is resumed.
  331. * @note This function is declared as __weak to be overwritten in case of other
  332. * implementations in user file.
  333. * @retval None
  334. */
  335. __weak void HAL_ResumeTick(void)
  336. {
  337. /* Enable SysTick Interrupt */
  338. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  339. }
  340. /**
  341. * @brief Returns the HAL revision
  342. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  343. */
  344. uint32_t HAL_GetHalVersion(void)
  345. {
  346. return __STM32F4xx_HAL_VERSION;
  347. }
  348. /**
  349. * @brief Returns the device revision identifier.
  350. * @retval Device revision identifier
  351. */
  352. uint32_t HAL_GetREVID(void)
  353. {
  354. return((DBGMCU->IDCODE) >> 16);
  355. }
  356. /**
  357. * @brief Returns the device identifier.
  358. * @retval Device identifier
  359. */
  360. uint32_t HAL_GetDEVID(void)
  361. {
  362. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  363. }
  364. /**
  365. * @brief Enable the Debug Module during SLEEP mode
  366. * @retval None
  367. */
  368. void HAL_DBGMCU_EnableDBGSleepMode(void)
  369. {
  370. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  371. }
  372. /**
  373. * @brief Disable the Debug Module during SLEEP mode
  374. * @retval None
  375. */
  376. void HAL_DBGMCU_DisableDBGSleepMode(void)
  377. {
  378. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  379. }
  380. /**
  381. * @brief Enable the Debug Module during STOP mode
  382. * @retval None
  383. */
  384. void HAL_DBGMCU_EnableDBGStopMode(void)
  385. {
  386. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  387. }
  388. /**
  389. * @brief Disable the Debug Module during STOP mode
  390. * @retval None
  391. */
  392. void HAL_DBGMCU_DisableDBGStopMode(void)
  393. {
  394. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  395. }
  396. /**
  397. * @brief Enable the Debug Module during STANDBY mode
  398. * @retval None
  399. */
  400. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  401. {
  402. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  403. }
  404. /**
  405. * @brief Disable the Debug Module during STANDBY mode
  406. * @retval None
  407. */
  408. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  409. {
  410. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  411. }
  412. /**
  413. * @brief Enables the I/O Compensation Cell.
  414. * @note The I/O compensation cell can be used only when the device supply
  415. * voltage ranges from 2.4 to 3.6 V.
  416. * @retval None
  417. */
  418. void HAL_EnableCompensationCell(void)
  419. {
  420. *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)ENABLE;
  421. }
  422. /**
  423. * @brief Power-down the I/O Compensation Cell.
  424. * @note The I/O compensation cell can be used only when the device supply
  425. * voltage ranges from 2.4 to 3.6 V.
  426. * @retval None
  427. */
  428. void HAL_DisableCompensationCell(void)
  429. {
  430. *(__IO uint32_t *)CMPCR_CMP_PD_BB = (uint32_t)DISABLE;
  431. }
  432. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) ||\
  433. defined(STM32F469xx) || defined(STM32F479xx)
  434. /**
  435. * @brief Enables the Internal FLASH Bank Swapping.
  436. *
  437. * @note This function can be used only for STM32F42xxx/43xxx devices.
  438. *
  439. * @note Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
  440. * and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
  441. *
  442. * @retval None
  443. */
  444. void HAL_EnableMemorySwappingBank(void)
  445. {
  446. *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)ENABLE;
  447. }
  448. /**
  449. * @brief Disables the Internal FLASH Bank Swapping.
  450. *
  451. * @note This function can be used only for STM32F42xxx/43xxx devices.
  452. *
  453. * @note The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000)
  454. * and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
  455. *
  456. * @retval None
  457. */
  458. void HAL_DisableMemorySwappingBank(void)
  459. {
  460. *(__IO uint32_t *)UFB_MODE_BB = (uint32_t)DISABLE;
  461. }
  462. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  463. /**
  464. * @}
  465. */
  466. /**
  467. * @}
  468. */
  469. /**
  470. * @}
  471. */
  472. /**
  473. * @}
  474. */
  475. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/