stm32f1xx_hal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-2016
  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) 2016 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 "stm32f1xx_hal.h"
  53. /** @addtogroup STM32F1xx_HAL_Driver
  54. * @{
  55. */
  56. /** @defgroup HAL HAL
  57. * @brief HAL module driver.
  58. * @{
  59. */
  60. #ifdef HAL_MODULE_ENABLED
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* Private define ------------------------------------------------------------*/
  63. /** @defgroup HAL_Private_Constants HAL Private Constants
  64. * @{
  65. */
  66. /**
  67. * @brief STM32F1xx HAL Driver version number
  68. */
  69. #define __STM32F1xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
  70. #define __STM32F1xx_HAL_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */
  71. #define __STM32F1xx_HAL_VERSION_SUB2 (0x04) /*!< [15:8] sub2 version */
  72. #define __STM32F1xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
  73. #define __STM32F1xx_HAL_VERSION ((__STM32F1xx_HAL_VERSION_MAIN << 24)\
  74. |(__STM32F1xx_HAL_VERSION_SUB1 << 16)\
  75. |(__STM32F1xx_HAL_VERSION_SUB2 << 8 )\
  76. |(__STM32F1xx_HAL_VERSION_RC))
  77. #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
  78. /**
  79. * @}
  80. */
  81. /* Private macro -------------------------------------------------------------*/
  82. /* Private variables ---------------------------------------------------------*/
  83. /** @defgroup HAL_Private_Variables HAL Private Variables
  84. * @{
  85. */
  86. static __IO uint32_t uwTick;
  87. /**
  88. * @}
  89. */
  90. /* Private function prototypes -----------------------------------------------*/
  91. /* Exported 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 de-initialization 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 source of time base also when timeout
  105. is needed 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 configures the Flash prefetch,
  129. * Configures time base source, NVIC and Low level hardware
  130. * @note This function is called at the beginning of program after reset and before
  131. * the clock configuration
  132. * @note The time base configuration is based on MSI clock when exiting from Reset.
  133. * Once done, time base tick start incrementing.
  134. * In the default implementation,Systick is used as source of time base.
  135. * The tick variable is incremented each 1ms in its ISR.
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef HAL_Init(void)
  139. {
  140. /* Configure Flash prefetch */
  141. #if (PREFETCH_ENABLE != 0)
  142. #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
  143. defined(STM32F102x6) || defined(STM32F102xB) || \
  144. defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
  145. defined(STM32F105xC) || defined(STM32F107xC)
  146. /* Prefetch buffer is not available on value line devices */
  147. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  148. #endif
  149. #endif /* PREFETCH_ENABLE */
  150. /* Set Interrupt Group Priority */
  151. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  152. /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  153. HAL_InitTick(TICK_INT_PRIORITY);
  154. /* Init the low level hardware */
  155. HAL_MspInit();
  156. /* Return function status */
  157. return HAL_OK;
  158. }
  159. /**
  160. * @brief This function de-Initializes common part of the HAL and stops the source
  161. * of time base.
  162. * @note This function is optional.
  163. * @retval HAL status
  164. */
  165. HAL_StatusTypeDef HAL_DeInit(void)
  166. {
  167. /* Reset of all peripherals */
  168. __HAL_RCC_APB1_FORCE_RESET();
  169. __HAL_RCC_APB1_RELEASE_RESET();
  170. __HAL_RCC_APB2_FORCE_RESET();
  171. __HAL_RCC_APB2_RELEASE_RESET();
  172. #if defined(STM32F105xC) || defined(STM32F107xC)
  173. __HAL_RCC_AHB_FORCE_RESET();
  174. __HAL_RCC_AHB_RELEASE_RESET();
  175. #endif
  176. /* De-Init the low level hardware */
  177. HAL_MspDeInit();
  178. /* Return function status */
  179. return HAL_OK;
  180. }
  181. /**
  182. * @brief Initializes the MSP.
  183. * @retval None
  184. */
  185. __weak void HAL_MspInit(void)
  186. {
  187. /* NOTE : This function Should not be modified, when the callback is needed,
  188. the HAL_MspInit could be implemented in the user file
  189. */
  190. }
  191. /**
  192. * @brief DeInitializes the MSP.
  193. * @retval None
  194. */
  195. __weak void HAL_MspDeInit(void)
  196. {
  197. /* NOTE : This function Should not be modified, when the callback is needed,
  198. the HAL_MspDeInit could be implemented in the user file
  199. */
  200. }
  201. /**
  202. * @brief This function configures the source of the time base.
  203. * The time source is configured to have 1ms time base with a dedicated
  204. * Tick interrupt priority.
  205. * @note This function is called automatically at the beginning of program after
  206. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  207. * @note In the default implementation, SysTick timer is the source of time base.
  208. * It is used to generate interrupts at regular time intervals.
  209. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  210. * The the SysTick interrupt must have higher priority (numerically lower)
  211. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  212. * The function is declared as __Weak to be overwritten in case of other
  213. * implementation in user file.
  214. * @param TickPriority: Tick interrupt priority.
  215. * @retval HAL status
  216. */
  217. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  218. {
  219. /*Configure the SysTick to have interrupt in 1ms time basis*/
  220. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  221. /*Configure the SysTick IRQ priority */
  222. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  223. /* Return function status */
  224. return HAL_OK;
  225. }
  226. /**
  227. * @}
  228. */
  229. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  230. * @brief HAL Control functions
  231. *
  232. @verbatim
  233. ===============================================================================
  234. ##### HAL Control functions #####
  235. ===============================================================================
  236. [..] This section provides functions allowing to:
  237. (+) Provide a tick value in millisecond
  238. (+) Provide a blocking delay in millisecond
  239. (+) Suspend the time base source interrupt
  240. (+) Resume the time base source interrupt
  241. (+) Get the HAL API driver version
  242. (+) Get the device identifier
  243. (+) Get the device revision identifier
  244. (+) Enable/Disable Debug module during Sleep mode
  245. (+) Enable/Disable Debug module during STOP mode
  246. (+) Enable/Disable Debug module during STANDBY mode
  247. @endverbatim
  248. * @{
  249. */
  250. /**
  251. * @brief This function is called to increment a global variable "uwTick"
  252. * used as application time base.
  253. * @note In the default implementation, this variable is incremented each 1ms
  254. * in Systick ISR.
  255. * @note This function is declared as __weak to be overwritten in case of other
  256. * implementations in user file.
  257. * @retval None
  258. */
  259. __weak void HAL_IncTick(void)
  260. {
  261. uwTick++;
  262. }
  263. /**
  264. * @brief Provides a tick value in millisecond.
  265. * @note This function is declared as __weak to be overwritten in case of other
  266. * implementations in user file.
  267. * @retval tick value
  268. */
  269. __weak uint32_t HAL_GetTick(void)
  270. {
  271. return uwTick;
  272. }
  273. /**
  274. * @brief This function provides accurate delay (in milliseconds) based
  275. * on variable incremented.
  276. * @note In the default implementation , SysTick timer is the source of time base.
  277. * It is used to generate interrupts at regular time intervals where uwTick
  278. * is incremented.
  279. * @note ThiS function is declared as __weak to be overwritten in case of other
  280. * implementations in user file.
  281. * @param Delay: specifies the delay time length, in milliseconds.
  282. * @retval None
  283. */
  284. __weak void HAL_Delay(__IO uint32_t Delay)
  285. {
  286. uint32_t tickstart = 0;
  287. tickstart = HAL_GetTick();
  288. while((HAL_GetTick() - tickstart) < Delay)
  289. {
  290. }
  291. }
  292. /**
  293. * @brief Suspend Tick increment.
  294. * @note In the default implementation , SysTick timer is the source of time base. It is
  295. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  296. * is called, the the SysTick interrupt will be disabled and so Tick increment
  297. * is suspended.
  298. * @note This function is declared as __weak to be overwritten in case of other
  299. * implementations in user file.
  300. * @retval None
  301. */
  302. __weak void HAL_SuspendTick(void)
  303. {
  304. /* Disable SysTick Interrupt */
  305. CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  306. }
  307. /**
  308. * @brief Resume Tick increment.
  309. * @note In the default implementation , SysTick timer is the source of time base. It is
  310. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  311. * is called, the the SysTick interrupt will be enabled and so Tick increment
  312. * is resumed.
  313. * @note This function is declared as __weak to be overwritten in case of other
  314. * implementations in user file.
  315. * @retval None
  316. */
  317. __weak void HAL_ResumeTick(void)
  318. {
  319. /* Enable SysTick Interrupt */
  320. SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  321. }
  322. /**
  323. * @brief This method returns the HAL revision
  324. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  325. */
  326. uint32_t HAL_GetHalVersion(void)
  327. {
  328. return __STM32F1xx_HAL_VERSION;
  329. }
  330. /**
  331. * @brief Returns the device revision identifier.
  332. * Note: On devices STM32F10xx8 and STM32F10xxB,
  333. * STM32F101xC/D/E and STM32F103xC/D/E,
  334. * STM32F101xF/G and STM32F103xF/G
  335. * STM32F10xx4 and STM32F10xx6
  336. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  337. * debug mode (not accessible by the user software in normal mode).
  338. * Refer to errata sheet of these devices for more details.
  339. * @retval Device revision identifier
  340. */
  341. uint32_t HAL_GetREVID(void)
  342. {
  343. return((DBGMCU->IDCODE) >> POSITION_VAL(DBGMCU_IDCODE_REV_ID));
  344. }
  345. /**
  346. * @brief Returns the device identifier.
  347. * Note: On devices STM32F10xx8 and STM32F10xxB,
  348. * STM32F101xC/D/E and STM32F103xC/D/E,
  349. * STM32F101xF/G and STM32F103xF/G
  350. * STM32F10xx4 and STM32F10xx6
  351. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  352. * debug mode (not accessible by the user software in normal mode).
  353. * Refer to errata sheet of these devices for more details.
  354. * @retval Device identifier
  355. */
  356. uint32_t HAL_GetDEVID(void)
  357. {
  358. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  359. }
  360. /**
  361. * @brief Enable the Debug Module during SLEEP mode
  362. * @retval None
  363. */
  364. void HAL_DBGMCU_EnableDBGSleepMode(void)
  365. {
  366. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  367. }
  368. /**
  369. * @brief Disable the Debug Module during SLEEP mode
  370. * Note: On devices STM32F10xx8 and STM32F10xxB,
  371. * STM32F101xC/D/E and STM32F103xC/D/E,
  372. * STM32F101xF/G and STM32F103xF/G
  373. * STM32F10xx4 and STM32F10xx6
  374. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  375. * debug mode (not accessible by the user software in normal mode).
  376. * Refer to errata sheet of these devices for more details.
  377. * @retval None
  378. */
  379. void HAL_DBGMCU_DisableDBGSleepMode(void)
  380. {
  381. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  382. }
  383. /**
  384. * @brief Enable the Debug Module during STOP mode
  385. * Note: On devices STM32F10xx8 and STM32F10xxB,
  386. * STM32F101xC/D/E and STM32F103xC/D/E,
  387. * STM32F101xF/G and STM32F103xF/G
  388. * STM32F10xx4 and STM32F10xx6
  389. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  390. * debug mode (not accessible by the user software in normal mode).
  391. * Refer to errata sheet of these devices for more details.
  392. * Note: On all STM32F1 devices:
  393. * If the system tick timer interrupt is enabled during the Stop mode
  394. * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
  395. * the system from Stop mode.
  396. * Workaround: To debug the Stop mode, disable the system tick timer
  397. * interrupt.
  398. * Refer to errata sheet of these devices for more details.
  399. * Note: On all STM32F1 devices:
  400. * If the system tick timer interrupt is enabled during the Stop mode
  401. * debug (DBG_STOP bit set in the DBGMCU_CR register ), it will wakeup
  402. * the system from Stop mode.
  403. * Workaround: To debug the Stop mode, disable the system tick timer
  404. * interrupt.
  405. * Refer to errata sheet of these devices for more details.
  406. * @retval None
  407. */
  408. void HAL_DBGMCU_EnableDBGStopMode(void)
  409. {
  410. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  411. }
  412. /**
  413. * @brief Disable the Debug Module during STOP mode
  414. * Note: On devices STM32F10xx8 and STM32F10xxB,
  415. * STM32F101xC/D/E and STM32F103xC/D/E,
  416. * STM32F101xF/G and STM32F103xF/G
  417. * STM32F10xx4 and STM32F10xx6
  418. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  419. * debug mode (not accessible by the user software in normal mode).
  420. * Refer to errata sheet of these devices for more details.
  421. * @retval None
  422. */
  423. void HAL_DBGMCU_DisableDBGStopMode(void)
  424. {
  425. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  426. }
  427. /**
  428. * @brief Enable the Debug Module during STANDBY mode
  429. * Note: On devices STM32F10xx8 and STM32F10xxB,
  430. * STM32F101xC/D/E and STM32F103xC/D/E,
  431. * STM32F101xF/G and STM32F103xF/G
  432. * STM32F10xx4 and STM32F10xx6
  433. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  434. * debug mode (not accessible by the user software in normal mode).
  435. * Refer to errata sheet of these devices for more details.
  436. * @retval None
  437. */
  438. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  439. {
  440. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  441. }
  442. /**
  443. * @brief Disable the Debug Module during STANDBY mode
  444. * Note: On devices STM32F10xx8 and STM32F10xxB,
  445. * STM32F101xC/D/E and STM32F103xC/D/E,
  446. * STM32F101xF/G and STM32F103xF/G
  447. * STM32F10xx4 and STM32F10xx6
  448. * Debug registers DBGMCU_IDCODE and DBGMCU_CR are accessible only in
  449. * debug mode (not accessible by the user software in normal mode).
  450. * Refer to errata sheet of these devices for more details.
  451. * @retval None
  452. */
  453. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  454. {
  455. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  456. }
  457. /**
  458. * @}
  459. */
  460. /**
  461. * @}
  462. */
  463. #endif /* HAL_MODULE_ENABLED */
  464. /**
  465. * @}
  466. */
  467. /**
  468. * @}
  469. */
  470. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/