stm32l1xx_hal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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</center></h2>
  24. *
  25. * Redistribution and use in source and binary forms, with or without modification,
  26. * are permitted provided that the following conditions are met:
  27. * 1. Redistributions of source code must retain the above copyright notice,
  28. * this list of conditions and the following disclaimer.
  29. * 2. Redistributions in binary form must reproduce the above copyright notice,
  30. * this list of conditions and the following disclaimer in the documentation
  31. * and/or other materials provided with the distribution.
  32. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  37. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "stm32l1xx_hal.h"
  51. /** @addtogroup STM32L1xx_HAL_Driver
  52. * @{
  53. */
  54. /** @defgroup HAL HAL
  55. * @brief HAL module driver.
  56. * @{
  57. */
  58. #ifdef HAL_MODULE_ENABLED
  59. /* Private typedef -----------------------------------------------------------*/
  60. /* Private define ------------------------------------------------------------*/
  61. /** @defgroup HAL_Private_Defines HAL Private Defines
  62. * @{
  63. */
  64. /**
  65. * @brief STM32L1xx HAL Driver version number
  66. */
  67. #define __STM32L1xx_HAL_VERSION_MAIN (0x01) /*!< [31:24] main version */
  68. #define __STM32L1xx_HAL_VERSION_SUB1 (0x03) /*!< [23:16] sub1 version */
  69. #define __STM32L1xx_HAL_VERSION_SUB2 (0x01) /*!< [15:8] sub2 version */
  70. #define __STM32L1xx_HAL_VERSION_RC (0x00) /*!< [7:0] release candidate */
  71. #define __STM32L1xx_HAL_VERSION ((__STM32L1xx_HAL_VERSION_MAIN << 24)\
  72. |(__STM32L1xx_HAL_VERSION_SUB1 << 16)\
  73. |(__STM32L1xx_HAL_VERSION_SUB2 << 8 )\
  74. |(__STM32L1xx_HAL_VERSION_RC))
  75. #define IDCODE_DEVID_MASK (0x00000FFFU)
  76. /**
  77. * @}
  78. */
  79. /* Private macro -------------------------------------------------------------*/
  80. /* Private variables ---------------------------------------------------------*/
  81. /** @defgroup HAL_Private_Variables HAL Private Variables
  82. * @{
  83. */
  84. __IO uint32_t uwTick;
  85. /**
  86. * @}
  87. */
  88. /* Private function prototypes -----------------------------------------------*/
  89. /* Private functions ---------------------------------------------------------*/
  90. /** @defgroup HAL_Exported_Functions HAL Exported Functions
  91. * @{
  92. */
  93. /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
  94. * @brief Initialization and de-initialization functions
  95. *
  96. @verbatim
  97. ===============================================================================
  98. ##### Initialization and de-initialization functions #####
  99. ===============================================================================
  100. [..] This section provides functions allowing to:
  101. (+) Initializes the Flash interface, the NVIC allocation and initial clock
  102. configuration. It initializes the source of time base also when timeout
  103. is needed and the backup domain when enabled.
  104. (+) de-Initializes common part of the HAL.
  105. (+) Configure The time base source to have 1ms time base with a dedicated
  106. Tick interrupt priority.
  107. (++) Systick timer is used by default as source of time base, but user
  108. can eventually implement his proper time base source (a general purpose
  109. timer for example or other time source), keeping in mind that Time base
  110. duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  111. handled in milliseconds basis.
  112. (++) Time base configuration function (HAL_InitTick ()) is called automatically
  113. at the beginning of the program after reset by HAL_Init() or at any time
  114. when clock is configured, by HAL_RCC_ClockConfig().
  115. (++) Source of time base is configured to generate interrupts at regular
  116. time intervals. Care must be taken if HAL_Delay() is called from a
  117. peripheral ISR process, the Tick interrupt line must have higher priority
  118. (numerically lower) than the peripheral interrupt. Otherwise the caller
  119. ISR process will be blocked.
  120. (++) functions affecting time base configurations are declared as __Weak
  121. to make override possible in case of other implementations in user file.
  122. @endverbatim
  123. * @{
  124. */
  125. /**
  126. * @brief This function configures the Flash prefetch,
  127. * Configures time base source, NVIC and Low level hardware
  128. * @note This function is called at the beginning of program after reset and before
  129. * the clock configuration
  130. * @note The time base configuration is based on MSI clock when exiting from Reset.
  131. * Once done, time base tick start incrementing.
  132. * In the default implementation,Systick is used as source of time base.
  133. * the tick variable is incremented each 1ms in its ISR.
  134. * @retval HAL status
  135. */
  136. HAL_StatusTypeDef HAL_Init(void)
  137. {
  138. /* Configure Flash prefetch */
  139. #if (PREFETCH_ENABLE != 0)
  140. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  141. #endif /* PREFETCH_ENABLE */
  142. /* Set Interrupt Group Priority */
  143. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  144. /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  145. HAL_InitTick(TICK_INT_PRIORITY);
  146. /* Init the low level hardware */
  147. HAL_MspInit();
  148. /* Return function status */
  149. return HAL_OK;
  150. }
  151. /**
  152. * @brief This function de-Initializes common part of the HAL and stops the source
  153. * of time base.
  154. * @note This function is optional.
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_DeInit(void)
  158. {
  159. /* Reset of all peripherals */
  160. __HAL_RCC_APB1_FORCE_RESET();
  161. __HAL_RCC_APB1_RELEASE_RESET();
  162. __HAL_RCC_APB2_FORCE_RESET();
  163. __HAL_RCC_APB2_RELEASE_RESET();
  164. __HAL_RCC_AHB_FORCE_RESET();
  165. __HAL_RCC_AHB_RELEASE_RESET();
  166. /* De-Init the low level hardware */
  167. HAL_MspDeInit();
  168. /* Return function status */
  169. return HAL_OK;
  170. }
  171. /**
  172. * @brief Initializes the MSP.
  173. * @retval None
  174. */
  175. __weak void HAL_MspInit(void)
  176. {
  177. /* NOTE : This function Should not be modified, when the callback is needed,
  178. the HAL_MspInit could be implemented in the user file
  179. */
  180. }
  181. /**
  182. * @brief DeInitializes the MSP.
  183. * @retval None
  184. */
  185. __weak void HAL_MspDeInit(void)
  186. {
  187. /* NOTE : This function Should not be modified, when the callback is needed,
  188. the HAL_MspDeInit could be implemented in the user file
  189. */
  190. }
  191. /**
  192. * @brief This function configures the source of the time base.
  193. * The time source is configured to have 1ms time base with a dedicated
  194. * Tick interrupt priority.
  195. * @note This function is called automatically at the beginning of program after
  196. * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
  197. * @note In the default implementation, SysTick timer is the source of time base.
  198. * It is used to generate interrupts at regular time intervals.
  199. * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  200. * The the SysTick interrupt must have higher priority (numerically lower)
  201. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  202. * The function is declared as __Weak to be overwritten in case of other
  203. * implementation in user file.
  204. * @param TickPriority: Tick interrupt priority.
  205. * @retval HAL status
  206. */
  207. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  208. {
  209. /*Configure the SysTick to have interrupt in 1ms time basis*/
  210. HAL_SYSTICK_Config(SystemCoreClock /1000);
  211. /*Configure the SysTick IRQ priority */
  212. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  213. /* Return function status */
  214. return HAL_OK;
  215. }
  216. /**
  217. * @}
  218. */
  219. /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
  220. * @brief HAL Control functions
  221. *
  222. @verbatim
  223. ===============================================================================
  224. ##### HAL Control functions #####
  225. ===============================================================================
  226. [..] This section provides functions allowing to:
  227. (+) Provide a tick value in millisecond
  228. (+) Provide a blocking delay in millisecond
  229. (+) Suspend the time base source interrupt
  230. (+) Resume the time base source interrupt
  231. (+) Get the HAL API driver version
  232. (+) Get the device identifier
  233. (+) Get the device revision identifier
  234. (+) Enable/Disable Debug module during Sleep mode
  235. (+) Enable/Disable Debug module during STOP mode
  236. (+) Enable/Disable Debug module during STANDBY mode
  237. @endverbatim
  238. * @{
  239. */
  240. /**
  241. * @brief This function is called to increment a global variable "uwTick"
  242. * used as application time base.
  243. * @note In the default implementation, this variable is incremented each 1ms
  244. * in Systick ISR.
  245. * @note This function is declared as __weak to be overwritten in case of other
  246. * implementations in user file.
  247. * @retval None
  248. */
  249. __weak void HAL_IncTick(void)
  250. {
  251. uwTick++;
  252. }
  253. /**
  254. * @brief Provides a tick value in millisecond.
  255. * @note This function is declared as __weak to be overwritten in case of other
  256. * implementations in user file.
  257. * @retval tick value
  258. */
  259. __weak uint32_t HAL_GetTick(void)
  260. {
  261. return uwTick;
  262. }
  263. /**
  264. * @brief This function provides accurate delay (in milliseconds) based
  265. * on variable incremented.
  266. * @note In the default implementation , SysTick timer is the source of time base.
  267. * It is used to generate interrupts at regular time intervals where uwTick
  268. * is incremented.
  269. * @note ThiS function is declared as __weak to be overwritten in case of other
  270. * implementations in user file.
  271. * @param Delay: specifies the delay time length, in milliseconds.
  272. * @retval None
  273. */
  274. __weak void HAL_Delay(__IO uint32_t Delay)
  275. {
  276. uint32_t tickstart = 0;
  277. tickstart = HAL_GetTick();
  278. while((HAL_GetTick() - tickstart) < Delay)
  279. {
  280. }
  281. }
  282. /**
  283. * @brief Suspend Tick increment.
  284. * @note In the default implementation , SysTick timer is the source of time base. It is
  285. * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  286. * is called, the the SysTick interrupt will be disabled and so Tick increment
  287. * is suspended.
  288. * @note This function is declared as __weak to be overwritten in case of other
  289. * implementations in user file.
  290. * @retval None
  291. */
  292. __weak void HAL_SuspendTick(void)
  293. {
  294. /* Disable SysTick Interrupt */
  295. CLEAR_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  296. }
  297. /**
  298. * @brief Resume 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_ResumeTick()
  301. * is called, the the SysTick interrupt will be enabled and so Tick increment
  302. * is resumed.
  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_ResumeTick(void)
  308. {
  309. /* Enable SysTick Interrupt */
  310. SET_BIT(SysTick->CTRL,SysTick_CTRL_TICKINT_Msk);
  311. }
  312. /**
  313. * @brief Returns the HAL revision
  314. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  315. */
  316. uint32_t HAL_GetHalVersion(void)
  317. {
  318. return __STM32L1xx_HAL_VERSION;
  319. }
  320. /**
  321. * @brief Returns the device revision identifier.
  322. * @retval Device revision identifier
  323. */
  324. uint32_t HAL_GetREVID(void)
  325. {
  326. return((DBGMCU->IDCODE) >> 16);
  327. }
  328. /**
  329. * @brief Returns the device identifier.
  330. * @retval Device identifier
  331. */
  332. uint32_t HAL_GetDEVID(void)
  333. {
  334. return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
  335. }
  336. /**
  337. * @brief Enable the Debug Module during SLEEP mode
  338. * @retval None
  339. */
  340. void HAL_DBGMCU_EnableDBGSleepMode(void)
  341. {
  342. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  343. }
  344. /**
  345. * @brief Disable the Debug Module during SLEEP mode
  346. * @retval None
  347. */
  348. void HAL_DBGMCU_DisableDBGSleepMode(void)
  349. {
  350. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
  351. }
  352. /**
  353. * @brief Enable the Debug Module during STOP mode
  354. * @retval None
  355. */
  356. void HAL_DBGMCU_EnableDBGStopMode(void)
  357. {
  358. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  359. }
  360. /**
  361. * @brief Disable the Debug Module during STOP mode
  362. * @retval None
  363. */
  364. void HAL_DBGMCU_DisableDBGStopMode(void)
  365. {
  366. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
  367. }
  368. /**
  369. * @brief Enable the Debug Module during STANDBY mode
  370. * @retval None
  371. */
  372. void HAL_DBGMCU_EnableDBGStandbyMode(void)
  373. {
  374. SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  375. }
  376. /**
  377. * @brief Disable the Debug Module during STANDBY mode
  378. * @retval None
  379. */
  380. void HAL_DBGMCU_DisableDBGStandbyMode(void)
  381. {
  382. CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
  383. }
  384. /**
  385. * @}
  386. */
  387. /**
  388. * @}
  389. */
  390. #endif /* HAL_MODULE_ENABLED */
  391. /**
  392. * @}
  393. */
  394. /**
  395. * @}
  396. */
  397. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/