stm32g0xx_ll_lptim.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_lptim.c
  4. * @author MCD Application Team
  5. * @brief LPTIM LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32g0xx_ll_lptim.h"
  21. #include "stm32g0xx_ll_bus.h"
  22. #include "stm32g0xx_ll_rcc.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif /* USE_FULL_ASSERT */
  28. /** @addtogroup STM32G0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (LPTIM1) || defined (LPTIM2)
  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(const 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_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM2);
  95. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_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, const 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. * @brief Disable the LPTIM instance
  161. * @rmtoll CR ENABLE LL_LPTIM_Disable
  162. * @param LPTIMx Low-Power Timer instance
  163. * @note The following sequence is required to solve LPTIM disable HW limitation.
  164. * Please check Errata Sheet ES0335 for more details under "MCU may remain
  165. * stuck in LPTIM interrupt when entering Stop mode" section.
  166. * @retval None
  167. */
  168. void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
  169. {
  170. LL_RCC_ClocksTypeDef rcc_clock;
  171. uint32_t tmpclksource = 0;
  172. uint32_t tmpIER;
  173. uint32_t tmpCFGR;
  174. uint32_t tmpCMP;
  175. uint32_t tmpARR;
  176. uint32_t primask_bit;
  177. uint32_t tmpCFGR2;
  178. /* Check the parameters */
  179. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  180. /* Enter critical section */
  181. primask_bit = __get_PRIMASK();
  182. __set_PRIMASK(1) ;
  183. /********** Save LPTIM Config *********/
  184. /* Save LPTIM source clock */
  185. switch ((uint32_t)LPTIMx)
  186. {
  187. case LPTIM1_BASE:
  188. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  189. break;
  190. #if defined(LPTIM2)
  191. case LPTIM2_BASE:
  192. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  193. break;
  194. #endif /* LPTIM2 */
  195. default:
  196. break;
  197. }
  198. /* Save LPTIM configuration registers */
  199. tmpIER = LPTIMx->IER;
  200. tmpCFGR = LPTIMx->CFGR;
  201. tmpCMP = LPTIMx->CMP;
  202. tmpARR = LPTIMx->ARR;
  203. tmpCFGR2 = LPTIMx->CFGR2;
  204. /************* Reset LPTIM ************/
  205. (void)LL_LPTIM_DeInit(LPTIMx);
  206. /********* Restore LPTIM Config *******/
  207. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  208. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  209. {
  210. /* Force LPTIM source kernel clock from APB */
  211. switch ((uint32_t)LPTIMx)
  212. {
  213. case LPTIM1_BASE:
  214. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
  215. break;
  216. #if defined(LPTIM2)
  217. case LPTIM2_BASE:
  218. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK1);
  219. break;
  220. #endif /* LPTIM2 */
  221. default:
  222. break;
  223. }
  224. if (tmpCMP != 0UL)
  225. {
  226. /* Restore CMP and ARR registers (LPTIM should be enabled first) */
  227. LPTIMx->CR |= LPTIM_CR_ENABLE;
  228. LPTIMx->CMP = tmpCMP;
  229. /* Polling on CMP write ok status after above restore operation */
  230. do
  231. {
  232. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  233. } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  234. LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
  235. }
  236. if (tmpARR != 0UL)
  237. {
  238. LPTIMx->CR |= LPTIM_CR_ENABLE;
  239. LPTIMx->ARR = tmpARR;
  240. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  241. /* Polling on ARR write ok status after above restore operation */
  242. do
  243. {
  244. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  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->CFGR2 = tmpCFGR2;
  256. /* Exit critical section: restore previous priority mask */
  257. __set_PRIMASK(primask_bit);
  258. }
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. /**
  266. * @}
  267. */
  268. #endif /* LPTIM1 || LPTIM2 */
  269. /**
  270. * @}
  271. */
  272. #endif /* USE_FULL_LL_DRIVER */