You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

619 lines
18 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal.c
  4. * @author MCD Application Team
  5. * @brief HAL module driver.
  6. * This is the common part of the HAL initialization
  7. *
  8. @verbatim
  9. ==============================================================================
  10. ##### How to use this driver #####
  11. ==============================================================================
  12. [..]
  13. The common HAL driver contains a set of generic and common APIs that can be
  14. used by the PPP peripheral drivers and the user to start using the HAL.
  15. [..]
  16. The HAL contains two APIs categories:
  17. (+) Common HAL APIs
  18. (+) Services HAL APIs
  19. @endverbatim
  20. ******************************************************************************
  21. * @attention
  22. *
  23. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  24. * All rights reserved.</center></h2>
  25. *
  26. * This software component is licensed by ST under BSD 3-Clause license,
  27. * the "License"; You may not use this file except in compliance with the
  28. * License. You may obtain a copy of the License at:
  29. * opensource.org/licenses/BSD-3-Clause
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32l0xx_hal.h"
  35. /** @addtogroup STM32L0xx_HAL_Driver
  36. * @{
  37. */
  38. #ifdef HAL_MODULE_ENABLED
  39. /** @addtogroup HAL
  40. * @brief HAL module driver.
  41. * @{
  42. */
  43. /** @addtogroup HAL_Exported_Constants
  44. * @{
  45. */
  46. /** @defgroup HAL_Version HAL Version
  47. * @{
  48. */
  49. /**
  50. * @brief STM32L0xx HAL Driver version number
  51. */
  52. #define __STM32L0xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  53. #define __STM32L0xx_HAL_VERSION_SUB1 (0x0AU) /*!< [23:16] sub1 version */
  54. #define __STM32L0xx_HAL_VERSION_SUB2 (0x02U) /*!< [15:8] sub2 version */
  55. #define __STM32L0xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  56. #define __STM32L0xx_HAL_VERSION ((__STM32L0xx_HAL_VERSION_MAIN << 24U)\
  57. |(__STM32L0xx_HAL_VERSION_SUB1 << 16U)\
  58. |(__STM32L0xx_HAL_VERSION_SUB2 << 8U )\
  59. |(__STM32L0xx_HAL_VERSION_RC))
  60. #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFFU)
  61. /**
  62. * @}
  63. */
  64. /**
  65. * @}
  66. */
  67. /* Exported variables --------------------------------------------------------*/
  68. /** @addtogroup HAL_Exported_Variables
  69. * @{
  70. */
  71. __IO uint32_t uwTick;
  72. /**
  73. * @}
  74. */
  75. /* Exported functions --------------------------------------------------------*/
  76. /** @addtogroup HAL_Exported_Functions
  77. * @{
  78. */
  79. /** @addtogroup HAL_Exported_Functions_Group1
  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. (+) Initialize the Flash interface, the NVIC allocation and initial clock
  88. configuration. It initializes the source of time base also when timeout
  89. is needed and the backup domain when enabled.
  90. (+) De-initialize 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 configures the Flash prefetch, Flash preread and Buffer cache,
  113. * Configures time base source, NVIC and Low level hardware
  114. * @note This function is called at the beginning of program after reset and before
  115. * the clock configuration
  116. * @note The time base configuration is based on MSI clock when exiting from Reset.
  117. * Once done, time base tick start incrementing.
  118. * In the default implementation,Systick is used as source of time base.
  119. * the tick variable is incremented each 1ms in its ISR.
  120. * @retval HAL status
  121. */
  122. HAL_StatusTypeDef HAL_Init(void)
  123. {
  124. HAL_StatusTypeDef status = HAL_OK;
  125. /* Configure Buffer cache, Flash prefetch, Flash preread */
  126. #if (BUFFER_CACHE_DISABLE != 0)
  127. __HAL_FLASH_BUFFER_CACHE_DISABLE();
  128. #endif /* BUFFER_CACHE_DISABLE */
  129. #if (PREREAD_ENABLE != 0)
  130. __HAL_FLASH_PREREAD_BUFFER_ENABLE();
  131. #endif /* PREREAD_ENABLE */
  132. #if (PREFETCH_ENABLE != 0)
  133. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  134. #endif /* PREFETCH_ENABLE */
  135. /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  136. if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  137. {
  138. status = HAL_ERROR;
  139. }
  140. else
  141. {
  142. /* Init the low level hardware */
  143. HAL_MspInit();
  144. }
  145. /* Return function status */
  146. return status;
  147. }
  148. /**
  149. * @brief This function de-initializes common part of the HAL and stops the source
  150. * of time base.
  151. * @note This function is optional.
  152. * @retval HAL status
  153. */
  154. HAL_StatusTypeDef HAL_DeInit(void)
  155. {
  156. /* Reset of all peripherals */
  157. __HAL_RCC_APB1_FORCE_RESET();
  158. __HAL_RCC_APB1_RELEASE_RESET();
  159. __HAL_RCC_APB2_FORCE_RESET();
  160. __HAL_RCC_APB2_RELEASE_RESET();
  161. __HAL_RCC_AHB_FORCE_RESET();
  162. __HAL_RCC_AHB_RELEASE_RESET();
  163. __HAL_RCC_IOP_FORCE_RESET();
  164. __HAL_RCC_IOP_RELEASE_RESET();
  165. /* De-Init the low level hardware */
  166. HAL_MspDeInit();
  167. /* Return function status */
  168. return HAL_OK;
  169. }
  170. /**
  171. * @brief Initializes the MSP.
  172. * @retval None
  173. */
  174. __weak void HAL_MspInit(void)
  175. {
  176. /* NOTE : This function should not be modified, when the callback is needed,
  177. the HAL_MspInit could be implemented in the user file
  178. */
  179. }
  180. /**
  181. * @brief DeInitializes the MSP.
  182. * @retval None
  183. */
  184. __weak void HAL_MspDeInit(void)
  185. {
  186. /* NOTE : This function should not be modified, when the callback is needed,
  187. the HAL_MspDeInit could be implemented in the user file
  188. */
  189. }
  190. /**
  191. * @brief This function configures the source of the time base:
  192. * The time source is configured to have 1ms time base with a dedicated
  193. * Tick interrupt priority.
  194. * @note This function is called automatically at the beginning of program after
  195. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  196. * @note In the default implementation, SysTick timer is the source of time base.
  197. * It is used to generate interrupts at regular time intervals.
  198. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  199. * The SysTick interrupt must have higher priority (numerically lower)
  200. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  201. * The function is declared as __weak to be overwritten in case of other
  202. * implementation in user file.
  203. * @param TickPriority Tick interrupt priority.
  204. * @retval HAL status
  205. */
  206. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  207. {
  208. HAL_StatusTypeDef status = HAL_OK;
  209. /*Configure the SysTick to have interrupt in 1ms time basis*/
  210. if (HAL_SYSTICK_Config(SystemCoreClock/1000UL) != 0U)
  211. {
  212. status = HAL_ERROR;
  213. }
  214. else
  215. {
  216. /*Configure the SysTick IRQ priority */
  217. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0);
  218. }
  219. /* Return function status */
  220. return status;
  221. }
  222. /**
  223. * @}
  224. */
  225. /** @addtogroup HAL_Exported_Functions_Group2
  226. * @brief HAL Control functions
  227. *
  228. @verbatim
  229. ===============================================================================
  230. ##### HAL Control functions #####
  231. ===============================================================================
  232. [..] This section provides functions allowing to:
  233. (+) Provide a tick value in millisecond
  234. (+) Provide a blocking delay in millisecond
  235. (+) Suspend the time base source interrupt
  236. (+) Resume the time base source interrupt
  237. (+) Get the HAL API driver version
  238. (+) Get the device identifier
  239. (+) Get the device revision identifier
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief This function is called to increment a global variable "uwTick"
  245. * used as application time base.
  246. * @note In the default implementation, this variable is incremented each 1ms
  247. * in SysTick ISR.
  248. * @note This function is declared as __weak to be overwritten in case of other
  249. * implementations in user file.
  250. * @retval None
  251. */
  252. __weak void HAL_IncTick(void)
  253. {
  254. uwTick++;
  255. }
  256. /**
  257. * @brief Provides a tick value in millisecond.
  258. * @note This function is declared as __weak to be overwritten in case of other
  259. * implementations in user file.
  260. * @retval tick value
  261. */
  262. __weak uint32_t HAL_GetTick(void)
  263. {
  264. return uwTick;
  265. }
  266. /**
  267. * @brief This function provides minimum delay (in milliseconds) based
  268. * on variable incremented.
  269. * @note In the default implementation , SysTick timer is the source of time base.
  270. * It is used to generate interrupts at regular time intervals where uwTick
  271. * is incremented.
  272. * @note This function is declared as __weak to be overwritten in case of other
  273. * implementations in user file.
  274. * @param Delay specifies the delay time length, in milliseconds.
  275. * @retval None
  276. */
  277. __weak void HAL_Delay(uint32_t Delay)
  278. {
  279. uint32_t tickstart = HAL_GetTick();
  280. uint32_t wait = Delay;
  281. /* Add a period to guaranty minimum wait */
  282. if (wait < HAL_MAX_DELAY)
  283. {
  284. wait++;
  285. }
  286. while((HAL_GetTick() - tickstart) < wait)
  287. {
  288. }
  289. }
  290. /**
  291. * @brief Suspends the Tick increment.
  292. * @note In the default implementation , SysTick timer is the source of time base. It is
  293. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  294. * is called, the SysTick interrupt will be disabled and so Tick increment
  295. * is suspended.
  296. * @note This function is declared as __weak to be overwritten in case of other
  297. * implementations in user file.
  298. * @retval None
  299. */
  300. __weak void HAL_SuspendTick(void)
  301. {
  302. /* Disable SysTick Interrupt */
  303. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  304. }
  305. /**
  306. * @brief Resumes the Tick increment.
  307. * @note In the default implementation , SysTick timer is the source of time base. It is
  308. * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  309. * is called, the SysTick interrupt will be enabled and so Tick increment
  310. * is resumed.
  311. * @note This function is declared as __weak to be overwritten in case of other
  312. * implementations in user file.
  313. * @retval None
  314. */
  315. __weak void HAL_ResumeTick(void)
  316. {
  317. /* Enable SysTick Interrupt */
  318. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  319. }
  320. /**
  321. * @brief Returns the HAL revision
  322. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  323. */
  324. uint32_t HAL_GetHalVersion(void)
  325. {
  326. return __STM32L0xx_HAL_VERSION;
  327. }
  328. /**
  329. * @brief Returns the device revision identifier.
  330. * @retval Device revision identifier
  331. */
  332. uint32_t HAL_GetREVID(void)
  333. {
  334. return((DBGMCU->IDCODE) >> 16U);
  335. }
  336. /**
  337. * @brief Returns the device identifier.
  338. * @retval Device identifier
  339. */
  340. uint32_t HAL_GetDEVID(void)
  341. {
  342. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  343. }
  344. /**
  345. * @brief Returns the first word of the unique device identifier (UID based on 96 bits)
  346. * @retval Device identifier
  347. */
  348. uint32_t HAL_GetUIDw0(void)
  349. {
  350. return(READ_REG(*((uint32_t *)UID_BASE)));
  351. }
  352. /**
  353. * @brief Returns the second word of the unique device identifier (UID based on 96 bits)
  354. * @retval Device identifier
  355. */
  356. uint32_t HAL_GetUIDw1(void)
  357. {
  358. return(READ_REG(*((uint32_t *)(UID_BASE + 0x04U))));
  359. }
  360. /**
  361. * @brief Returns the third word of the unique device identifier (UID based on 96 bits)
  362. * @retval Device identifier
  363. */
  364. uint32_t HAL_GetUIDw2(void)
  365. {
  366. return(READ_REG(*((uint32_t *)(UID_BASE + 0x14U))));
  367. }
  368. /**
  369. * @}
  370. */
  371. /** @addtogroup HAL_Exported_Functions_Group2
  372. * @brief HAL Debug functions
  373. *
  374. @verbatim
  375. ===============================================================================
  376. ##### HAL Debug functions #####
  377. ===============================================================================
  378. [..] This section provides functions allowing to:
  379. (+) Enable/Disable Debug module during SLEEP mode
  380. (+) Enable/Disable Debug module during STOP mode
  381. (+) Enable/Disable Debug module during STANDBY mode
  382. @endverbatim
  383. * @{
  384. */
  385. /**
  386. * @brief Enables the Debug Module during SLEEP mode
  387. * @retval None
  388. */
  389. void HAL_DBGMCU_EnableDBGSleepMode(void)
  390. {
  391. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  392. }
  393. /**
  394. * @brief Disables the Debug Module during SLEEP mode
  395. * @retval None
  396. */
  397. void HAL_DBGMCU_DisableDBGSleepMode(void)
  398. {
  399. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  400. }
  401. /**
  402. * @brief Enables the Debug Module during STOP mode
  403. * @retval None
  404. */
  405. void HAL_DBGMCU_EnableDBGStopMode(void)
  406. {
  407. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  408. }
  409. /**
  410. * @brief Disables the Debug Module during STOP mode
  411. * @retval None
  412. */
  413. void HAL_DBGMCU_DisableDBGStopMode(void)
  414. {
  415. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  416. }
  417. /**
  418. * @brief Enables the Debug Module during STANDBY mode
  419. * @retval None
  420. */
  421. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  422. {
  423. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  424. }
  425. /**
  426. * @brief Disables the Debug Module during STANDBY mode
  427. * @retval None
  428. */
  429. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  430. {
  431. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  432. }
  433. /**
  434. * @brief Enable low power mode behavior when the MCU is in Debug mode.
  435. * @param Periph: specifies the low power mode.
  436. * This parameter can be any combination of the following values:
  437. * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
  438. * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
  439. * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
  440. * @retval None
  441. */
  442. void HAL_DBGMCU_DBG_EnableLowPowerConfig(uint32_t Periph)
  443. {
  444. /* Check the parameters */
  445. assert_param(IS_DBGMCU_PERIPH(Periph));
  446. DBGMCU->CR |= Periph;
  447. }
  448. /**
  449. * @brief Disable low power mode behavior when the MCU is in Debug mode.
  450. * @param Periph: specifies the low power mode.
  451. * This parameter can be any combination of the following values:
  452. * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
  453. * @arg DBGMCU_STOP: Keep debugger connection during STOP mode
  454. * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
  455. * @retval None
  456. */
  457. void HAL_DBGMCU_DBG_DisableLowPowerConfig(uint32_t Periph)
  458. {
  459. /* Check the parameters */
  460. assert_param(IS_DBGMCU_PERIPH(Periph));
  461. {
  462. DBGMCU->CR &= ~Periph;
  463. }
  464. }
  465. /**
  466. * @}
  467. */
  468. /** @addtogroup HAL_Exported_Functions_Group3
  469. * @brief HAL SYSCFG configuration functions
  470. *
  471. @verbatim
  472. ===============================================================================
  473. ##### HAL SYSCFG configuration functions #####
  474. ===============================================================================
  475. [..] This section provides functions allowing to:
  476. (+) Return the boot mode
  477. (+) Select the output of internal reference voltage (VREFINT)
  478. (+) Lock/Unlock the SYSCFG VREF register values
  479. @endverbatim
  480. * @{
  481. */
  482. /**
  483. * @brief Returns the boot mode as configured by user.
  484. * @retval The boot mode as configured by user. The returned value can be one
  485. * of the following values:
  486. * - 0x00000000 : Boot is configured in Main Flash memory
  487. * - 0x00000100 : Boot is configured in System Flash memory
  488. * - 0x00000300 : Boot is configured in Embedded SRAM memory
  489. */
  490. uint32_t HAL_SYSCFG_GetBootMode(void)
  491. {
  492. return (SYSCFG->CFGR1 & SYSCFG_CFGR1_BOOT_MODE);
  493. }
  494. /**
  495. * @brief Selects the output of internal reference voltage (VREFINT).
  496. * The VREFINT output can be routed to(PB0) or
  497. * (PB1) or both.
  498. * @param SYSCFG_Vrefint_OUTPUT: new state of the Vrefint output.
  499. * This parameter can be one of the following values:
  500. * @arg SYSCFG_VREFINT_OUT_NONE
  501. * @arg SYSCFG_VREFINT_OUT_PB0
  502. * @arg SYSCFG_VREFINT_OUT_PB1
  503. * @arg SYSCFG_VREFINT_OUT_PB0_PB1
  504. * @retval None
  505. */
  506. void HAL_SYSCFG_VREFINT_OutputSelect(uint32_t SYSCFG_Vrefint_OUTPUT)
  507. {
  508. /* Check the parameters */
  509. assert_param(IS_SYSCFG_VREFINT_OUT_SELECT(SYSCFG_Vrefint_OUTPUT));
  510. /* Set the output Vrefint pin */
  511. SYSCFG->CFGR3 &= ~(SYSCFG_CFGR3_VREF_OUT);
  512. SYSCFG->CFGR3 |= (uint32_t)(SYSCFG_Vrefint_OUTPUT);
  513. }
  514. /**
  515. * @brief Lock the SYSCFG VREF register values
  516. * @retval None
  517. */
  518. void HAL_SYSCFG_Enable_Lock_VREFINT(void)
  519. {
  520. /* Enable the LOCK by setting REF_LOCK bit in the CFGR3 register */
  521. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
  522. }
  523. /**
  524. * @brief Unlock the overall SYSCFG VREF register values
  525. * @retval None
  526. */
  527. void HAL_SYSCFG_Disable_Lock_VREFINT(void)
  528. {
  529. /* Disable the LOCK by setting REF_LOCK bit in the CFGR3 register */
  530. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
  531. }
  532. /**
  533. * @}
  534. */
  535. /**
  536. * @}
  537. */
  538. /**
  539. * @}
  540. */
  541. #endif /* HAL_MODULE_ENABLED */
  542. /**
  543. * @}
  544. */
  545. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/