@@ -204,30 +204,105 @@ void _stopTimers(HardwareTimer **timers_to_stop, int timer_num)
204204}
205205
206206
207- // function finds the appropriate timer source trigger for the master timer index provided
207+ #if defined(STM32G4xx)
208+ // function finds the appropriate timer source trigger for the master/slave timer combination
208209// returns -1 if no trigger source is found
209210// currently supports the master timers to be from TIM1 to TIM4 and TIM8
210- int _getTriggerSourceRegister (HardwareTimer* timer) {
211+ int _getInternalSourceTrigger (HardwareTimer* master, HardwareTimer* slave) { // put master and slave in temp variables to avoid arrows
212+ TIM_TypeDef *TIM_master = master->getHandle ()->Instance ;
211213 #if defined(TIM1) && defined(LL_TIM_TS_ITR0)
212- if (timer-> getHandle ()-> Instance == TIM1) return LL_TIM_TS_ITR0;// return TIM_TS_ITR0;
214+ if (TIM_master == TIM1) return LL_TIM_TS_ITR0;// return TIM_TS_ITR0;
213215 #endif
214216 #if defined(TIM2) && defined(LL_TIM_TS_ITR1)
215- if (timer-> getHandle ()-> Instance == TIM2) return LL_TIM_TS_ITR1;// return TIM_TS_ITR1;
217+ else if (TIM_master == TIM2) return LL_TIM_TS_ITR1;// return TIM_TS_ITR1;
216218 #endif
217219 #if defined(TIM3) && defined(LL_TIM_TS_ITR2)
218- if (timer-> getHandle ()-> Instance == TIM3) return LL_TIM_TS_ITR2;// return TIM_TS_ITR2;
220+ else if (TIM_master == TIM3) return LL_TIM_TS_ITR2;// return TIM_TS_ITR2;
219221 #endif
220222 #if defined(TIM4) && defined(LL_TIM_TS_ITR3)
221- if (timer-> getHandle ()-> Instance == TIM4) return LL_TIM_TS_ITR3;// return TIM_TS_ITR3;
223+ else if (TIM_master == TIM4) return LL_TIM_TS_ITR3;// return TIM_TS_ITR3;
222224 #endif
223225 #if defined(TIM5) && defined(LL_TIM_TS_ITR4)
224- if (timer-> getHandle ()-> Instance == TIM5) return LL_TIM_TS_ITR4;// return TIM_TS_ITR4;
226+ else if (TIM_master == TIM5) return LL_TIM_TS_ITR4;// return TIM_TS_ITR4;
225227 #endif
226228 #if defined(TIM8) && defined(LL_TIM_TS_ITR5)
227- if (timer-> getHandle ()-> Instance == TIM8) return LL_TIM_TS_ITR5;// return TIM_TS_ITR5;
229+ else if (TIM_master == TIM8) return LL_TIM_TS_ITR5;// return TIM_TS_ITR5;
228230 #endif
229231 return -1 ;
230232}
233+ #elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx)
234+
235+ // function finds the appropriate timer source trigger for the master/slave timer combination
236+ // returns -1 if no trigger source is found
237+ // currently supports the master timers to be from TIM1 to TIM4 and TIM8
238+ int _getTriggerSourceRegister (HardwareTimer* master, HardwareTimer* slave) {
239+ // put master and slave in temp variables to avoid arrows
240+ TIM_TypeDef *TIM_master = master->getHandle ()->Instance ;
241+ TIM_TypeDef *TIM_slave = slave->getHandle ()->Instance ;
242+ #if defined(TIM1) && defined(LL_TIM_TS_ITR0)
243+ if (TIM_master == TIM1){
244+ if (TIM_slave == TIM2 || TIM_slave == TIM3 || TIM_slave == TIM4) return LL_TIM_TS_ITR0;
245+ #if defined(TIM8)
246+ else if (TIM_slave == TIM8) return LL_TIM_TS_ITR0;
247+ #endif
248+ }
249+ #endif
250+ #if defined(TIM2) && defined(LL_TIM_TS_ITR1)
251+ else if (TIM_master == TIM2){
252+ if (TIM_slave == TIM1 || TIM_slave == TIM3 || TIM_slave == TIM4) return LL_TIM_TS_ITR1;
253+ #if defined(TIM8)
254+ else if (TIM_slave == TIM8) return LL_TIM_TS_ITR1;
255+ #endif
256+ #if defined(TIM5)
257+ else if (TIM_slave == TIM5) return LL_TIM_TS_ITR0;
258+ #endif
259+ }
260+ #endif
261+ #if defined(TIM3) && defined(LL_TIM_TS_ITR2)
262+ else if (TIM_master == TIM3){
263+ if (TIM_slave== TIM1 || TIM_slave == TIM2 || TIM_slave == TIM4) return LL_TIM_TS_ITR2;
264+ #if defined(TIM5)
265+ else if (TIM_slave == TIM5) return LL_TIM_TS_ITR1;
266+ #endif
267+ }
268+ #endif
269+ #if defined(TIM4) && defined(LL_TIM_TS_ITR3)
270+ else if (TIM_master == TIM4){
271+ if (TIM_slave == TIM1 || TIM_slave == TIM2 || TIM_slave == TIM3) return LL_TIM_TS_ITR3;
272+ #if defined(TIM8)
273+ else if (TIM_slave == TIM8) return LL_TIM_TS_ITR2;
274+ #endif
275+ #if defined(TIM5)
276+ else if (TIM_slave == TIM5) return LL_TIM_TS_ITR1;
277+ #endif
278+ }
279+ #endif
280+ #if defined(TIM5)
281+ else if (TIM_master == TIM5){
282+ #if !defined(STM32L4xx) // only difference between F4,F1 and L4
283+ if (TIM_slave == TIM1) return LL_TIM_TS_ITR0;
284+ else if (TIM_slave == TIM3) return LL_TIM_TS_ITR2;
285+ #endif
286+ #if defined(TIM8)
287+ if (TIM_slave == TIM8) return LL_TIM_TS_ITR3;
288+ #endif
289+ }
290+ #endif
291+ #if defined(TIM8)
292+ else if (TIM_master == TIM8){
293+ if (TIM_slave==TIM2) return LL_TIM_TS_ITR1;
294+ else if (TIM_slave ==TIM4 || TIM_slave ==TIM5) return LL_TIM_TS_ITR3;
295+ }
296+ #endif
297+ return -1 ; // combination not supported
298+ }
299+ #else
300+ // Alignment not supported for this architecture
301+ int _getTriggerSourceRegister (HardwareTimer* master, HardwareTimer* slave) {
302+ return -1 ;
303+ }
304+ #endif
305+
231306
232307void _alignTimersNew () {
233308 int numTimers = 0 ;
@@ -256,44 +331,50 @@ void _alignTimersNew() {
256331 // if yes, try to align timers
257332 if (numTimers > 1 ){
258333 // find the master timer
259- uint8_t masterTimerIndex = 0 ;
334+ int16_t master_index = - 1 ;
260335 int triggerEvent = -1 ;
261336 for (int i=0 ; i<numTimers; i++) {
262337 // check if timer can be master
263338 if (IS_TIM_MASTER_INSTANCE (timers[i]->getHandle ()->Instance )) {
264339 // check if timer already configured in TRGO update mode (used for ADC triggering)
265340 // in that case we should not change its TRGO configuration
266341 if (timers[i]->getHandle ()->Instance ->CR2 & LL_TIM_TRGO_UPDATE) continue ;
267- // check if it has the supported internal trigger
268- triggerEvent = _getTriggerSourceRegister (timers[i]);
269- if (triggerEvent == -1 ) continue ; // not supported keep searching
270- masterTimerIndex = i; // found the master timer
342+ // check if the timer has the supported internal trigger for other timers
343+ for (int slave_i=0 ; slave_i<numTimers; slave_i++) {
344+ if (i==slave_i) continue ; // skip self
345+ // check if it has the supported internal trigger
346+ triggerEvent = _getTriggerSourceRegister (timers[i],timers[slave_i]);
347+ if (triggerEvent == -1 ) break ; // not supported keep searching
348+ }
349+ if (triggerEvent == -1 ) continue ; // cannot be master, keep searching
350+ // otherwise the master has been found, remember the index
351+ master_index = i; // found the master timer
271352 break ;
272353 }
273354 }
274355
275356
276357 // if no master timer found do not perform alignment
277- if (triggerEvent == -1 ) {
358+ if (master_index == -1 ) {
278359 #ifdef SIMPLEFOC_STM32_DEBUG
279360 SIMPLEFOC_DEBUG (" STM32-DRV: ERR: No master timer found, cannot align timers!" );
280361 #endif
281362 }else {
282363 #ifdef SIMPLEFOC_STM32_DEBUG
283- SIMPLEFOC_DEBUG (" STM32-DRV: Aligning PWM to master timer: " , getTimerNumber (get_timer_index (timers[masterTimerIndex ]->getHandle ()->Instance )));
364+ SIMPLEFOC_DEBUG (" STM32-DRV: Aligning PWM to master timer: " , getTimerNumber (get_timer_index (timers[master_index ]->getHandle ()->Instance )));
284365 #endif
285366 // make the master timer generate ITRGx event
286367 // if it was already configured in slave mode
287- LL_TIM_SetSlaveMode (timers[masterTimerIndex ]->getHandle ()->Instance , LL_TIM_SLAVEMODE_DISABLED );
368+ LL_TIM_SetSlaveMode (timers[master_index ]->getHandle ()->Instance , LL_TIM_SLAVEMODE_DISABLED );
288369 // Configure the master timer to send a trigger signal on enable
289- LL_TIM_SetTriggerOutput (timers[masterTimerIndex ]->getHandle ()->Instance , LL_TIM_TRGO_ENABLE);
370+ LL_TIM_SetTriggerOutput (timers[master_index ]->getHandle ()->Instance , LL_TIM_TRGO_ENABLE);
290371 // configure other timers to get the input trigger from the master timer
291- for (int i =0 ; i< numTimers; i ++) {
292- if (i==masterTimerIndex )
372+ for (int slave_index =0 ; slave_index < numTimers; slave_index ++) {
373+ if (slave_index == master_index )
293374 continue ;
294375 // Configure the slave timer to be triggered by the master enable signal
295- LL_TIM_SetTriggerInput (timers[i ]->getHandle ()->Instance , triggerEvent );
296- LL_TIM_SetSlaveMode (timers[i ]->getHandle ()->Instance , LL_TIM_SLAVEMODE_TRIGGER);
376+ LL_TIM_SetTriggerInput (timers[slave_index ]->getHandle ()->Instance , _getTriggerSourceRegister (timers[master_index], timers[slave_index]) );
377+ LL_TIM_SetSlaveMode (timers[slave_index ]->getHandle ()->Instance , LL_TIM_SLAVEMODE_TRIGGER);
297378 }
298379 }
299380 }
0 commit comments