stm32l0xx_hal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal.c
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-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 "stm32l0xx_hal.h"
  53. /** @addtogroup STM32L0xx_HAL_Driver
  54. * @{
  55. */
  56. #ifdef HAL_MODULE_ENABLED
  57. /** @addtogroup HAL
  58. * @brief HAL module driver.
  59. * @{
  60. */
  61. /** @addtogroup HAL_Exported_Constants
  62. * @{
  63. */
  64. /** @defgroup SysTick System Tick
  65. * @{
  66. */
  67. /**
  68. * @brief uwTick_variable uwTick variable
  69. */
  70. __IO uint32_t uwTick;
  71. /**
  72. * @}
  73. */
  74. /** @defgroup HAL_Version HAL Version
  75. * @{
  76. */
  77. /**
  78. * @brief STM32L0xx HAL Driver version number V1.7.0
  79. */
  80. #define __STM32L0xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  81. #define __STM32L0xx_HAL_VERSION_SUB1 (0x07U) /*!< [23:16] sub1 version */
  82. #define __STM32L0xx_HAL_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
  83. #define __STM32L0xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  84. #define __STM32L0xx_HAL_VERSION ((__STM32L0xx_HAL_VERSION_MAIN << 24U)\
  85. |(__STM32L0xx_HAL_VERSION_SUB1 << 16U)\
  86. |(__STM32L0xx_HAL_VERSION_SUB2 << 8U )\
  87. |(__STM32L0xx_HAL_VERSION_RC))
  88. #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFFU)
  89. /**
  90. * @}
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup HAL_Private HAL Private
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /** @addtogroup HAL_Exported_Functions HAL Exported Functions
  102. * @{
  103. */
  104. /** @addtogroup HAL_Exported_Functions_Group1
  105. * @brief Initialization and de-initialization functions
  106. *
  107. @verbatim
  108. ===============================================================================
  109. ##### Initialization and de-initialization functions #####
  110. ===============================================================================
  111. [..] This section provides functions allowing to:
  112. (+) Initializes the Flash interface, the NVIC allocation and initial clock
  113. configuration. It initializes the source of time base also when timeout
  114. is needed and the backup domain when enabled.
  115. (+) de-Initializes common part of the HAL.
  116. (+) Configure The time base source to have 1ms time base with a dedicated
  117. Tick interrupt priority.
  118. (++) Systick timer is used by default as source of time base, but user
  119. can eventually implement his proper time base source (a general purpose
  120. timer for example or other time source), keeping in mind that Time base
  121. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  122. handled in milliseconds basis.
  123. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  124. at the beginning of the program after reset by HAL_Init() or at any time
  125. when clock is configured, by HAL_RCC_ClockConfig().
  126. (++) Source of time base is configured to generate interrupts at regular
  127. time intervals. Care must be taken if HAL_Delay() is called from a
  128. peripheral ISR process, the Tick interrupt line must have higher priority
  129. (numerically lower) than the peripheral interrupt. Otherwise the caller
  130. ISR process will be blocked.
  131. (++) functions affecting time base configurations are declared as __weak
  132. to make override possible in case of other implementations in user file.
  133. @endverbatim
  134. * @{
  135. */
  136. /**
  137. * @brief This function configures the Flash prefetch, Flash preread and Buffer cache,
  138. * Configures time base source, NVIC and Low level hardware
  139. * @note This function is called at the beginning of program after reset and before
  140. * the clock configuration
  141. * @note The time base configuration is based on MSI clock when exiting from Reset.
  142. * Once done, time base tick start incrementing.
  143. * In the default implementation,Systick is used as source of time base.
  144. * the tick variable is incremented each 1ms in its ISR.
  145. * @retval HAL status
  146. */
  147. HAL_StatusTypeDef HAL_Init(void)
  148. {
  149. /* Configure Buffer cache, Flash prefetch, Flash preread */
  150. #if (BUFFER_CACHE_DISABLE != 0)
  151. __HAL_FLASH_BUFFER_CACHE_DISABLE();
  152. #endif /* BUFFER_CACHE_DISABLE */
  153. #if (PREREAD_ENABLE != 0)
  154. __HAL_FLASH_PREREAD_BUFFER_ENABLE();
  155. #endif /* PREREAD_ENABLE */
  156. #if (PREFETCH_ENABLE != 0)
  157. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  158. #endif /* PREFETCH_ENABLE */
  159. /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  160. HAL_InitTick(TICK_INT_PRIORITY);
  161. /* Init the low level hardware */
  162. HAL_MspInit();
  163. /* Return function status */
  164. return HAL_OK;
  165. }
  166. /**
  167. * @brief This function de-Initializes common part of the HAL and stops the source
  168. * of time base.
  169. * @note This function is optional.
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_DeInit(void)
  173. {
  174. /* Reset of all peripherals */
  175. __HAL_RCC_APB1_FORCE_RESET();
  176. __HAL_RCC_APB1_RELEASE_RESET();
  177. __HAL_RCC_APB2_FORCE_RESET();
  178. __HAL_RCC_APB2_RELEASE_RESET();
  179. __HAL_RCC_AHB_FORCE_RESET();
  180. __HAL_RCC_AHB_RELEASE_RESET();
  181. __HAL_RCC_IOP_FORCE_RESET();
  182. __HAL_RCC_IOP_RELEASE_RESET();
  183. /* De-Init the low level hardware */
  184. HAL_MspDeInit();
  185. /* Return function status */
  186. return HAL_OK;
  187. }
  188. /**
  189. * @brief Initializes 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 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. HAL_SYSTICK_Config(SystemCoreClock/1000U);
  228. /*Configure the SysTick IRQ priority */
  229. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
  230. /* Return function status */
  231. return HAL_OK;
  232. }
  233. /**
  234. * @}
  235. */
  236. /** @addtogroup HAL_Exported_Functions_Group2
  237. * @brief Peripheral Control functions
  238. *
  239. @verbatim
  240. ===============================================================================
  241. ##### HAL Control functions #####
  242. ===============================================================================
  243. [..] This section provides functions allowing to:
  244. (+) Provide a tick value in millisecond
  245. (+) Provide a blocking delay in millisecond
  246. (+) Suspend the time base source interrupt
  247. (+) Resume the time base source interrupt
  248. (+) Get the HAL API driver version
  249. (+) Get the device identifier
  250. (+) Get the device revision identifier
  251. (+) Configure low power mode behavior when the MCU is in Debug mode
  252. (+) Manage the VEREFINT feature (activation, lock, output selection)
  253. @endverbatim
  254. * @{
  255. */
  256. /**
  257. * @brief This function is called to increment a global variable "uwTick"
  258. * used as application time base.
  259. * @note In the default implementation, this variable is incremented each 1ms
  260. * in Systick ISR.
  261. * @note This function is declared as __weak to be overwritten in case of other
  262. * implementations in user file.
  263. * @retval None
  264. */
  265. __weak void HAL_IncTick(void)
  266. {
  267. uwTick++;
  268. }
  269. /**
  270. * @brief Provides a tick value in millisecond.
  271. * @note This function is declared as __weak to be overwritten in case of other
  272. * implementations in user file.
  273. * @retval tick value
  274. */
  275. __weak uint32_t HAL_GetTick(void)
  276. {
  277. return uwTick;
  278. }
  279. /**
  280. * @brief This function provides accurate delay (in ms) based on a variable incremented.
  281. * @note In the default implementation , SysTick timer is the source of time base.
  282. * It is used to generate interrupts at regular time intervals where uwTick
  283. * is incremented.
  284. * @note ThiS function is declared as __weak to be overwritten in case of other
  285. * implementations in user file.
  286. * @param Delay: specifies the delay time length, in milliseconds.
  287. * @retval None
  288. */
  289. __weak void HAL_Delay(__IO uint32_t Delay)
  290. {
  291. uint32_t tickstart = 0U;
  292. tickstart = HAL_GetTick();
  293. while((HAL_GetTick() - tickstart) < Delay)
  294. {
  295. }
  296. }
  297. /**
  298. * @brief Suspends the Tick increment.
  299. * @note In the default implementation , SysTick timer is the source of time base. It is
  300. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  301. * is called, the the SysTick interrupt will be disabled and so Tick increment
  302. * is suspended.
  303. * @note This function is declared as __weak to be overwritten in case of other
  304. * implementations in user file.
  305. * @retval None
  306. */
  307. __weak void HAL_SuspendTick(void)
  308. {
  309. /* Disable SysTick Interrupt */
  310. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  311. }
  312. /**
  313. * @brief Resumes the Tick increment.
  314. * @note In the default implementation , SysTick timer is the source of time base. It is
  315. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  316. * is called, the the SysTick interrupt will be enabled and so Tick increment
  317. * is resumed.
  318. * @note This function is declared as __weak to be overwritten in case of other
  319. * implementations in user file.
  320. * @retval None
  321. */
  322. __weak void HAL_ResumeTick(void)
  323. {
  324. /* Enable SysTick Interrupt */
  325. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  326. }
  327. /**
  328. * @brief Returns the HAL revision
  329. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  330. */
  331. uint32_t HAL_GetHalVersion(void)
  332. {
  333. return __STM32L0xx_HAL_VERSION;
  334. }
  335. /**
  336. * @brief Returns the device revision identifier.
  337. * @retval Device revision identifier
  338. */
  339. uint32_t HAL_GetREVID(void)
  340. {
  341. return((DBGMCU->IDCODE) >> 16U);
  342. }
  343. /**
  344. * @brief Returns the device identifier.
  345. * @retval Device identifier
  346. */
  347. uint32_t HAL_GetDEVID(void)
  348. {
  349. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  350. }
  351. /**
  352. * @brief Enables the Debug Module during SLEEP mode
  353. * @retval None
  354. */
  355. void HAL_DBGMCU_EnableDBGSleepMode(void)
  356. {
  357. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  358. }
  359. /**
  360. * @brief Disables the Debug Module during SLEEP mode
  361. * @retval None
  362. */
  363. void HAL_DBGMCU_DisableDBGSleepMode(void)
  364. {
  365. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  366. }
  367. /**
  368. * @brief Enables the Debug Module during STOP mode
  369. * @retval None
  370. */
  371. void HAL_DBGMCU_EnableDBGStopMode(void)
  372. {
  373. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  374. }
  375. /**
  376. * @brief Disables the Debug Module during STOP mode
  377. * @retval None
  378. */
  379. void HAL_DBGMCU_DisableDBGStopMode(void)
  380. {
  381. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  382. }
  383. /**
  384. * @brief Enables the Debug Module during STANDBY mode
  385. * @retval None
  386. */
  387. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  388. {
  389. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  390. }
  391. /**
  392. * @brief Disables the Debug Module during STANDBY mode
  393. * @retval None
  394. */
  395. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  396. {
  397. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  398. }
  399. /**
  400. * @brief Enable low power mode behavior when the MCU is in Debug mode.
  401. * @param Periph: specifies the low power mode.
  402. * This parameter can be any combination of the following values:
  403. * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
  404. * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
  405. * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
  406. * @retval None
  407. */
  408. void HAL_DBGMCU_DBG_EnableLowPowerConfig(uint32_t Periph)
  409. {
  410. /* Check the parameters */
  411. assert_param(IS_DBGMCU_PERIPH(Periph));
  412. DBGMCU->CR |= Periph;
  413. }
  414. /**
  415. * @brief Disable low power mode behavior when the MCU is in Debug mode.
  416. * @param Periph: specifies the low power mode.
  417. * This parameter can be any combination of the following values:
  418. * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
  419. * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
  420. * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
  421. * @retval None
  422. */
  423. void HAL_DBGMCU_DBG_DisableLowPowerConfig(uint32_t Periph)
  424. {
  425. /* Check the parameters */
  426. assert_param(IS_DBGMCU_PERIPH(Periph));
  427. {
  428. DBGMCU->CR &= ~Periph;
  429. }
  430. }
  431. /**
  432. * @brief Returns the boot mode as configured by user.
  433. * @retval The boot mode as configured by user. The returned value can be one
  434. * of the following values:
  435. * - 0x00000000 : Boot is configured in Main Flash memory
  436. * - 0x00000100 : Boot is configured in System Flash memory
  437. * - 0x00000300 : Boot is configured in Embedded SRAM memory
  438. */
  439. uint32_t HAL_SYSCFG_GetBootMode(void)
  440. {
  441. return (SYSCFG->CFGR1 & SYSCFG_CFGR1_BOOT_MODE);
  442. }
  443. /**
  444. * @brief Selects the output of internal reference voltage (VREFINT).
  445. * The VREFINT output can be routed to(PB0) or
  446. * (PB1) or both.
  447. * @param SYSCFG_Vrefint_OUTPUT: new state of the Vrefint output.
  448. * This parameter can be one of the following values:
  449. * @arg SYSCFG_VREFINT_OUT_NONE
  450. * @arg SYSCFG_VREFINT_OUT_PB0
  451. * @arg SYSCFG_VREFINT_OUT_PB1
  452. * @arg SYSCFG_VREFINT_OUT_PB0_PB1
  453. * @retval None
  454. */
  455. void HAL_SYSCFG_VREFINT_OutputSelect(uint32_t SYSCFG_Vrefint_OUTPUT)
  456. {
  457. /* Check the parameters */
  458. assert_param(IS_SYSCFG_VREFINT_OUT_SELECT(SYSCFG_Vrefint_OUTPUT));
  459. /* Set the output Vrefint pin */
  460. SYSCFG->CFGR3 &= ~(SYSCFG_CFGR3_VREF_OUT);
  461. SYSCFG->CFGR3 |= (uint32_t)(SYSCFG_Vrefint_OUTPUT);
  462. }
  463. /**
  464. * @brief Lock the SYSCFG VREF register values
  465. * @retval None
  466. */
  467. void HAL_SYSCFG_Enable_Lock_VREFINT(void)
  468. {
  469. /* Enable the LOCK by setting REF_LOCK bit in the CFGR3 register */
  470. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
  471. }
  472. /**
  473. * @brief Unlock the overall SYSCFG VREF register values
  474. * @retval None
  475. */
  476. void HAL_SYSCFG_Disable_Lock_VREFINT(void)
  477. {
  478. /* Disable the LOCK by setting REF_LOCK bit in the CFGR3 register */
  479. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
  480. }
  481. /**
  482. * @}
  483. */
  484. /**
  485. * @}
  486. */
  487. /**
  488. * @}
  489. */
  490. #endif /* HAL_MODULE_ENABLED */
  491. /**
  492. * @}
  493. */
  494. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/