stm32f4xx_ll_lptim.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f4xx_ll_lptim.h"
  22. #include "stm32f4xx_ll_bus.h"
  23. #include "stm32f4xx_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 STM32F4xx_LL_Driver
  30. * @{
  31. */
  32. #if defined (LPTIM1)
  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. else
  93. {
  94. result = ERROR;
  95. }
  96. return result;
  97. }
  98. /**
  99. * @brief Set each fields of the LPTIM_InitStruct structure to its default
  100. * value.
  101. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  102. * @retval None
  103. */
  104. void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
  105. {
  106. /* Set the default configuration */
  107. LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
  108. LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
  109. LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
  110. LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
  111. }
  112. /**
  113. * @brief Configure the LPTIMx peripheral according to the specified parameters.
  114. * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
  115. * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
  116. * @param LPTIMx LP Timer Instance
  117. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  118. * @retval An ErrorStatus enumeration value:
  119. * - SUCCESS: LPTIMx instance has been initialized
  120. * - ERROR: LPTIMx instance hasn't been initialized
  121. */
  122. ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
  123. {
  124. ErrorStatus result = SUCCESS;
  125. /* Check the parameters */
  126. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  127. assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
  128. assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
  129. assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
  130. assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
  131. /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
  132. (ENABLE bit is reset to 0).
  133. */
  134. if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
  135. {
  136. result = ERROR;
  137. }
  138. else
  139. {
  140. /* Set CKSEL bitfield according to ClockSource value */
  141. /* Set PRESC bitfield according to Prescaler value */
  142. /* Set WAVE bitfield according to Waveform value */
  143. /* Set WAVEPOL bitfield according to Polarity value */
  144. MODIFY_REG(LPTIMx->CFGR,
  145. (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
  146. LPTIM_InitStruct->ClockSource | \
  147. LPTIM_InitStruct->Prescaler | \
  148. LPTIM_InitStruct->Waveform | \
  149. LPTIM_InitStruct->Polarity);
  150. }
  151. return result;
  152. }
  153. /**
  154. * @brief Disable the LPTIM instance
  155. * @rmtoll CR ENABLE LL_LPTIM_Disable
  156. * @param LPTIMx Low-Power Timer instance
  157. * @note The following sequence is required to solve LPTIM disable HW limitation.
  158. * Please check Errata Sheet ES0335 for more details under "MCU may remain
  159. * stuck in LPTIM interrupt when entering Stop mode" section.
  160. * @retval None
  161. */
  162. void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
  163. {
  164. LL_RCC_ClocksTypeDef rcc_clock;
  165. uint32_t tmpclksource = 0;
  166. uint32_t tmpIER;
  167. uint32_t tmpCFGR;
  168. uint32_t tmpCMP;
  169. uint32_t tmpARR;
  170. uint32_t tmpOR;
  171. /* Check the parameters */
  172. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  173. __disable_irq();
  174. /********** Save LPTIM Config *********/
  175. /* Save LPTIM source clock */
  176. switch ((uint32_t)LPTIMx)
  177. {
  178. case LPTIM1_BASE:
  179. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  180. break;
  181. default:
  182. break;
  183. }
  184. /* Save LPTIM configuration registers */
  185. tmpIER = LPTIMx->IER;
  186. tmpCFGR = LPTIMx->CFGR;
  187. tmpCMP = LPTIMx->CMP;
  188. tmpARR = LPTIMx->ARR;
  189. tmpOR = LPTIMx->OR;
  190. /************* Reset LPTIM ************/
  191. (void)LL_LPTIM_DeInit(LPTIMx);
  192. /********* Restore LPTIM Config *******/
  193. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  194. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  195. {
  196. /* Force LPTIM source kernel clock from APB */
  197. switch ((uint32_t)LPTIMx)
  198. {
  199. case LPTIM1_BASE:
  200. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
  201. break;
  202. default:
  203. break;
  204. }
  205. if (tmpCMP != 0UL)
  206. {
  207. /* Restore CMP and ARR registers (LPTIM should be enabled first) */
  208. LPTIMx->CR |= LPTIM_CR_ENABLE;
  209. LPTIMx->CMP = tmpCMP;
  210. /* Polling on CMP write ok status after above restore operation */
  211. do
  212. {
  213. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  214. }
  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. }
  228. while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  229. LL_LPTIM_ClearFlag_ARROK(LPTIMx);
  230. }
  231. /* Restore LPTIM source kernel clock */
  232. LL_RCC_SetLPTIMClockSource(tmpclksource);
  233. }
  234. /* Restore configuration registers (LPTIM should be disabled first) */
  235. LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
  236. LPTIMx->IER = tmpIER;
  237. LPTIMx->CFGR = tmpCFGR;
  238. LPTIMx->OR = tmpOR;
  239. __enable_irq();
  240. }
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /**
  248. * @}
  249. */
  250. #endif /* LPTIM1 */
  251. /**
  252. * @}
  253. */
  254. #endif /* USE_FULL_LL_DRIVER */
  255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/