Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Crash in STM32 driver initialization with multiple motors #437

Open
dekutree64 opened this issue Oct 10, 2024 · 2 comments
Open

[BUG] Crash in STM32 driver initialization with multiple motors #437

dekutree64 opened this issue Oct 10, 2024 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dekutree64
Copy link
Contributor

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
    }
  }
@runger1101001
Copy link
Member

ooh, thanks for spotting this! Definitely a bug :-(

I think we will switch to my new "HAL only" PWM driver for all the STM32 MCUs (including Arduino Portenta) in our next release.

In the "HAL only" branch, I think the error is already fixed:
https://github.com/runger1101001/Arduino-FOC/blob/4d8fa4a6c15c3bce15d536235e0516851359fb34/src/drivers/hardware_specific/stm32/stm32_searchtimers.cpp#L19

@runger1101001 runger1101001 self-assigned this Oct 11, 2024
@runger1101001 runger1101001 added bug Something isn't working and removed possible bug labels Oct 11, 2024
@runger1101001 runger1101001 added this to the 2.3.5_Release milestone Oct 11, 2024
@runger1101001
Copy link
Member

Fixed in #430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants