stm32f1xx_ll_tim.c 44 KB

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