stm32g4xx_ll_lptim.c 9.0 KB

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