1 /**
2 ******************************************************************************
3 * @file stm32n6xx_hal_tim_ex.c
4 * @author MCD Application Team
5 * @brief TIM HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Timer Extended peripheral:
8 * + Time Hall Sensor Interface Initialization
9 * + Time Hall Sensor Interface Start
10 * + Time Complementary signal break and dead time configuration
11 * + Time Master and Slave synchronization configuration
12 * + Time Output Compare/PWM Channel Configuration (for channels 5 and 6)
13 * + Timer remapping capabilities configuration
14 * + Timer encoder index configuration
15 ******************************************************************************
16 * @attention
17 *
18 * Copyright (c) 2023 STMicroelectronics.
19 * All rights reserved.
20 *
21 * This software is licensed under terms that can be found in the LICENSE file
22 * in the root directory of this software component.
23 * If no LICENSE file comes with this software, it is provided AS-IS.
24 *
25 ******************************************************************************
26 @verbatim
27 ==============================================================================
28 ##### TIMER Extended features #####
29 ==============================================================================
30 [..]
31 The Timer Extended features include:
32 (#) Complementary outputs with programmable dead-time for :
33 (++) Output Compare
34 (++) PWM generation (Edge and Center-aligned Mode)
35 (++) One-pulse mode output
36 (#) Synchronization circuit to control the timer with external signals and to
37 interconnect several timers together.
38 (#) Break input to put the timer output signals in reset state or in a known state.
39 (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
40 positioning purposes
41 (#) In case of Pulse on compare, configure pulse length and delay
42 (#) Encoder index configuration
43
44 ##### How to use this driver #####
45 ==============================================================================
46 [..]
47 (#) Initialize the TIM low level resources by implementing the following functions
48 depending on the selected feature:
49 (++) Hall Sensor output : HAL_TIMEx_HallSensor_MspInit()
50
51 (#) Initialize the TIM low level resources :
52 (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
53 (##) TIM pins configuration
54 (+++) Enable the clock for the TIM GPIOs using the following function:
55 __HAL_RCC_GPIOx_CLK_ENABLE();
56 (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
57
58 (#) The external Clock can be configured, if needed (the default clock is the
59 internal clock from the APBx), using the following function:
60 HAL_TIM_ConfigClockSource, the clock configuration should be done before
61 any start function.
62
63 (#) Configure the TIM in the desired functioning mode using one of the
64 initialization function of this driver:
65 (++) HAL_TIMEx_HallSensor_Init() and HAL_TIMEx_ConfigCommutEvent(): to use the
66 Timer Hall Sensor Interface and the commutation event with the corresponding
67 Interrupt and DMA request if needed (Note that One Timer is used to interface
68 with the Hall sensor Interface and another Timer should be used to use
69 the commutation event).
70 (#) In case of Pulse On Compare:
71 (++) HAL_TIMEx_OC_ConfigPulseOnCompare(): to configure pulse width and prescaler
72
73
74 (#) Activate the TIM peripheral using one of the start functions:
75 (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(),
76 HAL_TIMEx_OCN_Start_IT()
77 (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(),
78 HAL_TIMEx_PWMN_Start_IT()
79 (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
80 (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(),
81 HAL_TIMEx_HallSensor_Start_IT().
82
83 @endverbatim
84 ******************************************************************************
85 */
86
87 /* Includes ------------------------------------------------------------------*/
88 #include "stm32n6xx_hal.h"
89
90 /** @addtogroup STM32N6xx_HAL_Driver
91 * @{
92 */
93
94 /** @defgroup TIMEx TIMEx
95 * @brief TIM Extended HAL module driver
96 * @{
97 */
98
99 #ifdef HAL_TIM_MODULE_ENABLED
100
101 /* Private typedef -----------------------------------------------------------*/
102 /* Private define ------------------------------------------------------------*/
103 /* Private constants ---------------------------------------------------------*/
104 /** @defgroup TIMEx_Private_Constants TIM Extended Private Constants
105 * @{
106 */
107 /* Timeout for break input rearm */
108 #define TIM_BREAKINPUT_REARM_TIMEOUT 5UL /* 5 milliseconds */
109 /**
110 * @}
111 */
112 /* End of private constants --------------------------------------------------*/
113
114 /* Private macros ------------------------------------------------------------*/
115 /* Private variables ---------------------------------------------------------*/
116 /* Private function prototypes -----------------------------------------------*/
117 static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma);
118 static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma);
119 static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);
120
121 /* Exported functions --------------------------------------------------------*/
122 /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
123 * @{
124 */
125
126 /** @defgroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions
127 * @brief Timer Hall Sensor functions
128 *
129 @verbatim
130 ==============================================================================
131 ##### Timer Hall Sensor functions #####
132 ==============================================================================
133 [..]
134 This section provides functions allowing to:
135 (+) Initialize and configure TIM HAL Sensor.
136 (+) De-initialize TIM HAL Sensor.
137 (+) Start the Hall Sensor Interface.
138 (+) Stop the Hall Sensor Interface.
139 (+) Start the Hall Sensor Interface and enable interrupts.
140 (+) Stop the Hall Sensor Interface and disable interrupts.
141 (+) Start the Hall Sensor Interface and enable DMA transfers.
142 (+) Stop the Hall Sensor Interface and disable DMA transfers.
143
144 @endverbatim
145 * @{
146 */
147 /**
148 * @brief Initializes the TIM Hall Sensor Interface and initialize the associated handle.
149 * @note When the timer instance is initialized in Hall Sensor Interface mode,
150 * timer channels 1 and channel 2 are reserved and cannot be used for
151 * other purpose.
152 * @param htim TIM Hall Sensor Interface handle
153 * @param sConfig TIM Hall Sensor configuration structure
154 * @retval HAL status
155 */
HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef * htim,const TIM_HallSensor_InitTypeDef * sConfig)156 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, const TIM_HallSensor_InitTypeDef *sConfig)
157 {
158 TIM_OC_InitTypeDef OC_Config;
159
160 /* Check the TIM handle allocation */
161 if (htim == NULL)
162 {
163 return HAL_ERROR;
164 }
165
166 /* Check the parameters */
167 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
168 assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
169 assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
170 assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
171 assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
172 assert_param(IS_TIM_PERIOD(htim, htim->Init.Period));
173 assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
174 assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
175
176 if (htim->State == HAL_TIM_STATE_RESET)
177 {
178 /* Allocate lock resource and initialize it */
179 htim->Lock = HAL_UNLOCKED;
180
181 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
182 /* Reset interrupt callbacks to legacy week callbacks */
183 TIM_ResetCallback(htim);
184
185 if (htim->HallSensor_MspInitCallback == NULL)
186 {
187 htim->HallSensor_MspInitCallback = HAL_TIMEx_HallSensor_MspInit;
188 }
189 /* Init the low level hardware : GPIO, CLOCK, NVIC */
190 htim->HallSensor_MspInitCallback(htim);
191 #else
192 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
193 HAL_TIMEx_HallSensor_MspInit(htim);
194 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
195 }
196
197 /* Set the TIM state */
198 htim->State = HAL_TIM_STATE_BUSY;
199
200 /* Configure the Time base in the Encoder Mode */
201 TIM_Base_SetConfig(htim->Instance, &htim->Init);
202
203 /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
204 TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
205
206 /* Reset the IC1PSC Bits */
207 htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
208 /* Set the IC1PSC value */
209 htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
210
211 /* Enable the Hall sensor interface (XOR function of the three inputs) */
212 htim->Instance->CR2 |= TIM_CR2_TI1S;
213
214 /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
215 htim->Instance->SMCR &= ~TIM_SMCR_TS;
216 htim->Instance->SMCR |= TIM_TS_TI1F_ED;
217
218 /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
219 htim->Instance->SMCR &= ~TIM_SMCR_SMS;
220 htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
221
222 /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
223 OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
224 OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
225 OC_Config.OCMode = TIM_OCMODE_PWM2;
226 OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
227 OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
228 OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
229 OC_Config.Pulse = sConfig->Commutation_Delay;
230
231 TIM_OC2_SetConfig(htim->Instance, &OC_Config);
232
233 /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
234 register to 101 */
235 htim->Instance->CR2 &= ~TIM_CR2_MMS;
236 htim->Instance->CR2 |= TIM_TRGO_OC2REF;
237
238 /* Initialize the DMA burst operation state */
239 htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
240
241 /* Initialize the TIM channels state */
242 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
243 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
244 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
245 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
246
247 /* Initialize the TIM state*/
248 htim->State = HAL_TIM_STATE_READY;
249
250 return HAL_OK;
251 }
252
253 /**
254 * @brief DeInitializes the TIM Hall Sensor interface
255 * @param htim TIM Hall Sensor Interface handle
256 * @retval HAL status
257 */
HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef * htim)258 HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
259 {
260 /* Check the parameters */
261 assert_param(IS_TIM_INSTANCE(htim->Instance));
262
263 htim->State = HAL_TIM_STATE_BUSY;
264
265 /* Disable the TIM Peripheral Clock */
266 __HAL_TIM_DISABLE(htim);
267
268 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
269 if (htim->HallSensor_MspDeInitCallback == NULL)
270 {
271 htim->HallSensor_MspDeInitCallback = HAL_TIMEx_HallSensor_MspDeInit;
272 }
273 /* DeInit the low level hardware */
274 htim->HallSensor_MspDeInitCallback(htim);
275 #else
276 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
277 HAL_TIMEx_HallSensor_MspDeInit(htim);
278 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
279
280 /* Change the DMA burst operation state */
281 htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
282
283 /* Change the TIM channels state */
284 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
285 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
286 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
287 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
288
289 /* Change TIM state */
290 htim->State = HAL_TIM_STATE_RESET;
291
292 /* Release Lock */
293 __HAL_UNLOCK(htim);
294
295 return HAL_OK;
296 }
297
298 /**
299 * @brief Initializes the TIM Hall Sensor MSP.
300 * @param htim TIM Hall Sensor Interface handle
301 * @retval None
302 */
HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef * htim)303 __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
304 {
305 /* Prevent unused argument(s) compilation warning */
306 UNUSED(htim);
307
308 /* NOTE : This function should not be modified, when the callback is needed,
309 the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
310 */
311 }
312
313 /**
314 * @brief DeInitializes TIM Hall Sensor MSP.
315 * @param htim TIM Hall Sensor Interface handle
316 * @retval None
317 */
HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef * htim)318 __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
319 {
320 /* Prevent unused argument(s) compilation warning */
321 UNUSED(htim);
322
323 /* NOTE : This function should not be modified, when the callback is needed,
324 the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
325 */
326 }
327
328 /**
329 * @brief Starts the TIM Hall Sensor Interface.
330 * @param htim TIM Hall Sensor Interface handle
331 * @retval HAL status
332 */
HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef * htim)333 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
334 {
335 uint32_t tmpsmcr;
336 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
337 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
338 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
339 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
340
341 /* Check the parameters */
342 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
343
344 /* Check the TIM channels state */
345 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
346 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
347 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
348 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
349 {
350 return HAL_ERROR;
351 }
352
353 /* Set the TIM channels state */
354 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
355 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
356 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
357 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
358
359 /* Enable the Input Capture channel 1
360 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
361 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
362 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
363
364 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
365 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
366 {
367 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
368 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
369 {
370 __HAL_TIM_ENABLE(htim);
371 }
372 }
373 else
374 {
375 __HAL_TIM_ENABLE(htim);
376 }
377
378 /* Return function status */
379 return HAL_OK;
380 }
381
382 /**
383 * @brief Stops the TIM Hall sensor Interface.
384 * @param htim TIM Hall Sensor Interface handle
385 * @retval HAL status
386 */
HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef * htim)387 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
388 {
389 /* Check the parameters */
390 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
391
392 /* Disable the Input Capture channels 1, 2 and 3
393 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
394 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
395 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
396
397 /* Disable the Peripheral */
398 __HAL_TIM_DISABLE(htim);
399
400 /* Set the TIM channels state */
401 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
402 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
403 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
404 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
405
406 /* Return function status */
407 return HAL_OK;
408 }
409
410 /**
411 * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
412 * @param htim TIM Hall Sensor Interface handle
413 * @retval HAL status
414 */
HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef * htim)415 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
416 {
417 uint32_t tmpsmcr;
418 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
419 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
420 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
421 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
422
423 /* Check the parameters */
424 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
425
426 /* Check the TIM channels state */
427 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
428 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
429 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
430 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
431 {
432 return HAL_ERROR;
433 }
434
435 /* Set the TIM channels state */
436 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
437 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
438 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
439 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
440
441 /* Enable the capture compare Interrupts 1 event */
442 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
443
444 /* Enable the Input Capture channel 1
445 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
446 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
447 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
448
449 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
450 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
451 {
452 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
453 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
454 {
455 __HAL_TIM_ENABLE(htim);
456 }
457 }
458 else
459 {
460 __HAL_TIM_ENABLE(htim);
461 }
462
463 /* Return function status */
464 return HAL_OK;
465 }
466
467 /**
468 * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
469 * @param htim TIM Hall Sensor Interface handle
470 * @retval HAL status
471 */
HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef * htim)472 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
473 {
474 /* Check the parameters */
475 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
476
477 /* Disable the Input Capture channel 1
478 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
479 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
480 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
481
482 /* Disable the capture compare Interrupts event */
483 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
484
485 /* Disable the Peripheral */
486 __HAL_TIM_DISABLE(htim);
487
488 /* Set the TIM channels state */
489 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
490 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
491 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
492 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
493
494 /* Return function status */
495 return HAL_OK;
496 }
497
498 /**
499 * @brief Starts the TIM Hall Sensor Interface in DMA mode.
500 * @param htim TIM Hall Sensor Interface handle
501 * @param pData The destination Buffer address.
502 * @param Length The length of data to be transferred from TIM peripheral to memory.
503 * @retval HAL status
504 */
HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef * htim,uint32_t * pData,uint16_t Length)505 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
506 {
507 uint32_t tmpsmcr;
508 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
509 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
510
511 /* Check the parameters */
512 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
513
514 /* Set the TIM channel state */
515 if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
516 || (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY))
517 {
518 return HAL_BUSY;
519 }
520 else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
521 && (complementary_channel_1_state == HAL_TIM_CHANNEL_STATE_READY))
522 {
523 if ((pData == NULL) || (Length == 0U))
524 {
525 return HAL_ERROR;
526 }
527 else
528 {
529 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
530 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
531 }
532 }
533 else
534 {
535 return HAL_ERROR;
536 }
537
538 /* Enable the Input Capture channel 1
539 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
540 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
541 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
542
543 /* Set the DMA Input Capture 1 Callbacks */
544 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
545 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
546 /* Set the DMA error callback */
547 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
548
549 /* Enable the DMA channel for Capture 1*/
550 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length) != HAL_OK)
551 {
552 /* Return error status */
553 return HAL_ERROR;
554 }
555 /* Enable the capture compare 1 Interrupt */
556 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
557
558 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
559 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
560 {
561 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
562 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
563 {
564 __HAL_TIM_ENABLE(htim);
565 }
566 }
567 else
568 {
569 __HAL_TIM_ENABLE(htim);
570 }
571
572 /* Return function status */
573 return HAL_OK;
574 }
575
576 /**
577 * @brief Stops the TIM Hall Sensor Interface in DMA mode.
578 * @param htim TIM Hall Sensor Interface handle
579 * @retval HAL status
580 */
HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef * htim)581 HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
582 {
583 /* Check the parameters */
584 assert_param(IS_TIM_HALL_SENSOR_INTERFACE_INSTANCE(htim->Instance));
585
586 /* Disable the Input Capture channel 1
587 (in the Hall Sensor Interface the three possible channels that can be used are TIM_CHANNEL_1,
588 TIM_CHANNEL_2 and TIM_CHANNEL_3) */
589 TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
590
591
592 /* Disable the capture compare Interrupts 1 event */
593 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
594
595 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
596
597 /* Disable the Peripheral */
598 __HAL_TIM_DISABLE(htim);
599
600 /* Set the TIM channel state */
601 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
602 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
603
604 /* Return function status */
605 return HAL_OK;
606 }
607
608 /**
609 * @}
610 */
611
612 /** @defgroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions
613 * @brief Timer Complementary Output Compare functions
614 *
615 @verbatim
616 ==============================================================================
617 ##### Timer Complementary Output Compare functions #####
618 ==============================================================================
619 [..]
620 This section provides functions allowing to:
621 (+) Start the Complementary Output Compare/PWM.
622 (+) Stop the Complementary Output Compare/PWM.
623 (+) Start the Complementary Output Compare/PWM and enable interrupts.
624 (+) Stop the Complementary Output Compare/PWM and disable interrupts.
625 (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
626 (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
627
628 @endverbatim
629 * @{
630 */
631
632 /**
633 * @brief Starts the TIM Output Compare signal generation on the complementary
634 * output.
635 * @param htim TIM Output Compare handle
636 * @param Channel TIM Channel to be enabled
637 * This parameter can be one of the following values:
638 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
639 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
640 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
641 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
642 * @retval HAL status
643 */
HAL_TIMEx_OCN_Start(TIM_HandleTypeDef * htim,uint32_t Channel)644 HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
645 {
646 uint32_t tmpsmcr;
647
648 /* Check the parameters */
649 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
650
651 /* Check the TIM complementary channel state */
652 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
653 {
654 return HAL_ERROR;
655 }
656
657 /* Set the TIM complementary channel state */
658 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
659
660 /* Enable the Capture compare channel N */
661 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
662
663 /* Enable the Main Output */
664 __HAL_TIM_MOE_ENABLE(htim);
665
666 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
667 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
668 {
669 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
670 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
671 {
672 __HAL_TIM_ENABLE(htim);
673 }
674 }
675 else
676 {
677 __HAL_TIM_ENABLE(htim);
678 }
679
680 /* Return function status */
681 return HAL_OK;
682 }
683
684 /**
685 * @brief Stops the TIM Output Compare signal generation on the complementary
686 * output.
687 * @param htim TIM handle
688 * @param Channel TIM Channel to be disabled
689 * This parameter can be one of the following values:
690 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
691 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
692 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
693 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
694 * @retval HAL status
695 */
HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)696 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
697 {
698 /* Check the parameters */
699 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
700
701 /* Disable the Capture compare channel N */
702 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
703
704 /* Disable the Main Output */
705 __HAL_TIM_MOE_DISABLE(htim);
706
707 /* Disable the Peripheral */
708 __HAL_TIM_DISABLE(htim);
709
710 /* Set the TIM complementary channel state */
711 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
712
713 /* Return function status */
714 return HAL_OK;
715 }
716
717 /**
718 * @brief Starts the TIM Output Compare signal generation in interrupt mode
719 * on the complementary output.
720 * @param htim TIM OC handle
721 * @param Channel TIM Channel to be enabled
722 * This parameter can be one of the following values:
723 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
724 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
725 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
726 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
727 * @retval HAL status
728 */
HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)729 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
730 {
731 HAL_StatusTypeDef status = HAL_OK;
732 uint32_t tmpsmcr;
733
734 /* Check the parameters */
735 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
736
737 /* Check the TIM complementary channel state */
738 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
739 {
740 return HAL_ERROR;
741 }
742
743 /* Set the TIM complementary channel state */
744 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
745
746 switch (Channel)
747 {
748 case TIM_CHANNEL_1:
749 {
750 /* Enable the TIM Output Compare interrupt */
751 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
752 break;
753 }
754
755 case TIM_CHANNEL_2:
756 {
757 /* Enable the TIM Output Compare interrupt */
758 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
759 break;
760 }
761
762 case TIM_CHANNEL_3:
763 {
764 /* Enable the TIM Output Compare interrupt */
765 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
766 break;
767 }
768
769
770 case TIM_CHANNEL_4:
771 {
772 /* Enable the TIM Output Compare interrupt */
773 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
774 break;
775 }
776
777 default:
778 status = HAL_ERROR;
779 break;
780 }
781
782 if (status == HAL_OK)
783 {
784 /* Enable the TIM Break interrupt */
785 __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
786
787 /* Enable the Capture compare channel N */
788 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
789
790 /* Enable the Main Output */
791 __HAL_TIM_MOE_ENABLE(htim);
792
793 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
794 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
795 {
796 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
797 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
798 {
799 __HAL_TIM_ENABLE(htim);
800 }
801 }
802 else
803 {
804 __HAL_TIM_ENABLE(htim);
805 }
806 }
807
808 /* Return function status */
809 return status;
810 }
811
812 /**
813 * @brief Stops the TIM Output Compare signal generation in interrupt mode
814 * on the complementary output.
815 * @param htim TIM Output Compare handle
816 * @param Channel TIM Channel to be disabled
817 * This parameter can be one of the following values:
818 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
819 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
820 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
821 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
822 * @retval HAL status
823 */
HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)824 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
825 {
826 HAL_StatusTypeDef status = HAL_OK;
827 uint32_t tmpccer;
828
829 /* Check the parameters */
830 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
831
832 switch (Channel)
833 {
834 case TIM_CHANNEL_1:
835 {
836 /* Disable the TIM Output Compare interrupt */
837 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
838 break;
839 }
840
841 case TIM_CHANNEL_2:
842 {
843 /* Disable the TIM Output Compare interrupt */
844 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
845 break;
846 }
847
848 case TIM_CHANNEL_3:
849 {
850 /* Disable the TIM Output Compare interrupt */
851 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
852 break;
853 }
854
855 case TIM_CHANNEL_4:
856 {
857 /* Disable the TIM Output Compare interrupt */
858 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
859 break;
860 }
861
862 default:
863 status = HAL_ERROR;
864 break;
865 }
866
867 if (status == HAL_OK)
868 {
869 /* Disable the Capture compare channel N */
870 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
871
872 /* Disable the TIM Break interrupt (only if no more channel is active) */
873 tmpccer = htim->Instance->CCER;
874 if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
875 {
876 __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
877 }
878
879 /* Disable the Main Output */
880 __HAL_TIM_MOE_DISABLE(htim);
881
882 /* Disable the Peripheral */
883 __HAL_TIM_DISABLE(htim);
884
885 /* Set the TIM complementary channel state */
886 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
887 }
888
889 /* Return function status */
890 return status;
891 }
892
893 /**
894 * @brief Starts the TIM Output Compare signal generation in DMA mode
895 * on the complementary output.
896 * @param htim TIM Output Compare handle
897 * @param Channel TIM Channel to be enabled
898 * This parameter can be one of the following values:
899 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
900 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
901 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
902 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
903 * @param pData The source Buffer address.
904 * @param Length The length of data to be transferred from memory to TIM peripheral
905 * @retval HAL status
906 */
HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,const uint32_t * pData,uint16_t Length)907 HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
908 uint16_t Length)
909 {
910 HAL_StatusTypeDef status = HAL_OK;
911 uint32_t tmpsmcr;
912
913 /* Check the parameters */
914 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
915
916 /* Set the TIM complementary channel state */
917 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
918 {
919 return HAL_BUSY;
920 }
921 else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
922 {
923 if ((pData == NULL) || (Length == 0U))
924 {
925 return HAL_ERROR;
926 }
927 else
928 {
929 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
930 }
931 }
932 else
933 {
934 return HAL_ERROR;
935 }
936
937 switch (Channel)
938 {
939 case TIM_CHANNEL_1:
940 {
941 /* Set the DMA compare callbacks */
942 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
943 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
944
945 /* Set the DMA error callback */
946 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
947
948 /* Enable the DMA channel */
949 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
950 Length) != HAL_OK)
951 {
952 /* Return error status */
953 return HAL_ERROR;
954 }
955 /* Enable the TIM Output Compare DMA request */
956 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
957 break;
958 }
959
960 case TIM_CHANNEL_2:
961 {
962 /* Set the DMA compare callbacks */
963 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
964 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
965
966 /* Set the DMA error callback */
967 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
968
969 /* Enable the DMA channel */
970 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
971 Length) != HAL_OK)
972 {
973 /* Return error status */
974 return HAL_ERROR;
975 }
976 /* Enable the TIM Output Compare DMA request */
977 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
978 break;
979 }
980
981 case TIM_CHANNEL_3:
982 {
983 /* Set the DMA compare callbacks */
984 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
985 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
986
987 /* Set the DMA error callback */
988 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
989
990 /* Enable the DMA channel */
991 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
992 Length) != HAL_OK)
993 {
994 /* Return error status */
995 return HAL_ERROR;
996 }
997 /* Enable the TIM Output Compare DMA request */
998 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
999 break;
1000 }
1001
1002 case TIM_CHANNEL_4:
1003 {
1004 /* Set the DMA compare callbacks */
1005 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1006 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1007
1008 /* Set the DMA error callback */
1009 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAErrorCCxN ;
1010
1011 /* Enable the DMA channel */
1012 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
1013 Length) != HAL_OK)
1014 {
1015 /* Return error status */
1016 return HAL_ERROR;
1017 }
1018 /* Enable the TIM Output Compare DMA request */
1019 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1020 break;
1021 }
1022
1023 default:
1024 status = HAL_ERROR;
1025 break;
1026 }
1027
1028 if (status == HAL_OK)
1029 {
1030 /* Enable the Capture compare channel N */
1031 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1032
1033 /* Enable the Main Output */
1034 __HAL_TIM_MOE_ENABLE(htim);
1035
1036 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1037 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1038 {
1039 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1040 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1041 {
1042 __HAL_TIM_ENABLE(htim);
1043 }
1044 }
1045 else
1046 {
1047 __HAL_TIM_ENABLE(htim);
1048 }
1049 }
1050
1051 /* Return function status */
1052 return status;
1053 }
1054
1055 /**
1056 * @brief Stops the TIM Output Compare signal generation in DMA mode
1057 * on the complementary output.
1058 * @param htim TIM Output Compare handle
1059 * @param Channel TIM Channel to be disabled
1060 * This parameter can be one of the following values:
1061 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1062 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1063 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1064 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1065 * @retval HAL status
1066 */
HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)1067 HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1068 {
1069 HAL_StatusTypeDef status = HAL_OK;
1070
1071 /* Check the parameters */
1072 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1073
1074 switch (Channel)
1075 {
1076 case TIM_CHANNEL_1:
1077 {
1078 /* Disable the TIM Output Compare DMA request */
1079 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1080 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1081 break;
1082 }
1083
1084 case TIM_CHANNEL_2:
1085 {
1086 /* Disable the TIM Output Compare DMA request */
1087 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1088 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1089 break;
1090 }
1091
1092 case TIM_CHANNEL_3:
1093 {
1094 /* Disable the TIM Output Compare DMA request */
1095 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1096 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1097 break;
1098 }
1099
1100 case TIM_CHANNEL_4:
1101 {
1102 /* Disable the TIM Output Compare interrupt */
1103 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1104 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1105 break;
1106 }
1107
1108 default:
1109 status = HAL_ERROR;
1110 break;
1111 }
1112
1113 if (status == HAL_OK)
1114 {
1115 /* Disable the Capture compare channel N */
1116 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1117
1118 /* Disable the Main Output */
1119 __HAL_TIM_MOE_DISABLE(htim);
1120
1121 /* Disable the Peripheral */
1122 __HAL_TIM_DISABLE(htim);
1123
1124 /* Set the TIM complementary channel state */
1125 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1126 }
1127
1128 /* Return function status */
1129 return status;
1130 }
1131
1132 /**
1133 * @}
1134 */
1135
1136 /** @defgroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions
1137 * @brief Timer Complementary PWM functions
1138 *
1139 @verbatim
1140 ==============================================================================
1141 ##### Timer Complementary PWM functions #####
1142 ==============================================================================
1143 [..]
1144 This section provides functions allowing to:
1145 (+) Start the Complementary PWM.
1146 (+) Stop the Complementary PWM.
1147 (+) Start the Complementary PWM and enable interrupts.
1148 (+) Stop the Complementary PWM and disable interrupts.
1149 (+) Start the Complementary PWM and enable DMA transfers.
1150 (+) Stop the Complementary PWM and disable DMA transfers.
1151 @endverbatim
1152 * @{
1153 */
1154
1155 /**
1156 * @brief Starts the PWM signal generation on the complementary output.
1157 * @param htim TIM handle
1158 * @param Channel TIM Channel to be enabled
1159 * This parameter can be one of the following values:
1160 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1161 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1162 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1163 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1164 * @retval HAL status
1165 */
HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef * htim,uint32_t Channel)1166 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
1167 {
1168 uint32_t tmpsmcr;
1169
1170 /* Check the parameters */
1171 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1172
1173 /* Check the TIM complementary channel state */
1174 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1175 {
1176 return HAL_ERROR;
1177 }
1178
1179 /* Set the TIM complementary channel state */
1180 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1181
1182 /* Enable the complementary PWM output */
1183 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1184
1185 /* Enable the Main Output */
1186 __HAL_TIM_MOE_ENABLE(htim);
1187
1188 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1189 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1190 {
1191 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1192 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1193 {
1194 __HAL_TIM_ENABLE(htim);
1195 }
1196 }
1197 else
1198 {
1199 __HAL_TIM_ENABLE(htim);
1200 }
1201
1202 /* Return function status */
1203 return HAL_OK;
1204 }
1205
1206 /**
1207 * @brief Stops the PWM signal generation on the complementary output.
1208 * @param htim TIM handle
1209 * @param Channel TIM Channel to be disabled
1210 * This parameter can be one of the following values:
1211 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1212 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1213 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1214 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1215 * @retval HAL status
1216 */
HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef * htim,uint32_t Channel)1217 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
1218 {
1219 /* Check the parameters */
1220 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1221
1222 /* Disable the complementary PWM output */
1223 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1224
1225 /* Disable the Main Output */
1226 __HAL_TIM_MOE_DISABLE(htim);
1227
1228 /* Disable the Peripheral */
1229 __HAL_TIM_DISABLE(htim);
1230
1231 /* Set the TIM complementary channel state */
1232 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1233
1234 /* Return function status */
1235 return HAL_OK;
1236 }
1237
1238 /**
1239 * @brief Starts the PWM signal generation in interrupt mode on the
1240 * complementary output.
1241 * @param htim TIM handle
1242 * @param Channel TIM Channel to be disabled
1243 * This parameter can be one of the following values:
1244 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1245 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1246 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1247 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1248 * @retval HAL status
1249 */
HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef * htim,uint32_t Channel)1250 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1251 {
1252 HAL_StatusTypeDef status = HAL_OK;
1253 uint32_t tmpsmcr;
1254
1255 /* Check the parameters */
1256 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1257
1258 /* Check the TIM complementary channel state */
1259 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
1260 {
1261 return HAL_ERROR;
1262 }
1263
1264 /* Set the TIM complementary channel state */
1265 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1266
1267 switch (Channel)
1268 {
1269 case TIM_CHANNEL_1:
1270 {
1271 /* Enable the TIM Capture/Compare 1 interrupt */
1272 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1273 break;
1274 }
1275
1276 case TIM_CHANNEL_2:
1277 {
1278 /* Enable the TIM Capture/Compare 2 interrupt */
1279 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1280 break;
1281 }
1282
1283 case TIM_CHANNEL_3:
1284 {
1285 /* Enable the TIM Capture/Compare 3 interrupt */
1286 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
1287 break;
1288 }
1289
1290 case TIM_CHANNEL_4:
1291 {
1292 /* Enable the TIM Capture/Compare 4 interrupt */
1293 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
1294 break;
1295 }
1296
1297 default:
1298 status = HAL_ERROR;
1299 break;
1300 }
1301
1302 if (status == HAL_OK)
1303 {
1304 /* Enable the TIM Break interrupt */
1305 __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
1306
1307 /* Enable the complementary PWM output */
1308 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1309
1310 /* Enable the Main Output */
1311 __HAL_TIM_MOE_ENABLE(htim);
1312
1313 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1314 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1315 {
1316 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1317 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1318 {
1319 __HAL_TIM_ENABLE(htim);
1320 }
1321 }
1322 else
1323 {
1324 __HAL_TIM_ENABLE(htim);
1325 }
1326 }
1327
1328 /* Return function status */
1329 return status;
1330 }
1331
1332 /**
1333 * @brief Stops the PWM signal generation in interrupt mode on the
1334 * complementary output.
1335 * @param htim TIM handle
1336 * @param Channel TIM Channel to be disabled
1337 * This parameter can be one of the following values:
1338 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1339 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1340 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1341 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1342 * @retval HAL status
1343 */
HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef * htim,uint32_t Channel)1344 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
1345 {
1346 HAL_StatusTypeDef status = HAL_OK;
1347 uint32_t tmpccer;
1348
1349 /* Check the parameters */
1350 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1351
1352 switch (Channel)
1353 {
1354 case TIM_CHANNEL_1:
1355 {
1356 /* Disable the TIM Capture/Compare 1 interrupt */
1357 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1358 break;
1359 }
1360
1361 case TIM_CHANNEL_2:
1362 {
1363 /* Disable the TIM Capture/Compare 2 interrupt */
1364 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1365 break;
1366 }
1367
1368 case TIM_CHANNEL_3:
1369 {
1370 /* Disable the TIM Capture/Compare 3 interrupt */
1371 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
1372 break;
1373 }
1374
1375 case TIM_CHANNEL_4:
1376 {
1377 /* Disable the TIM Capture/Compare 4 interrupt */
1378 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
1379 break;
1380 }
1381
1382 default:
1383 status = HAL_ERROR;
1384 break;
1385 }
1386
1387 if (status == HAL_OK)
1388 {
1389 /* Disable the complementary PWM output */
1390 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1391
1392 /* Disable the TIM Break interrupt (only if no more channel is active) */
1393 tmpccer = htim->Instance->CCER;
1394 if ((tmpccer & TIM_CCER_CCxNE_MASK) == (uint32_t)RESET)
1395 {
1396 __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
1397 }
1398
1399 /* Disable the Main Output */
1400 __HAL_TIM_MOE_DISABLE(htim);
1401
1402 /* Disable the Peripheral */
1403 __HAL_TIM_DISABLE(htim);
1404
1405 /* Set the TIM complementary channel state */
1406 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1407 }
1408
1409 /* Return function status */
1410 return status;
1411 }
1412
1413 /**
1414 * @brief Starts the TIM PWM signal generation in DMA mode on the
1415 * complementary output
1416 * @param htim TIM handle
1417 * @param Channel TIM Channel to be enabled
1418 * This parameter can be one of the following values:
1419 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1420 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1421 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1422 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1423 * @param pData The source Buffer address.
1424 * @param Length The length of data to be transferred from memory to TIM peripheral
1425 * @retval HAL status
1426 */
HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef * htim,uint32_t Channel,const uint32_t * pData,uint16_t Length)1427 HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
1428 uint16_t Length)
1429 {
1430 HAL_StatusTypeDef status = HAL_OK;
1431 uint32_t tmpsmcr;
1432
1433 /* Check the parameters */
1434 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1435
1436 /* Set the TIM complementary channel state */
1437 if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
1438 {
1439 return HAL_BUSY;
1440 }
1441 else if (TIM_CHANNEL_N_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
1442 {
1443 if ((pData == NULL) || (Length == 0U))
1444 {
1445 return HAL_ERROR;
1446 }
1447 else
1448 {
1449 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
1450 }
1451 }
1452 else
1453 {
1454 return HAL_ERROR;
1455 }
1456
1457 switch (Channel)
1458 {
1459 case TIM_CHANNEL_1:
1460 {
1461 /* Set the DMA compare callbacks */
1462 htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1463 htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1464
1465 /* Set the DMA error callback */
1466 htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAErrorCCxN ;
1467
1468 /* Enable the DMA channel */
1469 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
1470 Length) != HAL_OK)
1471 {
1472 /* Return error status */
1473 return HAL_ERROR;
1474 }
1475 /* Enable the TIM Capture/Compare 1 DMA request */
1476 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
1477 break;
1478 }
1479
1480 case TIM_CHANNEL_2:
1481 {
1482 /* Set the DMA compare callbacks */
1483 htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1484 htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1485
1486 /* Set the DMA error callback */
1487 htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAErrorCCxN ;
1488
1489 /* Enable the DMA channel */
1490 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
1491 Length) != HAL_OK)
1492 {
1493 /* Return error status */
1494 return HAL_ERROR;
1495 }
1496 /* Enable the TIM Capture/Compare 2 DMA request */
1497 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
1498 break;
1499 }
1500
1501 case TIM_CHANNEL_3:
1502 {
1503 /* Set the DMA compare callbacks */
1504 htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1505 htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1506
1507 /* Set the DMA error callback */
1508 htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAErrorCCxN ;
1509
1510 /* Enable the DMA channel */
1511 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
1512 Length) != HAL_OK)
1513 {
1514 /* Return error status */
1515 return HAL_ERROR;
1516 }
1517 /* Enable the TIM Capture/Compare 3 DMA request */
1518 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
1519 break;
1520 }
1521
1522 case TIM_CHANNEL_4:
1523 {
1524 /* Set the DMA compare callbacks */
1525 htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseNCplt;
1526 htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
1527
1528 /* Set the DMA error callback */
1529 htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAErrorCCxN ;
1530
1531 /* Enable the DMA channel */
1532 if (TIM_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
1533 Length) != HAL_OK)
1534 {
1535 /* Return error status */
1536 return HAL_ERROR;
1537 }
1538 /* Enable the TIM Capture/Compare 4 DMA request */
1539 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
1540 break;
1541 }
1542
1543 default:
1544 status = HAL_ERROR;
1545 break;
1546 }
1547
1548 if (status == HAL_OK)
1549 {
1550 /* Enable the complementary PWM output */
1551 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
1552
1553 /* Enable the Main Output */
1554 __HAL_TIM_MOE_ENABLE(htim);
1555
1556 /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
1557 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
1558 {
1559 tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
1560 if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
1561 {
1562 __HAL_TIM_ENABLE(htim);
1563 }
1564 }
1565 else
1566 {
1567 __HAL_TIM_ENABLE(htim);
1568 }
1569 }
1570
1571 /* Return function status */
1572 return status;
1573 }
1574
1575 /**
1576 * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
1577 * output
1578 * @param htim TIM handle
1579 * @param Channel TIM Channel to be disabled
1580 * This parameter can be one of the following values:
1581 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1582 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1583 * @arg TIM_CHANNEL_3: TIM Channel 3 selected
1584 * @arg TIM_CHANNEL_4: TIM Channel 4 selected
1585 * @retval HAL status
1586 */
HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef * htim,uint32_t Channel)1587 HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
1588 {
1589 HAL_StatusTypeDef status = HAL_OK;
1590
1591 /* Check the parameters */
1592 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
1593
1594 switch (Channel)
1595 {
1596 case TIM_CHANNEL_1:
1597 {
1598 /* Disable the TIM Capture/Compare 1 DMA request */
1599 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
1600 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
1601 break;
1602 }
1603
1604 case TIM_CHANNEL_2:
1605 {
1606 /* Disable the TIM Capture/Compare 2 DMA request */
1607 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
1608 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
1609 break;
1610 }
1611
1612 case TIM_CHANNEL_3:
1613 {
1614 /* Disable the TIM Capture/Compare 3 DMA request */
1615 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
1616 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
1617 break;
1618 }
1619
1620 case TIM_CHANNEL_4:
1621 {
1622 /* Disable the TIM Capture/Compare 4 DMA request */
1623 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
1624 (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
1625 break;
1626 }
1627
1628 default:
1629 status = HAL_ERROR;
1630 break;
1631 }
1632
1633 if (status == HAL_OK)
1634 {
1635 /* Disable the complementary PWM output */
1636 TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
1637
1638 /* Disable the Main Output */
1639 __HAL_TIM_MOE_DISABLE(htim);
1640
1641 /* Disable the Peripheral */
1642 __HAL_TIM_DISABLE(htim);
1643
1644 /* Set the TIM complementary channel state */
1645 TIM_CHANNEL_N_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
1646 }
1647
1648 /* Return function status */
1649 return status;
1650 }
1651
1652 /**
1653 * @}
1654 */
1655
1656 /** @defgroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions
1657 * @brief Timer Complementary One Pulse functions
1658 *
1659 @verbatim
1660 ==============================================================================
1661 ##### Timer Complementary One Pulse functions #####
1662 ==============================================================================
1663 [..]
1664 This section provides functions allowing to:
1665 (+) Start the Complementary One Pulse generation.
1666 (+) Stop the Complementary One Pulse.
1667 (+) Start the Complementary One Pulse and enable interrupts.
1668 (+) Stop the Complementary One Pulse and disable interrupts.
1669
1670 @endverbatim
1671 * @{
1672 */
1673
1674 /**
1675 * @brief Starts the TIM One Pulse signal generation on the complementary
1676 * output.
1677 * @note OutputChannel must match the pulse output channel chosen when calling
1678 * @ref HAL_TIM_OnePulse_ConfigChannel().
1679 * @param htim TIM One Pulse handle
1680 * @param OutputChannel pulse output channel to enable
1681 * This parameter can be one of the following values:
1682 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1683 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1684 * @retval HAL status
1685 */
HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef * htim,uint32_t OutputChannel)1686 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1687 {
1688 uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1689 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
1690 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
1691 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
1692 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
1693
1694 /* Check the parameters */
1695 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1696
1697 /* Check the TIM channels state */
1698 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1699 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
1700 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1701 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
1702 {
1703 return HAL_ERROR;
1704 }
1705
1706 /* Set the TIM channels state */
1707 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1708 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1709 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1710 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1711
1712 /* Enable the complementary One Pulse output channel and the Input Capture channel */
1713 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1714 TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
1715
1716 /* Enable the Main Output */
1717 __HAL_TIM_MOE_ENABLE(htim);
1718
1719 /* Return function status */
1720 return HAL_OK;
1721 }
1722
1723 /**
1724 * @brief Stops the TIM One Pulse signal generation on the complementary
1725 * output.
1726 * @note OutputChannel must match the pulse output channel chosen when calling
1727 * @ref HAL_TIM_OnePulse_ConfigChannel().
1728 * @param htim TIM One Pulse handle
1729 * @param OutputChannel pulse output channel to disable
1730 * This parameter can be one of the following values:
1731 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1732 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1733 * @retval HAL status
1734 */
HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef * htim,uint32_t OutputChannel)1735 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1736 {
1737 uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1738
1739 /* Check the parameters */
1740 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1741
1742 /* Disable the complementary One Pulse output channel and the Input Capture channel */
1743 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1744 TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
1745
1746 /* Disable the Main Output */
1747 __HAL_TIM_MOE_DISABLE(htim);
1748
1749 /* Disable the Peripheral */
1750 __HAL_TIM_DISABLE(htim);
1751
1752 /* Set the TIM channels state */
1753 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1754 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1755 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1756 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1757
1758 /* Return function status */
1759 return HAL_OK;
1760 }
1761
1762 /**
1763 * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
1764 * complementary channel.
1765 * @note OutputChannel must match the pulse output channel chosen when calling
1766 * @ref HAL_TIM_OnePulse_ConfigChannel().
1767 * @param htim TIM One Pulse handle
1768 * @param OutputChannel pulse output channel to enable
1769 * This parameter can be one of the following values:
1770 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1771 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1772 * @retval HAL status
1773 */
HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef * htim,uint32_t OutputChannel)1774 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1775 {
1776 uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1777 HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
1778 HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
1779 HAL_TIM_ChannelStateTypeDef complementary_channel_1_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_1);
1780 HAL_TIM_ChannelStateTypeDef complementary_channel_2_state = TIM_CHANNEL_N_STATE_GET(htim, TIM_CHANNEL_2);
1781
1782 /* Check the parameters */
1783 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1784
1785 /* Check the TIM channels state */
1786 if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1787 || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
1788 || (complementary_channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
1789 || (complementary_channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
1790 {
1791 return HAL_ERROR;
1792 }
1793
1794 /* Set the TIM channels state */
1795 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1796 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1797 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
1798 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
1799
1800 /* Enable the TIM Capture/Compare 1 interrupt */
1801 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
1802
1803 /* Enable the TIM Capture/Compare 2 interrupt */
1804 __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
1805
1806 /* Enable the complementary One Pulse output channel and the Input Capture channel */
1807 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
1808 TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_ENABLE);
1809
1810 /* Enable the Main Output */
1811 __HAL_TIM_MOE_ENABLE(htim);
1812
1813 /* Return function status */
1814 return HAL_OK;
1815 }
1816
1817 /**
1818 * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
1819 * complementary channel.
1820 * @note OutputChannel must match the pulse output channel chosen when calling
1821 * @ref HAL_TIM_OnePulse_ConfigChannel().
1822 * @param htim TIM One Pulse handle
1823 * @param OutputChannel pulse output channel to disable
1824 * This parameter can be one of the following values:
1825 * @arg TIM_CHANNEL_1: TIM Channel 1 selected
1826 * @arg TIM_CHANNEL_2: TIM Channel 2 selected
1827 * @retval HAL status
1828 */
HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef * htim,uint32_t OutputChannel)1829 HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
1830 {
1831 uint32_t input_channel = (OutputChannel == TIM_CHANNEL_1) ? TIM_CHANNEL_2 : TIM_CHANNEL_1;
1832
1833 /* Check the parameters */
1834 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
1835
1836 /* Disable the TIM Capture/Compare 1 interrupt */
1837 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
1838
1839 /* Disable the TIM Capture/Compare 2 interrupt */
1840 __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
1841
1842 /* Disable the complementary One Pulse output channel and the Input Capture channel */
1843 TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
1844 TIM_CCxChannelCmd(htim->Instance, input_channel, TIM_CCx_DISABLE);
1845
1846 /* Disable the Main Output */
1847 __HAL_TIM_MOE_DISABLE(htim);
1848
1849 /* Disable the Peripheral */
1850 __HAL_TIM_DISABLE(htim);
1851
1852 /* Set the TIM channels state */
1853 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1854 TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1855 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
1856 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
1857
1858 /* Return function status */
1859 return HAL_OK;
1860 }
1861
1862 /**
1863 * @}
1864 */
1865
1866 /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
1867 * @brief Peripheral Control functions
1868 *
1869 @verbatim
1870 ==============================================================================
1871 ##### Peripheral Control functions #####
1872 ==============================================================================
1873 [..]
1874 This section provides functions allowing to:
1875 (+) Configure the commutation event in case of use of the Hall sensor interface.
1876 (+) Configure Output channels for OC and PWM mode.
1877
1878 (+) Configure Complementary channels, break features and dead time.
1879 (+) Configure Master synchronization.
1880 (+) Configure timer remapping capabilities.
1881 (+) Select timer input source.
1882 (+) Enable or disable channel grouping.
1883 (+) Configure Pulse on compare.
1884 (+) Configure Encoder index.
1885
1886 @endverbatim
1887 * @{
1888 */
1889
1890 /**
1891 * @brief Configure the TIM commutation event sequence.
1892 * @note This function is mandatory to use the commutation event in order to
1893 * update the configuration at each commutation detection on the TRGI input of the Timer,
1894 * the typical use of this feature is with the use of another Timer(interface Timer)
1895 * configured in Hall sensor interface, this interface Timer will generate the
1896 * commutation at its TRGO output (connected to Timer used in this function) each time
1897 * the TI1 of the Interface Timer detect a commutation at its input TI1.
1898 * @param htim TIM handle
1899 * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1900 * This parameter can be one of the following values:
1901 * @arg TIM_TS_ITR0: Internal trigger 0 selected
1902 * @arg TIM_TS_ITR1: Internal trigger 1 selected
1903 * @arg TIM_TS_ITR2: Internal trigger 2 selected
1904 * @arg TIM_TS_ITR3: Internal trigger 3 selected
1905 * @arg TIM_TS_ITR4: Internal trigger 4 selected
1906 * @arg TIM_TS_ITR5: Internal trigger 5 selected
1907 * @arg TIM_TS_ITR6: Internal trigger 6 selected
1908 * @arg TIM_TS_ITR7: Internal trigger 7 selected
1909 * @arg TIM_TS_ITR8: Internal trigger 8 selected
1910 * @arg TIM_TS_ITR9: Internal trigger 9 selected
1911 * @arg TIM_TS_ITR10: Internal trigger 10 selected
1912 * @arg TIM_TS_ITR11: Internal trigger 11 selected
1913 * @arg TIM_TS_ITR12: Internal trigger 12 selected
1914 * @arg TIM_TS_ITR13: Internal trigger 13 selected
1915 * @arg TIM_TS_NONE: No trigger is needed
1916 * @param CommutationSource the Commutation Event source
1917 * This parameter can be one of the following values:
1918 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1919 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
1920 * @retval HAL status
1921 */
HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef * htim,uint32_t InputTrigger,uint32_t CommutationSource)1922 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
1923 uint32_t CommutationSource)
1924 {
1925 /* Check the parameters */
1926 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1927 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
1928
1929 __HAL_LOCK(htim);
1930
1931 if (CommutationSource == TIM_COMMUTATION_TRGI)
1932 {
1933 /* Select the Input trigger */
1934 htim->Instance->SMCR &= ~TIM_SMCR_TS;
1935 htim->Instance->SMCR |= InputTrigger;
1936 }
1937
1938 /* Select the Capture Compare preload feature */
1939 htim->Instance->CR2 |= TIM_CR2_CCPC;
1940 /* Select the Commutation event source */
1941 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
1942 htim->Instance->CR2 |= CommutationSource;
1943
1944 /* Disable Commutation Interrupt */
1945 __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
1946
1947 /* Disable Commutation DMA request */
1948 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
1949
1950 __HAL_UNLOCK(htim);
1951
1952 return HAL_OK;
1953 }
1954
1955 /**
1956 * @brief Configure the TIM commutation event sequence with interrupt.
1957 * @note This function is mandatory to use the commutation event in order to
1958 * update the configuration at each commutation detection on the TRGI input of the Timer,
1959 * the typical use of this feature is with the use of another Timer(interface Timer)
1960 * configured in Hall sensor interface, this interface Timer will generate the
1961 * commutation at its TRGO output (connected to Timer used in this function) each time
1962 * the TI1 of the Interface Timer detect a commutation at its input TI1.
1963 * @param htim TIM handle
1964 * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
1965 * This parameter can be one of the following values:
1966 * @arg TIM_TS_ITR0: Internal trigger 0 selected
1967 * @arg TIM_TS_ITR1: Internal trigger 1 selected
1968 * @arg TIM_TS_ITR2: Internal trigger 2 selected
1969 * @arg TIM_TS_ITR3: Internal trigger 3 selected
1970 * @arg TIM_TS_ITR4: Internal trigger 4 selected
1971 * @arg TIM_TS_ITR5: Internal trigger 5 selected
1972 * @arg TIM_TS_ITR6: Internal trigger 6 selected
1973 * @arg TIM_TS_ITR7: Internal trigger 7 selected
1974 * @arg TIM_TS_ITR8: Internal trigger 8 selected
1975 * @arg TIM_TS_ITR9: Internal trigger 9 selected
1976 * @arg TIM_TS_ITR10: Internal trigger 10 selected
1977 * @arg TIM_TS_ITR11: Internal trigger 11 selected
1978 * @arg TIM_TS_ITR12: Internal trigger 12 selected
1979 * @arg TIM_TS_ITR13: Internal trigger 13 selected
1980 * @arg TIM_TS_NONE: No trigger is needed
1981 * @param CommutationSource the Commutation Event source
1982 * This parameter can be one of the following values:
1983 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
1984 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
1985 * @retval HAL status
1986 */
HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef * htim,uint32_t InputTrigger,uint32_t CommutationSource)1987 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
1988 uint32_t CommutationSource)
1989 {
1990 /* Check the parameters */
1991 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
1992 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
1993
1994 __HAL_LOCK(htim);
1995
1996 if (CommutationSource == TIM_COMMUTATION_TRGI)
1997 {
1998 /* Select the Input trigger */
1999 htim->Instance->SMCR &= ~TIM_SMCR_TS;
2000 htim->Instance->SMCR |= InputTrigger;
2001 }
2002
2003 /* Select the Capture Compare preload feature */
2004 htim->Instance->CR2 |= TIM_CR2_CCPC;
2005 /* Select the Commutation event source */
2006 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
2007 htim->Instance->CR2 |= CommutationSource;
2008
2009 /* Disable Commutation DMA request */
2010 __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
2011
2012 /* Enable the Commutation Interrupt */
2013 __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
2014
2015 __HAL_UNLOCK(htim);
2016
2017 return HAL_OK;
2018 }
2019
2020 /**
2021 * @brief Configure the TIM commutation event sequence with DMA.
2022 * @note This function is mandatory to use the commutation event in order to
2023 * update the configuration at each commutation detection on the TRGI input of the Timer,
2024 * the typical use of this feature is with the use of another Timer(interface Timer)
2025 * configured in Hall sensor interface, this interface Timer will generate the
2026 * commutation at its TRGO output (connected to Timer used in this function) each time
2027 * the TI1 of the Interface Timer detect a commutation at its input TI1.
2028 * @note The user should configure the DMA in his own software, in This function only the COMDE bit is set
2029 * @param htim TIM handle
2030 * @param InputTrigger the Internal trigger corresponding to the Timer Interfacing with the Hall sensor
2031 * This parameter can be one of the following values:
2032 * @arg TIM_TS_ITR0: Internal trigger 0 selected
2033 * @arg TIM_TS_ITR1: Internal trigger 1 selected
2034 * @arg TIM_TS_ITR2: Internal trigger 2 selected
2035 * @arg TIM_TS_ITR3: Internal trigger 3 selected
2036 * @arg TIM_TS_ITR4: Internal trigger 4 selected
2037 * @arg TIM_TS_ITR5: Internal trigger 5 selected
2038 * @arg TIM_TS_ITR6: Internal trigger 6 selected
2039 * @arg TIM_TS_ITR7: Internal trigger 7 selected
2040 * @arg TIM_TS_ITR8: Internal trigger 8 selected
2041 * @arg TIM_TS_ITR9: Internal trigger 9 selected
2042 * @arg TIM_TS_ITR10: Internal trigger 10 selected
2043 * @arg TIM_TS_ITR11: Internal trigger 11 selected
2044 * @arg TIM_TS_ITR12: Internal trigger 12 selected
2045 * @arg TIM_TS_ITR13: Internal trigger 13 selected
2046 * @arg TIM_TS_NONE: No trigger is needed
2047 * @param CommutationSource the Commutation Event source
2048 * This parameter can be one of the following values:
2049 * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
2050 * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
2051 * @retval HAL status
2052 */
HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef * htim,uint32_t InputTrigger,uint32_t CommutationSource)2053 HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger,
2054 uint32_t CommutationSource)
2055 {
2056 /* Check the parameters */
2057 assert_param(IS_TIM_COMMUTATION_EVENT_INSTANCE(htim->Instance));
2058 assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_INSTANCE(htim->Instance, InputTrigger));
2059
2060 __HAL_LOCK(htim);
2061
2062 if (CommutationSource == TIM_COMMUTATION_TRGI)
2063 {
2064 /* Select the Input trigger */
2065 htim->Instance->SMCR &= ~TIM_SMCR_TS;
2066 htim->Instance->SMCR |= InputTrigger;
2067 }
2068
2069 /* Select the Capture Compare preload feature */
2070 htim->Instance->CR2 |= TIM_CR2_CCPC;
2071 /* Select the Commutation event source */
2072 htim->Instance->CR2 &= ~TIM_CR2_CCUS;
2073 htim->Instance->CR2 |= CommutationSource;
2074
2075 /* Enable the Commutation DMA Request */
2076 /* Set the DMA Commutation Callback */
2077 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
2078 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferHalfCpltCallback = TIMEx_DMACommutationHalfCplt;
2079 /* Set the DMA error callback */
2080 htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
2081
2082 /* Disable Commutation Interrupt */
2083 __HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
2084
2085 /* Enable the Commutation DMA Request */
2086 __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
2087
2088 __HAL_UNLOCK(htim);
2089
2090 return HAL_OK;
2091 }
2092
2093 /**
2094 * @brief Configures the TIM in master mode.
2095 * @param htim TIM handle.
2096 * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
2097 * contains the selected trigger output (TRGO) and the Master/Slave
2098 * mode.
2099 * @retval HAL status
2100 */
HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef * htim,const TIM_MasterConfigTypeDef * sMasterConfig)2101 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
2102 const TIM_MasterConfigTypeDef *sMasterConfig)
2103 {
2104 uint32_t tmpcr2;
2105 uint32_t tmpsmcr;
2106
2107 /* Check the parameters */
2108 assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
2109 assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
2110 assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
2111
2112 /* Check input state */
2113 __HAL_LOCK(htim);
2114
2115 /* Change the handler state */
2116 htim->State = HAL_TIM_STATE_BUSY;
2117
2118 /* Get the TIMx CR2 register value */
2119 tmpcr2 = htim->Instance->CR2;
2120
2121 /* Get the TIMx SMCR register value */
2122 tmpsmcr = htim->Instance->SMCR;
2123
2124 /* If the timer supports ADC synchronization through TRGO2, set the master mode selection 2 */
2125 if (IS_TIM_TRGO2_INSTANCE(htim->Instance))
2126 {
2127 /* Check the parameters */
2128 assert_param(IS_TIM_TRGO2_SOURCE(sMasterConfig->MasterOutputTrigger2));
2129
2130 /* Clear the MMS2 bits */
2131 tmpcr2 &= ~TIM_CR2_MMS2;
2132 /* Select the TRGO2 source*/
2133 tmpcr2 |= sMasterConfig->MasterOutputTrigger2;
2134 }
2135
2136 /* Reset the MMS Bits */
2137 tmpcr2 &= ~TIM_CR2_MMS;
2138 /* Select the TRGO source */
2139 tmpcr2 |= sMasterConfig->MasterOutputTrigger;
2140
2141 /* Update TIMx CR2 */
2142 htim->Instance->CR2 = tmpcr2;
2143
2144 if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
2145 {
2146 /* Reset the MSM Bit */
2147 tmpsmcr &= ~TIM_SMCR_MSM;
2148 /* Set master mode */
2149 tmpsmcr |= sMasterConfig->MasterSlaveMode;
2150
2151 /* Update TIMx SMCR */
2152 htim->Instance->SMCR = tmpsmcr;
2153 }
2154
2155 /* Change the htim state */
2156 htim->State = HAL_TIM_STATE_READY;
2157
2158 __HAL_UNLOCK(htim);
2159
2160 return HAL_OK;
2161 }
2162
2163 /**
2164 * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
2165 * and the AOE(automatic output enable).
2166 * @param htim TIM handle
2167 * @param sBreakDeadTimeConfig pointer to a TIM_ConfigBreakDeadConfigTypeDef structure that
2168 * contains the BDTR Register configuration information for the TIM peripheral.
2169 * @note Interrupts can be generated when an active level is detected on the
2170 * break input, the break 2 input or the system break input. Break
2171 * interrupt can be enabled by calling the @ref __HAL_TIM_ENABLE_IT macro.
2172 * @retval HAL status
2173 */
HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef * htim,const TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)2174 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
2175 const TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig)
2176 {
2177 /* Keep this variable initialized to 0 as it is used to configure BDTR register */
2178 uint32_t tmpbdtr = 0U;
2179
2180 /* Check the parameters */
2181 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2182 assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
2183 assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
2184 assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
2185 assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
2186 assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
2187 assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
2188 assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->BreakFilter));
2189 assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
2190 assert_param(IS_TIM_BREAK_AFMODE(sBreakDeadTimeConfig->BreakAFMode));
2191
2192 /* Check input state */
2193 __HAL_LOCK(htim);
2194
2195 /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
2196 the OSSI State, the dead time value and the Automatic Output Enable Bit */
2197
2198 /* Set the BDTR bits */
2199 MODIFY_REG(tmpbdtr, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
2200 MODIFY_REG(tmpbdtr, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
2201 MODIFY_REG(tmpbdtr, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
2202 MODIFY_REG(tmpbdtr, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
2203 MODIFY_REG(tmpbdtr, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
2204 MODIFY_REG(tmpbdtr, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
2205 MODIFY_REG(tmpbdtr, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
2206 MODIFY_REG(tmpbdtr, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
2207 MODIFY_REG(tmpbdtr, TIM_BDTR_BKBID, sBreakDeadTimeConfig->BreakAFMode);
2208
2209 if (IS_TIM_BKIN2_INSTANCE(htim->Instance))
2210 {
2211 /* Check the parameters */
2212 assert_param(IS_TIM_BREAK2_STATE(sBreakDeadTimeConfig->Break2State));
2213 assert_param(IS_TIM_BREAK2_POLARITY(sBreakDeadTimeConfig->Break2Polarity));
2214 assert_param(IS_TIM_BREAK_FILTER(sBreakDeadTimeConfig->Break2Filter));
2215 assert_param(IS_TIM_BREAK2_AFMODE(sBreakDeadTimeConfig->Break2AFMode));
2216
2217 /* Set the BREAK2 input related BDTR bits */
2218 MODIFY_REG(tmpbdtr, TIM_BDTR_BK2F, (sBreakDeadTimeConfig->Break2Filter << TIM_BDTR_BK2F_Pos));
2219 MODIFY_REG(tmpbdtr, TIM_BDTR_BK2E, sBreakDeadTimeConfig->Break2State);
2220 MODIFY_REG(tmpbdtr, TIM_BDTR_BK2P, sBreakDeadTimeConfig->Break2Polarity);
2221 MODIFY_REG(tmpbdtr, TIM_BDTR_BK2BID, sBreakDeadTimeConfig->Break2AFMode);
2222 }
2223
2224 /* Set TIMx_BDTR */
2225 htim->Instance->BDTR = tmpbdtr;
2226
2227 __HAL_UNLOCK(htim);
2228
2229 return HAL_OK;
2230 }
2231
2232 /**
2233 * @brief Configures the break input source.
2234 * @param htim TIM handle.
2235 * @param BreakInput Break input to configure
2236 * This parameter can be one of the following values:
2237 * @arg TIM_BREAKINPUT_BRK: Timer break input
2238 * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2239 * @param sBreakInputConfig Break input source configuration
2240 * @retval HAL status
2241 */
HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef * htim,uint32_t BreakInput,const TIMEx_BreakInputConfigTypeDef * sBreakInputConfig)2242 HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim,
2243 uint32_t BreakInput,
2244 const TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
2245 {
2246 HAL_StatusTypeDef status = HAL_OK;
2247 uint32_t tmporx;
2248 uint32_t bkin_enable_mask;
2249 uint32_t bkin_polarity_mask;
2250 uint32_t bkin_enable_bitpos;
2251 uint32_t bkin_polarity_bitpos;
2252
2253 /* Check the parameters */
2254 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2255 assert_param(IS_TIM_BREAKINPUT(BreakInput));
2256 assert_param(IS_TIM_BREAKINPUTSOURCE(sBreakInputConfig->Source));
2257 assert_param(IS_TIM_BREAKINPUTSOURCE_STATE(sBreakInputConfig->Enable));
2258 if (sBreakInputConfig->Source != TIM_BREAKINPUTSOURCE_MDF1)
2259 {
2260 assert_param(IS_TIM_BREAKINPUTSOURCE_POLARITY(sBreakInputConfig->Polarity));
2261 }
2262
2263 /* Check input state */
2264 __HAL_LOCK(htim);
2265
2266 switch (sBreakInputConfig->Source)
2267 {
2268 case TIM_BREAKINPUTSOURCE_BKIN:
2269 {
2270 bkin_enable_mask = TIM_AF1_BKINE;
2271 bkin_enable_bitpos = TIM_AF1_BKINE_Pos;
2272 bkin_polarity_mask = TIM_AF1_BKINP;
2273 bkin_polarity_bitpos = TIM_AF1_BKINP_Pos;
2274 break;
2275 }
2276 case TIM_BREAKINPUTSOURCE_MDF1:
2277 {
2278 bkin_enable_mask = TIM_AF1_BKCMP8E;
2279 bkin_enable_bitpos = TIM_AF1_BKCMP8E_Pos;
2280 /* No polarity bit for MDF. Variable bkin_polarity_mask keeps its default value 0 */
2281 bkin_polarity_mask = 0U;
2282 bkin_polarity_bitpos = 0U;
2283 break;
2284 }
2285
2286 default:
2287 {
2288 bkin_enable_mask = 0U;
2289 bkin_polarity_mask = 0U;
2290 bkin_enable_bitpos = 0U;
2291 bkin_polarity_bitpos = 0U;
2292 break;
2293 }
2294 }
2295
2296 switch (BreakInput)
2297 {
2298 case TIM_BREAKINPUT_BRK:
2299 {
2300 /* Get the TIMx_AF1 register value */
2301 tmporx = htim->Instance->AF1;
2302
2303 /* Enable the break input */
2304 tmporx &= ~bkin_enable_mask;
2305 tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
2306
2307 /* Set the break input polarity */
2308 tmporx &= ~bkin_polarity_mask;
2309 tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
2310
2311 /* Set TIMx_AF1 */
2312 htim->Instance->AF1 = tmporx;
2313 break;
2314 }
2315 case TIM_BREAKINPUT_BRK2:
2316 {
2317 /* Get the TIMx_AF2 register value */
2318 tmporx = htim->Instance->AF2;
2319
2320 /* Enable the break input */
2321 tmporx &= ~bkin_enable_mask;
2322 tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
2323
2324 /* Set the break input polarity */
2325 tmporx &= ~bkin_polarity_mask;
2326 tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
2327
2328 /* Set TIMx_AF2 */
2329 htim->Instance->AF2 = tmporx;
2330 break;
2331 }
2332 default:
2333 status = HAL_ERROR;
2334 break;
2335 }
2336
2337 __HAL_UNLOCK(htim);
2338
2339 return status;
2340 }
2341
2342 /**
2343 * @brief Configures the TIMx Remapping input capabilities.
2344 * @param htim TIM handle.
2345 * @param Remap specifies the TIM remapping source.
2346 * For TIM1, the parameter can take one of the following values:
2347 * @arg TIM_TIM1_ETR_GPIO TIM1 ETR is connected to GPIO
2348 * @arg TIM_TIM1_ETR_ADC1_AWD1 TIM1 ETR is connected to ADC1 AWD1
2349 * @arg TIM_TIM1_ETR_ADC1_AWD2 TIM1 ETR is connected to ADC1 AWD2
2350 * @arg TIM_TIM1_ETR_ADC1_AWD3 TIM1 ETR is connected to ADC1 AWD3
2351 * @arg TIM_TIM1_ETR_ADC2_AWD1 TIM1 ETR is connected to ADC4 AWD1
2352 * @arg TIM_TIM1_ETR_ADC2_AWD2 TIM1 ETR is connected to ADC4 AWD2
2353 * @arg TIM_TIM1_ETR_ADC2_AWD3 TIM1 ETR is connected to ADC4 AWD3
2354 *
2355 * For TIM2, the parameter can take one of the following values:
2356 * @arg TIM_TIM2_ETR_GPIO TIM2 ETR is connected to GPIO
2357 * @arg TIM_TIM2_ETR_DCMIPP_HSYNC TIM2_ETR is connected to DCMIPP HSYNC
2358 * @arg TIM_TIM2_ETR_LCD_HSYNC TIM2_ETR is connected to LCD HSYNC
2359 * @arg TIM_TIM2_ETR_SAI1_FSA TIM2_ETR is connected to SAI1 FS_A
2360 * @arg TIM_TIM2_ETR_SAI1_FSB TIM2_ETR is connected to SAI1 FS_B
2361 * @arg TIM_TIM2_ETR_GFXTIM_TE TIM2_ETR is connected to GFXTIM TE
2362 * @arg TIM_TIM2_ETR_DCMIPP_VSYNC TIM2_ETR is connected to DCMIPP VSYNC
2363 * @arg TIM_TIM2_ETR_LCD_VSYNC TIM2_ETR is connected to LCD VSYNC
2364 * @arg TIM_TIM2_ETR_TIM3_ETR TIM2_ETR is connected to TIM3_ETR
2365 * @arg TIM_TIM2_ETR_TIM4_ETR TIM2_ETR is connected to TIM4_ETR
2366 * @arg TIM_TIM2_ETR_TIM5_ETR TIM2_ETR is connected to TIM5_ETR
2367 * @arg TIM_TIM2_ETR_USB1_SOF TIM2_ETR is connected to USB1 OTG SOF
2368 * @arg TIM_TIM2_ETR_USB2_SOF TIM2_ETR is connected to USB2 OTG SOF
2369 *
2370 * For TIM3, the parameter can take one of the following values:
2371 * @arg TIM_TIM3_ETR_GPIO TIM3 ETR is connected to GPIO
2372 * @arg TIM_TIM3_ETR_DCMIPP_HSYNC TIM3 ETR is connected to DCMIPP HSYNC
2373 * @arg TIM_TIM3_ETR_LCD_HSYNC TIM3 ETR is connected to LCD HSYNC
2374 * @arg TIM_TIM3_ETR_GFXTIM_TE TIM3 ETR is connected to GFXTIM TE
2375 * @arg TIM_TIM3_ETR_DCMIPP_VSYNC TIM3 ETR is connected to DCMIPP VSYNC
2376 * @arg TIM_TIM3_ETR_LCD_VSYNC TIM3 ETR is connected to LCD VSYNC
2377 * @arg TIM_TIM3_ETR_TIM2_ETR TIM3 ETR is connected to TIM2 ETR
2378 * @arg TIM_TIM3_ETR_TIM4_ETR TIM3 ETR is connected to TIM4_ETR
2379 * @arg TIM_TIM3_ETR_TIM5_ETR TIM3 ETR is connected to TIM5_ETR
2380 *
2381 * For TIM4, the parameter can take one of the following values:
2382 * @arg TIM_TIM4_ETR_GPIO TIM4 ETR is connected to GPIO
2383 * @arg TIM_TIM4_ETR_DCMIPP_HSYNC TIM4 ETR is connected to DCMIPP HSYNC
2384 * @arg TIM_TIM4_ETR_LCD_HSYNC TIM4 ETR is connected to LCD HSYNC
2385 * @arg TIM_TIM4_ETR_GFXTIM_TE TIM4 ETR is connected to GFXTIM TE
2386 * @arg TIM_TIM4_ETR_DCMIPP_VSYNC TIM4 ETR is connected to DCMIPP VSYNC
2387 * @arg TIM_TIM4_ETR_LCD_VSYNC TIM4 ETR is connected to LCD VSYNC
2388 * @arg TIM_TIM4_ETR_TIM2_ETR TIM4 ETR is connected to TIM2 ETR
2389 * @arg TIM_TIM4_ETR_TIM3_ETR TIM4 ETR is connected to TIM3_ETR
2390 * @arg TIM_TIM4_ETR_TIM5_ETR TIM4 ETR is connected to TIM5_ETR
2391 *
2392 * For TIM5, the parameter can take one of the following values:
2393 * @arg TIM_TIM5_ETR_GPIO TIM5 ETR is connected to GPIO
2394 * @arg TIM_TIM5_ETR_SAI2_FSA TIM5_ETR is connected to SAI2 FS_A
2395 * @arg TIM_TIM5_ETR_SAI2_FSB TIM5_ETR is connected to SAI2 FS_B
2396 * @arg TIM_TIM5_ETR_DCMIPP_HSYNC TIM5 ETR is connected to DCMIPP HSYNC
2397 * @arg TIM_TIM5_ETR_LCD_HSYNC TIM5 ETR is connected to LCD HSYNC
2398 * @arg TIM_TIM5_ETR_GFXTIM_TE TIM5 ETR is connected to GFXTIM TE
2399 * @arg TIM_TIM5_ETR_DCMIPP_VSYNC TIM5 ETR is connected to DCMIPP VSYNC
2400 * @arg TIM_TIM5_ETR_LCD_VSYNC TIM5 ETR is connected to LCD VSYNC
2401 * @arg TIM_TIM5_ETR_TIM2_ETR TIM5 ETR is connected to TIM2_ETR
2402 * @arg TIM_TIM5_ETR_TIM3_ETR TIM5 ETR is connected to TIM3_ETR
2403 * @arg TIM_TIM5_ETR_TIM4_ETR TIM5 ETR is connected to TIM4_ETR
2404 * @arg TIM_TIM5_ETR_USB1_SOF TIM5_ETR is connected to USB1 OTG SOF
2405 * @arg TIM_TIM5_ETR_USB2_SOF TIM5_ETR is connected to USB2 OTG SOF
2406 *
2407 * For TIM8, the parameter can take one of the following values:
2408 * @arg TIM_TIM8_ETR_GPIO TIM8 ETR is connected to GPIO
2409 * @arg TIM_TIM8_ETR_ADC2_AWD1 TIM8 ETR is connected to ADC2 AWD1
2410 * @arg TIM_TIM8_ETR_ADC2_AWD2 TIM8 ETR is connected to ADC2 AWD2
2411 * @arg TIM_TIM8_ETR_ADC2_AWD3 TIM8 ETR is connected to ADC2 AWD3
2412 *
2413 * @retval HAL status
2414 */
HAL_TIMEx_RemapConfig(TIM_HandleTypeDef * htim,uint32_t Remap)2415 HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
2416 {
2417 /* Check parameters */
2418 assert_param(IS_TIM_REMAP(htim->Instance, Remap));
2419
2420 __HAL_LOCK(htim);
2421
2422 MODIFY_REG(htim->Instance->AF1, TIM_AF1_ETRSEL_Msk, Remap);
2423
2424 __HAL_UNLOCK(htim);
2425
2426 return HAL_OK;
2427 }
2428
2429 /**
2430 * @brief Select the timer input source
2431 * @param htim TIM handle.
2432 * @param Channel specifies the TIM Channel
2433 * This parameter can be one of the following values:
2434 * @arg TIM_CHANNEL_1: TI1 input channel
2435 * @arg TIM_CHANNEL_2: TI2 input channel
2436 * @param TISelection parameter of the TIM_TISelectionStruct structure is detailed as follows:
2437 * For TIM2, the parameter is one of the following values:
2438 * @arg TIM_TIM2_TI1_GPIO: TIM2 TI1 is connected to GPIO
2439 * @arg TIM_TIM2_TI1_ETH1_PPS: TIM2 TI1 is connected to ETH1 PPS
2440 *
2441 * For TIM3, the parameter is one of the following values:
2442 * @arg TIM_TIM3_TI1_GPIO: TIM3 TI1 is connected to GPIO
2443 * @arg TIM_TIM3_TI1_ETH1_PPS: TIM3 TI1 is connected to ETH1 PPS
2444 * @arg TIM_TIM3_TI1_FDCAN_RTP TIM3_TI1 is connected to FDCAN RTP
2445 * @arg TIM_TIM3_TI1_FDCAN_TMP TIM3_TI1 is connected to FDCAN TMP
2446 * @arg TIM_TIM3_TI1_FDCAN_SOC TIM3_TI1 is connected to FDCAN SOC
2447 *
2448 * For TIM5, the parameter is one of the following values:
2449 * @arg TIM_TIM5_TI1_GPIO: TIM5 TI1 is connected to GPIO
2450 * @arg TIM_TIM5_TI1_FDCAN_RTP TIM5_TI1 is connected to FDCAN RTP
2451 * @arg TIM_TIM5_TI1_FDCAN_TMP TIM5_TI1 is connected to FDCAN TMP
2452 *
2453 * For TIM9, the parameter is one of the following values:
2454 * @arg TIM_TIM9_TI1_GPIO: TIM9 TI1 is connected to GPIO
2455 * @arg TIM_TIM9_TI1_MCO1: TIM9 TI1 is connected to MCO1
2456 * @arg TIM_TIM9_TI1_MCO2: TIM9 TI1 is connected to MCO2
2457 *
2458 * For TIM10, the parameter is one of the following values:
2459 * @arg TIM_TIM10_TI1_GPIO TIM10_TI1 is connected to GPIO
2460 * @arg TIM_TIM10_TI1_I3C1_IBIACK TIM10_TI1 is connected to I3C1 IBI ACK
2461 *
2462 * For TIM11, the parameter is one of the following values:
2463 * @arg TIM_TIM11_TI1_GPIO TIM11_TI1 is connected to GPIO
2464 * @arg TIM_TIM11_TI1_I3C2_IBIACK TIM11_TI1 is connected to I3C2 IBI ACK
2465 *
2466 * For TIM12, the parameter is one of the following values:
2467 * @arg TIM_TIM12_TI1_GPIO: TIM12 TI1 is connected to GPIO
2468 * @arg TIM_TIM12_TI1_SPDIF_FS: TIM12 TI1 is connected to SPDIF FS
2469 * @arg TIM_TIM12_TI1_HSI_1024: TIM12 TI1 is connected to HSI/1024
2470 * @arg TIM_TIM12_TI1_MSI_128: TIM12_TI1 is connected to MSI/128
2471 * @arg TIM_TIM12_TI1_MCO1: TIM12 TI1 is connected to MCO1
2472 * @arg TIM_TIM12_TI1_MCO2: TIM12 TI1 is connected to MCO2
2473 *
2474 * For TIM13, the parameter is one of the following values:
2475 * @arg TIM_TIM13_TI1_GPIO TIM13_TI1 is connected to GPIO
2476 * @arg TIM_TIM13_TI1_I3C1_IBIACK TIM13_TI1 is connected to I3C1 IBI ACK
2477 *
2478 * For TIM14, the parameter is one of the following values:
2479 * @arg TIM_TIM14_TI1_GPIO TIM14_TI1 is connected to GPIO
2480 * @arg TIM_TIM14_TI1_I3C2_IBIACK TIM14_TI1 is connected to I3C2 IBI ACK
2481 *
2482 * For TIM15, the parameter is one of the following values:
2483 * @arg TIM_TIM15_TI1_GPIO: TIM15 TI1 is connected to GPIO
2484 * @arg TIM_TIM15_TI1_TIM2_CH1: TIM15 TI1 is connected to TIM2 CH1
2485 * @arg TIM_TIM15_TI1_TIM3_CH1: TIM15 TI1 is connected to TIM3 CH1
2486 * @arg TIM_TIM15_TI1_TIM4_CH1: TIM15 TI1 is connected to TIM4 CH1
2487 * @arg TIM_TIM15_TI1_MCO1: TIM15 TI1 is connected to MCO1
2488 * @arg TIM_TIM15_TI1_MCO2: TIM15 TI1 is connected to MCO2
2489 * @arg TIM_TIM15_TI2_GPIO: TIM15 TI2 is connected to GPIO
2490 * @arg TIM_TIM15_TI2_TIM2_CH2: TIM15 TI2 is connected to TIM2 CH2
2491 * @arg TIM_TIM15_TI2_TIM3_CH2: TIM15 TI2 is connected to TIM3 CH2
2492 * @arg TIM_TIM15_TI2_TIM4_CH2: TIM15 TI2 is connected to TIM4 CH2
2493 *
2494 * For TIM16, the parameter is one of the following values:
2495 * @arg TIM_TIM16_TI1_GPIO: TIM16 TI1 is connected to GPIO
2496 * @arg TIM_TIM16_TI1_LSI TIM16 TI1 is connected to LSI
2497 * @arg TIM_TIM16_TI1_LSE TIM16 TI1 is connected to LSE
2498 * @arg TIM_TIM16_TI1_RTC_WKUP: TIM16 TI1 is connected to RTC wakeup interrupt
2499 *
2500 * For TIM17, the parameter is one of the following values:
2501 * @arg TIM_TIM17_TI1_GPIO: TIM17 TI1 is connected to GPIO
2502 * @arg TIM_TIM17_TI1_SPDIF_FS: TIM17 TI1 is connected to SPDIF FS
2503 * @arg TIM_TIM17_TI1_HSE_1024: TIM17 TI1 is connected to HSE/1024
2504 * @retval HAL status
2505 */
HAL_TIMEx_TISelection(TIM_HandleTypeDef * htim,uint32_t TISelection,uint32_t Channel)2506 HAL_StatusTypeDef HAL_TIMEx_TISelection(TIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel)
2507 {
2508 HAL_StatusTypeDef status = HAL_OK;
2509
2510 /* Check parameters */
2511 assert_param(IS_TIM_TISEL_TIX_INSTANCE(htim->Instance, Channel));
2512 assert_param(IS_TIM_TISEL(TISelection));
2513
2514 __HAL_LOCK(htim);
2515
2516 switch (Channel)
2517 {
2518 case TIM_CHANNEL_1:
2519 MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI1SEL, TISelection);
2520 break;
2521 case TIM_CHANNEL_2:
2522 MODIFY_REG(htim->Instance->TISEL, TIM_TISEL_TI2SEL, TISelection);
2523 break;
2524 default:
2525 status = HAL_ERROR;
2526 break;
2527 }
2528
2529 __HAL_UNLOCK(htim);
2530
2531 return status;
2532 }
2533
2534 /**
2535 * @brief Group channel 5 and channel 1, 2 or 3
2536 * @param htim TIM handle.
2537 * @param Channels specifies the reference signal(s) the OC5REF is combined with.
2538 * This parameter can be any combination of the following values:
2539 * TIM_GROUPCH5_NONE: No effect of OC5REF on OC1REFC, OC2REFC and OC3REFC
2540 * TIM_GROUPCH5_OC1REFC: OC1REFC is the logical AND of OC1REFC and OC5REF
2541 * TIM_GROUPCH5_OC2REFC: OC2REFC is the logical AND of OC2REFC and OC5REF
2542 * TIM_GROUPCH5_OC3REFC: OC3REFC is the logical AND of OC3REFC and OC5REF
2543 * @retval HAL status
2544 */
HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef * htim,uint32_t Channels)2545 HAL_StatusTypeDef HAL_TIMEx_GroupChannel5(TIM_HandleTypeDef *htim, uint32_t Channels)
2546 {
2547 /* Check parameters */
2548 assert_param(IS_TIM_COMBINED3PHASEPWM_INSTANCE(htim->Instance));
2549 assert_param(IS_TIM_GROUPCH5(Channels));
2550
2551 /* Process Locked */
2552 __HAL_LOCK(htim);
2553
2554 htim->State = HAL_TIM_STATE_BUSY;
2555
2556 /* Clear GC5Cx bit fields */
2557 htim->Instance->CCR5 &= ~(TIM_CCR5_GC5C3 | TIM_CCR5_GC5C2 | TIM_CCR5_GC5C1);
2558
2559 /* Set GC5Cx bit fields */
2560 htim->Instance->CCR5 |= Channels;
2561
2562 /* Change the htim state */
2563 htim->State = HAL_TIM_STATE_READY;
2564
2565 __HAL_UNLOCK(htim);
2566
2567 return HAL_OK;
2568 }
2569
2570 /**
2571 * @brief Disarm the designated break input (when it operates in bidirectional mode).
2572 * @param htim TIM handle.
2573 * @param BreakInput Break input to disarm
2574 * This parameter can be one of the following values:
2575 * @arg TIM_BREAKINPUT_BRK: Timer break input
2576 * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2577 * @note The break input can be disarmed only when it is configured in
2578 * bidirectional mode and when when MOE is reset.
2579 * @note Purpose is to be able to have the input voltage back to high-state,
2580 * whatever the time constant on the output .
2581 * @retval HAL status
2582 */
HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef * htim,uint32_t BreakInput)2583 HAL_StatusTypeDef HAL_TIMEx_DisarmBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput)
2584 {
2585 HAL_StatusTypeDef status = HAL_OK;
2586 uint32_t tmpbdtr;
2587
2588 /* Check the parameters */
2589 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2590 assert_param(IS_TIM_BREAKINPUT(BreakInput));
2591
2592 switch (BreakInput)
2593 {
2594 case TIM_BREAKINPUT_BRK:
2595 {
2596 /* Check initial conditions */
2597 tmpbdtr = READ_REG(htim->Instance->BDTR);
2598 if ((READ_BIT(tmpbdtr, TIM_BDTR_BKBID) == TIM_BDTR_BKBID) &&
2599 (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
2600 {
2601 /* Break input BRK is disarmed */
2602 SET_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM);
2603 }
2604 break;
2605 }
2606 case TIM_BREAKINPUT_BRK2:
2607 {
2608 /* Check initial conditions */
2609 tmpbdtr = READ_REG(htim->Instance->BDTR);
2610 if ((READ_BIT(tmpbdtr, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID) &&
2611 (READ_BIT(tmpbdtr, TIM_BDTR_MOE) == 0U))
2612 {
2613 /* Break input BRK is disarmed */
2614 SET_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM);
2615 }
2616 break;
2617 }
2618 default:
2619 status = HAL_ERROR;
2620 break;
2621 }
2622
2623 return status;
2624 }
2625
2626 /**
2627 * @brief Arm the designated break input (when it operates in bidirectional mode).
2628 * @param htim TIM handle.
2629 * @param BreakInput Break input to arm
2630 * This parameter can be one of the following values:
2631 * @arg TIM_BREAKINPUT_BRK: Timer break input
2632 * @arg TIM_BREAKINPUT_BRK2: Timer break 2 input
2633 * @note Arming is possible at anytime, even if fault is present.
2634 * @note Break input is automatically armed as soon as MOE bit is set.
2635 * @retval HAL status
2636 */
HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef * htim,uint32_t BreakInput)2637 HAL_StatusTypeDef HAL_TIMEx_ReArmBreakInput(const TIM_HandleTypeDef *htim, uint32_t BreakInput)
2638 {
2639 HAL_StatusTypeDef status = HAL_OK;
2640 uint32_t tickstart;
2641
2642 /* Check the parameters */
2643 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2644 assert_param(IS_TIM_BREAKINPUT(BreakInput));
2645
2646 switch (BreakInput)
2647 {
2648 case TIM_BREAKINPUT_BRK:
2649 {
2650 /* Check initial conditions */
2651 if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKBID) == TIM_BDTR_BKBID)
2652 {
2653 /* Break input BRK is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
2654 /* Init tickstart for timeout management */
2655 tickstart = HAL_GetTick();
2656 while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
2657 {
2658 if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
2659 {
2660 /* New check to avoid false timeout detection in case of preemption */
2661 if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BKDSRM) != 0UL)
2662 {
2663 return HAL_TIMEOUT;
2664 }
2665 }
2666 }
2667 }
2668 break;
2669 }
2670
2671 case TIM_BREAKINPUT_BRK2:
2672 {
2673 /* Check initial conditions */
2674 if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2BID) == TIM_BDTR_BK2BID)
2675 {
2676 /* Break input BRK2 is re-armed automatically by hardware. Poll to check whether fault condition disappeared */
2677 /* Init tickstart for timeout management */
2678 tickstart = HAL_GetTick();
2679 while (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
2680 {
2681 if ((HAL_GetTick() - tickstart) > TIM_BREAKINPUT_REARM_TIMEOUT)
2682 {
2683 /* New check to avoid false timeout detection in case of preemption */
2684 if (READ_BIT(htim->Instance->BDTR, TIM_BDTR_BK2DSRM) != 0UL)
2685 {
2686 return HAL_TIMEOUT;
2687 }
2688 }
2689 }
2690 }
2691 break;
2692 }
2693 default:
2694 status = HAL_ERROR;
2695 break;
2696 }
2697
2698 return status;
2699 }
2700
2701 /**
2702 * @brief Enable dithering
2703 * @param htim TIM handle
2704 * @note Main usage is PWM mode
2705 * @note This function must be called when timer is stopped or disabled (CEN =0)
2706 * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
2707 * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
2708 * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
2709 * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
2710 * - ARR and CCRx values are limited to 0xFFEF in dithering mode for 16b timers
2711 * (corresponds to 4094 for the integer part and 15 for the dithered part).
2712 * @note Macros @ref __HAL_TIM_CALC_PERIOD_DITHER() __HAL_TIM_CALC_DELAY_DITHER() __HAL_TIM_CALC_PULSE_DITHER()
2713 * can be used to calculate period (ARR) and delay (CCRx) value.
2714 * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
2715 * @note Enabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
2716 * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
2717 * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
2718 * @retval HAL status
2719 */
HAL_TIMEx_DitheringEnable(TIM_HandleTypeDef * htim)2720 HAL_StatusTypeDef HAL_TIMEx_DitheringEnable(TIM_HandleTypeDef *htim)
2721 {
2722 /* Check the parameters */
2723 assert_param(IS_TIM_INSTANCE(htim->Instance));
2724
2725 SET_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
2726 return HAL_OK;
2727 }
2728
2729 /**
2730 * @brief Disable dithering
2731 * @param htim TIM handle
2732 * @note This function must be called when timer is stopped or disabled (CEN =0)
2733 * @note If dithering is activated, pay attention to ARR, CCRx, CNT interpretation:
2734 * - CNT: only CNT[11:0] holds the non-dithered part for 16b timers (or CNT[26:0] for 32b timers)
2735 * - ARR: ARR[15:4] holds the non-dithered part, and ARR[3:0] the dither part for 16b timers
2736 * - CCRx: CCRx[15:4] holds the non-dithered part, and CCRx[3:0] the dither part for 16b timers
2737 * - ARR and CCRx values are limited to 0xFFEF in dithering mode
2738 * (corresponds to 4094 for the integer part and 15 for the dithered part).
2739 * @note Disabling dithering, modifies automatically values of registers ARR/CCRx to keep the same integer part.
2740 * So it may be necessary to read ARR value or CCRx value with macros @ref __HAL_TIM_GET_AUTORELOAD()
2741 * __HAL_TIM_GET_COMPARE() and if necessary update Init structure field htim->Init.Period .
2742 * @retval HAL status
2743 */
HAL_TIMEx_DitheringDisable(TIM_HandleTypeDef * htim)2744 HAL_StatusTypeDef HAL_TIMEx_DitheringDisable(TIM_HandleTypeDef *htim)
2745 {
2746 /* Check the parameters */
2747 assert_param(IS_TIM_INSTANCE(htim->Instance));
2748
2749 CLEAR_BIT(htim->Instance->CR1, TIM_CR1_DITHEN);
2750 return HAL_OK;
2751 }
2752
2753 /**
2754 * @brief Initializes the pulse on compare pulse width and pulse prescaler
2755 * @param htim TIM Output Compare handle
2756 * @param PulseWidthPrescaler Pulse width prescaler
2757 * This parameter can be a number between Min_Data = 0x0 and Max_Data = 0x7
2758 * @param PulseWidth Pulse width
2759 * This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
2760 * @retval HAL status
2761 */
HAL_TIMEx_OC_ConfigPulseOnCompare(TIM_HandleTypeDef * htim,uint32_t PulseWidthPrescaler,uint32_t PulseWidth)2762 HAL_StatusTypeDef HAL_TIMEx_OC_ConfigPulseOnCompare(TIM_HandleTypeDef *htim,
2763 uint32_t PulseWidthPrescaler,
2764 uint32_t PulseWidth)
2765 {
2766 uint32_t tmpecr;
2767
2768 /* Check the parameters */
2769 assert_param(IS_TIM_PULSEONCOMPARE_INSTANCE(htim->Instance));
2770 assert_param(IS_TIM_PULSEONCOMPARE_WIDTH(PulseWidth));
2771 assert_param(IS_TIM_PULSEONCOMPARE_WIDTHPRESCALER(PulseWidthPrescaler));
2772
2773 /* Process Locked */
2774 __HAL_LOCK(htim);
2775
2776 /* Set the TIM state */
2777 htim->State = HAL_TIM_STATE_BUSY;
2778
2779 /* Get the TIMx ECR register value */
2780 tmpecr = htim->Instance->ECR;
2781 /* Reset the Pulse width prescaler and the Pulse width */
2782 tmpecr &= ~(TIM_ECR_PWPRSC | TIM_ECR_PW);
2783 /* Set the Pulse width prescaler and Pulse width*/
2784 tmpecr |= PulseWidthPrescaler << TIM_ECR_PWPRSC_Pos;
2785 tmpecr |= PulseWidth << TIM_ECR_PW_Pos;
2786 /* Write to TIMx ECR */
2787 htim->Instance->ECR = tmpecr;
2788
2789 /* Change the TIM state */
2790 htim->State = HAL_TIM_STATE_READY;
2791
2792 /* Release Lock */
2793 __HAL_UNLOCK(htim);
2794
2795 return HAL_OK;
2796 }
2797
2798 /**
2799 * @brief Configure preload source of Slave Mode Selection bitfield (SMS in SMCR register)
2800 * @param htim TIM handle
2801 * @param Source Source of slave mode selection preload
2802 * This parameter can be one of the following values:
2803 * @arg TIM_SMS_PRELOAD_SOURCE_UPDATE: Timer update event is used as source of Slave Mode Selection preload
2804 * @arg TIM_SMS_PRELOAD_SOURCE_INDEX: Timer index event is used as source of Slave Mode Selection preload
2805 * @retval HAL status
2806 */
HAL_TIMEx_ConfigSlaveModePreload(TIM_HandleTypeDef * htim,uint32_t Source)2807 HAL_StatusTypeDef HAL_TIMEx_ConfigSlaveModePreload(TIM_HandleTypeDef *htim, uint32_t Source)
2808 {
2809 /* Check the parameters */
2810 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
2811 assert_param(IS_TIM_SLAVE_PRELOAD_SOURCE(Source));
2812
2813 MODIFY_REG(htim->Instance->SMCR, TIM_SMCR_SMSPS, Source);
2814 return HAL_OK;
2815 }
2816
2817 /**
2818 * @brief Enable preload of Slave Mode Selection bitfield (SMS in SMCR register)
2819 * @param htim TIM handle
2820 * @retval HAL status
2821 */
HAL_TIMEx_EnableSlaveModePreload(TIM_HandleTypeDef * htim)2822 HAL_StatusTypeDef HAL_TIMEx_EnableSlaveModePreload(TIM_HandleTypeDef *htim)
2823 {
2824 /* Check the parameters */
2825 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
2826
2827 SET_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
2828 return HAL_OK;
2829 }
2830
2831 /**
2832 * @brief Disable preload of Slave Mode Selection bitfield (SMS in SMCR register)
2833 * @param htim TIM handle
2834 * @retval HAL status
2835 */
HAL_TIMEx_DisableSlaveModePreload(TIM_HandleTypeDef * htim)2836 HAL_StatusTypeDef HAL_TIMEx_DisableSlaveModePreload(TIM_HandleTypeDef *htim)
2837 {
2838 /* Check the parameters */
2839 assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
2840
2841 CLEAR_BIT(htim->Instance->SMCR, TIM_SMCR_SMSPE);
2842 return HAL_OK;
2843 }
2844
2845 /**
2846 * @brief Enable deadtime preload
2847 * @param htim TIM handle
2848 * @retval HAL status
2849 */
HAL_TIMEx_EnableDeadTimePreload(TIM_HandleTypeDef * htim)2850 HAL_StatusTypeDef HAL_TIMEx_EnableDeadTimePreload(TIM_HandleTypeDef *htim)
2851 {
2852 /* Check the parameters */
2853 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2854
2855 SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
2856 return HAL_OK;
2857 }
2858
2859 /**
2860 * @brief Disable deadtime preload
2861 * @param htim TIM handle
2862 * @retval HAL status
2863 */
HAL_TIMEx_DisableDeadTimePreload(TIM_HandleTypeDef * htim)2864 HAL_StatusTypeDef HAL_TIMEx_DisableDeadTimePreload(TIM_HandleTypeDef *htim)
2865 {
2866 /* Check the parameters */
2867 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2868
2869 CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTPE);
2870 return HAL_OK;
2871 }
2872
2873 /**
2874 * @brief Configure deadtime
2875 * @param htim TIM handle
2876 * @param Deadtime Deadtime value
2877 * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
2878 * @retval HAL status
2879 */
HAL_TIMEx_ConfigDeadTime(TIM_HandleTypeDef * htim,uint32_t Deadtime)2880 HAL_StatusTypeDef HAL_TIMEx_ConfigDeadTime(TIM_HandleTypeDef *htim, uint32_t Deadtime)
2881 {
2882 /* Check the parameters */
2883 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2884 assert_param(IS_TIM_DEADTIME(Deadtime));
2885
2886 MODIFY_REG(htim->Instance->BDTR, TIM_BDTR_DTG, Deadtime);
2887 return HAL_OK;
2888 }
2889
2890 /**
2891 * @brief Configure asymmetrical deadtime
2892 * @param htim TIM handle
2893 * @param FallingDeadtime Falling edge deadtime value
2894 * @note This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFF
2895 * @retval HAL status
2896 */
HAL_TIMEx_ConfigAsymmetricalDeadTime(TIM_HandleTypeDef * htim,uint32_t FallingDeadtime)2897 HAL_StatusTypeDef HAL_TIMEx_ConfigAsymmetricalDeadTime(TIM_HandleTypeDef *htim, uint32_t FallingDeadtime)
2898 {
2899 /* Check the parameters */
2900 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2901 assert_param(IS_TIM_DEADTIME(FallingDeadtime));
2902
2903 MODIFY_REG(htim->Instance->DTR2, TIM_DTR2_DTGF, FallingDeadtime);
2904 return HAL_OK;
2905 }
2906
2907 /**
2908 * @brief Enable asymmetrical deadtime
2909 * @param htim TIM handle
2910 * @retval HAL status
2911 */
HAL_TIMEx_EnableAsymmetricalDeadTime(TIM_HandleTypeDef * htim)2912 HAL_StatusTypeDef HAL_TIMEx_EnableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
2913 {
2914 /* Check the parameters */
2915 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2916
2917 SET_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
2918 return HAL_OK;
2919 }
2920
2921 /**
2922 * @brief Disable asymmetrical deadtime
2923 * @param htim TIM handle
2924 * @retval HAL status
2925 */
HAL_TIMEx_DisableAsymmetricalDeadTime(TIM_HandleTypeDef * htim)2926 HAL_StatusTypeDef HAL_TIMEx_DisableAsymmetricalDeadTime(TIM_HandleTypeDef *htim)
2927 {
2928 /* Check the parameters */
2929 assert_param(IS_TIM_BREAK_INSTANCE(htim->Instance));
2930
2931 CLEAR_BIT(htim->Instance->DTR2, TIM_DTR2_DTAE);
2932 return HAL_OK;
2933 }
2934
2935 /**
2936 * @brief Configures the encoder index.
2937 * @note warning in case of encoder mode clock plus direction
2938 * @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X1 or @ref TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X2
2939 * Direction must be set to @ref TIM_ENCODERINDEX_DIRECTION_UP_DOWN
2940 * @param htim TIM handle.
2941 * @param sEncoderIndexConfig Encoder index configuration
2942 * @retval HAL status
2943 */
HAL_TIMEx_ConfigEncoderIndex(TIM_HandleTypeDef * htim,TIMEx_EncoderIndexConfigTypeDef * sEncoderIndexConfig)2944 HAL_StatusTypeDef HAL_TIMEx_ConfigEncoderIndex(TIM_HandleTypeDef *htim,
2945 TIMEx_EncoderIndexConfigTypeDef *sEncoderIndexConfig)
2946 {
2947 /* Check the parameters */
2948 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
2949 assert_param(IS_TIM_ENCODERINDEX_POLARITY(sEncoderIndexConfig->Polarity));
2950 assert_param(IS_TIM_ENCODERINDEX_PRESCALER(sEncoderIndexConfig->Prescaler));
2951 assert_param(IS_TIM_ENCODERINDEX_FILTER(sEncoderIndexConfig->Filter));
2952 assert_param(IS_TIM_ENCODERINDEX_BLANKING(sEncoderIndexConfig->Blanking));
2953 assert_param(IS_FUNCTIONAL_STATE(sEncoderIndexConfig->FirstIndexEnable));
2954 assert_param(IS_TIM_ENCODERINDEX_POSITION(sEncoderIndexConfig->Position));
2955 assert_param(IS_TIM_ENCODERINDEX_DIRECTION(sEncoderIndexConfig->Direction));
2956
2957 /* Process Locked */
2958 __HAL_LOCK(htim);
2959
2960 /* Configures the TIMx External Trigger (ETR) which is used as Index input */
2961 TIM_ETR_SetConfig(htim->Instance,
2962 sEncoderIndexConfig->Prescaler,
2963 sEncoderIndexConfig->Polarity,
2964 sEncoderIndexConfig->Filter);
2965
2966 /* Configures the encoder index */
2967 MODIFY_REG(htim->Instance->ECR,
2968 TIM_ECR_IDIR_Msk | TIM_ECR_IBLK_Msk | TIM_ECR_FIDX_Msk | TIM_ECR_IPOS_Msk,
2969 (sEncoderIndexConfig->Direction |
2970 (sEncoderIndexConfig->Blanking) |
2971 ((sEncoderIndexConfig->FirstIndexEnable == ENABLE) ? (0x1U << TIM_ECR_FIDX_Pos) : 0U) |
2972 sEncoderIndexConfig->Position |
2973 TIM_ECR_IE));
2974
2975 __HAL_UNLOCK(htim);
2976
2977 return HAL_OK;
2978 }
2979
2980 /**
2981 * @brief Enable encoder index
2982 * @param htim TIM handle
2983 * @retval HAL status
2984 */
HAL_TIMEx_EnableEncoderIndex(TIM_HandleTypeDef * htim)2985 HAL_StatusTypeDef HAL_TIMEx_EnableEncoderIndex(TIM_HandleTypeDef *htim)
2986 {
2987 /* Check the parameters */
2988 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
2989
2990 SET_BIT(htim->Instance->ECR, TIM_ECR_IE);
2991 return HAL_OK;
2992 }
2993
2994 /**
2995 * @brief Disable encoder index
2996 * @param htim TIM handle
2997 * @retval HAL status
2998 */
HAL_TIMEx_DisableEncoderIndex(TIM_HandleTypeDef * htim)2999 HAL_StatusTypeDef HAL_TIMEx_DisableEncoderIndex(TIM_HandleTypeDef *htim)
3000 {
3001 /* Check the parameters */
3002 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3003
3004 CLEAR_BIT(htim->Instance->ECR, TIM_ECR_IE);
3005 return HAL_OK;
3006 }
3007
3008 /**
3009 * @brief Enable encoder first index
3010 * @param htim TIM handle
3011 * @retval HAL status
3012 */
HAL_TIMEx_EnableEncoderFirstIndex(TIM_HandleTypeDef * htim)3013 HAL_StatusTypeDef HAL_TIMEx_EnableEncoderFirstIndex(TIM_HandleTypeDef *htim)
3014 {
3015 /* Check the parameters */
3016 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3017
3018 SET_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
3019 return HAL_OK;
3020 }
3021
3022 /**
3023 * @brief Disable encoder first index
3024 * @param htim TIM handle
3025 * @retval HAL status
3026 */
HAL_TIMEx_DisableEncoderFirstIndex(TIM_HandleTypeDef * htim)3027 HAL_StatusTypeDef HAL_TIMEx_DisableEncoderFirstIndex(TIM_HandleTypeDef *htim)
3028 {
3029 /* Check the parameters */
3030 assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
3031
3032 CLEAR_BIT(htim->Instance->ECR, TIM_ECR_FIDX);
3033 return HAL_OK;
3034 }
3035
3036 /**
3037 * @brief Enable ADC synchronization
3038 * @param htim TIM handle
3039 * @note This mode can be enabled only when the counter period or the compare
3040 * value - as per configured ADC trigger - is a multiple of the ADC clock
3041 * period.
3042 * @retval HAL status
3043 */
HAL_TIMEx_EnableADCSynchronization(TIM_HandleTypeDef * htim)3044 HAL_StatusTypeDef HAL_TIMEx_EnableADCSynchronization(TIM_HandleTypeDef *htim)
3045 {
3046 /* Check the parameters */
3047 assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
3048
3049 SET_BIT(htim->Instance->CR2, TIM_CR2_ADSYNC);
3050 return HAL_OK;
3051 }
3052
3053 /**
3054 * @brief Disable ADC synchronization
3055 * @param htim TIM handle
3056 * @retval HAL status
3057 */
HAL_TIMEx_DisableADCSynchronization(TIM_HandleTypeDef * htim)3058 HAL_StatusTypeDef HAL_TIMEx_DisableADCSynchronization(TIM_HandleTypeDef *htim)
3059 {
3060 /* Check the parameters */
3061 assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
3062
3063 CLEAR_BIT(htim->Instance->CR2, TIM_CR2_ADSYNC);
3064 return HAL_OK;
3065 }
3066
3067 /**
3068 * @}
3069 */
3070
3071 /** @defgroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions
3072 * @brief Extended Callbacks functions
3073 *
3074 @verbatim
3075 ==============================================================================
3076 ##### Extended Callbacks functions #####
3077 ==============================================================================
3078 [..]
3079 This section provides Extended TIM callback functions:
3080 (+) Timer Commutation callback
3081 (+) Timer Break callback
3082
3083 @endverbatim
3084 * @{
3085 */
3086
3087 /**
3088 * @brief Commutation callback in non-blocking mode
3089 * @param htim TIM handle
3090 * @retval None
3091 */
HAL_TIMEx_CommutCallback(TIM_HandleTypeDef * htim)3092 __weak void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
3093 {
3094 /* Prevent unused argument(s) compilation warning */
3095 UNUSED(htim);
3096
3097 /* NOTE : This function should not be modified, when the callback is needed,
3098 the HAL_TIMEx_CommutCallback could be implemented in the user file
3099 */
3100 }
3101 /**
3102 * @brief Commutation half complete callback in non-blocking mode
3103 * @param htim TIM handle
3104 * @retval None
3105 */
HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef * htim)3106 __weak void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim)
3107 {
3108 /* Prevent unused argument(s) compilation warning */
3109 UNUSED(htim);
3110
3111 /* NOTE : This function should not be modified, when the callback is needed,
3112 the HAL_TIMEx_CommutHalfCpltCallback could be implemented in the user file
3113 */
3114 }
3115
3116 /**
3117 * @brief Break detection callback in non-blocking mode
3118 * @param htim TIM handle
3119 * @retval None
3120 */
HAL_TIMEx_BreakCallback(TIM_HandleTypeDef * htim)3121 __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
3122 {
3123 /* Prevent unused argument(s) compilation warning */
3124 UNUSED(htim);
3125
3126 /* NOTE : This function should not be modified, when the callback is needed,
3127 the HAL_TIMEx_BreakCallback could be implemented in the user file
3128 */
3129 }
3130
3131 /**
3132 * @brief Break2 detection callback in non blocking mode
3133 * @param htim: TIM handle
3134 * @retval None
3135 */
HAL_TIMEx_Break2Callback(TIM_HandleTypeDef * htim)3136 __weak void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim)
3137 {
3138 /* Prevent unused argument(s) compilation warning */
3139 UNUSED(htim);
3140
3141 /* NOTE : This function Should not be modified, when the callback is needed,
3142 the HAL_TIMEx_Break2Callback could be implemented in the user file
3143 */
3144 }
3145
3146 /**
3147 * @brief Encoder index callback in non-blocking mode
3148 * @param htim TIM handle
3149 * @retval None
3150 */
HAL_TIMEx_EncoderIndexCallback(TIM_HandleTypeDef * htim)3151 __weak void HAL_TIMEx_EncoderIndexCallback(TIM_HandleTypeDef *htim)
3152 {
3153 /* Prevent unused argument(s) compilation warning */
3154 UNUSED(htim);
3155
3156 /* NOTE : This function should not be modified, when the callback is needed,
3157 the HAL_TIMEx_EncoderIndexCallback could be implemented in the user file
3158 */
3159 }
3160
3161 /**
3162 * @brief Direction change callback in non-blocking mode
3163 * @param htim TIM handle
3164 * @retval None
3165 */
HAL_TIMEx_DirectionChangeCallback(TIM_HandleTypeDef * htim)3166 __weak void HAL_TIMEx_DirectionChangeCallback(TIM_HandleTypeDef *htim)
3167 {
3168 /* Prevent unused argument(s) compilation warning */
3169 UNUSED(htim);
3170
3171 /* NOTE : This function should not be modified, when the callback is needed,
3172 the HAL_TIMEx_DirectionChangeCallback could be implemented in the user file
3173 */
3174 }
3175
3176 /**
3177 * @brief Index error callback in non-blocking mode
3178 * @param htim TIM handle
3179 * @retval None
3180 */
HAL_TIMEx_IndexErrorCallback(TIM_HandleTypeDef * htim)3181 __weak void HAL_TIMEx_IndexErrorCallback(TIM_HandleTypeDef *htim)
3182 {
3183 /* Prevent unused argument(s) compilation warning */
3184 UNUSED(htim);
3185
3186 /* NOTE : This function should not be modified, when the callback is needed,
3187 the HAL_TIMEx_IndexErrorCallback could be implemented in the user file
3188 */
3189 }
3190
3191 /**
3192 * @brief Transition error callback in non-blocking mode
3193 * @param htim TIM handle
3194 * @retval None
3195 */
HAL_TIMEx_TransitionErrorCallback(TIM_HandleTypeDef * htim)3196 __weak void HAL_TIMEx_TransitionErrorCallback(TIM_HandleTypeDef *htim)
3197 {
3198 /* Prevent unused argument(s) compilation warning */
3199 UNUSED(htim);
3200
3201 /* NOTE : This function should not be modified, when the callback is needed,
3202 the HAL_TIMEx_TransitionErrorCallback could be implemented in the user file
3203 */
3204 }
3205
3206 /**
3207 * @}
3208 */
3209
3210 /** @defgroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions
3211 * @brief Extended Peripheral State functions
3212 *
3213 @verbatim
3214 ==============================================================================
3215 ##### Extended Peripheral State functions #####
3216 ==============================================================================
3217 [..]
3218 This subsection permits to get in run-time the status of the peripheral
3219 and the data flow.
3220
3221 @endverbatim
3222 * @{
3223 */
3224
3225 /**
3226 * @brief Return the TIM Hall Sensor interface handle state.
3227 * @param htim TIM Hall Sensor handle
3228 * @retval HAL state
3229 */
HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef * htim)3230 HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(const TIM_HandleTypeDef *htim)
3231 {
3232 return htim->State;
3233 }
3234
3235 /**
3236 * @brief Return actual state of the TIM complementary channel.
3237 * @param htim TIM handle
3238 * @param ChannelN TIM Complementary channel
3239 * This parameter can be one of the following values:
3240 * @arg TIM_CHANNEL_1: TIM Channel 1
3241 * @arg TIM_CHANNEL_2: TIM Channel 2
3242 * @arg TIM_CHANNEL_3: TIM Channel 3
3243 * @arg TIM_CHANNEL_4: TIM Channel 4
3244 * @retval TIM Complementary channel state
3245 */
HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef * htim,uint32_t ChannelN)3246 HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(const TIM_HandleTypeDef *htim, uint32_t ChannelN)
3247 {
3248 HAL_TIM_ChannelStateTypeDef channel_state;
3249
3250 /* Check the parameters */
3251 assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, ChannelN));
3252
3253 channel_state = TIM_CHANNEL_N_STATE_GET(htim, ChannelN);
3254
3255 return channel_state;
3256 }
3257 /**
3258 * @}
3259 */
3260
3261 /**
3262 * @}
3263 */
3264
3265 /* Private functions ---------------------------------------------------------*/
3266 /** @defgroup TIMEx_Private_Functions TIM Extended Private Functions
3267 * @{
3268 */
3269
3270 /**
3271 * @brief TIM DMA Commutation callback.
3272 * @param hdma pointer to DMA handle.
3273 * @retval None
3274 */
TIMEx_DMACommutationCplt(DMA_HandleTypeDef * hdma)3275 void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
3276 {
3277 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3278
3279 /* Change the htim state */
3280 htim->State = HAL_TIM_STATE_READY;
3281
3282 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3283 htim->CommutationCallback(htim);
3284 #else
3285 HAL_TIMEx_CommutCallback(htim);
3286 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3287 }
3288
3289 /**
3290 * @brief TIM DMA Commutation half complete callback.
3291 * @param hdma pointer to DMA handle.
3292 * @retval None
3293 */
TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef * hdma)3294 void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma)
3295 {
3296 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3297
3298 /* Change the htim state */
3299 htim->State = HAL_TIM_STATE_READY;
3300
3301 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3302 htim->CommutationHalfCpltCallback(htim);
3303 #else
3304 HAL_TIMEx_CommutHalfCpltCallback(htim);
3305 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3306 }
3307
3308
3309 /**
3310 * @brief TIM DMA Delay Pulse complete callback (complementary channel).
3311 * @param hdma pointer to DMA handle.
3312 * @retval None
3313 */
TIM_DMADelayPulseNCplt(DMA_HandleTypeDef * hdma)3314 static void TIM_DMADelayPulseNCplt(DMA_HandleTypeDef *hdma)
3315 {
3316 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3317
3318 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
3319 {
3320 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
3321 }
3322 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
3323 {
3324 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
3325 }
3326 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
3327 {
3328 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
3329 }
3330 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
3331 {
3332 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
3333 }
3334 else
3335 {
3336 /* nothing to do */
3337 }
3338
3339 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3340 htim->PWM_PulseFinishedCallback(htim);
3341 #else
3342 HAL_TIM_PWM_PulseFinishedCallback(htim);
3343 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3344
3345 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3346 }
3347
3348 /**
3349 * @brief TIM DMA error callback (complementary channel)
3350 * @param hdma pointer to DMA handle.
3351 * @retval None
3352 */
TIM_DMAErrorCCxN(DMA_HandleTypeDef * hdma)3353 static void TIM_DMAErrorCCxN(DMA_HandleTypeDef *hdma)
3354 {
3355 TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3356
3357 if (hdma == htim->hdma[TIM_DMA_ID_CC1])
3358 {
3359 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
3360 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
3361 }
3362 else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
3363 {
3364 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
3365 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
3366 }
3367 else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
3368 {
3369 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
3370 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
3371 }
3372 else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
3373 {
3374 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
3375 TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
3376 }
3377 else
3378 {
3379 /* nothing to do */
3380 }
3381
3382 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
3383 htim->ErrorCallback(htim);
3384 #else
3385 HAL_TIM_ErrorCallback(htim);
3386 #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
3387
3388 htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
3389 }
3390
3391 /**
3392 * @brief Enables or disables the TIM Capture Compare Channel xN.
3393 * @param TIMx to select the TIM peripheral
3394 * @param Channel specifies the TIM Channel
3395 * This parameter can be one of the following values:
3396 * @arg TIM_CHANNEL_1: TIM Channel 1
3397 * @arg TIM_CHANNEL_2: TIM Channel 2
3398 * @arg TIM_CHANNEL_3: TIM Channel 3
3399 * @arg TIM_CHANNEL_4: TIM Channel 4
3400 * @param ChannelNState specifies the TIM Channel CCxNE bit new state.
3401 * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
3402 * @retval None
3403 */
TIM_CCxNChannelCmd(TIM_TypeDef * TIMx,uint32_t Channel,uint32_t ChannelNState)3404 static void TIM_CCxNChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState)
3405 {
3406 uint32_t tmp;
3407
3408 tmp = TIM_CCER_CC1NE << (Channel & 0xFU); /* 0xFU = 15 bits max shift */
3409
3410 /* Reset the CCxNE Bit */
3411 TIMx->CCER &= ~tmp;
3412
3413 /* Set or reset the CCxNE Bit */
3414 TIMx->CCER |= (uint32_t)(ChannelNState << (Channel & 0xFU)); /* 0xFU = 15 bits max shift */
3415 }
3416 /**
3417 * @}
3418 */
3419
3420 #endif /* HAL_TIM_MODULE_ENABLED */
3421 /**
3422 * @}
3423 */
3424
3425 /**
3426 * @}
3427 */
3428