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.
 
 
 

354 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_ll_lptim.c
  4. * @author MCD Application Team
  5. * @brief LPTIM LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 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 "stm32h7xx_ll_lptim.h"
  22. #include "stm32h7xx_ll_bus.h"
  23. #include "stm32h7xx_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 STM32H7xx_LL_Driver
  30. * @{
  31. */
  32. #if defined (LPTIM1) || defined (LPTIM2) || defined (LPTIM3) || defined (LPTIM4) || defined (LPTIM5)
  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 if (LPTIMx == LPTIM2)
  93. {
  94. LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM2);
  95. LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM2);
  96. }
  97. #if defined(LPTIM3)
  98. else if (LPTIMx == LPTIM3)
  99. {
  100. LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM3);
  101. LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM3);
  102. }
  103. #endif /* LPTIM3 */
  104. #if defined(LPTIM4)
  105. else if (LPTIMx == LPTIM4)
  106. {
  107. LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM4);
  108. LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM4);
  109. }
  110. #endif /* LPTIM4 */
  111. #if defined(LPTIM5)
  112. else if (LPTIMx == LPTIM5)
  113. {
  114. LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM5);
  115. LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM5);
  116. }
  117. #endif /* LPTIM5 */
  118. else
  119. {
  120. result = ERROR;
  121. }
  122. return result;
  123. }
  124. /**
  125. * @brief Set each fields of the LPTIM_InitStruct structure to its default
  126. * value.
  127. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  128. * @retval None
  129. */
  130. void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
  131. {
  132. /* Set the default configuration */
  133. LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
  134. LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
  135. LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
  136. LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
  137. }
  138. /**
  139. * @brief Configure the LPTIMx peripheral according to the specified parameters.
  140. * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
  141. * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
  142. * @param LPTIMx LP Timer Instance
  143. * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
  144. * @retval An ErrorStatus enumeration value:
  145. * - SUCCESS: LPTIMx instance has been initialized
  146. * - ERROR: LPTIMx instance hasn't been initialized
  147. */
  148. ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
  149. {
  150. ErrorStatus result = SUCCESS;
  151. /* Check the parameters */
  152. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  153. assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
  154. assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
  155. assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
  156. assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
  157. /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
  158. (ENABLE bit is reset to 0).
  159. */
  160. if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
  161. {
  162. result = ERROR;
  163. }
  164. else
  165. {
  166. /* Set CKSEL bitfield according to ClockSource value */
  167. /* Set PRESC bitfield according to Prescaler value */
  168. /* Set WAVE bitfield according to Waveform value */
  169. /* Set WAVEPOL bitfield according to Polarity value */
  170. MODIFY_REG(LPTIMx->CFGR,
  171. (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
  172. LPTIM_InitStruct->ClockSource | \
  173. LPTIM_InitStruct->Prescaler | \
  174. LPTIM_InitStruct->Waveform | \
  175. LPTIM_InitStruct->Polarity);
  176. }
  177. return result;
  178. }
  179. /**
  180. * @brief Disable the LPTIM instance
  181. * @rmtoll CR ENABLE LL_LPTIM_Disable
  182. * @param LPTIMx Low-Power Timer instance
  183. * @note The following sequence is required to solve LPTIM disable HW limitation.
  184. * Please check Errata Sheet ES0335 for more details under "MCU may remain
  185. * stuck in LPTIM interrupt when entering Stop mode" section.
  186. * @retval None
  187. */
  188. void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
  189. {
  190. LL_RCC_ClocksTypeDef rcc_clock;
  191. uint32_t tmpclksource = 0;
  192. uint32_t tmpIER;
  193. uint32_t tmpCFGR;
  194. uint32_t tmpCMP;
  195. uint32_t tmpARR;
  196. uint32_t tmpCFGR2;
  197. /* Check the parameters */
  198. assert_param(IS_LPTIM_INSTANCE(LPTIMx));
  199. __disable_irq();
  200. /********** Save LPTIM Config *********/
  201. /* Save LPTIM source clock */
  202. switch ((uint32_t)LPTIMx)
  203. {
  204. case LPTIM1_BASE:
  205. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
  206. break;
  207. case LPTIM2_BASE:
  208. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
  209. break;
  210. #if defined(LPTIM3)&&defined(LPTIM4)&&defined(LPTIM5)
  211. case LPTIM3_BASE:
  212. case LPTIM4_BASE:
  213. case LPTIM5_BASE:
  214. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM345_CLKSOURCE);
  215. break;
  216. #elif defined(LPTIM3)
  217. case LPTIM3_BASE:
  218. tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM3_CLKSOURCE);
  219. break;
  220. #endif /* LPTIM3 && LPTIM4 && LPTIM5 */
  221. default:
  222. break;
  223. }
  224. /* Save LPTIM configuration registers */
  225. tmpIER = LPTIMx->IER;
  226. tmpCFGR = LPTIMx->CFGR;
  227. tmpCMP = LPTIMx->CMP;
  228. tmpARR = LPTIMx->ARR;
  229. tmpCFGR2 = LPTIMx->CFGR2;
  230. /************* Reset LPTIM ************/
  231. (void)LL_LPTIM_DeInit(LPTIMx);
  232. /********* Restore LPTIM Config *******/
  233. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  234. if ((tmpCMP != 0UL) || (tmpARR != 0UL))
  235. {
  236. /* Force LPTIM source kernel clock from APB */
  237. switch ((uint32_t)LPTIMx)
  238. {
  239. case LPTIM1_BASE:
  240. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
  241. break;
  242. case LPTIM2_BASE:
  243. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK4);
  244. break;
  245. #if defined(LPTIM3)&&defined(LPTIM4)&&defined(LPTIM5)
  246. case LPTIM3_BASE:
  247. case LPTIM4_BASE:
  248. case LPTIM5_BASE:
  249. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM345_CLKSOURCE_PCLK4);
  250. break;
  251. #elif defined(LPTIM3)
  252. case LPTIM3_BASE:
  253. LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM3_CLKSOURCE_PCLK4);
  254. break;
  255. #endif /* LPTIM3 && LPTIM4 && LPTIM5*/
  256. default:
  257. break;
  258. }
  259. if (tmpCMP != 0UL)
  260. {
  261. /* Restore CMP and ARR registers (LPTIM should be enabled first) */
  262. LPTIMx->CR |= LPTIM_CR_ENABLE;
  263. LPTIMx->CMP = tmpCMP;
  264. /* Polling on CMP write ok status after above restore operation */
  265. do
  266. {
  267. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  268. } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  269. LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
  270. }
  271. if (tmpARR != 0UL)
  272. {
  273. LPTIMx->CR |= LPTIM_CR_ENABLE;
  274. LPTIMx->ARR = tmpARR;
  275. LL_RCC_GetSystemClocksFreq(&rcc_clock);
  276. /* Polling on ARR write ok status after above restore operation */
  277. do
  278. {
  279. rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
  280. } while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
  281. LL_LPTIM_ClearFlag_ARROK(LPTIMx);
  282. }
  283. /* Restore LPTIM source kernel clock */
  284. LL_RCC_SetLPTIMClockSource(tmpclksource);
  285. }
  286. /* Restore configuration registers (LPTIM should be disabled first) */
  287. LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
  288. LPTIMx->IER = tmpIER;
  289. LPTIMx->CFGR = tmpCFGR;
  290. LPTIMx->CFGR2 = tmpCFGR2;
  291. __enable_irq();
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. #endif /* LPTIM1 || LPTIM2 || LPTIM3 || LPTIM4 || LPTIM5 */
  303. /**
  304. * @}
  305. */
  306. #endif /* USE_FULL_LL_DRIVER */
  307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/