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.
 
 
 

468 lines
15 KiB

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