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.
 
 
 

1199 lines
45 KiB

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