训练营PLSR题目
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.
 
 
 
 
 
 

302 lines
9.1 KiB

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