stm32f4xx_ll_tim.c 44 KB

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