Arduino-FOC
Arduino-FOC copied to clipboard
[BUG] Crash in STM32 driver initialization with multiple motors
This bit of code in stm32_mcu.cpp reads off the end of the pinTimers array if there is more than one motor. It doesn't always crash, which made it very difficult to narrow down where the problem was.
int scoreCombination(int numPins, PinMap* pinTimers[]) {
// check already used - TODO move this to outer loop also...
for (int i=0; i<numTimerPinsUsed; i++) {
if (pinTimers[i]->peripheral == timerPinsUsed[i]->peripheral
&& STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(timerPinsUsed[i]->function))
return -2; // bad combination - timer channel already used
}
...
I think this is the proper fix for it, but hopefully whoever wrote the original can verify that the logic is correct.
// check already used - TODO move this to outer loop also...
for (int i=0; i<numPins; i++) {
for (int j=0; j<numTimerPinsUsed; j++) {
if (pinTimers[i] != NP && pinTimers[i]->peripheral == timerPinsUsed[j]->peripheral
&& STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(timerPinsUsed[j]->function))
return -2; // bad combination - timer channel already used
}
}