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.
 
 
 

319 lines
9.5 KiB

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