Skip to content

Commit 4d8fa4a

Browse files
committed
add comments for triggers
1 parent e535bba commit 4d8fa4a

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/drivers/hardware_specific/stm32/stm32_mcu.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ TIM_HandleTypeDef* stm32_useTimer(PinMap* timer) {
8989
handle->hdma[6] = NULL;
9090
handle->Init.Prescaler = 0;
9191
handle->Init.Period = ((1 << 16) - 1);
92-
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
92+
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
9393
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
9494
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
9595
#if defined(TIM_RCR_REP)
@@ -201,19 +201,21 @@ else inactive. In downcounting, channel 1 is inactive (OC1REF=‘0’) as long a
201201
TIMx_CNT>TIMx_CCR1 else active (OC1REF=’1’).
202202
0111: PWM mode 2 - In upcounting, channel 1 is inactive as long as
203203
TIMx_CNT<TIMx_CCR1 else active. In downcounting, channel 1 is active as long as
204-
TIMx_CNT>TIMx_CCR1 else inactiv
204+
TIMx_CNT>TIMx_CCR1 else inactive
205205
*/
206206
// init high side pin
207207
TIM_HandleTypeDef* _stm32_initPinPWMHigh(uint32_t PWM_freq, PinMap* timer) {
208-
uint32_t polarity = SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW;
208+
uint32_t polarity = SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW ;
209209
TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM1, polarity);
210+
LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
210211
return handle;
211212
}
212213

213214
// init low side pin
214215
TIM_HandleTypeDef* _stm32_initPinPWMLow(uint32_t PWM_freq, PinMap* timer) {
215216
uint32_t polarity = SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW;
216217
TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM2, polarity);
218+
LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
217219
return handle;
218220
}
219221

@@ -322,6 +324,8 @@ STM32DriverParams* _stm32_initHardware6PWMPair(long PWM_freq, float dead_zone, P
322324
params->timers_handle[paramsPos+1] = handle;
323325
params->channels[paramsPos] = channel1;
324326
params->channels[paramsPos+1] = channel2;
327+
params->llchannels[paramsPos] = stm32_getLLChannel(pinH);
328+
params->llchannels[paramsPos+1] = stm32_getLLChannel(pinL);
325329
return params;
326330
}
327331

src/drivers/hardware_specific/stm32/stm32_mcu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../../hardware_api.h"
44

55
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
6+
// TARGET_M4 / TARGET_M7
67

78
#ifndef SIMPLEFOC_STM32_MAX_TIMERSUSED
89
#define SIMPLEFOC_STM32_MAX_TIMERSUSED 6

src/drivers/hardware_specific/stm32/stm32_timerutils.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ TIM_HandleTypeDef* stm32_alignTimers(TIM_HandleTypeDef *timers_in[], uint8_t num
397397

398398

399399
#ifdef SIMPLEFOC_STM32_DEBUG
400-
SIMPLEFOC_DEBUG("STM32-DRV: Syncronising timers! Timer no. ", numTimers);
400+
SimpleFOCDebug::print("STM32-DRV: Synchronising ");
401+
SimpleFOCDebug::print(numTimers);
402+
SimpleFOCDebug::println(" timers");
401403
#endif
402404

403405
// see if there is more then 1 timers used for the pwm
@@ -441,7 +443,7 @@ TIM_HandleTypeDef* stm32_alignTimers(TIM_HandleTypeDef *timers_in[], uint8_t num
441443
LL_TIM_SetSlaveMode(timers[master_index]->Instance, LL_TIM_SLAVEMODE_DISABLED );
442444
// Configure the master timer to send a trigger signal on enable
443445
LL_TIM_SetTriggerOutput(timers[master_index]->Instance, LL_TIM_TRGO_ENABLE);
444-
LL_TIM_EnableMasterSlaveMode(timers[master_index]->Instance);
446+
//LL_TIM_EnableMasterSlaveMode(timers[master_index]->Instance);
445447

446448
// configure other timers to get the input trigger from the master timer
447449
for (int slave_index=0; slave_index < numTimers; slave_index++) {
@@ -451,15 +453,24 @@ TIM_HandleTypeDef* stm32_alignTimers(TIM_HandleTypeDef *timers_in[], uint8_t num
451453
SIMPLEFOC_DEBUG("STM32-DRV: slave timer: TIM", stm32_getTimerNumber(timers[slave_index]->Instance));
452454
#endif
453455
// Configure the slave timer to be triggered by the master enable signal
454-
LL_TIM_SetTriggerInput(timers[slave_index]->Instance, stm32_getInternalSourceTrigger(timers[master_index], timers[slave_index]));
455-
#if defined(STM32G4xx)
456-
LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_COMBINED_GATEDRESET);
457-
#else
456+
uint32_t trigger = stm32_getInternalSourceTrigger(timers[master_index], timers[slave_index]);
457+
// #ifdef SIMPLEFOC_STM32_DEBUG
458+
// SIMPLEFOC_DEBUG("STM32-DRV: slave trigger ITR ", (int)trigger);
459+
// #endif
460+
LL_TIM_SetTriggerInput(timers[slave_index]->Instance, trigger);
461+
// #if defined(STM32G4xx)
462+
// LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_COMBINED_GATEDRESET);
463+
// #else
458464
LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_GATED);
459-
#endif
465+
// #endif
466+
}
467+
for (int i=0; i<numTimers; i++) { // resume the timers TODO at the moment the first PWM cycle is not well-aligned
468+
stm32_refreshTimer(timers[i]);
469+
if (i != master_index)
470+
stm32_resumeTimer(timers[i]);
471+
SIMPLEFOC_DEBUG("STM32-DRV: slave counter: ", (int)timers[i]->Instance->CNT);
460472
}
461-
for (int i=0; i<numTimers; i++) // resume the timers TODO at the moment the first PWM cycle is not well-aligned
462-
stm32_resumeTimer(timers[i]);
473+
stm32_resumeTimer(timers[master_index]);
463474
return timers[master_index];
464475
}
465476
}

0 commit comments

Comments
 (0)