uStepper / uStepperS32

7 stars 3 forks source link

I2C disrupted by setup() #1

Closed semedek19 closed 1 year ago

semedek19 commented 1 year ago

For starters, great repo and work. I have some UStepperS32 controllers that I am trying to use with an I2C bus for communication. I've found that running setup() for the UstepperS32 object in my sketch causes the I2C to randomly hang on the secondary device.

I am using the Arduino IDE 2.0.4, UStepper STM Boards 1.0.0, and the UStepperS32 1.0.0 library.

I am currently trying to use 2 UStepperS32 controllers. I have wire connections for GND -> GND, PB9 -> PB9, PB8 -> PB8, and PB5 -> PB5. Each UStepperS32 is connected to my computer via USB.

My primary controller is running the following sketch:

#include <Wire.h>

const int SDA_PIN = PB9;
const int SCL_PIN = PB8;
const int SECONDARYIN = PA5;

int good = 0;
int bad = 0;
int result;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Wire.setSCL(SDA_PIN);
  Wire.setSDA(SCL_PIN);

  pinMode(SECONDARYIN, INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  waitUntilReady();
  delay(10);
  Wire.beginTransmission(0x14);
  Wire.write("H");
  result = Wire.endTransmission();
  delay(10);

  if(result == 0)
    good++;
  else
    bad++;

  if ((good + bad) % 100 == 0){
    Serial.println("Finished " + String(good + bad) + " Transmissions");
    Serial.println(String(good) + " Good");
    Serial.println(String(bad) + " Bad");
    Serial.println("\n\n");
  }
}

void waitUntilReady(){
  while(digitalRead(SECONDARYIN) == LOW){
    delay(20);
  }
}

Running the following sketch, the two controllers work as expected with I2C and good continues to increment by 100 while bad stays at zero. I get repeated Receive messages from the secondary controller.

#include <Wire.h>
#include <UstepperS32.h>

const int SECONDARYOUT = PA5;
volatile bool message = false;

UstepperS32 stepper;

void setup() {
  pinMode(SECONDARYOUT, OUTPUT);
  digitalWrite(SECONDARYOUT, HIGH);

  Serial.begin(9600);

  // stepper.setup();
  // stepper.disablePid();

  Wire.begin(0x14);
  Wire.onReceive(OnReceive);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(message){
    delay(500);
    message = false;
    digitalWrite(SECONDARYOUT, HIGH);
  }
  delay(100);
}

void OnReceive(int numBytes){
  digitalWrite(SECONDARYOUT, LOW);
  Serial.println("Receive");
  while(Wire.available()){
    Wire.read();
  }
  message = true;
}

The problem is if I uncomment stepper.setup() then I only get between 1-30 transmissions before the secondary controller appears to hang. Running disablePid() does not change the behavior. The observations I've made so far:

Am I missing something about the setup of the UStepperS32 objects? Thanks in advance.

uStepperOld commented 1 year ago

Hi semedek19,

Thank you for the nice words !

we are aware of the issue, and we are currently working on fixing it as we speak.

Best regards, Thomas Olsen

semedek19 commented 1 year ago

hi! I am just checking to see if there are any updates on this issue?

uStepperOld commented 1 year ago

Hi ,

I found the issue, and have pushed the fix to the "develop" branch. we have to fully test the branch before we are able to release it, but in the meantime, you can download this branch and replace your library installation with it

Best Regards, Thomas Olsen

semedek19 commented 1 year ago

I went ahead and loaded dev as my library and it appears to have fixed the issue. Much thanks!