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.
 
 
 

848 lines
30 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_ll_tim.c
  4. * @author MCD Application Team
  5. * @brief TIM 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 "stm32l0xx_ll_tim.h"
  22. #include "stm32l0xx_ll_bus.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32L0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (TIM2) || defined (TIM3) || defined (TIM21) || defined (TIM22) || defined (TIM6) || defined (TIM7)
  32. /** @addtogroup TIM_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /* Private macros ------------------------------------------------------------*/
  39. /** @addtogroup TIM_LL_Private_Macros
  40. * @{
  41. */
  42. #define IS_LL_TIM_COUNTERMODE(__VALUE__) (((__VALUE__) == LL_TIM_COUNTERMODE_UP) \
  43. || ((__VALUE__) == LL_TIM_COUNTERMODE_DOWN) \
  44. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_UP) \
  45. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_DOWN) \
  46. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_UP_DOWN))
  47. #define IS_LL_TIM_CLOCKDIVISION(__VALUE__) (((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV1) \
  48. || ((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV2) \
  49. || ((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV4))
  50. #define IS_LL_TIM_OCMODE(__VALUE__) (((__VALUE__) == LL_TIM_OCMODE_FROZEN) \
  51. || ((__VALUE__) == LL_TIM_OCMODE_ACTIVE) \
  52. || ((__VALUE__) == LL_TIM_OCMODE_INACTIVE) \
  53. || ((__VALUE__) == LL_TIM_OCMODE_TOGGLE) \
  54. || ((__VALUE__) == LL_TIM_OCMODE_FORCED_INACTIVE) \
  55. || ((__VALUE__) == LL_TIM_OCMODE_FORCED_ACTIVE) \
  56. || ((__VALUE__) == LL_TIM_OCMODE_PWM1) \
  57. || ((__VALUE__) == LL_TIM_OCMODE_PWM2))
  58. #define IS_LL_TIM_OCSTATE(__VALUE__) (((__VALUE__) == LL_TIM_OCSTATE_DISABLE) \
  59. || ((__VALUE__) == LL_TIM_OCSTATE_ENABLE))
  60. #define IS_LL_TIM_OCPOLARITY(__VALUE__) (((__VALUE__) == LL_TIM_OCPOLARITY_HIGH) \
  61. || ((__VALUE__) == LL_TIM_OCPOLARITY_LOW))
  62. #define IS_LL_TIM_ACTIVEINPUT(__VALUE__) (((__VALUE__) == LL_TIM_ACTIVEINPUT_DIRECTTI) \
  63. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_INDIRECTTI) \
  64. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_TRC))
  65. #define IS_LL_TIM_ICPSC(__VALUE__) (((__VALUE__) == LL_TIM_ICPSC_DIV1) \
  66. || ((__VALUE__) == LL_TIM_ICPSC_DIV2) \
  67. || ((__VALUE__) == LL_TIM_ICPSC_DIV4) \
  68. || ((__VALUE__) == LL_TIM_ICPSC_DIV8))
  69. #define IS_LL_TIM_IC_FILTER(__VALUE__) (((__VALUE__) == LL_TIM_IC_FILTER_FDIV1) \
  70. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N2) \
  71. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N4) \
  72. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N8) \
  73. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N6) \
  74. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N8) \
  75. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N6) \
  76. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N8) \
  77. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N6) \
  78. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N8) \
  79. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N5) \
  80. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N6) \
  81. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N8) \
  82. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N5) \
  83. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N6) \
  84. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N8))
  85. #define IS_LL_TIM_IC_POLARITY(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  86. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING) \
  87. || ((__VALUE__) == LL_TIM_IC_POLARITY_BOTHEDGE))
  88. #define IS_LL_TIM_ENCODERMODE(__VALUE__) (((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI1) \
  89. || ((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI2) \
  90. || ((__VALUE__) == LL_TIM_ENCODERMODE_X4_TI12))
  91. #define IS_LL_TIM_IC_POLARITY_ENCODER(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  92. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING))
  93. /**
  94. * @}
  95. */
  96. /* Private function prototypes -----------------------------------------------*/
  97. /** @defgroup TIM_LL_Private_Functions TIM Private Functions
  98. * @{
  99. */
  100. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  101. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  102. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  103. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  104. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  105. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  106. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  107. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  108. /**
  109. * @}
  110. */
  111. /* Exported functions --------------------------------------------------------*/
  112. /** @addtogroup TIM_LL_Exported_Functions
  113. * @{
  114. */
  115. /** @addtogroup TIM_LL_EF_Init
  116. * @{
  117. */
  118. /**
  119. * @brief Set TIMx registers to their reset values.
  120. * @param TIMx Timer instance
  121. * @retval An ErrorStatus enumeration value:
  122. * - SUCCESS: TIMx registers are de-initialized
  123. * - ERROR: invalid TIMx instance
  124. */
  125. ErrorStatus LL_TIM_DeInit(TIM_TypeDef *TIMx)
  126. {
  127. ErrorStatus result = SUCCESS;
  128. /* Check the parameters */
  129. assert_param(IS_TIM_INSTANCE(TIMx));
  130. if (TIMx == TIM2)
  131. {
  132. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM2);
  133. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM2);
  134. }
  135. #if defined(TIM3)
  136. else if (TIMx == TIM3)
  137. {
  138. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM3);
  139. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM3);
  140. }
  141. #endif /* TIM3 */
  142. #if defined(TIM6)
  143. else if (TIMx == TIM6)
  144. {
  145. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM6);
  146. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM6);
  147. }
  148. #endif /* TIM6 */
  149. #if defined(TIM7)
  150. else if (TIMx == TIM7)
  151. {
  152. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM7);
  153. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM7);
  154. }
  155. #endif /* TIM7 */
  156. else if (TIMx == TIM21)
  157. {
  158. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM21);
  159. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM21);
  160. }
  161. #if defined(TIM22)
  162. else if (TIMx == TIM22)
  163. {
  164. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM22);
  165. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM22);
  166. }
  167. #endif /* TIM22 */
  168. else
  169. {
  170. result = ERROR;
  171. }
  172. return result;
  173. }
  174. /**
  175. * @brief Set the fields of the time base unit configuration data structure
  176. * to their default values.
  177. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure (time base unit configuration data structure)
  178. * @retval None
  179. */
  180. void LL_TIM_StructInit(LL_TIM_InitTypeDef *TIM_InitStruct)
  181. {
  182. /* Set the default configuration */
  183. TIM_InitStruct->Prescaler = (uint16_t)0x0000;
  184. TIM_InitStruct->CounterMode = LL_TIM_COUNTERMODE_UP;
  185. TIM_InitStruct->Autoreload = 0xFFFFFFFFU;
  186. TIM_InitStruct->ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  187. }
  188. /**
  189. * @brief Configure the TIMx time base unit.
  190. * @param TIMx Timer Instance
  191. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure (TIMx time base unit configuration data structure)
  192. * @retval An ErrorStatus enumeration value:
  193. * - SUCCESS: TIMx registers are de-initialized
  194. * - ERROR: not applicable
  195. */
  196. ErrorStatus LL_TIM_Init(TIM_TypeDef *TIMx, LL_TIM_InitTypeDef *TIM_InitStruct)
  197. {
  198. uint32_t tmpcr1;
  199. /* Check the parameters */
  200. assert_param(IS_TIM_INSTANCE(TIMx));
  201. assert_param(IS_LL_TIM_COUNTERMODE(TIM_InitStruct->CounterMode));
  202. assert_param(IS_LL_TIM_CLOCKDIVISION(TIM_InitStruct->ClockDivision));
  203. tmpcr1 = LL_TIM_ReadReg(TIMx, CR1);
  204. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  205. {
  206. /* Select the Counter Mode */
  207. MODIFY_REG(tmpcr1, (TIM_CR1_DIR | TIM_CR1_CMS), TIM_InitStruct->CounterMode);
  208. }
  209. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  210. {
  211. /* Set the clock division */
  212. MODIFY_REG(tmpcr1, TIM_CR1_CKD, TIM_InitStruct->ClockDivision);
  213. }
  214. /* Write to TIMx CR1 */
  215. LL_TIM_WriteReg(TIMx, CR1, tmpcr1);
  216. /* Set the Autoreload value */
  217. LL_TIM_SetAutoReload(TIMx, TIM_InitStruct->Autoreload);
  218. /* Set the Prescaler value */
  219. LL_TIM_SetPrescaler(TIMx, TIM_InitStruct->Prescaler);
  220. /* Generate an update event to reload the Prescaler
  221. and the repetition counter value (if applicable) immediately */
  222. LL_TIM_GenerateEvent_UPDATE(TIMx);
  223. return SUCCESS;
  224. }
  225. /**
  226. * @brief Set the fields of the TIMx output channel configuration data
  227. * structure to their default values.
  228. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure (the output channel configuration data structure)
  229. * @retval None
  230. */
  231. void LL_TIM_OC_StructInit(LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  232. {
  233. /* Set the default configuration */
  234. TIM_OC_InitStruct->OCMode = LL_TIM_OCMODE_FROZEN;
  235. TIM_OC_InitStruct->OCState = LL_TIM_OCSTATE_DISABLE;
  236. TIM_OC_InitStruct->CompareValue = 0x00000000U;
  237. TIM_OC_InitStruct->OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  238. }
  239. /**
  240. * @brief Configure the TIMx output channel.
  241. * @param TIMx Timer Instance
  242. * @param Channel This parameter can be one of the following values:
  243. * @arg @ref LL_TIM_CHANNEL_CH1
  244. * @arg @ref LL_TIM_CHANNEL_CH2
  245. * @arg @ref LL_TIM_CHANNEL_CH3
  246. * @arg @ref LL_TIM_CHANNEL_CH4
  247. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure (TIMx output channel configuration data structure)
  248. * @retval An ErrorStatus enumeration value:
  249. * - SUCCESS: TIMx output channel is initialized
  250. * - ERROR: TIMx output channel is not initialized
  251. */
  252. ErrorStatus LL_TIM_OC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  253. {
  254. ErrorStatus result = ERROR;
  255. switch (Channel)
  256. {
  257. case LL_TIM_CHANNEL_CH1:
  258. result = OC1Config(TIMx, TIM_OC_InitStruct);
  259. break;
  260. case LL_TIM_CHANNEL_CH2:
  261. result = OC2Config(TIMx, TIM_OC_InitStruct);
  262. break;
  263. case LL_TIM_CHANNEL_CH3:
  264. result = OC3Config(TIMx, TIM_OC_InitStruct);
  265. break;
  266. case LL_TIM_CHANNEL_CH4:
  267. result = OC4Config(TIMx, TIM_OC_InitStruct);
  268. break;
  269. default:
  270. break;
  271. }
  272. return result;
  273. }
  274. /**
  275. * @brief Set the fields of the TIMx input channel configuration data
  276. * structure to their default values.
  277. * @param TIM_ICInitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (the input channel configuration data structure)
  278. * @retval None
  279. */
  280. void LL_TIM_IC_StructInit(LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  281. {
  282. /* Set the default configuration */
  283. TIM_ICInitStruct->ICPolarity = LL_TIM_IC_POLARITY_RISING;
  284. TIM_ICInitStruct->ICActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  285. TIM_ICInitStruct->ICPrescaler = LL_TIM_ICPSC_DIV1;
  286. TIM_ICInitStruct->ICFilter = LL_TIM_IC_FILTER_FDIV1;
  287. }
  288. /**
  289. * @brief Configure the TIMx input channel.
  290. * @param TIMx Timer Instance
  291. * @param Channel This parameter can be one of the following values:
  292. * @arg @ref LL_TIM_CHANNEL_CH1
  293. * @arg @ref LL_TIM_CHANNEL_CH2
  294. * @arg @ref LL_TIM_CHANNEL_CH3
  295. * @arg @ref LL_TIM_CHANNEL_CH4
  296. * @param TIM_IC_InitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (TIMx input channel configuration data structure)
  297. * @retval An ErrorStatus enumeration value:
  298. * - SUCCESS: TIMx output channel is initialized
  299. * - ERROR: TIMx output channel is not initialized
  300. */
  301. ErrorStatus LL_TIM_IC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_IC_InitTypeDef *TIM_IC_InitStruct)
  302. {
  303. ErrorStatus result = ERROR;
  304. switch (Channel)
  305. {
  306. case LL_TIM_CHANNEL_CH1:
  307. result = IC1Config(TIMx, TIM_IC_InitStruct);
  308. break;
  309. case LL_TIM_CHANNEL_CH2:
  310. result = IC2Config(TIMx, TIM_IC_InitStruct);
  311. break;
  312. case LL_TIM_CHANNEL_CH3:
  313. result = IC3Config(TIMx, TIM_IC_InitStruct);
  314. break;
  315. case LL_TIM_CHANNEL_CH4:
  316. result = IC4Config(TIMx, TIM_IC_InitStruct);
  317. break;
  318. default:
  319. break;
  320. }
  321. return result;
  322. }
  323. /**
  324. * @brief Fills each TIM_EncoderInitStruct field with its default value
  325. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (encoder interface configuration data structure)
  326. * @retval None
  327. */
  328. void LL_TIM_ENCODER_StructInit(LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  329. {
  330. /* Set the default configuration */
  331. TIM_EncoderInitStruct->EncoderMode = LL_TIM_ENCODERMODE_X2_TI1;
  332. TIM_EncoderInitStruct->IC1Polarity = LL_TIM_IC_POLARITY_RISING;
  333. TIM_EncoderInitStruct->IC1ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  334. TIM_EncoderInitStruct->IC1Prescaler = LL_TIM_ICPSC_DIV1;
  335. TIM_EncoderInitStruct->IC1Filter = LL_TIM_IC_FILTER_FDIV1;
  336. TIM_EncoderInitStruct->IC2Polarity = LL_TIM_IC_POLARITY_RISING;
  337. TIM_EncoderInitStruct->IC2ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  338. TIM_EncoderInitStruct->IC2Prescaler = LL_TIM_ICPSC_DIV1;
  339. TIM_EncoderInitStruct->IC2Filter = LL_TIM_IC_FILTER_FDIV1;
  340. }
  341. /**
  342. * @brief Configure the encoder interface of the timer instance.
  343. * @param TIMx Timer Instance
  344. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (TIMx encoder interface configuration data structure)
  345. * @retval An ErrorStatus enumeration value:
  346. * - SUCCESS: TIMx registers are de-initialized
  347. * - ERROR: not applicable
  348. */
  349. ErrorStatus LL_TIM_ENCODER_Init(TIM_TypeDef *TIMx, LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  350. {
  351. uint32_t tmpccmr1;
  352. uint32_t tmpccer;
  353. /* Check the parameters */
  354. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(TIMx));
  355. assert_param(IS_LL_TIM_ENCODERMODE(TIM_EncoderInitStruct->EncoderMode));
  356. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC1Polarity));
  357. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC1ActiveInput));
  358. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC1Prescaler));
  359. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC1Filter));
  360. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC2Polarity));
  361. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC2ActiveInput));
  362. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC2Prescaler));
  363. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC2Filter));
  364. /* Disable the CC1 and CC2: Reset the CC1E and CC2E Bits */
  365. TIMx->CCER &= (uint32_t)~(TIM_CCER_CC1E | TIM_CCER_CC2E);
  366. /* Get the TIMx CCMR1 register value */
  367. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  368. /* Get the TIMx CCER register value */
  369. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  370. /* Configure TI1 */
  371. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC);
  372. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1ActiveInput >> 16U);
  373. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Filter >> 16U);
  374. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Prescaler >> 16U);
  375. /* Configure TI2 */
  376. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC);
  377. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2ActiveInput >> 8U);
  378. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Filter >> 8U);
  379. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Prescaler >> 8U);
  380. /* Set TI1 and TI2 polarity and enable TI1 and TI2 */
  381. tmpccer &= (uint32_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC2P | TIM_CCER_CC2NP);
  382. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC1Polarity);
  383. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC2Polarity << 4U);
  384. tmpccer |= (uint32_t)(TIM_CCER_CC1E | TIM_CCER_CC2E);
  385. /* Set encoder mode */
  386. LL_TIM_SetEncoderMode(TIMx, TIM_EncoderInitStruct->EncoderMode);
  387. /* Write to TIMx CCMR1 */
  388. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  389. /* Write to TIMx CCER */
  390. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  391. return SUCCESS;
  392. }
  393. /**
  394. * @}
  395. */
  396. /**
  397. * @}
  398. */
  399. /** @addtogroup TIM_LL_Private_Functions TIM Private Functions
  400. * @brief Private functions
  401. * @{
  402. */
  403. /**
  404. * @brief Configure the TIMx output channel 1.
  405. * @param TIMx Timer Instance
  406. * @param TIM_OCInitStruct pointer to the the TIMx output channel 1 configuration data structure
  407. * @retval An ErrorStatus enumeration value:
  408. * - SUCCESS: TIMx registers are de-initialized
  409. * - ERROR: not applicable
  410. */
  411. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  412. {
  413. uint32_t tmpccmr1;
  414. uint32_t tmpccer;
  415. uint32_t tmpcr2;
  416. /* Check the parameters */
  417. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  418. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  419. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  420. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  421. /* Disable the Channel 1: Reset the CC1E Bit */
  422. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC1E);
  423. /* Get the TIMx CCER register value */
  424. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  425. /* Get the TIMx CR2 register value */
  426. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  427. /* Get the TIMx CCMR1 register value */
  428. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  429. /* Reset Capture/Compare selection Bits */
  430. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC1S);
  431. /* Set the Output Compare Mode */
  432. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC1M, TIM_OCInitStruct->OCMode);
  433. /* Set the Output Compare Polarity */
  434. MODIFY_REG(tmpccer, TIM_CCER_CC1P, TIM_OCInitStruct->OCPolarity);
  435. /* Set the Output State */
  436. MODIFY_REG(tmpccer, TIM_CCER_CC1E, TIM_OCInitStruct->OCState);
  437. /* Write to TIMx CR2 */
  438. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  439. /* Write to TIMx CCMR1 */
  440. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  441. /* Set the Capture Compare Register value */
  442. LL_TIM_OC_SetCompareCH1(TIMx, TIM_OCInitStruct->CompareValue);
  443. /* Write to TIMx CCER */
  444. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  445. return SUCCESS;
  446. }
  447. /**
  448. * @brief Configure the TIMx output channel 2.
  449. * @param TIMx Timer Instance
  450. * @param TIM_OCInitStruct pointer to the the TIMx output channel 2 configuration data structure
  451. * @retval An ErrorStatus enumeration value:
  452. * - SUCCESS: TIMx registers are de-initialized
  453. * - ERROR: not applicable
  454. */
  455. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  456. {
  457. uint32_t tmpccmr1;
  458. uint32_t tmpccer;
  459. uint32_t tmpcr2;
  460. /* Check the parameters */
  461. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  462. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  463. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  464. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  465. /* Disable the Channel 2: Reset the CC2E Bit */
  466. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC2E);
  467. /* Get the TIMx CCER register value */
  468. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  469. /* Get the TIMx CR2 register value */
  470. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  471. /* Get the TIMx CCMR1 register value */
  472. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  473. /* Reset Capture/Compare selection Bits */
  474. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC2S);
  475. /* Select the Output Compare Mode */
  476. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC2M, TIM_OCInitStruct->OCMode << 8U);
  477. /* Set the Output Compare Polarity */
  478. MODIFY_REG(tmpccer, TIM_CCER_CC2P, TIM_OCInitStruct->OCPolarity << 4U);
  479. /* Set the Output State */
  480. MODIFY_REG(tmpccer, TIM_CCER_CC2E, TIM_OCInitStruct->OCState << 4U);
  481. /* Write to TIMx CR2 */
  482. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  483. /* Write to TIMx CCMR1 */
  484. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  485. /* Set the Capture Compare Register value */
  486. LL_TIM_OC_SetCompareCH2(TIMx, TIM_OCInitStruct->CompareValue);
  487. /* Write to TIMx CCER */
  488. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  489. return SUCCESS;
  490. }
  491. /**
  492. * @brief Configure the TIMx output channel 3.
  493. * @param TIMx Timer Instance
  494. * @param TIM_OCInitStruct pointer to the the TIMx output channel 3 configuration data structure
  495. * @retval An ErrorStatus enumeration value:
  496. * - SUCCESS: TIMx registers are de-initialized
  497. * - ERROR: not applicable
  498. */
  499. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  500. {
  501. uint32_t tmpccmr2;
  502. uint32_t tmpccer;
  503. uint32_t tmpcr2;
  504. /* Check the parameters */
  505. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  506. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  507. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  508. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  509. /* Disable the Channel 3: Reset the CC3E Bit */
  510. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC3E);
  511. /* Get the TIMx CCER register value */
  512. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  513. /* Get the TIMx CR2 register value */
  514. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  515. /* Get the TIMx CCMR2 register value */
  516. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  517. /* Reset Capture/Compare selection Bits */
  518. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC3S);
  519. /* Select the Output Compare Mode */
  520. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC3M, TIM_OCInitStruct->OCMode);
  521. /* Set the Output Compare Polarity */
  522. MODIFY_REG(tmpccer, TIM_CCER_CC3P, TIM_OCInitStruct->OCPolarity << 8U);
  523. /* Set the Output State */
  524. MODIFY_REG(tmpccer, TIM_CCER_CC3E, TIM_OCInitStruct->OCState << 8U);
  525. /* Write to TIMx CR2 */
  526. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  527. /* Write to TIMx CCMR2 */
  528. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  529. /* Set the Capture Compare Register value */
  530. LL_TIM_OC_SetCompareCH3(TIMx, TIM_OCInitStruct->CompareValue);
  531. /* Write to TIMx CCER */
  532. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  533. return SUCCESS;
  534. }
  535. /**
  536. * @brief Configure the TIMx output channel 4.
  537. * @param TIMx Timer Instance
  538. * @param TIM_OCInitStruct pointer to the the TIMx output channel 4 configuration data structure
  539. * @retval An ErrorStatus enumeration value:
  540. * - SUCCESS: TIMx registers are de-initialized
  541. * - ERROR: not applicable
  542. */
  543. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  544. {
  545. uint32_t tmpccmr2;
  546. uint32_t tmpccer;
  547. uint32_t tmpcr2;
  548. /* Check the parameters */
  549. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  550. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  551. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  552. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  553. /* Disable the Channel 4: Reset the CC4E Bit */
  554. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC4E);
  555. /* Get the TIMx CCER register value */
  556. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  557. /* Get the TIMx CR2 register value */
  558. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  559. /* Get the TIMx CCMR2 register value */
  560. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  561. /* Reset Capture/Compare selection Bits */
  562. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC4S);
  563. /* Select the Output Compare Mode */
  564. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC4M, TIM_OCInitStruct->OCMode << 8U);
  565. /* Set the Output Compare Polarity */
  566. MODIFY_REG(tmpccer, TIM_CCER_CC4P, TIM_OCInitStruct->OCPolarity << 12U);
  567. /* Set the Output State */
  568. MODIFY_REG(tmpccer, TIM_CCER_CC4E, TIM_OCInitStruct->OCState << 12U);
  569. /* Write to TIMx CR2 */
  570. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  571. /* Write to TIMx CCMR2 */
  572. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  573. /* Set the Capture Compare Register value */
  574. LL_TIM_OC_SetCompareCH4(TIMx, TIM_OCInitStruct->CompareValue);
  575. /* Write to TIMx CCER */
  576. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  577. return SUCCESS;
  578. }
  579. /**
  580. * @brief Configure the TIMx input channel 1.
  581. * @param TIMx Timer Instance
  582. * @param TIM_ICInitStruct pointer to the the TIMx input channel 1 configuration data structure
  583. * @retval An ErrorStatus enumeration value:
  584. * - SUCCESS: TIMx registers are de-initialized
  585. * - ERROR: not applicable
  586. */
  587. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  588. {
  589. /* Check the parameters */
  590. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  591. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  592. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  593. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  594. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  595. /* Disable the Channel 1: Reset the CC1E Bit */
  596. TIMx->CCER &= (uint32_t)~TIM_CCER_CC1E;
  597. /* Select the Input and set the filter and the prescaler value */
  598. MODIFY_REG(TIMx->CCMR1,
  599. (TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC),
  600. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  601. /* Select the Polarity and set the CC1E Bit */
  602. MODIFY_REG(TIMx->CCER,
  603. (TIM_CCER_CC1P | TIM_CCER_CC1NP),
  604. (TIM_ICInitStruct->ICPolarity | TIM_CCER_CC1E));
  605. return SUCCESS;
  606. }
  607. /**
  608. * @brief Configure the TIMx input channel 2.
  609. * @param TIMx Timer Instance
  610. * @param TIM_ICInitStruct pointer to the the TIMx input channel 2 configuration data structure
  611. * @retval An ErrorStatus enumeration value:
  612. * - SUCCESS: TIMx registers are de-initialized
  613. * - ERROR: not applicable
  614. */
  615. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  616. {
  617. /* Check the parameters */
  618. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  619. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  620. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  621. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  622. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  623. /* Disable the Channel 2: Reset the CC2E Bit */
  624. TIMx->CCER &= (uint32_t)~TIM_CCER_CC2E;
  625. /* Select the Input and set the filter and the prescaler value */
  626. MODIFY_REG(TIMx->CCMR1,
  627. (TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC),
  628. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  629. /* Select the Polarity and set the CC2E Bit */
  630. MODIFY_REG(TIMx->CCER,
  631. (TIM_CCER_CC2P | TIM_CCER_CC2NP),
  632. ((TIM_ICInitStruct->ICPolarity << 4U) | TIM_CCER_CC2E));
  633. return SUCCESS;
  634. }
  635. /**
  636. * @brief Configure the TIMx input channel 3.
  637. * @param TIMx Timer Instance
  638. * @param TIM_ICInitStruct pointer to the the TIMx input channel 3 configuration data structure
  639. * @retval An ErrorStatus enumeration value:
  640. * - SUCCESS: TIMx registers are de-initialized
  641. * - ERROR: not applicable
  642. */
  643. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  644. {
  645. /* Check the parameters */
  646. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  647. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  648. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  649. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  650. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  651. /* Disable the Channel 3: Reset the CC3E Bit */
  652. TIMx->CCER &= (uint32_t)~TIM_CCER_CC3E;
  653. /* Select the Input and set the filter and the prescaler value */
  654. MODIFY_REG(TIMx->CCMR2,
  655. (TIM_CCMR2_CC3S | TIM_CCMR2_IC3F | TIM_CCMR2_IC3PSC),
  656. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  657. /* Select the Polarity and set the CC3E Bit */
  658. MODIFY_REG(TIMx->CCER,
  659. (TIM_CCER_CC3P | TIM_CCER_CC3NP),
  660. ((TIM_ICInitStruct->ICPolarity << 8U) | TIM_CCER_CC3E));
  661. return SUCCESS;
  662. }
  663. /**
  664. * @brief Configure the TIMx input channel 4.
  665. * @param TIMx Timer Instance
  666. * @param TIM_ICInitStruct pointer to the the TIMx input channel 4 configuration data structure
  667. * @retval An ErrorStatus enumeration value:
  668. * - SUCCESS: TIMx registers are de-initialized
  669. * - ERROR: not applicable
  670. */
  671. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  672. {
  673. /* Check the parameters */
  674. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  675. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  676. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  677. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  678. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  679. /* Disable the Channel 4: Reset the CC4E Bit */
  680. TIMx->CCER &= (uint32_t)~TIM_CCER_CC4E;
  681. /* Select the Input and set the filter and the prescaler value */
  682. MODIFY_REG(TIMx->CCMR2,
  683. (TIM_CCMR2_CC4S | TIM_CCMR2_IC4F | TIM_CCMR2_IC4PSC),
  684. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  685. /* Select the Polarity and set the CC2E Bit */
  686. MODIFY_REG(TIMx->CCER,
  687. (TIM_CCER_CC4P | TIM_CCER_CC4NP),
  688. ((TIM_ICInitStruct->ICPolarity << 12U) | TIM_CCER_CC4E));
  689. return SUCCESS;
  690. }
  691. /**
  692. * @}
  693. */
  694. /**
  695. * @}
  696. */
  697. #endif /* TIM1 || TIM3 || TIM21 || TIM22 || TIM6 || TIM7 */
  698. /**
  699. * @}
  700. */
  701. #endif /* USE_FULL_LL_DRIVER */
  702. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/