stm32l1xx_hal.c 17 KB

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