You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1176 lines
44 KiB

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