Projet voilier 4IRA1 Arnaud Vergnet Marino Benassai Bastien Picco Yohan Simard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f1xx_ll_tim.c 44KB

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