stm32f1xx_ll_tim.c 45 KB

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