stm32f1xx_hal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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) 2016 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 "stm32f1xx_hal.h"
  35. /** @addtogroup STM32F1xx_HAL_Driver
  36. * @{
  37. */
  38. /** @defgroup HAL HAL
  39. * @brief HAL module driver.
  40. * @{
  41. */
  42. #ifdef HAL_MODULE_ENABLED
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /** @defgroup HAL_Private_Constants HAL Private Constants
  46. * @{
  47. */
  48. /**
  49. * @brief STM32F1xx HAL Driver version number
  50. */
  51. #define __STM32F1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  52. #define __STM32F1xx_HAL_VERSION_SUB1 (0x01U) /*!< [23:16] sub1 version */
  53. #define __STM32F1xx_HAL_VERSION_SUB2 (0x09U) /*!< [15:8] sub2 version */
  54. #define __STM32F1xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  55. #define __STM32F1xx_HAL_VERSION ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
  56. |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
  57. |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
  58. |(__STM32F1xx_HAL_VERSION_RC))
  59. #define IDCODE_DEVID_MASK 0x00000FFFU
  60. /**
  61. * @}
  62. */
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /** @defgroup HAL_Private_Variables HAL Private Variables
  66. * @{
  67. */
  68. __IO uint32_t uwTick;
  69. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
  70. HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  71. /**
  72. * @}
  73. */
  74. /* Private function prototypes -----------------------------------------------*/
  75. /* Exported functions ---------------------------------------------------------*/
  76. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  77. * @{
  78. */
  79. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  80. * @brief Initialization and de-initialization functions
  81. *
  82. @verbatim
  83. ===============================================================================
  84. ##### Initialization and de-initialization functions #####
  85. ===============================================================================
  86. [..] This section provides functions allowing to:
  87. (+) Initializes the Flash interface, the NVIC allocation and initial clock
  88. configuration. It initializes the systick also when timeout is needed
  89. and the backup domain when enabled.
  90. (+) de-Initializes common part of the HAL.
  91. (+) Configure The time base source to have 1ms time base with a dedicated
  92. Tick interrupt priority.
  93. (++) SysTick timer is used by default as source of time base, but user
  94. can eventually implement his proper time base source (a general purpose
  95. timer for example or other time source), keeping in mind that Time base
  96. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  97. handled in milliseconds basis.
  98. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  99. at the beginning of the program after reset by HAL_Init() or at any time
  100. when clock is configured, by HAL_RCC_ClockConfig().
  101. (++) Source of time base is configured to generate interrupts at regular
  102. time intervals. Care must be taken if HAL_Delay() is called from a
  103. peripheral ISR process, the Tick interrupt line must have higher priority
  104. (numerically lower) than the peripheral interrupt. Otherwise the caller
  105. ISR process will be blocked.
  106. (++) functions affecting time base configurations are declared as __weak
  107. to make override possible in case of other implementations in user file.
  108. @endverbatim
  109. * @{
  110. */
  111. /**
  112. * @brief This function is used to initialize the HAL Library; it must be the first
  113. * instruction to be executed in the main program (before to call any other
  114. * HAL function), it performs the following:
  115. * Configure the Flash prefetch.
  116. * Configures the SysTick to generate an interrupt each 1 millisecond,
  117. * which is clocked by the HSI (at this stage, the clock is not yet
  118. * configured and thus the system is running from the internal HSI at 16 MHz).
  119. * Set NVIC Group Priority to 4.
  120. * Calls the HAL_MspInit() callback function defined in user file
  121. * "stm32f1xx_hal_msp.c" to do the global low level hardware initialization
  122. *
  123. * @note SysTick is used as time base for the HAL_Delay() function, the application
  124. * need to ensure that the SysTick time base is always set to 1 millisecond
  125. * to have correct HAL operation.
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_Init(void)
  129. {
  130. /* Configure Flash prefetch */
  131. #if (PREFETCH_ENABLE != 0)
  132. #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
  133. defined(STM32F102x6) || defined(STM32F102xB) || \
  134. defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
  135. defined(STM32F105xC) || defined(STM32F107xC)
  136. /* Prefetch buffer is not available on value line devices */
  137. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  138. #endif
  139. #endif /* PREFETCH_ENABLE */
  140. /* Set Interrupt Group Priority */
  141. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  142. /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  143. HAL_InitTick(TICK_INT_PRIORITY);
  144. /* Init the low level hardware */
  145. HAL_MspInit();
  146. /* Return function status */
  147. return HAL_OK;
  148. }
  149. /**
  150. * @brief This function de-Initializes common part of the HAL and stops the systick.
  151. * of time base.
  152. * @note This function is optional.
  153. * @retval HAL status
  154. */
  155. HAL_StatusTypeDef HAL_DeInit(void)
  156. {
  157. /* Reset of all peripherals */
  158. __HAL_RCC_APB1_FORCE_RESET();
  159. __HAL_RCC_APB1_RELEASE_RESET();
  160. __HAL_RCC_APB2_FORCE_RESET();
  161. __HAL_RCC_APB2_RELEASE_RESET();
  162. #if defined(STM32F105xC) || defined(STM32F107xC)
  163. __HAL_RCC_AHB_FORCE_RESET();
  164. __HAL_RCC_AHB_RELEASE_RESET();
  165. #endif
  166. /* De-Init the low level hardware */
  167. HAL_MspDeInit();
  168. /* Return function status */
  169. return HAL_OK;
  170. }
  171. /**
  172. * @brief Initialize the MSP.
  173. * @retval None
  174. */
  175. __weak void HAL_MspInit(void)
  176. {
  177. /* NOTE : This function should not be modified, when the callback is needed,
  178. the HAL_MspInit could be implemented in the user file
  179. */
  180. }
  181. /**
  182. * @brief DeInitializes the MSP.
  183. * @retval None
  184. */
  185. __weak void HAL_MspDeInit(void)
  186. {
  187. /* NOTE : This function should not be modified, when the callback is needed,
  188. the HAL_MspDeInit could be implemented in the user file
  189. */
  190. }
  191. /**
  192. * @brief This function configures the source of the time base.
  193. * The time source is configured to have 1ms time base with a dedicated
  194. * Tick interrupt priority.
  195. * @note This function is called automatically at the beginning of program after
  196. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  197. * @note In the default implementation, SysTick timer is the source of time base.
  198. * It is used to generate interrupts at regular time intervals.
  199. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  200. * The SysTick interrupt must have higher priority (numerically lower)
  201. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  202. * The function is declared as __weak to be overwritten in case of other
  203. * implementation in user file.
  204. * @param TickPriority Tick interrupt priority.
  205. * @retval HAL status
  206. */
  207. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  208. {
  209. /* Configure the SysTick to have interrupt in 1ms time basis*/
  210. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
  211. {
  212. return HAL_ERROR;
  213. }
  214. /* Configure the SysTick IRQ priority */
  215. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  216. {
  217. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  218. uwTickPrio = TickPriority;
  219. }
  220. else
  221. {
  222. return HAL_ERROR;
  223. }
  224. /* Return function status */
  225. return HAL_OK;
  226. }
  227. /**
  228. * @}
  229. */
  230. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  231. * @brief HAL Control functions
  232. *
  233. @verbatim
  234. ===============================================================================
  235. ##### HAL Control functions #####
  236. ===============================================================================
  237. [..] This section provides functions allowing to:
  238. (+) Provide a tick value in millisecond
  239. (+) Provide a blocking delay in millisecond
  240. (+) Suspend the time base source interrupt
  241. (+) Resume the time base source interrupt
  242. (+) Get the HAL API driver version
  243. (+) Get the device identifier
  244. (+) Get the device revision identifier
  245. (+) Enable/Disable Debug module during SLEEP mode
  246. (+) Enable/Disable Debug module during STOP mode
  247. (+) Enable/Disable Debug module during STANDBY mode
  248. @endverbatim
  249. * @{
  250. */
  251. /**
  252. * @brief This function is called to increment a global variable "uwTick"
  253. * used as application time base.
  254. * @note In the default implementation, this variable is incremented each 1ms
  255. * in SysTick ISR.
  256. * @note This function is declared as __weak to be overwritten in case of other
  257. * implementations in user file.
  258. * @retval None
  259. */
  260. __weak void HAL_IncTick(void)
  261. {
  262. uwTick += uwTickFreq;
  263. }
  264. /**
  265. * @brief Provides a tick value in millisecond.
  266. * @note This function is declared as __weak to be overwritten in case of other
  267. * implementations in user file.
  268. * @retval tick value
  269. */
  270. __weak uint32_t HAL_GetTick(void)
  271. {
  272. return uwTick;
  273. }
  274. /**
  275. * @brief This function returns a tick priority.
  276. * @retval tick priority
  277. */
  278. uint32_t HAL_GetTickPrio(void)
  279. {
  280. return uwTickPrio;
  281. }
  282. /**
  283. * @brief Set new tick Freq.
  284. * @retval status
  285. */
  286. HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
  287. {
  288. HAL_StatusTypeDef status = HAL_OK;
  289. HAL_TickFreqTypeDef prevTickFreq;
  290. assert_param(IS_TICKFREQ(Freq));
  291. if (uwTickFreq != Freq)
  292. {
  293. /* Back up uwTickFreq frequency */
  294. prevTickFreq = uwTickFreq;
  295. /* Update uwTickFreq global variable used by HAL_InitTick() */
  296. uwTickFreq = Freq;
  297. /* Apply the new tick Freq */
  298. status = HAL_InitTick(uwTickPrio);
  299. if (status != HAL_OK)
  300. {
  301. /* Restore previous tick frequency */
  302. uwTickFreq = prevTickFreq;
  303. }
  304. }
  305. return status;
  306. }
  307. /**
  308. * @brief Return tick frequency.
  309. * @retval Tick frequency.
  310. * Value of @ref HAL_TickFreqTypeDef.
  311. */
  312. HAL_TickFreqTypeDef HAL_GetTickFreq(void)
  313. {
  314. return uwTickFreq;
  315. }
  316. /**
  317. * @brief This function provides minimum delay (in milliseconds) based
  318. * on variable incremented.
  319. * @note In the default implementation , SysTick timer is the source of time base.
  320. * It is used to generate interrupts at regular time intervals where uwTick
  321. * is incremented.
  322. * @note This function is declared as __weak to be overwritten in case of other
  323. * implementations in user file.
  324. * @param Delay specifies the delay time length, in milliseconds.
  325. * @retval None
  326. */
  327. __weak void HAL_Delay(uint32_t Delay)
  328. {
  329. uint32_t tickstart = HAL_GetTick();
  330. uint32_t wait = Delay;
  331. /* Add a freq to guarantee minimum wait */
  332. if (wait < HAL_MAX_DELAY)
  333. {
  334. wait += (uint32_t)(uwTickFreq);
  335. }
  336. while ((HAL_GetTick() - tickstart) < wait)
  337. {
  338. }
  339. }
  340. /**
  341. * @brief Suspend Tick increment.
  342. * @note In the default implementation , SysTick timer is the source of time base. It is
  343. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  344. * is called, the SysTick interrupt will be disabled and so Tick increment
  345. * is suspended.
  346. * @note This function is declared as __weak to be overwritten in case of other
  347. * implementations in user file.
  348. * @retval None
  349. */
  350. __weak void HAL_SuspendTick(void)
  351. {
  352. /* Disable SysTick Interrupt */
  353. CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  354. }
  355. /**
  356. * @brief Resume Tick increment.
  357. * @note In the default implementation , SysTick timer is the source of time base. It is
  358. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  359. * is called, the SysTick interrupt will be enabled and so Tick increment
  360. * is resumed.
  361. * @note This function is declared as __weak to be overwritten in case of other
  362. * implementations in user file.
  363. * @retval None
  364. */
  365. __weak void HAL_ResumeTick(void)
  366. {
  367. /* Enable SysTick Interrupt */
  368. SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  369. }
  370. /**
  371. * @brief Returns the HAL revision
  372. * @retval version 0xXYZR (8bits for each decimal, R for RC)
  373. */
  374. uint32_t HAL_GetHalVersion(void)
  375. {
  376. return __STM32F1xx_HAL_VERSION;
  377. }
  378. /**
  379. * @brief Returns the device revision identifier.
  380. * Note: On devices STM32F10xx8 and STM32F10xxB,
  381. * STM32F101xC/D/E and STM32F103xC/D/E,
  382. * STM32F101xF/G and STM32F103xF/G
  383. * STM32F10xx4 and STM32F10xx6
  384. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  385. * debug mode (not accessible by the user software in normal mode).
  386. * Refer to errata sheet of these devices for more details.
  387. * @retval Device revision identifier
  388. */
  389. uint32_t HAL_GetREVID(void)
  390. {
  391. return ((DBGMCU->IDCODE) >> DBGMCU_IDCODE_REV_ID_Pos);
  392. }
  393. /**
  394. * @brief Returns the device identifier.
  395. * Note: On devices STM32F10xx8 and STM32F10xxB,
  396. * STM32F101xC/D/E and STM32F103xC/D/E,
  397. * STM32F101xF/G and STM32F103xF/G
  398. * STM32F10xx4 and STM32F10xx6
  399. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  400. * debug mode (not accessible by the user software in normal mode).
  401. * Refer to errata sheet of these devices for more details.
  402. * @retval Device identifier
  403. */
  404. uint32_t HAL_GetDEVID(void)
  405. {
  406. return ((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  407. }
  408. /**
  409. * @brief Returns first word of the unique device identifier (UID based on 96 bits)
  410. * @retval Device identifier
  411. */
  412. uint32_t HAL_GetUIDw0(void)
  413. {
  414. return(READ_REG(*((uint32_t *)UID_BASE)));
  415. }
  416. /**
  417. * @brief Returns second word of the unique device identifier (UID based on 96 bits)
  418. * @retval Device identifier
  419. */
  420. uint32_t HAL_GetUIDw1(void)
  421. {
  422. return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  423. }
  424. /**
  425. * @brief Returns third word of the unique device identifier (UID based on 96 bits)
  426. * @retval Device identifier
  427. */
  428. uint32_t HAL_GetUIDw2(void)
  429. {
  430. return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  431. }
  432. /**
  433. * @brief Enable the Debug Module during SLEEP mode
  434. * @retval None
  435. */
  436. void HAL_DBGMCU_EnableDBGSleepMode(void)
  437. {
  438. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  439. }
  440. /**
  441. * @brief Disable the Debug Module during SLEEP mode
  442. * Note: On devices STM32F10xx8 and STM32F10xxB,
  443. * STM32F101xC/D/E and STM32F103xC/D/E,
  444. * STM32F101xF/G and STM32F103xF/G
  445. * STM32F10xx4 and STM32F10xx6
  446. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  447. * debug mode (not accessible by the user software in normal mode).
  448. * Refer to errata sheet of these devices for more details.
  449. * @retval None
  450. */
  451. void HAL_DBGMCU_DisableDBGSleepMode(void)
  452. {
  453. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  454. }
  455. /**
  456. * @brief Enable the Debug Module during STOP mode
  457. * Note: On devices STM32F10xx8 and STM32F10xxB,
  458. * STM32F101xC/D/E and STM32F103xC/D/E,
  459. * STM32F101xF/G and STM32F103xF/G
  460. * STM32F10xx4 and STM32F10xx6
  461. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  462. * debug mode (not accessible by the user software in normal mode).
  463. * Refer to errata sheet of these devices for more details.
  464. * Note: On all STM32F1 devices:
  465. * If the system tick timer interrupt is enabled during the Stop mode
  466. * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
  467. * the system from Stop mode.
  468. * Workaround: To debug the Stop mode, disable the system tick timer
  469. * interrupt.
  470. * Refer to errata sheet of these devices for more details.
  471. * Note: On all STM32F1 devices:
  472. * If the system tick timer interrupt is enabled during the Stop mode
  473. * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
  474. * the system from Stop mode.
  475. * Workaround: To debug the Stop mode, disable the system tick timer
  476. * interrupt.
  477. * Refer to errata sheet of these devices for more details.
  478. * @retval None
  479. */
  480. void HAL_DBGMCU_EnableDBGStopMode(void)
  481. {
  482. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  483. }
  484. /**
  485. * @brief Disable the Debug Module during STOP mode
  486. * Note: On devices STM32F10xx8 and STM32F10xxB,
  487. * STM32F101xC/D/E and STM32F103xC/D/E,
  488. * STM32F101xF/G and STM32F103xF/G
  489. * STM32F10xx4 and STM32F10xx6
  490. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  491. * debug mode (not accessible by the user software in normal mode).
  492. * Refer to errata sheet of these devices for more details.
  493. * @retval None
  494. */
  495. void HAL_DBGMCU_DisableDBGStopMode(void)
  496. {
  497. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  498. }
  499. /**
  500. * @brief Enable the Debug Module during STANDBY mode
  501. * Note: On devices STM32F10xx8 and STM32F10xxB,
  502. * STM32F101xC/D/E and STM32F103xC/D/E,
  503. * STM32F101xF/G and STM32F103xF/G
  504. * STM32F10xx4 and STM32F10xx6
  505. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  506. * debug mode (not accessible by the user software in normal mode).
  507. * Refer to errata sheet of these devices for more details.
  508. * @retval None
  509. */
  510. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  511. {
  512. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  513. }
  514. /**
  515. * @brief Disable the Debug Module during STANDBY mode
  516. * Note: On devices STM32F10xx8 and STM32F10xxB,
  517. * STM32F101xC/D/E and STM32F103xC/D/E,
  518. * STM32F101xF/G and STM32F103xF/G
  519. * STM32F10xx4 and STM32F10xx6
  520. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  521. * debug mode (not accessible by the user software in normal mode).
  522. * Refer to errata sheet of these devices for more details.
  523. * @retval None
  524. */
  525. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  526. {
  527. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  528. }
  529. /**
  530. * @}
  531. */
  532. /**
  533. * @}
  534. */
  535. #endif /* HAL_MODULE_ENABLED */
  536. /**
  537. * @}
  538. */
  539. /**
  540. * @}
  541. */