Skip to content

Commit ebeef3b

Browse files
author
Richard Unger
committed
working to repair software 6-pwm
1 parent 8ddddd2 commit ebeef3b

File tree

1 file changed

+62
-14
lines changed

1 file changed

+62
-14
lines changed

src/drivers/hardware_specific/stm32_mcu.cpp

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ int getTimerNumber(int timerIndex);
1010
#endif
1111

1212

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+
1326
// setting pwm to hardware pin - instead analogWrite()
1427
void _setPwm(HardwareTimer *HT, uint32_t channel, uint32_t value, int resolution)
1528
{
@@ -130,6 +143,38 @@ void _alignPWMTimers(HardwareTimer *HT1, HardwareTimer *HT2, HardwareTimer *HT3,
130143

131144

132145

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+
133178

134179
int getLLChannel(PinMap* timer) {
135180
#if defined(TIM_CCER_CC1NE)
@@ -243,13 +288,6 @@ STM32DriverParams* _initHardware6PWMInterface(long PWM_freq, float dead_zone, Pi
243288

244289

245290

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-
253291
/*
254292
timer combination scoring function
255293
assigns a score, and also checks the combination is valid
@@ -265,7 +303,7 @@ int scoreCombination(int numPins, PinMap* pinTimers[]) {
265303
&& STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(timerPinsUsed[i]->function))
266304
return -2; // bad combination - timer channel already used
267305
}
268-
// check for inverted channels - TODO move this to outer loop also...
306+
// check for inverted channels
269307
if (numPins < 6) {
270308
for (int i=0; i<numPins; i++) {
271309
if (STM_PIN_INVERTED(pinTimers[i]->function))
@@ -309,10 +347,13 @@ int scoreCombination(int numPins, PinMap* pinTimers[]) {
309347
// hardware 6pwm, score <10
310348
}
311349
else {
312-
313350
// check for inverted low-side channels
314351
if (STM_PIN_INVERTED(pinTimers[1]->function) || STM_PIN_INVERTED(pinTimers[3]->function) || STM_PIN_INVERTED(pinTimers[5]->function))
315352
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
316357
score += 10; // software 6pwm, score >10
317358
}
318359
}
@@ -387,12 +428,14 @@ int findBestTimerCombination(int numPins, int index, int pins[], PinMap* pinTime
387428
}
388429

389430

431+
432+
433+
390434
int findBestTimerCombination(int numPins, int pins[], PinMap* pinTimers[]) {
391435
int bestScore = findBestTimerCombination(numPins, 0, pins, pinTimers);
392436
if (bestScore == NOT_FOUND) {
393437
#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");
396439
#endif
397440
return -10; // no workable combination found
398441
}
@@ -476,7 +519,7 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
476519
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinTimers[1]);
477520
HardwareTimer* HT3 = _initPinPWM(pwm_frequency, pinTimers[2]);
478521
// allign the timers
479-
_alignPWMTimers(HT1, HT2, HT3);
522+
//_alignPWMTimers(HT1, HT2, HT3);
480523

481524
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
482525
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
490533
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
491534
timerPinsUsed[numTimerPinsUsed++] = pinTimers[1];
492535
timerPinsUsed[numTimerPinsUsed++] = pinTimers[2];
536+
537+
_alignTimersNew();
538+
493539
return params;
494540
}
495541

@@ -619,8 +665,10 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
619665
.interface_type = _SOFTWARE_6PWM
620666
};
621667
}
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+
}
624672
return params; // success
625673
}
626674

0 commit comments

Comments
 (0)