@@ -10,6 +10,19 @@ int getTimerNumber(int timerIndex);
10
10
#endif
11
11
12
12
13
+
14
+
15
+ #ifndef SIMPLEFOC_STM32_MAX_PINTIMERSUSED
16
+ #define SIMPLEFOC_STM32_MAX_PINTIMERSUSED 12
17
+ #endif
18
+ int numTimerPinsUsed;
19
+ PinMap* timerPinsUsed[SIMPLEFOC_STM32_MAX_PINTIMERSUSED];
20
+
21
+
22
+
23
+
24
+
25
+
13
26
// setting pwm to hardware pin - instead analogWrite()
14
27
void _setPwm (HardwareTimer *HT, uint32_t channel, uint32_t value, int resolution)
15
28
{
@@ -130,6 +143,38 @@ void _alignPWMTimers(HardwareTimer *HT1, HardwareTimer *HT2, HardwareTimer *HT3,
130
143
131
144
132
145
146
+ void _alignTimersNew () {
147
+ int numTimers = 0 ;
148
+ HardwareTimer *timers[numTimerPinsUsed];
149
+
150
+ // reset timer counters
151
+ for (int i=0 ; i<numTimerPinsUsed; i++) {
152
+ uint32_t index = get_timer_index ((TIM_TypeDef*)timerPinsUsed[i]->peripheral );
153
+ HardwareTimer *timer = (HardwareTimer *)(HardwareTimer_Handle[index]->__this );
154
+ bool found = false ;
155
+ for (int j=0 ; j<numTimers; j++) {
156
+ if (timers[j] == timer) {
157
+ found = true ;
158
+ break ;
159
+ }
160
+ }
161
+ if (!found)
162
+ timers[numTimers++] = timer;
163
+ }
164
+
165
+ // enable timer clock
166
+ for (int i=0 ; i<numTimers; i++) {
167
+ timers[i]->pause ();
168
+ timers[i]->refresh ();
169
+ }
170
+
171
+ for (int i=0 ; i<numTimers; i++)
172
+ timers[i]->resume ();
173
+
174
+ }
175
+
176
+
177
+
133
178
134
179
int getLLChannel (PinMap* timer) {
135
180
#if defined(TIM_CCER_CC1NE)
@@ -243,13 +288,6 @@ STM32DriverParams* _initHardware6PWMInterface(long PWM_freq, float dead_zone, Pi
243
288
244
289
245
290
246
- #ifndef SIMPLEFOC_STM32_MAX_PINTIMERSUSED
247
- #define SIMPLEFOC_STM32_MAX_PINTIMERSUSED 12
248
- #endif
249
- int numTimerPinsUsed;
250
- PinMap* timerPinsUsed[SIMPLEFOC_STM32_MAX_PINTIMERSUSED];
251
-
252
-
253
291
/*
254
292
timer combination scoring function
255
293
assigns a score, and also checks the combination is valid
@@ -265,7 +303,7 @@ int scoreCombination(int numPins, PinMap* pinTimers[]) {
265
303
&& STM_PIN_CHANNEL (pinTimers[i]->function ) == STM_PIN_CHANNEL (timerPinsUsed[i]->function ))
266
304
return -2 ; // bad combination - timer channel already used
267
305
}
268
- // check for inverted channels - TODO move this to outer loop also...
306
+ // check for inverted channels
269
307
if (numPins < 6 ) {
270
308
for (int i=0 ; i<numPins; i++) {
271
309
if (STM_PIN_INVERTED (pinTimers[i]->function ))
@@ -309,10 +347,13 @@ int scoreCombination(int numPins, PinMap* pinTimers[]) {
309
347
// hardware 6pwm, score <10
310
348
}
311
349
else {
312
-
313
350
// check for inverted low-side channels
314
351
if (STM_PIN_INVERTED (pinTimers[1 ]->function ) || STM_PIN_INVERTED (pinTimers[3 ]->function ) || STM_PIN_INVERTED (pinTimers[5 ]->function ))
315
352
return -6 ; // bad combination - inverted channel used on low-side channel in software 6-pwm
353
+ if (pinTimers[0 ]->peripheral != pinTimers[1 ]->peripheral
354
+ || pinTimers[2 ]->peripheral != pinTimers[3 ]->peripheral
355
+ || pinTimers[4 ]->peripheral != pinTimers[5 ]->peripheral )
356
+ return -7 ; // bad combination - non-matching timers for H/L side in software 6-pwm
316
357
score += 10 ; // software 6pwm, score >10
317
358
}
318
359
}
@@ -387,12 +428,14 @@ int findBestTimerCombination(int numPins, int index, int pins[], PinMap* pinTime
387
428
}
388
429
389
430
431
+
432
+
433
+
390
434
int findBestTimerCombination (int numPins, int pins[], PinMap* pinTimers[]) {
391
435
int bestScore = findBestTimerCombination (numPins, 0 , pins, pinTimers);
392
436
if (bestScore == NOT_FOUND) {
393
437
#ifdef SIMPLEFOC_STM32_DEBUG
394
- SimpleFOCDebug::print (" STM32: no workable combination found on these pins " );
395
- printTimerCombination (numPins, pinTimers, bestScore);
438
+ SimpleFOCDebug::println (" STM32: no workable combination found on these pins" );
396
439
#endif
397
440
return -10 ; // no workable combination found
398
441
}
@@ -476,7 +519,7 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
476
519
HardwareTimer* HT2 = _initPinPWM (pwm_frequency, pinTimers[1 ]);
477
520
HardwareTimer* HT3 = _initPinPWM (pwm_frequency, pinTimers[2 ]);
478
521
// allign the timers
479
- _alignPWMTimers (HT1, HT2, HT3);
522
+ // _alignPWMTimers(HT1, HT2, HT3);
480
523
481
524
uint32_t channel1 = STM_PIN_CHANNEL (pinTimers[0 ]->function );
482
525
uint32_t channel2 = STM_PIN_CHANNEL (pinTimers[1 ]->function );
@@ -490,6 +533,9 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
490
533
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0 ];
491
534
timerPinsUsed[numTimerPinsUsed++] = pinTimers[1 ];
492
535
timerPinsUsed[numTimerPinsUsed++] = pinTimers[2 ];
536
+
537
+ _alignTimersNew ();
538
+
493
539
return params;
494
540
}
495
541
@@ -619,8 +665,10 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
619
665
.interface_type = _SOFTWARE_6PWM
620
666
};
621
667
}
622
- for (int i=0 ; i<6 ; i++)
623
- timerPinsUsed[numTimerPinsUsed++] = pinTimers[i];
668
+ if (params>=0 ) {
669
+ for (int i=0 ; i<6 ; i++)
670
+ timerPinsUsed[numTimerPinsUsed++] = pinTimers[i];
671
+ }
624
672
return params; // success
625
673
}
626
674
0 commit comments