stm32f0xx_hal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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. (+) HAL Initialization and de-initialization functions
  29. (+) HAL Control functions
  30. @endverbatim
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f0xx_hal.h"
  35. /** @addtogroup STM32F0xx_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 STM32F0xx HAL Driver version number
  50. */
  51. #define __STM32F0xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  52. #define __STM32F0xx_HAL_VERSION_SUB1 (0x07U) /*!< [23:16] sub1 version */
  53. #define __STM32F0xx_HAL_VERSION_SUB2 (0x08U) /*!< [15:8] sub2 version */
  54. #define __STM32F0xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  55. #define __STM32F0xx_HAL_VERSION ((__STM32F0xx_HAL_VERSION_MAIN << 24U)\
  56. |(__STM32F0xx_HAL_VERSION_SUB1 << 16U)\
  57. |(__STM32F0xx_HAL_VERSION_SUB2 << 8U )\
  58. |(__STM32F0xx_HAL_VERSION_RC))
  59. #define IDCODE_DEVID_MASK (0x00000FFFU)
  60. /**
  61. * @}
  62. */
  63. /* Private macro -------------------------------------------------------------*/
  64. /** @defgroup HAL_Private_Macros HAL Private Macros
  65. * @{
  66. */
  67. /**
  68. * @}
  69. */
  70. /* Exported variables ---------------------------------------------------------*/
  71. /** @defgroup HAL_Private_Variables HAL Exported Variables
  72. * @{
  73. */
  74. __IO uint32_t uwTick;
  75. uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
  76. HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
  77. /**
  78. * @}
  79. */
  80. /* Private function prototypes -----------------------------------------------*/
  81. /* Exported functions ---------------------------------------------------------*/
  82. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  83. * @{
  84. */
  85. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  86. * @brief Initialization and de-initialization functions
  87. *
  88. @verbatim
  89. ===============================================================================
  90. ##### Initialization and de-initialization functions #####
  91. ===============================================================================
  92. [..] This section provides functions allowing to:
  93. (+) Initializes the Flash interface, the NVIC allocation and initial clock
  94. configuration. It initializes the systick also when timeout is needed
  95. and the backup domain when enabled.
  96. (+) de-Initializes common part of the HAL.
  97. (+) Configure The time base source to have 1ms time base with a dedicated
  98. Tick interrupt priority.
  99. (++) SysTick timer is used by default as source of time base, but user
  100. can eventually implement his proper time base source (a general purpose
  101. timer for example or other time source), keeping in mind that Time base
  102. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  103. handled in milliseconds basis.
  104. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  105. at the beginning of the program after reset by HAL_Init() or at any time
  106. when clock is configured, by HAL_RCC_ClockConfig().
  107. (++) Source of time base is configured to generate interrupts at regular
  108. time intervals. Care must be taken if HAL_Delay() is called from a
  109. peripheral ISR process, the Tick interrupt line must have higher priority
  110. (numerically lower) than the peripheral interrupt. Otherwise the caller
  111. ISR process will be blocked.
  112. (++) functions affecting time base configurations are declared as __Weak
  113. to make override possible in case of other implementations in user file.
  114. @endverbatim
  115. * @{
  116. */
  117. /**
  118. * @brief This function configures the Flash prefetch,
  119. * Configures time base source, NVIC and Low level hardware
  120. * @note This function is called at the beginning of program after reset and before
  121. * the clock configuration
  122. * @note The time base configuration is based on HSI clock when exiting from Reset.
  123. * Once done, time base tick start incrementing.
  124. * In the default implementation,Systick is used as source of time base.
  125. * The tick variable is incremented each 1ms in its ISR.
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_Init(void)
  129. {
  130. /* Configure Flash prefetch */
  131. #if (PREFETCH_ENABLE != 0)
  132. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  133. #endif /* PREFETCH_ENABLE */
  134. /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  135. HAL_InitTick(TICK_INT_PRIORITY);
  136. /* Init the low level hardware */
  137. HAL_MspInit();
  138. /* Return function status */
  139. return HAL_OK;
  140. }
  141. /**
  142. * @brief This function de-Initialize common part of the HAL and stops the SysTick
  143. * of time base.
  144. * @note This function is optional.
  145. * @retval HAL status
  146. */
  147. HAL_StatusTypeDef HAL_DeInit(void)
  148. {
  149. /* Reset of all peripherals */
  150. __HAL_RCC_APB1_FORCE_RESET();
  151. __HAL_RCC_APB1_RELEASE_RESET();
  152. __HAL_RCC_APB2_FORCE_RESET();
  153. __HAL_RCC_APB2_RELEASE_RESET();
  154. __HAL_RCC_AHB_FORCE_RESET();
  155. __HAL_RCC_AHB_RELEASE_RESET();
  156. /* De-Init the low level hardware */
  157. HAL_MspDeInit();
  158. /* Return function status */
  159. return HAL_OK;
  160. }
  161. /**
  162. * @brief Initialize the MSP.
  163. * @retval None
  164. */
  165. __weak void HAL_MspInit(void)
  166. {
  167. /* NOTE : This function should not be modified, when the callback is needed,
  168. the HAL_MspInit could be implemented in the user file
  169. */
  170. }
  171. /**
  172. * @brief DeInitializes the MSP.
  173. * @retval None
  174. */
  175. __weak void HAL_MspDeInit(void)
  176. {
  177. /* NOTE : This function should not be modified, when the callback is needed,
  178. the HAL_MspDeInit could be implemented in the user file
  179. */
  180. }
  181. /**
  182. * @brief This function configures the source of the time base.
  183. * The time source is configured to have 1ms time base with a dedicated
  184. * Tick interrupt priority.
  185. * @note This function is called automatically at the beginning of program after
  186. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  187. * @note In the default implementation, SysTick timer is the source of time base.
  188. * It is used to generate interrupts at regular time intervals.
  189. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  190. * The SysTick interrupt must have higher priority (numerically lower)
  191. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  192. * The function is declared as __Weak to be overwritten in case of other
  193. * implementation in user file.
  194. * @param TickPriority Tick interrupt priority.
  195. * @retval HAL status
  196. */
  197. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  198. {
  199. /*Configure the SysTick to have interrupt in 1ms time basis*/
  200. if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U)
  201. {
  202. return HAL_ERROR;
  203. }
  204. /* Configure the SysTick IRQ priority */
  205. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  206. {
  207. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
  208. uwTickPrio = TickPriority;
  209. }
  210. else
  211. {
  212. return HAL_ERROR;
  213. }
  214. /* Return function status */
  215. return HAL_OK;
  216. }
  217. /**
  218. * @}
  219. */
  220. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  221. * @brief HAL Control functions
  222. *
  223. @verbatim
  224. ===============================================================================
  225. ##### HAL Control functions #####
  226. ===============================================================================
  227. [..] This section provides functions allowing to:
  228. (+) Provide a tick value in millisecond
  229. (+) Provide a blocking delay in millisecond
  230. (+) Suspend the time base source interrupt
  231. (+) Resume the time base source interrupt
  232. (+) Get the HAL API driver version
  233. (+) Get the device identifier
  234. (+) Get the device revision identifier
  235. (+) Enable/Disable Debug module during Sleep mode
  236. (+) Enable/Disable Debug module during STOP mode
  237. (+) Enable/Disable Debug module during STANDBY mode
  238. @endverbatim
  239. * @{
  240. */
  241. /**
  242. * @brief This function is called to increment a global variable "uwTick"
  243. * used as application time base.
  244. * @note In the default implementation, this variable is incremented each 1ms
  245. * in SysTick ISR.
  246. * @note This function is declared as __weak to be overwritten in case of other
  247. * implementations in user file.
  248. * @retval None
  249. */
  250. __weak void HAL_IncTick(void)
  251. {
  252. uwTick += uwTickFreq;
  253. }
  254. /**
  255. * @brief Provides a tick value in millisecond.
  256. * @note This function is declared as __weak to be overwritten in case of other
  257. * implementations in user file.
  258. * @retval tick value
  259. */
  260. __weak uint32_t HAL_GetTick(void)
  261. {
  262. return uwTick;
  263. }
  264. /**
  265. * @brief This function returns a tick priority.
  266. * @retval tick priority
  267. */
  268. uint32_t HAL_GetTickPrio(void)
  269. {
  270. return uwTickPrio;
  271. }
  272. /**
  273. * @brief Set new tick Freq.
  274. * @retval status
  275. */
  276. HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
  277. {
  278. HAL_StatusTypeDef status = HAL_OK;
  279. HAL_TickFreqTypeDef prevTickFreq;
  280. assert_param(IS_TICKFREQ(Freq));
  281. if (uwTickFreq != Freq)
  282. {
  283. /* Back up uwTickFreq frequency */
  284. prevTickFreq = uwTickFreq;
  285. /* Update uwTickFreq global variable used by HAL_InitTick() */
  286. uwTickFreq = Freq;
  287. /* Apply the new tick Freq */
  288. status = HAL_InitTick(uwTickPrio);
  289. if (status != HAL_OK)
  290. {
  291. /* Restore previous tick frequency */
  292. uwTickFreq = prevTickFreq;
  293. }
  294. }
  295. return status;
  296. }
  297. /**
  298. * @brief return tick frequency.
  299. * @retval Tick frequency.
  300. * Value of @ref HAL_TickFreqTypeDef.
  301. */
  302. HAL_TickFreqTypeDef HAL_GetTickFreq(void)
  303. {
  304. return uwTickFreq;
  305. }
  306. /**
  307. * @brief This function provides accurate delay (in milliseconds) based
  308. * on variable incremented.
  309. * @note In the default implementation , SysTick timer is the source of time base.
  310. * It is used to generate interrupts at regular time intervals where uwTick
  311. * is incremented.
  312. * @note ThiS function is declared as __weak to be overwritten in case of other
  313. * implementations in user file.
  314. * @param Delay specifies the delay time length, in milliseconds.
  315. * @retval None
  316. */
  317. __weak void HAL_Delay(uint32_t Delay)
  318. {
  319. uint32_t tickstart = HAL_GetTick();
  320. uint32_t wait = Delay;
  321. /* Add a freq to guarantee minimum wait */
  322. if (wait < HAL_MAX_DELAY)
  323. {
  324. wait += (uint32_t)(uwTickFreq);
  325. }
  326. while((HAL_GetTick() - tickstart) < wait)
  327. {
  328. }
  329. }
  330. /**
  331. * @brief Suspend Tick increment.
  332. * @note In the default implementation , SysTick timer is the source of time base. It is
  333. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  334. * is called, the the SysTick interrupt will be disabled and so Tick increment
  335. * is suspended.
  336. * @note This function is declared as __weak to be overwritten in case of other
  337. * implementations in user file.
  338. * @retval None
  339. */
  340. __weak void HAL_SuspendTick(void)
  341. {
  342. /* Disable SysTick Interrupt */
  343. CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  344. }
  345. /**
  346. * @brief Resume Tick increment.
  347. * @note In the default implementation , SysTick timer is the source of time base. It is
  348. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  349. * is called, the the SysTick interrupt will be enabled and so Tick increment
  350. * is resumed.
  351. * @note This function is declared as __weak to be overwritten in case of other
  352. * implementations in user file.
  353. * @retval None
  354. */
  355. __weak void HAL_ResumeTick(void)
  356. {
  357. /* Enable SysTick Interrupt */
  358. SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  359. }
  360. /**
  361. * @brief This method returns the HAL revision
  362. * @retval version 0xXYZR (8bits for each decimal, R for RC)
  363. */
  364. uint32_t HAL_GetHalVersion(void)
  365. {
  366. return __STM32F0xx_HAL_VERSION;
  367. }
  368. /**
  369. * @brief Returns the device revision identifier.
  370. * @retval Device revision identifier
  371. */
  372. uint32_t HAL_GetREVID(void)
  373. {
  374. return((DBGMCU->IDCODE) >> 16U);
  375. }
  376. /**
  377. * @brief Returns the device identifier.
  378. * @retval Device identifier
  379. */
  380. uint32_t HAL_GetDEVID(void)
  381. {
  382. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  383. }
  384. /**
  385. * @brief Returns first word of the unique device identifier (UID based on 96 bits)
  386. * @retval Device identifier
  387. */
  388. uint32_t HAL_GetUIDw0(void)
  389. {
  390. return(READ_REG(*((uint32_t *)UID_BASE)));
  391. }
  392. /**
  393. * @brief Returns second word of the unique device identifier (UID based on 96 bits)
  394. * @retval Device identifier
  395. */
  396. uint32_t HAL_GetUIDw1(void)
  397. {
  398. return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  399. }
  400. /**
  401. * @brief Returns third word of the unique device identifier (UID based on 96 bits)
  402. * @retval Device identifier
  403. */
  404. uint32_t HAL_GetUIDw2(void)
  405. {
  406. return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
  407. }
  408. /**
  409. * @brief Enable the Debug Module during STOP mode
  410. * @retval None
  411. */
  412. void HAL_DBGMCU_EnableDBGStopMode(void)
  413. {
  414. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  415. }
  416. /**
  417. * @brief Disable the Debug Module during STOP mode
  418. * @retval None
  419. */
  420. void HAL_DBGMCU_DisableDBGStopMode(void)
  421. {
  422. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  423. }
  424. /**
  425. * @brief Enable the Debug Module during STANDBY mode
  426. * @retval None
  427. */
  428. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  429. {
  430. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  431. }
  432. /**
  433. * @brief Disable the Debug Module during STANDBY mode
  434. * @retval None
  435. */
  436. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  437. {
  438. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  439. }
  440. /**
  441. * @}
  442. */
  443. /**
  444. * @}
  445. */
  446. #endif /* HAL_MODULE_ENABLED */
  447. /**
  448. * @}
  449. */
  450. /**
  451. * @}
  452. */