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.

stm32f1xx_ll_tim.c 44KB

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