Skip to content

Commit

Permalink
add comments for triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed Oct 3, 2024
1 parent e535bba commit 4d8fa4a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/drivers/hardware_specific/stm32/stm32_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ TIM_HandleTypeDef* stm32_useTimer(PinMap* timer) {
handle->hdma[6] = NULL;
handle->Init.Prescaler = 0;
handle->Init.Period = ((1 << 16) - 1);
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
#if defined(TIM_RCR_REP)
Expand Down Expand Up @@ -201,19 +201,21 @@ else inactive. In downcounting, channel 1 is inactive (OC1REF=‘0’) as long a
TIMx_CNT>TIMx_CCR1 else active (OC1REF=’1’).
0111: PWM mode 2 - In upcounting, channel 1 is inactive as long as
TIMx_CNT<TIMx_CCR1 else active. In downcounting, channel 1 is active as long as
TIMx_CNT>TIMx_CCR1 else inactiv
TIMx_CNT>TIMx_CCR1 else inactive
*/
// init high side pin
TIM_HandleTypeDef* _stm32_initPinPWMHigh(uint32_t PWM_freq, PinMap* timer) {
uint32_t polarity = SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW;
uint32_t polarity = SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW ;
TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM1, polarity);
LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
return handle;
}

// init low side pin
TIM_HandleTypeDef* _stm32_initPinPWMLow(uint32_t PWM_freq, PinMap* timer) {
uint32_t polarity = SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH ? TIM_OCPOLARITY_HIGH : TIM_OCPOLARITY_LOW;
TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM2, polarity);
LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
return handle;
}

Expand Down Expand Up @@ -322,6 +324,8 @@ STM32DriverParams* _stm32_initHardware6PWMPair(long PWM_freq, float dead_zone, P
params->timers_handle[paramsPos+1] = handle;
params->channels[paramsPos] = channel1;
params->channels[paramsPos+1] = channel2;
params->llchannels[paramsPos] = stm32_getLLChannel(pinH);
params->llchannels[paramsPos+1] = stm32_getLLChannel(pinL);
return params;
}

Expand Down
1 change: 1 addition & 0 deletions src/drivers/hardware_specific/stm32/stm32_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../../hardware_api.h"

#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
// TARGET_M4 / TARGET_M7

#ifndef SIMPLEFOC_STM32_MAX_TIMERSUSED
#define SIMPLEFOC_STM32_MAX_TIMERSUSED 6
Expand Down
29 changes: 20 additions & 9 deletions src/drivers/hardware_specific/stm32/stm32_timerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ TIM_HandleTypeDef* stm32_alignTimers(TIM_HandleTypeDef *timers_in[], uint8_t num


#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Syncronising timers! Timer no. ", numTimers);
SimpleFOCDebug::print("STM32-DRV: Synchronising ");
SimpleFOCDebug::print(numTimers);
SimpleFOCDebug::println(" timers");
#endif

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

// configure other timers to get the input trigger from the master timer
for (int slave_index=0; slave_index < numTimers; slave_index++) {
Expand All @@ -451,15 +453,24 @@ TIM_HandleTypeDef* stm32_alignTimers(TIM_HandleTypeDef *timers_in[], uint8_t num
SIMPLEFOC_DEBUG("STM32-DRV: slave timer: TIM", stm32_getTimerNumber(timers[slave_index]->Instance));
#endif
// Configure the slave timer to be triggered by the master enable signal
LL_TIM_SetTriggerInput(timers[slave_index]->Instance, stm32_getInternalSourceTrigger(timers[master_index], timers[slave_index]));
#if defined(STM32G4xx)
LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_COMBINED_GATEDRESET);
#else
uint32_t trigger = stm32_getInternalSourceTrigger(timers[master_index], timers[slave_index]);
// #ifdef SIMPLEFOC_STM32_DEBUG
// SIMPLEFOC_DEBUG("STM32-DRV: slave trigger ITR ", (int)trigger);
// #endif
LL_TIM_SetTriggerInput(timers[slave_index]->Instance, trigger);
// #if defined(STM32G4xx)
// LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_COMBINED_GATEDRESET);
// #else
LL_TIM_SetSlaveMode(timers[slave_index]->Instance, LL_TIM_SLAVEMODE_GATED);
#endif
// #endif
}
for (int i=0; i<numTimers; i++) { // resume the timers TODO at the moment the first PWM cycle is not well-aligned
stm32_refreshTimer(timers[i]);
if (i != master_index)
stm32_resumeTimer(timers[i]);
SIMPLEFOC_DEBUG("STM32-DRV: slave counter: ", (int)timers[i]->Instance->CNT);
}
for (int i=0; i<numTimers; i++) // resume the timers TODO at the moment the first PWM cycle is not well-aligned
stm32_resumeTimer(timers[i]);
stm32_resumeTimer(timers[master_index]);
return timers[master_index];
}
}
Expand Down

0 comments on commit 4d8fa4a

Please sign in to comment.