训练营PLSR题目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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