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.
 
 
 

312 lines
9.4 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_ll_lptim.c
  4. * @author MCD Application Team
  5. * @brief LPTIM LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. * ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32l0xx_ll_lptim.h"
  21. #include "stm32l0xx_ll_bus.h"
  22. #include "stm32l0xx_ll_rcc.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32L0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (LPTIM1)
  32. /** @addtogroup LPTIM_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /* Private macros ------------------------------------------------------------*/
  39. /** @addtogroup LPTIM_LL_Private_Macros
  40. * @{
  41. */
  42. #define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
  43. || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
  44. #define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1) \
  45. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2) \
  46. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4) \
  47. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8) \
  48. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16) \
  49. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32) \
  50. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64) \
  51. || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
  52. #define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
  53. || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
  54. #define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
  55. || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
  56. /**
  57. * @}
  58. */
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
  62. * @{
  63. */
  64. /**
  65. * @}
  66. */
  67. /* Exported functions --------------------------------------------------------*/
  68. /** @addtogroup LPTIM_LL_Exported_Functions
  69. * @{
  70. */
  71. /** @addtogroup LPTIM_LL_EF_Init
  72. * @{
  73. */
  74. /**
  75. * @brief Set LPTIMx registers to their reset values.
  76. * @param LPTIMx LP Timer instance
  77. * @retval An ErrorStatus enumeration value:
  78. * - SUCCESS: LPTIMx registers are de-initialized
  79. * - ERROR: invalid LPTIMx instance
  80. */
  81. ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef* LPTIMx)
  82. {
  83. ErrorStatus result = SUCCESS;
  84. /* Check the parameters */
  85. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  86. if (LPTIMx == LPTIM1)
  87. {
  88. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
  89. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
  90. }
  91. #if defined(LPTIM2)
  92. else if (LPTIMx == LPTIM2)
  93. {
  94. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPTIM2);
  95. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPTIM2);
  96. }
  97. #endif /* LPTIM2 */
  98. else
  99. {
  100. result = ERROR;
  101. }
  102. return result;
  103. }
  104. /**
  105. * @brief Set each fields of the LPTIM_InitStruct structure to its default
  106. * value.
  107. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  108. * @retval None
  109. */
  110. void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef* LPTIM_InitStruct)
  111. {
  112. /* Set the default configuration */
  113. LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
  114. LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
  115. LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
  116. LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
  117. }
  118. /**
  119. * @brief Configure the LPTIMx peripheral according to the specified parameters.
  120. * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
  121. * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
  122. * @param LPTIMx LP Timer Instance
  123. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  124. * @retval An ErrorStatus enumeration value:
  125. * - SUCCESS: LPTIMx instance has been initialized
  126. * - ERROR: LPTIMx instance hasn't been initialized
  127. */
  128. ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef * LPTIMx, LL_LPTIM_InitTypeDef* LPTIM_InitStruct)
  129. {
  130. ErrorStatus result = SUCCESS;
  131. /* Check the parameters */
  132. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  133. assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
  134. assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
  135. assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
  136. assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
  137. /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
  138. (ENABLE bit is reset to 0).
  139. */
  140. if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
  141. {
  142. result = ERROR;
  143. }
  144. else
  145. {
  146. /* Set CKSEL bitfield according to ClockSource value */
  147. /* Set PRESC bitfield according to Prescaler value */
  148. /* Set WAVE bitfield according to Waveform value */
  149. /* Set WAVEPOL bitfield according to Polarity value */
  150. MODIFY_REG(LPTIMx->CFGR,
  151. (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE| LPTIM_CFGR_WAVPOL),
  152. LPTIM_InitStruct->ClockSource | \
  153. LPTIM_InitStruct->Prescaler | \
  154. LPTIM_InitStruct->Waveform | \
  155. LPTIM_InitStruct->Polarity);
  156. }
  157. return result;
  158. }
  159. /**
  160. * @}
  161. */
  162. /**
  163. * @}
  164. */
  165. /**
  166. * @brief Disable the LPTIM instance
  167. * @rmtoll CR ENABLE LL_LPTIM_Disable
  168. * @param LPTIMx Low-Power Timer instance
  169. * @note The following sequence is required to solve LPTIM disable HW limitation.
  170. * Please check Errata Sheet ES0335 for more details under "MCU may remain
  171. * stuck in LPTIM interrupt when entering Stop mode" section.
  172. * @retval None
  173. */
  174. void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
  175. {
  176. LL_RCC_ClocksTypeDef rcc_clock;
  177. uint32_t tmpclksource = 0;
  178. uint32_t tmpIER;
  179. uint32_t tmpCFGR;
  180. uint32_t tmpCMP;
  181. uint32_t tmpARR;
  182. /* Check the parameters */
  183. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  184. __disable_irq();
  185. /********** Save LPTIM Config *********/
  186. /* Save LPTIM source clock */
  187. switch ((uint32_t)LPTIMx)
  188. {
  189. case LPTIM1_BASE:
  190. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  191. break;
  192. #if defined(LPTIM2)
  193. case LPTIM2_BASE:
  194. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  195. break;
  196. #endif /* LPTIM2 */
  197. default:
  198. break;
  199. }
  200. /* Save LPTIM configuration registers */
  201. tmpIER = LPTIMx->IER;
  202. tmpCFGR = LPTIMx->CFGR;
  203. tmpCMP = LPTIMx->CMP;
  204. tmpARR = LPTIMx->ARR;
  205. /************* Reset LPTIM ************/
  206. (void)LL_LPTIM_DeInit(LPTIMx);
  207. /********* Restore LPTIM Config *******/
  208. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  209. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  210. {
  211. /* Force LPTIM source kernel clock from APB */
  212. switch ((uint32_t)LPTIMx)
  213. {
  214. case LPTIM1_BASE:
  215. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
  216. break;
  217. #if defined(LPTIM2)
  218. case LPTIM2_BASE:
  219. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK1);
  220. break;
  221. #endif /* LPTIM2 */
  222. default:
  223. break;
  224. }
  225. if (tmpCMP != 0UL)
  226. {
  227. /* Restore CMP and ARR registers (LPTIM should be enabled first) */
  228. LPTIMx->CR |= LPTIM_CR_ENABLE;
  229. LPTIMx->CMP = tmpCMP;
  230. /* Polling on CMP write ok status after above restore operation */
  231. do
  232. {
  233. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  234. } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  235. LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
  236. }
  237. if (tmpARR != 0UL)
  238. {
  239. LPTIMx->CR |= LPTIM_CR_ENABLE;
  240. LPTIMx->ARR = tmpARR;
  241. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  242. /* Polling on ARR write ok status after above restore operation */
  243. do
  244. {
  245. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  246. } while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  247. LL_LPTIM_ClearFlag_ARROK(LPTIMx);
  248. }
  249. /* Restore LPTIM source kernel clock */
  250. LL_RCC_SetLPTIMClockSource(tmpclksource);
  251. }
  252. /* Restore configuration registers (LPTIM should be disabled first) */
  253. LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
  254. LPTIMx->IER = tmpIER;
  255. LPTIMx->CFGR = tmpCFGR;
  256. __enable_irq();
  257. }
  258. /**
  259. * @}
  260. */
  261. #endif /* LPTIM1 */
  262. /**
  263. * @}
  264. */
  265. #endif /* USE_FULL_LL_DRIVER */
  266. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/