py32f0xx_ll_tim.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_ll_tim.c
  4. * @author MCU Application Team
  5. * @brief TIM LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya 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. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f0xx_ll_tim.h"
  33. #include "py32f0xx_ll_bus.h"
  34. #ifdef USE_FULL_ASSERT
  35. #include "py32_assert.h"
  36. #else
  37. #define assert_param(expr) ((void)0U)
  38. #endif /* USE_FULL_ASSERT */
  39. /** @addtogroup PY32F0xx_LL_Driver
  40. * @{
  41. */
  42. #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)
  43. /** @addtogroup TIM_LL
  44. * @{
  45. */
  46. /* Private types -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private constants ---------------------------------------------------------*/
  49. /* Private macros ------------------------------------------------------------*/
  50. /** @addtogroup TIM_LL_Private_Macros
  51. * @{
  52. */
  53. #define IS_LL_TIM_COUNTERMODE(__VALUE__) (((__VALUE__) == LL_TIM_COUNTERMODE_UP) \
  54. || ((__VALUE__) == LL_TIM_COUNTERMODE_DOWN) \
  55. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_UP) \
  56. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_DOWN) \
  57. || ((__VALUE__) == LL_TIM_COUNTERMODE_CENTER_UP_DOWN))
  58. #define IS_LL_TIM_CLOCKDIVISION(__VALUE__) (((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV1) \
  59. || ((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV2) \
  60. || ((__VALUE__) == LL_TIM_CLOCKDIVISION_DIV4))
  61. #define IS_LL_TIM_OCMODE(__VALUE__) (((__VALUE__) == LL_TIM_OCMODE_FROZEN) \
  62. || ((__VALUE__) == LL_TIM_OCMODE_ACTIVE) \
  63. || ((__VALUE__) == LL_TIM_OCMODE_INACTIVE) \
  64. || ((__VALUE__) == LL_TIM_OCMODE_TOGGLE) \
  65. || ((__VALUE__) == LL_TIM_OCMODE_FORCED_INACTIVE) \
  66. || ((__VALUE__) == LL_TIM_OCMODE_FORCED_ACTIVE) \
  67. || ((__VALUE__) == LL_TIM_OCMODE_PWM1) \
  68. || ((__VALUE__) == LL_TIM_OCMODE_PWM2))
  69. #define IS_LL_TIM_OCSTATE(__VALUE__) (((__VALUE__) == LL_TIM_OCSTATE_DISABLE) \
  70. || ((__VALUE__) == LL_TIM_OCSTATE_ENABLE))
  71. #define IS_LL_TIM_OCPOLARITY(__VALUE__) (((__VALUE__) == LL_TIM_OCPOLARITY_HIGH) \
  72. || ((__VALUE__) == LL_TIM_OCPOLARITY_LOW))
  73. #define IS_LL_TIM_OCIDLESTATE(__VALUE__) (((__VALUE__) == LL_TIM_OCIDLESTATE_LOW) \
  74. || ((__VALUE__) == LL_TIM_OCIDLESTATE_HIGH))
  75. #define IS_LL_TIM_ACTIVEINPUT(__VALUE__) (((__VALUE__) == LL_TIM_ACTIVEINPUT_DIRECTTI) \
  76. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_INDIRECTTI) \
  77. || ((__VALUE__) == LL_TIM_ACTIVEINPUT_TRC))
  78. #define IS_LL_TIM_ICPSC(__VALUE__) (((__VALUE__) == LL_TIM_ICPSC_DIV1) \
  79. || ((__VALUE__) == LL_TIM_ICPSC_DIV2) \
  80. || ((__VALUE__) == LL_TIM_ICPSC_DIV4) \
  81. || ((__VALUE__) == LL_TIM_ICPSC_DIV8))
  82. #define IS_LL_TIM_IC_FILTER(__VALUE__) (((__VALUE__) == LL_TIM_IC_FILTER_FDIV1) \
  83. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N2) \
  84. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N4) \
  85. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV1_N8) \
  86. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N6) \
  87. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV2_N8) \
  88. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N6) \
  89. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV4_N8) \
  90. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N6) \
  91. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV8_N8) \
  92. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N5) \
  93. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N6) \
  94. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV16_N8) \
  95. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N5) \
  96. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N6) \
  97. || ((__VALUE__) == LL_TIM_IC_FILTER_FDIV32_N8))
  98. #define IS_LL_TIM_IC_POLARITY(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  99. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING))
  100. #define IS_LL_TIM_ENCODERMODE(__VALUE__) (((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI1) \
  101. || ((__VALUE__) == LL_TIM_ENCODERMODE_X2_TI2) \
  102. || ((__VALUE__) == LL_TIM_ENCODERMODE_X4_TI12))
  103. #define IS_LL_TIM_IC_POLARITY_ENCODER(__VALUE__) (((__VALUE__) == LL_TIM_IC_POLARITY_RISING) \
  104. || ((__VALUE__) == LL_TIM_IC_POLARITY_FALLING))
  105. #define IS_LL_TIM_OSSR_STATE(__VALUE__) (((__VALUE__) == LL_TIM_OSSR_DISABLE) \
  106. || ((__VALUE__) == LL_TIM_OSSR_ENABLE))
  107. #define IS_LL_TIM_OSSI_STATE(__VALUE__) (((__VALUE__) == LL_TIM_OSSI_DISABLE) \
  108. || ((__VALUE__) == LL_TIM_OSSI_ENABLE))
  109. #define IS_LL_TIM_LOCK_LEVEL(__VALUE__) (((__VALUE__) == LL_TIM_LOCKLEVEL_OFF) \
  110. || ((__VALUE__) == LL_TIM_LOCKLEVEL_1) \
  111. || ((__VALUE__) == LL_TIM_LOCKLEVEL_2) \
  112. || ((__VALUE__) == LL_TIM_LOCKLEVEL_3))
  113. #define IS_LL_TIM_BREAK_STATE(__VALUE__) (((__VALUE__) == LL_TIM_BREAK_DISABLE) \
  114. || ((__VALUE__) == LL_TIM_BREAK_ENABLE))
  115. #define IS_LL_TIM_BREAK_POLARITY(__VALUE__) (((__VALUE__) == LL_TIM_BREAK_POLARITY_LOW) \
  116. || ((__VALUE__) == LL_TIM_BREAK_POLARITY_HIGH))
  117. #define IS_LL_TIM_AUTOMATIC_OUTPUT_STATE(__VALUE__) (((__VALUE__) == LL_TIM_AUTOMATICOUTPUT_DISABLE) \
  118. || ((__VALUE__) == LL_TIM_AUTOMATICOUTPUT_ENABLE))
  119. /**
  120. * @}
  121. */
  122. /* Private function prototypes -----------------------------------------------*/
  123. /** @defgroup TIM_LL_Private_Functions TIM Private Functions
  124. * @{
  125. */
  126. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  127. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  128. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  129. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct);
  130. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  131. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  132. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  133. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct);
  134. /**
  135. * @}
  136. */
  137. /* Exported functions --------------------------------------------------------*/
  138. /** @addtogroup TIM_LL_Exported_Functions
  139. * @{
  140. */
  141. /** @addtogroup TIM_LL_EF_Init
  142. * @{
  143. */
  144. /**
  145. * @brief Set TIMx registers to their reset values.
  146. * @param TIMx Timer instance
  147. * @retval An ErrorStatus enumeration value:
  148. * - SUCCESS: TIMx registers are de-initialized
  149. * - ERROR: invalid TIMx instance
  150. */
  151. ErrorStatus LL_TIM_DeInit(TIM_TypeDef *TIMx)
  152. {
  153. ErrorStatus result = SUCCESS;
  154. /* Check the parameters */
  155. assert_param(IS_TIM_INSTANCE(TIMx));
  156. if (TIMx == TIM1)
  157. {
  158. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM1);
  159. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM1);
  160. }
  161. #if defined(TIM2)
  162. if (TIMx == TIM2)
  163. {
  164. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM2);
  165. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM2);
  166. }
  167. #endif
  168. #if defined(TIM3)
  169. else if (TIMx == TIM3)
  170. {
  171. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM3);
  172. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM3);
  173. }
  174. #endif
  175. #if defined(TIM4)
  176. else if (TIMx == TIM4)
  177. {
  178. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM4);
  179. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM4);
  180. }
  181. #endif
  182. #if defined(TIM5)
  183. else if (TIMx == TIM5)
  184. {
  185. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM5);
  186. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM5);
  187. }
  188. #endif
  189. #if defined(TIM6)
  190. else if (TIMx == TIM6)
  191. {
  192. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM6);
  193. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM6);
  194. }
  195. #endif
  196. #if defined (TIM7)
  197. else if (TIMx == TIM7)
  198. {
  199. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM7);
  200. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM7);
  201. }
  202. #endif
  203. #if defined(TIM8)
  204. else if (TIMx == TIM8)
  205. {
  206. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM8);
  207. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM8);
  208. }
  209. #endif
  210. #if defined(TIM9)
  211. else if (TIMx == TIM9)
  212. {
  213. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM9);
  214. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM9);
  215. }
  216. #endif
  217. #if defined(TIM10)
  218. else if (TIMx == TIM10)
  219. {
  220. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM10);
  221. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM10);
  222. }
  223. #endif
  224. #if defined(TIM11)
  225. else if (TIMx == TIM11)
  226. {
  227. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM11);
  228. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM11);
  229. }
  230. #endif
  231. #if defined(TIM12)
  232. else if (TIMx == TIM12)
  233. {
  234. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM12);
  235. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM12);
  236. }
  237. #endif
  238. #if defined(TIM13)
  239. else if (TIMx == TIM13)
  240. {
  241. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_TIM13);
  242. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_TIM13);
  243. }
  244. #endif
  245. #if defined(TIM14)
  246. else if (TIMx == TIM14)
  247. {
  248. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM14);
  249. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM14);
  250. }
  251. #endif
  252. #if defined(TIM15)
  253. else if (TIMx == TIM15)
  254. {
  255. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM15);
  256. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM15);
  257. }
  258. #endif
  259. #if defined(TIM16)
  260. else if (TIMx == TIM16)
  261. {
  262. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM16);
  263. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM16);
  264. }
  265. #endif
  266. #if defined(TIM17)
  267. else if (TIMx == TIM17)
  268. {
  269. LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_TIM17);
  270. LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_TIM17);
  271. }
  272. #endif
  273. else
  274. {
  275. result = ERROR;
  276. }
  277. return result;
  278. }
  279. /**
  280. * @brief Set the fields of the time base unit configuration data structure
  281. * to their default values.
  282. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure (time base unit configuration data structure)
  283. * @retval None
  284. */
  285. void LL_TIM_StructInit(LL_TIM_InitTypeDef *TIM_InitStruct)
  286. {
  287. /* Set the default configuration */
  288. TIM_InitStruct->Prescaler = (uint16_t)0x0000;
  289. TIM_InitStruct->CounterMode = LL_TIM_COUNTERMODE_UP;
  290. TIM_InitStruct->Autoreload = 0xFFFFFFFFU;
  291. TIM_InitStruct->ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  292. TIM_InitStruct->RepetitionCounter = (uint8_t)0x00;
  293. }
  294. /**
  295. * @brief Configure the TIMx time base unit.
  296. * @param TIMx Timer Instance
  297. * @param TIM_InitStruct pointer to a @ref LL_TIM_InitTypeDef structure (TIMx time base unit configuration data structure)
  298. * @retval An ErrorStatus enumeration value:
  299. * - SUCCESS: TIMx registers are de-initialized
  300. * - ERROR: not applicable
  301. */
  302. ErrorStatus LL_TIM_Init(TIM_TypeDef *TIMx, LL_TIM_InitTypeDef *TIM_InitStruct)
  303. {
  304. uint32_t tmpcr1;
  305. /* Check the parameters */
  306. assert_param(IS_TIM_INSTANCE(TIMx));
  307. assert_param(IS_LL_TIM_COUNTERMODE(TIM_InitStruct->CounterMode));
  308. assert_param(IS_LL_TIM_CLOCKDIVISION(TIM_InitStruct->ClockDivision));
  309. tmpcr1 = LL_TIM_ReadReg(TIMx, CR1);
  310. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  311. {
  312. /* Select the Counter Mode */
  313. MODIFY_REG(tmpcr1, (TIM_CR1_DIR | TIM_CR1_CMS), TIM_InitStruct->CounterMode);
  314. }
  315. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  316. {
  317. /* Set the clock division */
  318. MODIFY_REG(tmpcr1, TIM_CR1_CKD, TIM_InitStruct->ClockDivision);
  319. }
  320. /* Write to TIMx CR1 */
  321. LL_TIM_WriteReg(TIMx, CR1, tmpcr1);
  322. /* Set the Autoreload value */
  323. LL_TIM_SetAutoReload(TIMx, TIM_InitStruct->Autoreload);
  324. /* Set the Prescaler value */
  325. LL_TIM_SetPrescaler(TIMx, TIM_InitStruct->Prescaler);
  326. if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
  327. {
  328. /* Set the Repetition Counter value */
  329. LL_TIM_SetRepetitionCounter(TIMx, TIM_InitStruct->RepetitionCounter);
  330. }
  331. /* Generate an update event to reload the Prescaler
  332. and the repetition counter value (if applicable) immediately */
  333. LL_TIM_GenerateEvent_UPDATE(TIMx);
  334. return SUCCESS;
  335. }
  336. /**
  337. * @brief Set the fields of the TIMx output channel configuration data
  338. * structure to their default values.
  339. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure (the output channel configuration data structure)
  340. * @retval None
  341. */
  342. void LL_TIM_OC_StructInit(LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  343. {
  344. /* Set the default configuration */
  345. TIM_OC_InitStruct->OCMode = LL_TIM_OCMODE_FROZEN;
  346. TIM_OC_InitStruct->OCState = LL_TIM_OCSTATE_DISABLE;
  347. TIM_OC_InitStruct->OCNState = LL_TIM_OCSTATE_DISABLE;
  348. TIM_OC_InitStruct->CompareValue = 0x00000000U;
  349. TIM_OC_InitStruct->OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  350. TIM_OC_InitStruct->OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  351. TIM_OC_InitStruct->OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  352. TIM_OC_InitStruct->OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  353. }
  354. /**
  355. * @brief Configure the TIMx output channel.
  356. * @param TIMx Timer Instance
  357. * @param Channel This parameter can be one of the following values:
  358. * @arg @ref LL_TIM_CHANNEL_CH1
  359. * @arg @ref LL_TIM_CHANNEL_CH2
  360. * @arg @ref LL_TIM_CHANNEL_CH3
  361. * @arg @ref LL_TIM_CHANNEL_CH4
  362. * @param TIM_OC_InitStruct pointer to a @ref LL_TIM_OC_InitTypeDef structure (TIMx output channel configuration data structure)
  363. * @retval An ErrorStatus enumeration value:
  364. * - SUCCESS: TIMx output channel is initialized
  365. * - ERROR: TIMx output channel is not initialized
  366. */
  367. ErrorStatus LL_TIM_OC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_OC_InitTypeDef *TIM_OC_InitStruct)
  368. {
  369. ErrorStatus result = ERROR;
  370. switch (Channel)
  371. {
  372. case LL_TIM_CHANNEL_CH1:
  373. result = OC1Config(TIMx, TIM_OC_InitStruct);
  374. break;
  375. case LL_TIM_CHANNEL_CH2:
  376. result = OC2Config(TIMx, TIM_OC_InitStruct);
  377. break;
  378. case LL_TIM_CHANNEL_CH3:
  379. result = OC3Config(TIMx, TIM_OC_InitStruct);
  380. break;
  381. case LL_TIM_CHANNEL_CH4:
  382. result = OC4Config(TIMx, TIM_OC_InitStruct);
  383. break;
  384. default:
  385. break;
  386. }
  387. return result;
  388. }
  389. /**
  390. * @brief Set the fields of the TIMx input channel configuration data
  391. * structure to their default values.
  392. * @param TIM_ICInitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (the input channel configuration data structure)
  393. * @retval None
  394. */
  395. void LL_TIM_IC_StructInit(LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  396. {
  397. /* Set the default configuration */
  398. TIM_ICInitStruct->ICPolarity = LL_TIM_IC_POLARITY_RISING;
  399. TIM_ICInitStruct->ICActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  400. TIM_ICInitStruct->ICPrescaler = LL_TIM_ICPSC_DIV1;
  401. TIM_ICInitStruct->ICFilter = LL_TIM_IC_FILTER_FDIV1;
  402. }
  403. /**
  404. * @brief Configure the TIMx input channel.
  405. * @param TIMx Timer Instance
  406. * @param Channel This parameter can be one of the following values:
  407. * @arg @ref LL_TIM_CHANNEL_CH1
  408. * @arg @ref LL_TIM_CHANNEL_CH2
  409. * @arg @ref LL_TIM_CHANNEL_CH3
  410. * @arg @ref LL_TIM_CHANNEL_CH4
  411. * @param TIM_IC_InitStruct pointer to a @ref LL_TIM_IC_InitTypeDef structure (TIMx input channel configuration data structure)
  412. * @retval An ErrorStatus enumeration value:
  413. * - SUCCESS: TIMx output channel is initialized
  414. * - ERROR: TIMx output channel is not initialized
  415. */
  416. ErrorStatus LL_TIM_IC_Init(TIM_TypeDef *TIMx, uint32_t Channel, LL_TIM_IC_InitTypeDef *TIM_IC_InitStruct)
  417. {
  418. ErrorStatus result = ERROR;
  419. switch (Channel)
  420. {
  421. case LL_TIM_CHANNEL_CH1:
  422. result = IC1Config(TIMx, TIM_IC_InitStruct);
  423. break;
  424. case LL_TIM_CHANNEL_CH2:
  425. result = IC2Config(TIMx, TIM_IC_InitStruct);
  426. break;
  427. case LL_TIM_CHANNEL_CH3:
  428. result = IC3Config(TIMx, TIM_IC_InitStruct);
  429. break;
  430. case LL_TIM_CHANNEL_CH4:
  431. result = IC4Config(TIMx, TIM_IC_InitStruct);
  432. break;
  433. default:
  434. break;
  435. }
  436. return result;
  437. }
  438. /**
  439. * @brief Fills each TIM_EncoderInitStruct field with its default value
  440. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (encoder interface configuration data structure)
  441. * @retval None
  442. */
  443. void LL_TIM_ENCODER_StructInit(LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  444. {
  445. /* Set the default configuration */
  446. TIM_EncoderInitStruct->EncoderMode = LL_TIM_ENCODERMODE_X2_TI1;
  447. TIM_EncoderInitStruct->IC1Polarity = LL_TIM_IC_POLARITY_RISING;
  448. TIM_EncoderInitStruct->IC1ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  449. TIM_EncoderInitStruct->IC1Prescaler = LL_TIM_ICPSC_DIV1;
  450. TIM_EncoderInitStruct->IC1Filter = LL_TIM_IC_FILTER_FDIV1;
  451. TIM_EncoderInitStruct->IC2Polarity = LL_TIM_IC_POLARITY_RISING;
  452. TIM_EncoderInitStruct->IC2ActiveInput = LL_TIM_ACTIVEINPUT_DIRECTTI;
  453. TIM_EncoderInitStruct->IC2Prescaler = LL_TIM_ICPSC_DIV1;
  454. TIM_EncoderInitStruct->IC2Filter = LL_TIM_IC_FILTER_FDIV1;
  455. }
  456. /**
  457. * @brief Configure the encoder interface of the timer instance.
  458. * @param TIMx Timer Instance
  459. * @param TIM_EncoderInitStruct pointer to a @ref LL_TIM_ENCODER_InitTypeDef structure (TIMx encoder interface configuration data structure)
  460. * @retval An ErrorStatus enumeration value:
  461. * - SUCCESS: TIMx registers are de-initialized
  462. * - ERROR: not applicable
  463. */
  464. ErrorStatus LL_TIM_ENCODER_Init(TIM_TypeDef *TIMx, LL_TIM_ENCODER_InitTypeDef *TIM_EncoderInitStruct)
  465. {
  466. uint32_t tmpccmr1;
  467. uint32_t tmpccer;
  468. /* Check the parameters */
  469. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(TIMx));
  470. assert_param(IS_LL_TIM_ENCODERMODE(TIM_EncoderInitStruct->EncoderMode));
  471. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC1Polarity));
  472. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC1ActiveInput));
  473. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC1Prescaler));
  474. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC1Filter));
  475. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_EncoderInitStruct->IC2Polarity));
  476. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_EncoderInitStruct->IC2ActiveInput));
  477. assert_param(IS_LL_TIM_ICPSC(TIM_EncoderInitStruct->IC2Prescaler));
  478. assert_param(IS_LL_TIM_IC_FILTER(TIM_EncoderInitStruct->IC2Filter));
  479. /* Disable the CC1 and CC2: Reset the CC1E and CC2E Bits */
  480. TIMx->CCER &= (uint32_t)~(TIM_CCER_CC1E | TIM_CCER_CC2E);
  481. /* Get the TIMx CCMR1 register value */
  482. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  483. /* Get the TIMx CCER register value */
  484. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  485. /* Configure TI1 */
  486. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC);
  487. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1ActiveInput >> 16U);
  488. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Filter >> 16U);
  489. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC1Prescaler >> 16U);
  490. /* Configure TI2 */
  491. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC);
  492. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2ActiveInput >> 8U);
  493. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Filter >> 8U);
  494. tmpccmr1 |= (uint32_t)(TIM_EncoderInitStruct->IC2Prescaler >> 8U);
  495. /* Set TI1 and TI2 polarity and enable TI1 and TI2 */
  496. tmpccer &= (uint32_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC2P | TIM_CCER_CC2NP);
  497. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC1Polarity);
  498. tmpccer |= (uint32_t)(TIM_EncoderInitStruct->IC2Polarity << 4U);
  499. tmpccer |= (uint32_t)(TIM_CCER_CC1E | TIM_CCER_CC2E);
  500. /* Set encoder mode */
  501. LL_TIM_SetEncoderMode(TIMx, TIM_EncoderInitStruct->EncoderMode);
  502. /* Write to TIMx CCMR1 */
  503. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  504. /* Write to TIMx CCER */
  505. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  506. return SUCCESS;
  507. }
  508. /**
  509. * @brief Set the fields of the TIMx Hall sensor interface configuration data
  510. * structure to their default values.
  511. * @param TIM_HallSensorInitStruct pointer to a @ref LL_TIM_HALLSENSOR_InitTypeDef structure (HALL sensor interface configuration data structure)
  512. * @retval None
  513. */
  514. void LL_TIM_HALLSENSOR_StructInit(LL_TIM_HALLSENSOR_InitTypeDef *TIM_HallSensorInitStruct)
  515. {
  516. /* Set the default configuration */
  517. TIM_HallSensorInitStruct->IC1Polarity = LL_TIM_IC_POLARITY_RISING;
  518. TIM_HallSensorInitStruct->IC1Prescaler = LL_TIM_ICPSC_DIV1;
  519. TIM_HallSensorInitStruct->IC1Filter = LL_TIM_IC_FILTER_FDIV1;
  520. TIM_HallSensorInitStruct->CommutationDelay = 0U;
  521. }
  522. /**
  523. * @brief Configure the Hall sensor interface of the timer instance.
  524. * @note TIMx CH1, CH2 and CH3 inputs connected through a XOR
  525. * to the TI1 input channel
  526. * @note TIMx slave mode controller is configured in reset mode.
  527. Selected internal trigger is TI1F_ED.
  528. * @note Channel 1 is configured as input, IC1 is mapped on TRC.
  529. * @note Captured value stored in TIMx_CCR1 correspond to the time elapsed
  530. * between 2 changes on the inputs. It gives information about motor speed.
  531. * @note Channel 2 is configured in output PWM 2 mode.
  532. * @note Compare value stored in TIMx_CCR2 corresponds to the commutation delay.
  533. * @note OC2REF is selected as trigger output on TRGO.
  534. * @param TIMx Timer Instance
  535. * @param TIM_HallSensorInitStruct pointer to a @ref LL_TIM_HALLSENSOR_InitTypeDef structure (TIMx HALL sensor interface configuration data structure)
  536. * @retval An ErrorStatus enumeration value:
  537. * - SUCCESS: TIMx registers are de-initialized
  538. * - ERROR: not applicable
  539. */
  540. ErrorStatus LL_TIM_HALLSENSOR_Init(TIM_TypeDef *TIMx, LL_TIM_HALLSENSOR_InitTypeDef *TIM_HallSensorInitStruct)
  541. {
  542. uint32_t tmpcr2;
  543. uint32_t tmpccmr1;
  544. uint32_t tmpccer;
  545. uint32_t tmpsmcr;
  546. /* Check the parameters */
  547. assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(TIMx));
  548. assert_param(IS_LL_TIM_IC_POLARITY_ENCODER(TIM_HallSensorInitStruct->IC1Polarity));
  549. assert_param(IS_LL_TIM_ICPSC(TIM_HallSensorInitStruct->IC1Prescaler));
  550. assert_param(IS_LL_TIM_IC_FILTER(TIM_HallSensorInitStruct->IC1Filter));
  551. /* Disable the CC1 and CC2: Reset the CC1E and CC2E Bits */
  552. TIMx->CCER &= (uint32_t)~(TIM_CCER_CC1E | TIM_CCER_CC2E);
  553. /* Get the TIMx CR2 register value */
  554. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  555. /* Get the TIMx CCMR1 register value */
  556. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  557. /* Get the TIMx CCER register value */
  558. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  559. /* Get the TIMx SMCR register value */
  560. tmpsmcr = LL_TIM_ReadReg(TIMx, SMCR);
  561. /* Connect TIMx_CH1, CH2 and CH3 pins to the TI1 input */
  562. tmpcr2 |= TIM_CR2_TI1S;
  563. /* OC2REF signal is used as trigger output (TRGO) */
  564. tmpcr2 |= LL_TIM_TRGO_OC2REF;
  565. /* Configure the slave mode controller */
  566. tmpsmcr &= (uint32_t)~(TIM_SMCR_TS | TIM_SMCR_SMS);
  567. tmpsmcr |= LL_TIM_TS_TI1F_ED;
  568. tmpsmcr |= LL_TIM_SLAVEMODE_RESET;
  569. /* Configure input channel 1 */
  570. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC);
  571. tmpccmr1 |= (uint32_t)(LL_TIM_ACTIVEINPUT_TRC >> 16U);
  572. tmpccmr1 |= (uint32_t)(TIM_HallSensorInitStruct->IC1Filter >> 16U);
  573. tmpccmr1 |= (uint32_t)(TIM_HallSensorInitStruct->IC1Prescaler >> 16U);
  574. /* Configure input channel 2 */
  575. tmpccmr1 &= (uint32_t)~(TIM_CCMR1_OC2M | TIM_CCMR1_OC2FE | TIM_CCMR1_OC2PE | TIM_CCMR1_OC2CE);
  576. tmpccmr1 |= (uint32_t)(LL_TIM_OCMODE_PWM2 << 8U);
  577. /* Set Channel 1 polarity and enable Channel 1 and Channel2 */
  578. tmpccer &= (uint32_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP | TIM_CCER_CC2P | TIM_CCER_CC2NP);
  579. tmpccer |= (uint32_t)(TIM_HallSensorInitStruct->IC1Polarity);
  580. tmpccer |= (uint32_t)(TIM_CCER_CC1E | TIM_CCER_CC2E);
  581. /* Write to TIMx CR2 */
  582. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  583. /* Write to TIMx SMCR */
  584. LL_TIM_WriteReg(TIMx, SMCR, tmpsmcr);
  585. /* Write to TIMx CCMR1 */
  586. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  587. /* Write to TIMx CCER */
  588. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  589. /* Write to TIMx CCR2 */
  590. LL_TIM_OC_SetCompareCH2(TIMx, TIM_HallSensorInitStruct->CommutationDelay);
  591. return SUCCESS;
  592. }
  593. /**
  594. * @brief Set the fields of the Break and Dead Time configuration data structure
  595. * to their default values.
  596. * @param TIM_BDTRInitStruct pointer to a @ref LL_TIM_BDTR_InitTypeDef structure (Break and Dead Time configuration data structure)
  597. * @retval None
  598. */
  599. void LL_TIM_BDTR_StructInit(LL_TIM_BDTR_InitTypeDef *TIM_BDTRInitStruct)
  600. {
  601. /* Set the default configuration */
  602. TIM_BDTRInitStruct->OSSRState = LL_TIM_OSSR_DISABLE;
  603. TIM_BDTRInitStruct->OSSIState = LL_TIM_OSSI_DISABLE;
  604. TIM_BDTRInitStruct->LockLevel = LL_TIM_LOCKLEVEL_OFF;
  605. TIM_BDTRInitStruct->DeadTime = (uint8_t)0x00;
  606. TIM_BDTRInitStruct->BreakState = LL_TIM_BREAK_DISABLE;
  607. TIM_BDTRInitStruct->BreakPolarity = LL_TIM_BREAK_POLARITY_LOW;
  608. TIM_BDTRInitStruct->AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  609. }
  610. /**
  611. * @brief Configure the Break and Dead Time feature of the timer instance.
  612. * @note As the bits AOE, BKP, BKE, OSSR, OSSI and DTG[7:0] can be write-locked
  613. * depending on the LOCK configuration, it can be necessary to configure all of
  614. * them during the first write access to the TIMx_BDTR register.
  615. * @note Macro IS_TIM_BREAK_INSTANCE(TIMx) can be used to check whether or not
  616. * a timer instance provides a break input.
  617. * @param TIMx Timer Instance
  618. * @param TIM_BDTRInitStruct pointer to a @ref LL_TIM_BDTR_InitTypeDef structure (Break and Dead Time configuration 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 | TIM_CCER_CC4NP),
  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 Puya *****END OF FILE****/