stm32l4xx_ll_lptim.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_lptim.c
  4. * @author MCD Application Team
  5. * @brief LPTIM LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 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 "stm32l4xx_ll_lptim.h"
  21. #include "stm32l4xx_ll_bus.h"
  22. #include "stm32l4xx_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 STM32L4xx_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_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, 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 tmpOR;
  178. #if defined(LPTIM_RCR_REP)
  179. uint32_t tmpRCR;
  180. #endif
  181. /* Check the parameters */
  182. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  183. /* Enter critical section */
  184. primask_bit = __get_PRIMASK();
  185. __set_PRIMASK(1) ;
  186. /********** Save LPTIM Config *********/
  187. /* Save LPTIM source clock */
  188. switch ((uint32_t)LPTIMx)
  189. {
  190. case LPTIM1_BASE:
  191. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  192. break;
  193. #if defined(LPTIM2)
  194. case LPTIM2_BASE:
  195. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  196. break;
  197. #endif /* LPTIM2 */
  198. default:
  199. break;
  200. }
  201. /* Save LPTIM configuration registers */
  202. tmpIER = LPTIMx->IER;
  203. tmpCFGR = LPTIMx->CFGR;
  204. tmpCMP = LPTIMx->CMP;
  205. tmpARR = LPTIMx->ARR;
  206. tmpOR = LPTIMx->OR;
  207. #if defined(LPTIM_RCR_REP)
  208. tmpRCR = LPTIMx->RCR;
  209. #endif
  210. /************* Reset LPTIM ************/
  211. (void)LL_LPTIM_DeInit(LPTIMx);
  212. /********* Restore LPTIM Config *******/
  213. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  214. #if defined(LPTIM_RCR_REP)
  215. if ((tmpCMP != 0UL) || (tmpARR != 0UL) || (tmpRCR != 0UL))
  216. #else
  217. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  218. #endif
  219. {
  220. /* Force LPTIM source kernel clock from APB */
  221. switch ((uint32_t)LPTIMx)
  222. {
  223. case LPTIM1_BASE:
  224. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
  225. break;
  226. #if defined(LPTIM2)
  227. case LPTIM2_BASE:
  228. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK1);
  229. break;
  230. #endif /* LPTIM2 */
  231. default:
  232. break;
  233. }
  234. if (tmpCMP != 0UL)
  235. {
  236. /* Restore CMP and ARR registers (LPTIM should be enabled first) */
  237. LPTIMx->CR |= LPTIM_CR_ENABLE;
  238. LPTIMx->CMP = tmpCMP;
  239. /* Polling on CMP write ok status after above restore operation */
  240. do
  241. {
  242. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  243. } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  244. LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
  245. }
  246. if (tmpARR != 0UL)
  247. {
  248. LPTIMx->CR |= LPTIM_CR_ENABLE;
  249. LPTIMx->ARR = tmpARR;
  250. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  251. /* Polling on ARR write ok status after above restore operation */
  252. do
  253. {
  254. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  255. } while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  256. LL_LPTIM_ClearFlag_ARROK(LPTIMx);
  257. }
  258. #if defined(LPTIM_RCR_REP)
  259. if (tmpRCR != 0UL)
  260. {
  261. LPTIMx->CR |= LPTIM_CR_ENABLE;
  262. LPTIMx->RCR = tmpRCR;
  263. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  264. /* Polling on RCR write ok status after above restore operation */
  265. do
  266. {
  267. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  268. } while (((LL_LPTIM_IsActiveFlag_REPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  269. LL_LPTIM_ClearFlag_REPOK(LPTIMx);
  270. }
  271. #endif
  272. /* Restore LPTIM source kernel clock */
  273. LL_RCC_SetLPTIMClockSource(tmpclksource);
  274. }
  275. /* Restore configuration registers (LPTIM should be disabled first) */
  276. LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
  277. LPTIMx->IER = tmpIER;
  278. LPTIMx->CFGR = tmpCFGR;
  279. LPTIMx->OR = tmpOR;
  280. /* Exit critical section: restore previous priority mask */
  281. __set_PRIMASK(primask_bit);
  282. }
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @}
  288. */
  289. /**
  290. * @}
  291. */
  292. #endif /* LPTIM1 || LPTIM2 */
  293. /**
  294. * @}
  295. */
  296. #endif /* USE_FULL_LL_DRIVER */