py32f002b_ll_tim.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_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 "py32f002b_ll_tim.h"
  33. #include "py32f002b_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 PY32F002B_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_GRP1_ForceReset(LL_APB1_GRP2_PERIPH_TIM14);
  249. LL_APB1_GRP1_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. /* Private functions --------------------------------------------------------*/
  656. /** @addtogroup TIM_LL_Private_Functions TIM Private Functions
  657. * @brief Private functions
  658. * @{
  659. */
  660. /**
  661. * @brief Configure the TIMx output channel 1.
  662. * @param TIMx Timer Instance
  663. * @param TIM_OCInitStruct pointer to the the TIMx output channel 1 configuration data structure
  664. * @retval An ErrorStatus enumeration value:
  665. * - SUCCESS: TIMx registers are de-initialized
  666. * - ERROR: not applicable
  667. */
  668. static ErrorStatus OC1Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  669. {
  670. uint32_t tmpccmr1;
  671. uint32_t tmpccer;
  672. uint32_t tmpcr2;
  673. /* Check the parameters */
  674. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  675. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  676. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  677. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  678. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  679. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  680. /* Disable the Channel 1: Reset the CC1E Bit */
  681. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC1E);
  682. /* Get the TIMx CCER register value */
  683. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  684. /* Get the TIMx CR2 register value */
  685. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  686. /* Get the TIMx CCMR1 register value */
  687. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  688. /* Reset Capture/Compare selection Bits */
  689. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC1S);
  690. /* Set the Output Compare Mode */
  691. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC1M, TIM_OCInitStruct->OCMode);
  692. /* Set the Output Compare Polarity */
  693. MODIFY_REG(tmpccer, TIM_CCER_CC1P, TIM_OCInitStruct->OCPolarity);
  694. /* Set the Output State */
  695. MODIFY_REG(tmpccer, TIM_CCER_CC1E, TIM_OCInitStruct->OCState);
  696. if (IS_TIM_BREAK_INSTANCE(TIMx))
  697. {
  698. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  699. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  700. /* Set the complementary output Polarity */
  701. MODIFY_REG(tmpccer, TIM_CCER_CC1NP, TIM_OCInitStruct->OCNPolarity << 2U);
  702. /* Set the complementary output State */
  703. MODIFY_REG(tmpccer, TIM_CCER_CC1NE, TIM_OCInitStruct->OCNState << 2U);
  704. /* Set the Output Idle state */
  705. MODIFY_REG(tmpcr2, TIM_CR2_OIS1, TIM_OCInitStruct->OCIdleState);
  706. /* Set the complementary output Idle state */
  707. MODIFY_REG(tmpcr2, TIM_CR2_OIS1N, TIM_OCInitStruct->OCNIdleState << 1U);
  708. }
  709. /* Write to TIMx CR2 */
  710. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  711. /* Write to TIMx CCMR1 */
  712. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  713. /* Set the Capture Compare Register value */
  714. LL_TIM_OC_SetCompareCH1(TIMx, TIM_OCInitStruct->CompareValue);
  715. /* Write to TIMx CCER */
  716. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  717. return SUCCESS;
  718. }
  719. /**
  720. * @brief Configure the TIMx output channel 2.
  721. * @param TIMx Timer Instance
  722. * @param TIM_OCInitStruct pointer to the the TIMx output channel 2 configuration data structure
  723. * @retval An ErrorStatus enumeration value:
  724. * - SUCCESS: TIMx registers are de-initialized
  725. * - ERROR: not applicable
  726. */
  727. static ErrorStatus OC2Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  728. {
  729. uint32_t tmpccmr1;
  730. uint32_t tmpccer;
  731. uint32_t tmpcr2;
  732. /* Check the parameters */
  733. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  734. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  735. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  736. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  737. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  738. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  739. /* Disable the Channel 2: Reset the CC2E Bit */
  740. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC2E);
  741. /* Get the TIMx CCER register value */
  742. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  743. /* Get the TIMx CR2 register value */
  744. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  745. /* Get the TIMx CCMR1 register value */
  746. tmpccmr1 = LL_TIM_ReadReg(TIMx, CCMR1);
  747. /* Reset Capture/Compare selection Bits */
  748. CLEAR_BIT(tmpccmr1, TIM_CCMR1_CC2S);
  749. /* Select the Output Compare Mode */
  750. MODIFY_REG(tmpccmr1, TIM_CCMR1_OC2M, TIM_OCInitStruct->OCMode << 8U);
  751. /* Set the Output Compare Polarity */
  752. MODIFY_REG(tmpccer, TIM_CCER_CC2P, TIM_OCInitStruct->OCPolarity << 4U);
  753. /* Set the Output State */
  754. MODIFY_REG(tmpccer, TIM_CCER_CC2E, TIM_OCInitStruct->OCState << 4U);
  755. if (IS_TIM_BREAK_INSTANCE(TIMx))
  756. {
  757. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  758. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  759. /* Set the complementary output Polarity */
  760. MODIFY_REG(tmpccer, TIM_CCER_CC2NP, TIM_OCInitStruct->OCNPolarity << 6U);
  761. /* Set the complementary output State */
  762. MODIFY_REG(tmpccer, TIM_CCER_CC2NE, TIM_OCInitStruct->OCNState << 6U);
  763. /* Set the Output Idle state */
  764. MODIFY_REG(tmpcr2, TIM_CR2_OIS2, TIM_OCInitStruct->OCIdleState << 2U);
  765. /* Set the complementary output Idle state */
  766. MODIFY_REG(tmpcr2, TIM_CR2_OIS2N, TIM_OCInitStruct->OCNIdleState << 3U);
  767. }
  768. /* Write to TIMx CR2 */
  769. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  770. /* Write to TIMx CCMR1 */
  771. LL_TIM_WriteReg(TIMx, CCMR1, tmpccmr1);
  772. /* Set the Capture Compare Register value */
  773. LL_TIM_OC_SetCompareCH2(TIMx, TIM_OCInitStruct->CompareValue);
  774. /* Write to TIMx CCER */
  775. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  776. return SUCCESS;
  777. }
  778. /**
  779. * @brief Configure the TIMx output channel 3.
  780. * @param TIMx Timer Instance
  781. * @param TIM_OCInitStruct pointer to the the TIMx output channel 3 configuration data structure
  782. * @retval An ErrorStatus enumeration value:
  783. * - SUCCESS: TIMx registers are de-initialized
  784. * - ERROR: not applicable
  785. */
  786. static ErrorStatus OC3Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  787. {
  788. uint32_t tmpccmr2;
  789. uint32_t tmpccer;
  790. uint32_t tmpcr2;
  791. /* Check the parameters */
  792. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  793. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  794. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  795. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  796. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  797. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  798. /* Disable the Channel 3: Reset the CC3E Bit */
  799. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC3E);
  800. /* Get the TIMx CCER register value */
  801. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  802. /* Get the TIMx CR2 register value */
  803. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  804. /* Get the TIMx CCMR2 register value */
  805. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  806. /* Reset Capture/Compare selection Bits */
  807. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC3S);
  808. /* Select the Output Compare Mode */
  809. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC3M, TIM_OCInitStruct->OCMode);
  810. /* Set the Output Compare Polarity */
  811. MODIFY_REG(tmpccer, TIM_CCER_CC3P, TIM_OCInitStruct->OCPolarity << 8U);
  812. /* Set the Output State */
  813. MODIFY_REG(tmpccer, TIM_CCER_CC3E, TIM_OCInitStruct->OCState << 8U);
  814. if (IS_TIM_BREAK_INSTANCE(TIMx))
  815. {
  816. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  817. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  818. /* Set the complementary output Polarity */
  819. MODIFY_REG(tmpccer, TIM_CCER_CC3NP, TIM_OCInitStruct->OCNPolarity << 10U);
  820. /* Set the complementary output State */
  821. MODIFY_REG(tmpccer, TIM_CCER_CC3NE, TIM_OCInitStruct->OCNState << 10U);
  822. /* Set the Output Idle state */
  823. MODIFY_REG(tmpcr2, TIM_CR2_OIS3, TIM_OCInitStruct->OCIdleState << 4U);
  824. /* Set the complementary output Idle state */
  825. MODIFY_REG(tmpcr2, TIM_CR2_OIS3N, TIM_OCInitStruct->OCNIdleState << 5U);
  826. }
  827. /* Write to TIMx CR2 */
  828. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  829. /* Write to TIMx CCMR2 */
  830. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  831. /* Set the Capture Compare Register value */
  832. LL_TIM_OC_SetCompareCH3(TIMx, TIM_OCInitStruct->CompareValue);
  833. /* Write to TIMx CCER */
  834. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  835. return SUCCESS;
  836. }
  837. /**
  838. * @brief Configure the TIMx output channel 4.
  839. * @param TIMx Timer Instance
  840. * @param TIM_OCInitStruct pointer to the the TIMx output channel 4 configuration data structure
  841. * @retval An ErrorStatus enumeration value:
  842. * - SUCCESS: TIMx registers are de-initialized
  843. * - ERROR: not applicable
  844. */
  845. static ErrorStatus OC4Config(TIM_TypeDef *TIMx, LL_TIM_OC_InitTypeDef *TIM_OCInitStruct)
  846. {
  847. uint32_t tmpccmr2;
  848. uint32_t tmpccer;
  849. uint32_t tmpcr2;
  850. /* Check the parameters */
  851. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  852. assert_param(IS_LL_TIM_OCMODE(TIM_OCInitStruct->OCMode));
  853. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCState));
  854. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCPolarity));
  855. assert_param(IS_LL_TIM_OCPOLARITY(TIM_OCInitStruct->OCNPolarity));
  856. assert_param(IS_LL_TIM_OCSTATE(TIM_OCInitStruct->OCNState));
  857. /* Disable the Channel 4: Reset the CC4E Bit */
  858. CLEAR_BIT(TIMx->CCER, TIM_CCER_CC4E);
  859. /* Get the TIMx CCER register value */
  860. tmpccer = LL_TIM_ReadReg(TIMx, CCER);
  861. /* Get the TIMx CR2 register value */
  862. tmpcr2 = LL_TIM_ReadReg(TIMx, CR2);
  863. /* Get the TIMx CCMR2 register value */
  864. tmpccmr2 = LL_TIM_ReadReg(TIMx, CCMR2);
  865. /* Reset Capture/Compare selection Bits */
  866. CLEAR_BIT(tmpccmr2, TIM_CCMR2_CC4S);
  867. /* Select the Output Compare Mode */
  868. MODIFY_REG(tmpccmr2, TIM_CCMR2_OC4M, TIM_OCInitStruct->OCMode << 8U);
  869. /* Set the Output Compare Polarity */
  870. MODIFY_REG(tmpccer, TIM_CCER_CC4P, TIM_OCInitStruct->OCPolarity << 12U);
  871. /* Set the Output State */
  872. MODIFY_REG(tmpccer, TIM_CCER_CC4E, TIM_OCInitStruct->OCState << 12U);
  873. if (IS_TIM_BREAK_INSTANCE(TIMx))
  874. {
  875. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCNIdleState));
  876. assert_param(IS_LL_TIM_OCIDLESTATE(TIM_OCInitStruct->OCIdleState));
  877. /* Set the Output Idle state */
  878. MODIFY_REG(tmpcr2, TIM_CR2_OIS4, TIM_OCInitStruct->OCIdleState << 6U);
  879. }
  880. /* Write to TIMx CR2 */
  881. LL_TIM_WriteReg(TIMx, CR2, tmpcr2);
  882. /* Write to TIMx CCMR2 */
  883. LL_TIM_WriteReg(TIMx, CCMR2, tmpccmr2);
  884. /* Set the Capture Compare Register value */
  885. LL_TIM_OC_SetCompareCH4(TIMx, TIM_OCInitStruct->CompareValue);
  886. /* Write to TIMx CCER */
  887. LL_TIM_WriteReg(TIMx, CCER, tmpccer);
  888. return SUCCESS;
  889. }
  890. /**
  891. * @brief Configure the TIMx input channel 1.
  892. * @param TIMx Timer Instance
  893. * @param TIM_ICInitStruct pointer to the the TIMx input channel 1 configuration data structure
  894. * @retval An ErrorStatus enumeration value:
  895. * - SUCCESS: TIMx registers are de-initialized
  896. * - ERROR: not applicable
  897. */
  898. static ErrorStatus IC1Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  899. {
  900. /* Check the parameters */
  901. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  902. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  903. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  904. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  905. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  906. /* Disable the Channel 1: Reset the CC1E Bit */
  907. TIMx->CCER &= (uint32_t)~TIM_CCER_CC1E;
  908. /* Select the Input and set the filter and the prescaler value */
  909. MODIFY_REG(TIMx->CCMR1,
  910. (TIM_CCMR1_CC1S | TIM_CCMR1_IC1F | TIM_CCMR1_IC1PSC),
  911. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  912. /* Select the Polarity and set the CC1E Bit */
  913. MODIFY_REG(TIMx->CCER,
  914. (TIM_CCER_CC1P | TIM_CCER_CC1NP),
  915. (TIM_ICInitStruct->ICPolarity | TIM_CCER_CC1E));
  916. return SUCCESS;
  917. }
  918. /**
  919. * @brief Configure the TIMx input channel 2.
  920. * @param TIMx Timer Instance
  921. * @param TIM_ICInitStruct pointer to the the TIMx input channel 2 configuration data structure
  922. * @retval An ErrorStatus enumeration value:
  923. * - SUCCESS: TIMx registers are de-initialized
  924. * - ERROR: not applicable
  925. */
  926. static ErrorStatus IC2Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  927. {
  928. /* Check the parameters */
  929. assert_param(IS_TIM_CC2_INSTANCE(TIMx));
  930. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  931. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  932. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  933. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  934. /* Disable the Channel 2: Reset the CC2E Bit */
  935. TIMx->CCER &= (uint32_t)~TIM_CCER_CC2E;
  936. /* Select the Input and set the filter and the prescaler value */
  937. MODIFY_REG(TIMx->CCMR1,
  938. (TIM_CCMR1_CC2S | TIM_CCMR1_IC2F | TIM_CCMR1_IC2PSC),
  939. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  940. /* Select the Polarity and set the CC2E Bit */
  941. MODIFY_REG(TIMx->CCER,
  942. (TIM_CCER_CC2P | TIM_CCER_CC2NP),
  943. ((TIM_ICInitStruct->ICPolarity << 4U) | TIM_CCER_CC2E));
  944. return SUCCESS;
  945. }
  946. /**
  947. * @brief Configure the TIMx input channel 3.
  948. * @param TIMx Timer Instance
  949. * @param TIM_ICInitStruct pointer to the the TIMx input channel 3 configuration data structure
  950. * @retval An ErrorStatus enumeration value:
  951. * - SUCCESS: TIMx registers are de-initialized
  952. * - ERROR: not applicable
  953. */
  954. static ErrorStatus IC3Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  955. {
  956. /* Check the parameters */
  957. assert_param(IS_TIM_CC3_INSTANCE(TIMx));
  958. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  959. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  960. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  961. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  962. /* Disable the Channel 3: Reset the CC3E Bit */
  963. TIMx->CCER &= (uint32_t)~TIM_CCER_CC3E;
  964. /* Select the Input and set the filter and the prescaler value */
  965. MODIFY_REG(TIMx->CCMR2,
  966. (TIM_CCMR2_CC3S | TIM_CCMR2_IC3F | TIM_CCMR2_IC3PSC),
  967. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 16U);
  968. /* Select the Polarity and set the CC3E Bit */
  969. MODIFY_REG(TIMx->CCER,
  970. (TIM_CCER_CC3P | TIM_CCER_CC3NP),
  971. ((TIM_ICInitStruct->ICPolarity << 8U) | TIM_CCER_CC3E));
  972. return SUCCESS;
  973. }
  974. /**
  975. * @brief Configure the TIMx input channel 4.
  976. * @param TIMx Timer Instance
  977. * @param TIM_ICInitStruct pointer to the the TIMx input channel 4 configuration data structure
  978. * @retval An ErrorStatus enumeration value:
  979. * - SUCCESS: TIMx registers are de-initialized
  980. * - ERROR: not applicable
  981. */
  982. static ErrorStatus IC4Config(TIM_TypeDef *TIMx, LL_TIM_IC_InitTypeDef *TIM_ICInitStruct)
  983. {
  984. /* Check the parameters */
  985. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  986. assert_param(IS_LL_TIM_IC_POLARITY(TIM_ICInitStruct->ICPolarity));
  987. assert_param(IS_LL_TIM_ACTIVEINPUT(TIM_ICInitStruct->ICActiveInput));
  988. assert_param(IS_LL_TIM_ICPSC(TIM_ICInitStruct->ICPrescaler));
  989. assert_param(IS_LL_TIM_IC_FILTER(TIM_ICInitStruct->ICFilter));
  990. /* Disable the Channel 4: Reset the CC4E Bit */
  991. TIMx->CCER &= (uint32_t)~TIM_CCER_CC4E;
  992. /* Select the Input and set the filter and the prescaler value */
  993. MODIFY_REG(TIMx->CCMR2,
  994. (TIM_CCMR2_CC4S | TIM_CCMR2_IC4F | TIM_CCMR2_IC4PSC),
  995. (TIM_ICInitStruct->ICActiveInput | TIM_ICInitStruct->ICFilter | TIM_ICInitStruct->ICPrescaler) >> 8U);
  996. /* Select the Polarity and set the CC4E Bit */
  997. MODIFY_REG(TIMx->CCER,
  998. (TIM_CCER_CC4P),
  999. ((TIM_ICInitStruct->ICPolarity << 12U) | TIM_CCER_CC4E));
  1000. return SUCCESS;
  1001. }
  1002. /**
  1003. * @}
  1004. */
  1005. /**
  1006. * @}
  1007. */
  1008. #endif /* TIM1 || TIM2 || TIM3 || TIM4 || TIM5 || TIM6 || TIM7 || TIM8 || TIM9 || TIM10 || TIM11 || TIM12 || TIM13 || TIM14 || TIM15 || TIM16 || TIM17 */
  1009. /**
  1010. * @}
  1011. */
  1012. #endif /* USE_FULL_LL_DRIVER */
  1013. /************************ (C) COPYRIGHT Puya *****END OF FILE****/