stm32f4xx_ll_tim.c 46 KB

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