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.

stm32l1xx_ll_tim.c 31KB

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