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.
 
 
 
 
 

1192 lines
44 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 "stm32f4xx_ll_tim.h"
  22. #include "stm32f4xx_ll_bus.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 (TIM1) || defined (TIM2) || defined (TIM3) || defined (TIM4) || defined (TIM5) || defined (TIM6) || defined (TIM7) || defined (TIM8) || defined (TIM9) || defined (TIM10) || defined (TIM11) || defined (TIM12) || defined (TIM13) || defined (TIM14)
  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_OCIDLESTATE(__VALUE__) (((__VALUE__) == LL_TIM_OCIDLESTATE_LOW) \
  63. || ((__VALUE__) == LL_TIM_OCIDLESTATE_HIGH))
  64. #define IS_LL_TIM_ACTIVEINPUT(__VALUE__) (((__VALUE__) == LL_TIM_ACTIVEINPUT_DIRECTTI) \
  65. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_INDIRECTTI) \
  66. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_TRC))
  67. #define IS_LL_TIM_ICPSC(__VALUE__) (((__VALUE__) == LL_TIM_ICPSC_DIV1) \
  68. || ((__VALUE__) == LL_TIM_ICPSC_DIV2) \
  69. || ((__VALUE__) == LL_TIM_ICPSC_DIV4) \
  70. || ((__VALUE__) == LL_TIM_ICPSC_DIV8))
  71. #define IS_LL_TIM_IC_FILTER(__VALUE__) (((__VALUE__) == LL_TIM_IC_FILTER_FDIV1) \
  72. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N2) \
  73. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N4) \
  74. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N8) \
  75. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N6) \
  76. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N8) \
  77. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N6) \
  78. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N8) \
  79. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N6) \
  80. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N8) \
  81. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N5) \
  82. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N6) \
  83. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N8) \
  84. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N5) \
  85. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N6) \
  86. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N8))
  87. #define IS_LL_TIM_IC_POLARITY(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  88. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING) \
  89. || ((__VALUE__) == LL_TIM_IC_POLARITY_BOTHEDGE))
  90. #define IS_LL_TIM_ENCODERMODE(__VALUE__) (((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI1) \
  91. || ((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI2) \
  92. || ((__VALUE__) == LL_TIM_ENCODERMODE_X4_TI12))
  93. #define IS_LL_TIM_IC_POLARITY_ENCODER(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  94. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING))
  95. #define IS_LL_TIM_OSSR_STATE(__VALUE__) (((__VALUE__) == LL_TIM_OSSR_DISABLE) \
  96. || ((__VALUE__) == LL_TIM_OSSR_ENABLE))
  97. #define IS_LL_TIM_OSSI_STATE(__VALUE__) (((__VALUE__) == LL_TIM_OSSI_DISABLE) \
  98. || ((__VALUE__) == LL_TIM_OSSI_ENABLE))
  99. #define IS_LL_TIM_LOCK_LEVEL(__VALUE__) (((__VALUE__) == LL_TIM_LOCKLEVEL_OFF) \
  100. || ((__VALUE__) == LL_TIM_LOCKLEVEL_1) \
  101. || ((__VALUE__) == LL_TIM_LOCKLEVEL_2) \
  102. || ((__VALUE__) == LL_TIM_LOCKLEVEL_3))
  103. #define IS_LL_TIM_BREAK_STATE(__VALUE__) (((__VALUE__) == LL_TIM_BREAK_DISABLE) \
  104. || ((__VALUE__) == LL_TIM_BREAK_ENABLE))
  105. #define IS_LL_TIM_BREAK_POLARITY(__VALUE__) (((__VALUE__) == LL_TIM_BREAK_POLARITY_LOW) \
  106. || ((__VALUE__) == LL_TIM_BREAK_POLARITY_HIGH))
  107. #define IS_LL_TIM_AUTOMATIC_OUTPUT_STATE(__VALUE__) (((__VALUE__) == LL_TIM_AUTOMATICOUTPUT_DISABLE) \
  108. || ((__VALUE__) == LL_TIM_AUTOMATICOUTPUT_ENABLE))
  109. /**
  110. * @}
  111. */
  112. /* Private function prototypes -----------------------------------------------*/
  113. /** @defgroup TIM_LL_Private_Functions TIM Private Functions
  114. * @{
  115. */
  116. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  117. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  118. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  119. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  120. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  121. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  122. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  123. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  124. /**
  125. * @}
  126. */
  127. /* Exported functions --------------------------------------------------------*/
  128. /** @addtogroup TIM_LL_Exported_Functions
  129. * @{
  130. */
  131. /** @addtogroup TIM_LL_EF_Init
  132. * @{
  133. */
  134. /**
  135. * @brief Set TIMx registers to their reset values.
  136. * @param TIMx Timer instance
  137. * @retval An ErrorStatus enumeration value:
  138. * - SUCCESS: TIMx registers are de-initialized
  139. * - ERROR: invalid TIMx instance
  140. */
  141. ErrorStatus LL_TIM_DeInit(TIM_TypeDef *TIMx)
  142. {
  143. ErrorStatus result = SUCCESS;
  144. /* Check the parameters */
  145. assert_param(IS_TIM_INSTANCE(TIMx));
  146. if (TIMx == TIM1)
  147. {
  148. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM1);
  149. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM1);
  150. }
  151. #if defined(TIM2)
  152. else if (TIMx == TIM2)
  153. {
  154. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM2);
  155. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM2);
  156. }
  157. #endif /* TIM2 */
  158. #if defined(TIM3)
  159. else if (TIMx == TIM3)
  160. {
  161. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM3);
  162. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM3);
  163. }
  164. #endif /* TIM3 */
  165. #if defined(TIM4)
  166. else if (TIMx == TIM4)
  167. {
  168. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM4);
  169. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM4);
  170. }
  171. #endif /* TIM4 */
  172. #if defined(TIM5)
  173. else if (TIMx == TIM5)
  174. {
  175. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM5);
  176. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM5);
  177. }
  178. #endif /* TIM5 */
  179. #if defined(TIM6)
  180. else if (TIMx == TIM6)
  181. {
  182. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM6);
  183. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM6);
  184. }
  185. #endif /* TIM6 */
  186. #if defined (TIM7)
  187. else if (TIMx == TIM7)
  188. {
  189. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM7);
  190. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM7);
  191. }
  192. #endif /* TIM7 */
  193. #if defined(TIM8)
  194. else if (TIMx == TIM8)
  195. {
  196. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM8);
  197. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM8);
  198. }
  199. #endif /* TIM8 */
  200. #if defined(TIM9)
  201. else if (TIMx == TIM9)
  202. {
  203. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM9);
  204. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM9);
  205. }
  206. #endif /* TIM9 */
  207. #if defined(TIM10)
  208. else if (TIMx == TIM10)
  209. {
  210. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM10);
  211. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM10);
  212. }
  213. #endif /* TIM10 */
  214. #if defined(TIM11)
  215. else if (TIMx == TIM11)
  216. {
  217. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_TIM11);
  218. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_TIM11);
  219. }
  220. #endif /* TIM11 */
  221. #if defined(TIM12)
  222. else if (TIMx == TIM12)
  223. {
  224. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM12);
  225. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM12);
  226. }
  227. #endif /* TIM12 */
  228. #if defined(TIM13)
  229. else if (TIMx == TIM13)
  230. {
  231. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM13);
  232. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM13);
  233. }
  234. #endif /* TIM13 */
  235. #if defined(TIM14)
  236. else if (TIMx == TIM14)
  237. {
  238. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM14);
  239. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM14);
  240. }
  241. #endif /* TIM14 */
  242. else
  243. {
  244. result = ERROR;
  245. }
  246. return result;
  247. }
  248. /**
  249. * @brief Set the fields of the time base unit configuration data structure
  250. * to their default values.
  251. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure (time base unit configuration data structure)
  252. * @retval None
  253. */
  254. void LL_TIM_StructInit(LL_TIM_InitTypeDef *TIM_InitStruct)
  255. {
  256. /* Set the default configuration */
  257. TIM_InitStruct->Prescaler = (uint16_t)0x0000;
  258. TIM_InitStruct->CounterMode = LL_TIM_COUNTERMODE_UP;
  259. TIM_InitStruct->Autoreload = 0xFFFFFFFFU;
  260. TIM_InitStruct->ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  261. TIM_InitStruct->RepetitionCounter = 0x00000000U;
  262. }
  263. /**
  264. * @brief Configure the TIMx time base unit.
  265. * @param TIMx Timer Instance
  266. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure
  267. * (TIMx time base unit configuration data structure)
  268. * @retval An ErrorStatus enumeration value:
  269. * - SUCCESS: TIMx registers are de-initialized
  270. * - ERROR: not applicable
  271. */
  272. ErrorStatus LL_TIM_Init(TIM_TypeDef *TIMx, LL_TIM_InitTypeDef *TIM_InitStruct)
  273. {
  274. uint32_t tmpcr1;
  275. /* Check the parameters */
  276. assert_param(IS_TIM_INSTANCE(TIMx));
  277. assert_param(IS_LL_TIM_COUNTERMODE(TIM_InitStruct->CounterMode));
  278. assert_param(IS_LL_TIM_CLOCKDIVISION(TIM_InitStruct->ClockDivision));
  279. tmpcr1 = LL_TIM_ReadReg(TIMx, CR1);
  280. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  281. {
  282. /* Select the Counter Mode */
  283. MODIFY_REG(tmpcr1, (TIM_CR1_DIR | TIM_CR1_CMS), TIM_InitStruct->CounterMode);
  284. }
  285. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  286. {
  287. /* Set the clock division */
  288. MODIFY_REG(tmpcr1, TIM_CR1_CKD, TIM_InitStruct->ClockDivision);
  289. }
  290. /* Write to TIMx CR1 */
  291. LL_TIM_WriteReg(TIMx, CR1, tmpcr1);
  292. /* Set the Autoreload value */
  293. LL_TIM_SetAutoReload(TIMx, TIM_InitStruct->Autoreload);
  294. /* Set the Prescaler value */
  295. LL_TIM_SetPrescaler(TIMx, TIM_InitStruct->Prescaler);
  296. if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
  297. {
  298. /* Set the Repetition Counter value */
  299. LL_TIM_SetRepetitionCounter(TIMx, TIM_InitStruct->RepetitionCounter);
  300. }
  301. /* Generate an update event to reload the Prescaler
  302. and the repetition counter value (if applicable) immediately */
  303. LL_TIM_GenerateEvent_UPDATE(TIMx);
  304. return SUCCESS;
  305. }
  306. /**
  307. * @brief Set the fields of the TIMx output channel configuration data
  308. * structure to their default values.
  309. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure
  310. * (the output channel configuration data structure)
  311. * @retval None
  312. */
  313. void LL_TIM_OC_StructInit(LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  314. {
  315. /* Set the default configuration */
  316. TIM_OC_InitStruct->OCMode = LL_TIM_OCMODE_FROZEN;
  317. TIM_OC_InitStruct->OCState = LL_TIM_OCSTATE_DISABLE;
  318. TIM_OC_InitStruct->OCNState = LL_TIM_OCSTATE_DISABLE;
  319. TIM_OC_InitStruct->CompareValue = 0x00000000U;
  320. TIM_OC_InitStruct->OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  321. TIM_OC_InitStruct->OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  322. TIM_OC_InitStruct->OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  323. TIM_OC_InitStruct->OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  324. }
  325. /**
  326. * @brief Configure the TIMx output channel.
  327. * @param TIMx Timer Instance
  328. * @param Channel This parameter can be one of the following values:
  329. * @arg @ref LL_TIM_CHANNEL_CH1
  330. * @arg @ref LL_TIM_CHANNEL_CH2
  331. * @arg @ref LL_TIM_CHANNEL_CH3
  332. * @arg @ref LL_TIM_CHANNEL_CH4
  333. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure (TIMx output channel configuration
  334. * data structure)
  335. * @retval An ErrorStatus enumeration value:
  336. * - SUCCESS: TIMx output channel is initialized
  337. * - ERROR: TIMx output channel is not initialized
  338. */
  339. ErrorStatus LL_TIM_OC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  340. {
  341. ErrorStatus result = ERROR;
  342. switch (Channel)
  343. {
  344. case LL_TIM_CHANNEL_CH1:
  345. result = OC1Config(TIMx, TIM_OC_InitStruct);
  346. break;
  347. case LL_TIM_CHANNEL_CH2:
  348. result = OC2Config(TIMx, TIM_OC_InitStruct);
  349. break;
  350. case LL_TIM_CHANNEL_CH3:
  351. result = OC3Config(TIMx, TIM_OC_InitStruct);
  352. break;
  353. case LL_TIM_CHANNEL_CH4:
  354. result = OC4Config(TIMx, TIM_OC_InitStruct);
  355. break;
  356. default:
  357. break;
  358. }
  359. return result;
  360. }
  361. /**
  362. * @brief Set the fields of the TIMx input channel configuration data
  363. * structure to their default values.
  364. * @param TIM_ICInitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (the input channel configuration
  365. * data structure)
  366. * @retval None
  367. */
  368. void LL_TIM_IC_StructInit(LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  369. {
  370. /* Set the default configuration */
  371. TIM_ICInitStruct->ICPolarity = LL_TIM_IC_POLARITY_RISING;
  372. TIM_ICInitStruct->ICActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  373. TIM_ICInitStruct->ICPrescaler = LL_TIM_ICPSC_DIV1;
  374. TIM_ICInitStruct->ICFilter = LL_TIM_IC_FILTER_FDIV1;
  375. }
  376. /**
  377. * @brief Configure the TIMx input channel.
  378. * @param TIMx Timer Instance
  379. * @param Channel This parameter can be one of the following values:
  380. * @arg @ref LL_TIM_CHANNEL_CH1
  381. * @arg @ref LL_TIM_CHANNEL_CH2
  382. * @arg @ref LL_TIM_CHANNEL_CH3
  383. * @arg @ref LL_TIM_CHANNEL_CH4
  384. * @param TIM_IC_InitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (TIMx input channel configuration data
  385. * structure)
  386. * @retval An ErrorStatus enumeration value:
  387. * - SUCCESS: TIMx output channel is initialized
  388. * - ERROR: TIMx output channel is not initialized
  389. */
  390. ErrorStatus LL_TIM_IC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_IC_InitTypeDef *TIM_IC_InitStruct)
  391. {
  392. ErrorStatus result = ERROR;
  393. switch (Channel)
  394. {
  395. case LL_TIM_CHANNEL_CH1:
  396. result = IC1Config(TIMx, TIM_IC_InitStruct);
  397. break;
  398. case LL_TIM_CHANNEL_CH2:
  399. result = IC2Config(TIMx, TIM_IC_InitStruct);
  400. break;
  401. case LL_TIM_CHANNEL_CH3:
  402. result = IC3Config(TIMx, TIM_IC_InitStruct);
  403. break;
  404. case LL_TIM_CHANNEL_CH4:
  405. result = IC4Config(TIMx, TIM_IC_InitStruct);
  406. break;
  407. default:
  408. break;
  409. }
  410. return result;
  411. }
  412. /**
  413. * @brief Fills each TIM_EncoderInitStruct field with its default value
  414. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (encoder interface
  415. * configuration data structure)
  416. * @retval None
  417. */
  418. void LL_TIM_ENCODER_StructInit(LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  419. {
  420. /* Set the default configuration */
  421. TIM_EncoderInitStruct->EncoderMode = LL_TIM_ENCODERMODE_X2_TI1;
  422. TIM_EncoderInitStruct->IC1Polarity = LL_TIM_IC_POLARITY_RISING;
  423. TIM_EncoderInitStruct->IC1ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  424. TIM_EncoderInitStruct->IC1Prescaler = LL_TIM_ICPSC_DIV1;
  425. TIM_EncoderInitStruct->IC1Filter = LL_TIM_IC_FILTER_FDIV1;
  426. TIM_EncoderInitStruct->IC2Polarity = LL_TIM_IC_POLARITY_RISING;
  427. TIM_EncoderInitStruct->IC2ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  428. TIM_EncoderInitStruct->IC2Prescaler = LL_TIM_ICPSC_DIV1;
  429. TIM_EncoderInitStruct->IC2Filter = LL_TIM_IC_FILTER_FDIV1;
  430. }
  431. /**
  432. * @brief Configure the encoder interface of the timer instance.
  433. * @param TIMx Timer Instance
  434. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (TIMx encoder interface
  435. * configuration data structure)
  436. * @retval An ErrorStatus enumeration value:
  437. * - SUCCESS: TIMx registers are de-initialized
  438. * - ERROR: not applicable
  439. */
  440. ErrorStatus LL_TIM_ENCODER_Init(TIM_TypeDef *TIMx, LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  441. {
  442. uint32_t tmpccmr1;
  443. uint32_t tmpccer;
  444. /* Check the parameters */
  445. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(TIMx));
  446. assert_param(IS_LL_TIM_ENCODERMODE(TIM_EncoderInitStruct->EncoderMode));
  447. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC1Polarity));
  448. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC1ActiveInput));
  449. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC1Prescaler));
  450. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC1Filter));
  451. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC2Polarity));
  452. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC2ActiveInput));
  453. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC2Prescaler));
  454. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC2Filter));
  455. /* Disable the CC1 and CC2: Reset the CC1E and CC2E Bits */
  456. TIMx->CCER &= (uint32_t)~(TIM_CCER_CC1E | TIM_CCER_CC2E);
  457. /* Get the TIMx CCMR1 register value */
  458. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  459. /* Get the TIMx CCER register value */
  460. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  461. /* Configure TI1 */
  462. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC);
  463. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1ActiveInput >> 16U);
  464. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Filter >> 16U);
  465. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Prescaler >> 16U);
  466. /* Configure TI2 */
  467. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC);
  468. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2ActiveInput >> 8U);
  469. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Filter >> 8U);
  470. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Prescaler >> 8U);
  471. /* Set TI1 and TI2 polarity and enable TI1 and TI2 */
  472. tmpccer &= (uint32_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC2P | TIM_CCER_CC2NP);
  473. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC1Polarity);
  474. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC2Polarity << 4U);
  475. tmpccer |= (uint32_t)(TIM_CCER_CC1E | TIM_CCER_CC2E);
  476. /* Set encoder mode */
  477. LL_TIM_SetEncoderMode(TIMx, TIM_EncoderInitStruct->EncoderMode);
  478. /* Write to TIMx CCMR1 */
  479. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  480. /* Write to TIMx CCER */
  481. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  482. return SUCCESS;
  483. }
  484. /**
  485. * @brief Set the fields of the TIMx Hall sensor interface configuration data
  486. * structure to their default values.
  487. * @param TIM_HallSensorInitStruct pointer to a @ref LL_TIM_HALLSENSOR_InitTypeDef structure (HALL sensor interface
  488. * configuration data structure)
  489. * @retval None
  490. */
  491. void LL_TIM_HALLSENSOR_StructInit(LL_TIM_HALLSENSOR_InitTypeDef *TIM_HallSensorInitStruct)
  492. {
  493. /* Set the default configuration */
  494. TIM_HallSensorInitStruct->IC1Polarity = LL_TIM_IC_POLARITY_RISING;
  495. TIM_HallSensorInitStruct->IC1Prescaler = LL_TIM_ICPSC_DIV1;
  496. TIM_HallSensorInitStruct->IC1Filter = LL_TIM_IC_FILTER_FDIV1;
  497. TIM_HallSensorInitStruct->CommutationDelay = 0U;
  498. }
  499. /**
  500. * @brief Configure the Hall sensor interface of the timer instance.
  501. * @note TIMx CH1, CH2 and CH3 inputs connected through a XOR
  502. * to the TI1 input channel
  503. * @note TIMx slave mode controller is configured in reset mode.
  504. Selected internal trigger is TI1F_ED.
  505. * @note Channel 1 is configured as input, IC1 is mapped on TRC.
  506. * @note Captured value stored in TIMx_CCR1 correspond to the time elapsed
  507. * between 2 changes on the inputs. It gives information about motor speed.
  508. * @note Channel 2 is configured in output PWM 2 mode.
  509. * @note Compare value stored in TIMx_CCR2 corresponds to the commutation delay.
  510. * @note OC2REF is selected as trigger output on TRGO.
  511. * @note LL_TIM_IC_POLARITY_BOTHEDGE must not be used for TI1 when it is used
  512. * when TIMx operates in Hall sensor interface mode.
  513. * @param TIMx Timer Instance
  514. * @param TIM_HallSensorInitStruct pointer to a @ref LL_TIM_HALLSENSOR_InitTypeDef structure (TIMx HALL sensor
  515. * interface configuration data structure)
  516. * @retval An ErrorStatus enumeration value:
  517. * - SUCCESS: TIMx registers are de-initialized
  518. * - ERROR: not applicable
  519. */
  520. ErrorStatus LL_TIM_HALLSENSOR_Init(TIM_TypeDef *TIMx, LL_TIM_HALLSENSOR_InitTypeDef *TIM_HallSensorInitStruct)
  521. {
  522. uint32_t tmpcr2;
  523. uint32_t tmpccmr1;
  524. uint32_t tmpccer;
  525. uint32_t tmpsmcr;
  526. /* Check the parameters */
  527. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(TIMx));
  528. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_HallSensorInitStruct->IC1Polarity));
  529. assert_param(IS_LL_TIM_ICPSC(TIM_HallSensorInitStruct->IC1Prescaler));
  530. assert_param(IS_LL_TIM_IC_FILTER(TIM_HallSensorInitStruct->IC1Filter));
  531. /* Disable the CC1 and CC2: Reset the CC1E and CC2E Bits */
  532. TIMx->CCER &= (uint32_t)~(TIM_CCER_CC1E | TIM_CCER_CC2E);
  533. /* Get the TIMx CR2 register value */
  534. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  535. /* Get the TIMx CCMR1 register value */
  536. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  537. /* Get the TIMx CCER register value */
  538. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  539. /* Get the TIMx SMCR register value */
  540. tmpsmcr = LL_TIM_ReadReg(TIMx, SMCR);
  541. /* Connect TIMx_CH1, CH2 and CH3 pins to the TI1 input */
  542. tmpcr2 |= TIM_CR2_TI1S;
  543. /* OC2REF signal is used as trigger output (TRGO) */
  544. tmpcr2 |= LL_TIM_TRGO_OC2REF;
  545. /* Configure the slave mode controller */
  546. tmpsmcr &= (uint32_t)~(TIM_SMCR_TS | TIM_SMCR_SMS);
  547. tmpsmcr |= LL_TIM_TS_TI1F_ED;
  548. tmpsmcr |= LL_TIM_SLAVEMODE_RESET;
  549. /* Configure input channel 1 */
  550. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC);
  551. tmpccmr1 |= (uint32_t)(LL_TIM_ACTIVEINPUT_TRC >> 16U);
  552. tmpccmr1 |= (uint32_t)(TIM_HallSensorInitStruct->IC1Filter >> 16U);
  553. tmpccmr1 |= (uint32_t)(TIM_HallSensorInitStruct->IC1Prescaler >> 16U);
  554. /* Configure input channel 2 */
  555. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_OC2M | TIM_CCMR1_OC2FE | TIM_CCMR1_OC2PE | TIM_CCMR1_OC2CE);
  556. tmpccmr1 |= (uint32_t)(LL_TIM_OCMODE_PWM2 << 8U);
  557. /* Set Channel 1 polarity and enable Channel 1 and Channel2 */
  558. tmpccer &= (uint32_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC2P | TIM_CCER_CC2NP);
  559. tmpccer |= (uint32_t)(TIM_HallSensorInitStruct->IC1Polarity);
  560. tmpccer |= (uint32_t)(TIM_CCER_CC1E | TIM_CCER_CC2E);
  561. /* Write to TIMx CR2 */
  562. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  563. /* Write to TIMx SMCR */
  564. LL_TIM_WriteReg(TIMx, SMCR, tmpsmcr);
  565. /* Write to TIMx CCMR1 */
  566. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  567. /* Write to TIMx CCER */
  568. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  569. /* Write to TIMx CCR2 */
  570. LL_TIM_OC_SetCompareCH2(TIMx, TIM_HallSensorInitStruct->CommutationDelay);
  571. return SUCCESS;
  572. }
  573. /**
  574. * @brief Set the fields of the Break and Dead Time configuration data structure
  575. * to their default values.
  576. * @param TIM_BDTRInitStruct pointer to a @ref LL_TIM_BDTR_InitTypeDef structure (Break and Dead Time configuration
  577. * data structure)
  578. * @retval None
  579. */
  580. void LL_TIM_BDTR_StructInit(LL_TIM_BDTR_InitTypeDef *TIM_BDTRInitStruct)
  581. {
  582. /* Set the default configuration */
  583. TIM_BDTRInitStruct->OSSRState = LL_TIM_OSSR_DISABLE;
  584. TIM_BDTRInitStruct->OSSIState = LL_TIM_OSSI_DISABLE;
  585. TIM_BDTRInitStruct->LockLevel = LL_TIM_LOCKLEVEL_OFF;
  586. TIM_BDTRInitStruct->DeadTime = (uint8_t)0x00;
  587. TIM_BDTRInitStruct->BreakState = LL_TIM_BREAK_DISABLE;
  588. TIM_BDTRInitStruct->BreakPolarity = LL_TIM_BREAK_POLARITY_LOW;
  589. TIM_BDTRInitStruct->AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  590. }
  591. /**
  592. * @brief Configure the Break and Dead Time feature of the timer instance.
  593. * @note As the bits AOE, BKP, BKE, OSSR, OSSI and DTG[7:0] can be write-locked
  594. * depending on the LOCK configuration, it can be necessary to configure all of
  595. * them during the first write access to the TIMx_BDTR register.
  596. * @note Macro IS_TIM_BREAK_INSTANCE(TIMx) can be used to check whether or not
  597. * a timer instance provides a break input.
  598. * @param TIMx Timer Instance
  599. * @param TIM_BDTRInitStruct pointer to a @ref LL_TIM_BDTR_InitTypeDef structure (Break and Dead Time configuration
  600. * data structure)
  601. * @retval An ErrorStatus enumeration value:
  602. * - SUCCESS: Break and Dead Time is initialized
  603. * - ERROR: not applicable
  604. */
  605. ErrorStatus LL_TIM_BDTR_Init(TIM_TypeDef *TIMx, LL_TIM_BDTR_InitTypeDef *TIM_BDTRInitStruct)
  606. {
  607. uint32_t tmpbdtr = 0;
  608. /* Check the parameters */
  609. assert_param(IS_TIM_BREAK_INSTANCE(TIMx));
  610. assert_param(IS_LL_TIM_OSSR_STATE(TIM_BDTRInitStruct->OSSRState));
  611. assert_param(IS_LL_TIM_OSSI_STATE(TIM_BDTRInitStruct->OSSIState));
  612. assert_param(IS_LL_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->LockLevel));
  613. assert_param(IS_LL_TIM_BREAK_STATE(TIM_BDTRInitStruct->BreakState));
  614. assert_param(IS_LL_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->BreakPolarity));
  615. assert_param(IS_LL_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->AutomaticOutput));
  616. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  617. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  618. /* Set the BDTR bits */
  619. MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, TIM_BDTRInitStruct->DeadTime);
  620. MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, TIM_BDTRInitStruct->LockLevel);
  621. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, TIM_BDTRInitStruct->OSSIState);
  622. MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, TIM_BDTRInitStruct->OSSRState);
  623. MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, TIM_BDTRInitStruct->BreakState);
  624. MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, TIM_BDTRInitStruct->BreakPolarity);
  625. MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, TIM_BDTRInitStruct->AutomaticOutput);
  626. MODIFY_REG(tmpbdtr, TIM_BDTR_MOE, TIM_BDTRInitStruct->AutomaticOutput);
  627. /* Set TIMx_BDTR */
  628. LL_TIM_WriteReg(TIMx, BDTR, tmpbdtr);
  629. return SUCCESS;
  630. }
  631. /**
  632. * @}
  633. */
  634. /**
  635. * @}
  636. */
  637. /** @addtogroup TIM_LL_Private_Functions TIM Private Functions
  638. * @brief Private functions
  639. * @{
  640. */
  641. /**
  642. * @brief Configure the TIMx output channel 1.
  643. * @param TIMx Timer Instance
  644. * @param TIM_OCInitStruct pointer to the the TIMx output channel 1 configuration data structure
  645. * @retval An ErrorStatus enumeration value:
  646. * - SUCCESS: TIMx registers are de-initialized
  647. * - ERROR: not applicable
  648. */
  649. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  650. {
  651. uint32_t tmpccmr1;
  652. uint32_t tmpccer;
  653. uint32_t tmpcr2;
  654. /* Check the parameters */
  655. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  656. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  657. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  658. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  659. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  660. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  661. /* Disable the Channel 1: Reset the CC1E Bit */
  662. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC1E);
  663. /* Get the TIMx CCER register value */
  664. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  665. /* Get the TIMx CR2 register value */
  666. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  667. /* Get the TIMx CCMR1 register value */
  668. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  669. /* Reset Capture/Compare selection Bits */
  670. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC1S);
  671. /* Set the Output Compare Mode */
  672. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC1M, TIM_OCInitStruct->OCMode);
  673. /* Set the Output Compare Polarity */
  674. MODIFY_REG(tmpccer, TIM_CCER_CC1P, TIM_OCInitStruct->OCPolarity);
  675. /* Set the Output State */
  676. MODIFY_REG(tmpccer, TIM_CCER_CC1E, TIM_OCInitStruct->OCState);
  677. if (IS_TIM_BREAK_INSTANCE(TIMx))
  678. {
  679. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  680. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  681. /* Set the complementary output Polarity */
  682. MODIFY_REG(tmpccer, TIM_CCER_CC1NP, TIM_OCInitStruct->OCNPolarity << 2U);
  683. /* Set the complementary output State */
  684. MODIFY_REG(tmpccer, TIM_CCER_CC1NE, TIM_OCInitStruct->OCNState << 2U);
  685. /* Set the Output Idle state */
  686. MODIFY_REG(tmpcr2, TIM_CR2_OIS1, TIM_OCInitStruct->OCIdleState);
  687. /* Set the complementary output Idle state */
  688. MODIFY_REG(tmpcr2, TIM_CR2_OIS1N, TIM_OCInitStruct->OCNIdleState << 1U);
  689. }
  690. /* Write to TIMx CR2 */
  691. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  692. /* Write to TIMx CCMR1 */
  693. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  694. /* Set the Capture Compare Register value */
  695. LL_TIM_OC_SetCompareCH1(TIMx, TIM_OCInitStruct->CompareValue);
  696. /* Write to TIMx CCER */
  697. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  698. return SUCCESS;
  699. }
  700. /**
  701. * @brief Configure the TIMx output channel 2.
  702. * @param TIMx Timer Instance
  703. * @param TIM_OCInitStruct pointer to the the TIMx output channel 2 configuration data structure
  704. * @retval An ErrorStatus enumeration value:
  705. * - SUCCESS: TIMx registers are de-initialized
  706. * - ERROR: not applicable
  707. */
  708. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  709. {
  710. uint32_t tmpccmr1;
  711. uint32_t tmpccer;
  712. uint32_t tmpcr2;
  713. /* Check the parameters */
  714. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  715. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  716. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  717. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  718. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  719. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  720. /* Disable the Channel 2: Reset the CC2E Bit */
  721. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC2E);
  722. /* Get the TIMx CCER register value */
  723. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  724. /* Get the TIMx CR2 register value */
  725. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  726. /* Get the TIMx CCMR1 register value */
  727. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  728. /* Reset Capture/Compare selection Bits */
  729. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC2S);
  730. /* Select the Output Compare Mode */
  731. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC2M, TIM_OCInitStruct->OCMode << 8U);
  732. /* Set the Output Compare Polarity */
  733. MODIFY_REG(tmpccer, TIM_CCER_CC2P, TIM_OCInitStruct->OCPolarity << 4U);
  734. /* Set the Output State */
  735. MODIFY_REG(tmpccer, TIM_CCER_CC2E, TIM_OCInitStruct->OCState << 4U);
  736. if (IS_TIM_BREAK_INSTANCE(TIMx))
  737. {
  738. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  739. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  740. /* Set the complementary output Polarity */
  741. MODIFY_REG(tmpccer, TIM_CCER_CC2NP, TIM_OCInitStruct->OCNPolarity << 6U);
  742. /* Set the complementary output State */
  743. MODIFY_REG(tmpccer, TIM_CCER_CC2NE, TIM_OCInitStruct->OCNState << 6U);
  744. /* Set the Output Idle state */
  745. MODIFY_REG(tmpcr2, TIM_CR2_OIS2, TIM_OCInitStruct->OCIdleState << 2U);
  746. /* Set the complementary output Idle state */
  747. MODIFY_REG(tmpcr2, TIM_CR2_OIS2N, TIM_OCInitStruct->OCNIdleState << 3U);
  748. }
  749. /* Write to TIMx CR2 */
  750. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  751. /* Write to TIMx CCMR1 */
  752. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  753. /* Set the Capture Compare Register value */
  754. LL_TIM_OC_SetCompareCH2(TIMx, TIM_OCInitStruct->CompareValue);
  755. /* Write to TIMx CCER */
  756. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  757. return SUCCESS;
  758. }
  759. /**
  760. * @brief Configure the TIMx output channel 3.
  761. * @param TIMx Timer Instance
  762. * @param TIM_OCInitStruct pointer to the the TIMx output channel 3 configuration data structure
  763. * @retval An ErrorStatus enumeration value:
  764. * - SUCCESS: TIMx registers are de-initialized
  765. * - ERROR: not applicable
  766. */
  767. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  768. {
  769. uint32_t tmpccmr2;
  770. uint32_t tmpccer;
  771. uint32_t tmpcr2;
  772. /* Check the parameters */
  773. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  774. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  775. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  776. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  777. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  778. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  779. /* Disable the Channel 3: Reset the CC3E Bit */
  780. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC3E);
  781. /* Get the TIMx CCER register value */
  782. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  783. /* Get the TIMx CR2 register value */
  784. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  785. /* Get the TIMx CCMR2 register value */
  786. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  787. /* Reset Capture/Compare selection Bits */
  788. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC3S);
  789. /* Select the Output Compare Mode */
  790. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC3M, TIM_OCInitStruct->OCMode);
  791. /* Set the Output Compare Polarity */
  792. MODIFY_REG(tmpccer, TIM_CCER_CC3P, TIM_OCInitStruct->OCPolarity << 8U);
  793. /* Set the Output State */
  794. MODIFY_REG(tmpccer, TIM_CCER_CC3E, TIM_OCInitStruct->OCState << 8U);
  795. if (IS_TIM_BREAK_INSTANCE(TIMx))
  796. {
  797. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  798. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  799. /* Set the complementary output Polarity */
  800. MODIFY_REG(tmpccer, TIM_CCER_CC3NP, TIM_OCInitStruct->OCNPolarity << 10U);
  801. /* Set the complementary output State */
  802. MODIFY_REG(tmpccer, TIM_CCER_CC3NE, TIM_OCInitStruct->OCNState << 10U);
  803. /* Set the Output Idle state */
  804. MODIFY_REG(tmpcr2, TIM_CR2_OIS3, TIM_OCInitStruct->OCIdleState << 4U);
  805. /* Set the complementary output Idle state */
  806. MODIFY_REG(tmpcr2, TIM_CR2_OIS3N, TIM_OCInitStruct->OCNIdleState << 5U);
  807. }
  808. /* Write to TIMx CR2 */
  809. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  810. /* Write to TIMx CCMR2 */
  811. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  812. /* Set the Capture Compare Register value */
  813. LL_TIM_OC_SetCompareCH3(TIMx, TIM_OCInitStruct->CompareValue);
  814. /* Write to TIMx CCER */
  815. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  816. return SUCCESS;
  817. }
  818. /**
  819. * @brief Configure the TIMx output channel 4.
  820. * @param TIMx Timer Instance
  821. * @param TIM_OCInitStruct pointer to the the TIMx output channel 4 configuration data structure
  822. * @retval An ErrorStatus enumeration value:
  823. * - SUCCESS: TIMx registers are de-initialized
  824. * - ERROR: not applicable
  825. */
  826. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  827. {
  828. uint32_t tmpccmr2;
  829. uint32_t tmpccer;
  830. uint32_t tmpcr2;
  831. /* Check the parameters */
  832. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  833. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  834. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  835. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  836. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  837. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  838. /* Disable the Channel 4: Reset the CC4E Bit */
  839. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC4E);
  840. /* Get the TIMx CCER register value */
  841. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  842. /* Get the TIMx CR2 register value */
  843. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  844. /* Get the TIMx CCMR2 register value */
  845. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  846. /* Reset Capture/Compare selection Bits */
  847. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC4S);
  848. /* Select the Output Compare Mode */
  849. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC4M, TIM_OCInitStruct->OCMode << 8U);
  850. /* Set the Output Compare Polarity */
  851. MODIFY_REG(tmpccer, TIM_CCER_CC4P, TIM_OCInitStruct->OCPolarity << 12U);
  852. /* Set the Output State */
  853. MODIFY_REG(tmpccer, TIM_CCER_CC4E, TIM_OCInitStruct->OCState << 12U);
  854. if (IS_TIM_BREAK_INSTANCE(TIMx))
  855. {
  856. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  857. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  858. /* Set the Output Idle state */
  859. MODIFY_REG(tmpcr2, TIM_CR2_OIS4, TIM_OCInitStruct->OCIdleState << 6U);
  860. }
  861. /* Write to TIMx CR2 */
  862. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  863. /* Write to TIMx CCMR2 */
  864. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  865. /* Set the Capture Compare Register value */
  866. LL_TIM_OC_SetCompareCH4(TIMx, TIM_OCInitStruct->CompareValue);
  867. /* Write to TIMx CCER */
  868. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  869. return SUCCESS;
  870. }
  871. /**
  872. * @brief Configure the TIMx input channel 1.
  873. * @param TIMx Timer Instance
  874. * @param TIM_ICInitStruct pointer to the the TIMx input channel 1 configuration data structure
  875. * @retval An ErrorStatus enumeration value:
  876. * - SUCCESS: TIMx registers are de-initialized
  877. * - ERROR: not applicable
  878. */
  879. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  880. {
  881. /* Check the parameters */
  882. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  883. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  884. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  885. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  886. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  887. /* Disable the Channel 1: Reset the CC1E Bit */
  888. TIMx->CCER &= (uint32_t)~TIM_CCER_CC1E;
  889. /* Select the Input and set the filter and the prescaler value */
  890. MODIFY_REG(TIMx->CCMR1,
  891. (TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC),
  892. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  893. /* Select the Polarity and set the CC1E Bit */
  894. MODIFY_REG(TIMx->CCER,
  895. (TIM_CCER_CC1P | TIM_CCER_CC1NP),
  896. (TIM_ICInitStruct->ICPolarity | TIM_CCER_CC1E));
  897. return SUCCESS;
  898. }
  899. /**
  900. * @brief Configure the TIMx input channel 2.
  901. * @param TIMx Timer Instance
  902. * @param TIM_ICInitStruct pointer to the the TIMx input channel 2 configuration data structure
  903. * @retval An ErrorStatus enumeration value:
  904. * - SUCCESS: TIMx registers are de-initialized
  905. * - ERROR: not applicable
  906. */
  907. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  908. {
  909. /* Check the parameters */
  910. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  911. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  912. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  913. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  914. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  915. /* Disable the Channel 2: Reset the CC2E Bit */
  916. TIMx->CCER &= (uint32_t)~TIM_CCER_CC2E;
  917. /* Select the Input and set the filter and the prescaler value */
  918. MODIFY_REG(TIMx->CCMR1,
  919. (TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC),
  920. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  921. /* Select the Polarity and set the CC2E Bit */
  922. MODIFY_REG(TIMx->CCER,
  923. (TIM_CCER_CC2P | TIM_CCER_CC2NP),
  924. ((TIM_ICInitStruct->ICPolarity << 4U) | TIM_CCER_CC2E));
  925. return SUCCESS;
  926. }
  927. /**
  928. * @brief Configure the TIMx input channel 3.
  929. * @param TIMx Timer Instance
  930. * @param TIM_ICInitStruct pointer to the the TIMx input channel 3 configuration data structure
  931. * @retval An ErrorStatus enumeration value:
  932. * - SUCCESS: TIMx registers are de-initialized
  933. * - ERROR: not applicable
  934. */
  935. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  936. {
  937. /* Check the parameters */
  938. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  939. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  940. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  941. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  942. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  943. /* Disable the Channel 3: Reset the CC3E Bit */
  944. TIMx->CCER &= (uint32_t)~TIM_CCER_CC3E;
  945. /* Select the Input and set the filter and the prescaler value */
  946. MODIFY_REG(TIMx->CCMR2,
  947. (TIM_CCMR2_CC3S | TIM_CCMR2_IC3F | TIM_CCMR2_IC3PSC),
  948. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  949. /* Select the Polarity and set the CC3E Bit */
  950. MODIFY_REG(TIMx->CCER,
  951. (TIM_CCER_CC3P | TIM_CCER_CC3NP),
  952. ((TIM_ICInitStruct->ICPolarity << 8U) | TIM_CCER_CC3E));
  953. return SUCCESS;
  954. }
  955. /**
  956. * @brief Configure the TIMx input channel 4.
  957. * @param TIMx Timer Instance
  958. * @param TIM_ICInitStruct pointer to the the TIMx input channel 4 configuration data structure
  959. * @retval An ErrorStatus enumeration value:
  960. * - SUCCESS: TIMx registers are de-initialized
  961. * - ERROR: not applicable
  962. */
  963. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  964. {
  965. /* Check the parameters */
  966. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  967. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  968. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  969. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  970. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  971. /* Disable the Channel 4: Reset the CC4E Bit */
  972. TIMx->CCER &= (uint32_t)~TIM_CCER_CC4E;
  973. /* Select the Input and set the filter and the prescaler value */
  974. MODIFY_REG(TIMx->CCMR2,
  975. (TIM_CCMR2_CC4S | TIM_CCMR2_IC4F | TIM_CCMR2_IC4PSC),
  976. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  977. /* Select the Polarity and set the CC2E Bit */
  978. MODIFY_REG(TIMx->CCER,
  979. (TIM_CCER_CC4P | TIM_CCER_CC4NP),
  980. ((TIM_ICInitStruct->ICPolarity << 12U) | TIM_CCER_CC4E));
  981. return SUCCESS;
  982. }
  983. /**
  984. * @}
  985. */
  986. /**
  987. * @}
  988. */
  989. #endif /* TIM1 || TIM2 || TIM3 || TIM4 || TIM5 || TIM6 || TIM7 || TIM8 || TIM9 || TIM10 || TIM11 || TIM12 || TIM13 || TIM14 */
  990. /**
  991. * @}
  992. */
  993. #endif /* USE_FULL_LL_DRIVER */
  994. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/