stm32f1xx_ll_tim.c 45 KB

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