stm32f1xx_ll_tim.c 44 KB

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