janelia-arduino / TMC2209

The TMC2209 is an ultra-silent motor driver IC for two phase stepper motors with both UART serial and step and direction interfaces.
Other
186 stars 29 forks source link

Bigtree and M4 Express; communication not working #59

Open Oegtsgeest opened 8 months ago

Oegtsgeest commented 8 months ago

Hi, My setup with an Adafruit M4-expressboard with a bigtree TMC2209 works when I use serial1. However I need the serial1 fo a GPS shield, so I created Serial2 by using sercom code. This created serialport on A3 (Rx) and A2 (Tx) is tested seperately and works.

But I can not get it to work with the TMC2209. The biderectional testcommunication program fails. Probably it has to do that I need an alternate Rx and Tx? But I do not understand then why the HardwareSerial & serial_stream = Serial2 statement does not work. Would it be enough if I change all the ESP32 words for for SAMD51 in the TMC2209.h and TMC2209.cpp? This before I start messing with the library. :-) Please find below the code of the program. Help is appreciated. Joost

include

include

include "wiring_private.h"

// Serial2 on SERCOM4

define PIN_SERIAL2_RX A3 // RX

define PIN_SERIAL2_TX A2 // Tx

define PAD_SERIAL2_RX (SERCOM_RX_PAD_1)

define PAD_SERIAL2_TX (UART_TX_PAD_0)

const long SERIAL_BAUD_RATE = 115200;

Uart Serial2(&sercom4, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);

// Instantiate TMC2209 TMC2209 stepper_driver; HardwareSerial & serial_stream = Serial2; // this connects the Rx and Tx pins to the libary

void SERCOM4_0_Handler() // required to get sercom4 working { Serial2.IrqHandler(); } void SERCOM4_1_Handler() { Serial2.IrqHandler(); } void SERCOM4_2_Handler() { Serial2.IrqHandler(); } void SERCOM4_3_Handler() { Serial2.IrqHandler(); }

void setup() { Serial.begin(115200); while (!Serial) {} Serial.println("Begin set-up"); Serial2.begin(115200); // Assign pins 12 & 13 SERCOM functionality pinPeripheral(PIN_SERIAL2_RX, PIO_SERCOM_ALT); pinPeripheral(PIN_SERIAL2_TX, PIO_SERCOM_ALT);

stepper_driver.setup(serial_stream); Serial.println("Set-up completed"); }

void loop() {

Serial.println("start loop");

if (stepper_driver.isSetupAndCommunicating()) { Serial.println("Stepper driver is setup and communicating!"); Serial.println("Try turning driver power off to see what happens."); } else if (stepper_driver.isCommunicatingButNotSetup()) { Serial.println("Stepper driver is communicating but not setup!"); Serial.println("Running setup again..."); stepper_driver.setup(serial_stream); } else { Serial.println("Stepper driver is not communicating!"); Serial.println("Try turning driver power on to see what happens."); } Serial.println(); stepper_driver.getVersion(); stepper_driver.getSettings(); delay(2000); }

peterpolidoro commented 8 months ago

How does it fail exactly? Your code seems to compile. Do you mean that when you run it you are unable to communicate with the TMC2209? Do you have an oscilloscope? Can you look at the serial lines and see if it is sending the messages properly?

Oegtsgeest commented 8 months ago

Thx for replying promptly. I did change in th TMC2209.h and .cpp files as follows #if defined ESP32 || defined SAMD51 And yes I have an oscilloscope but first i need to get the error solved. But at compiling I get following error:

d:......\Arduino\Sketches\libraries\TMC2209\src\TMC2209\TMC2209.cpp: In member function 'void TMC2209::setup(HardwareSerial&, long int, TMC2209::SerialAddress, int16_t, int16_t)': d:......\Arduino\Sketches\libraries\TMC2209\src\TMC2209\TMC2209.cpp:32:36: error: no matching function for call to 'HardwareSerial::end(bool)' 32 | hardware_serialptr->end(false); | ^ In file included from C:\Users...\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/Arduino.h:77, from d:.......\Arduino\Sketches\libraries\TMC2209\src/TMC2209.h:10, from d:.......\Arduino\Sketches\libraries\TMC2209\src\TMC2209\TMC2209.cpp:7: C:\Users.....\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:72:18: note: candidate: 'virtual void HardwareSerial::end()' 72 | virtual void end() {} | ^~~ C:\Users.....\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:72:18: note: candidate expects 0 arguments, 1 provided d:.........\Arduino\Sketches\libraries\TMC2209\src\TMC2209\TMC2209.cpp:38:97: error: no matching function for call to 'HardwareSerial::begin(long int&, long unsigned int, int16_t&, int16_t&)' 38 | hardware_serialptr->begin(serial_baud_rate, SERIAL_8N1, alternate_rx_pin, alternate_tx_pin); | ^ In file included from C:\Users.....AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/Arduino.h:77, from d:........\Arduino\Sketches\libraries\TMC2209\src/TMC2209.h:10, from d:........\Arduino\Sketches\libraries\TMC2209\src\TMC2209\TMC2209.cpp:7: C:\Users........\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:70:18: note: candidate: 'virtual void HardwareSerial::begin(long unsigned int)' 70 | virtual void begin(unsigned long) {} | ^~~~~ C:\Users........\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:70:18: note: candidate expects 1 argument, 4 provided C:\Users........\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:71:18: note: candidate: 'virtual void HardwareSerial::begin(long unsigned int, uint16_t)' 71 | virtual void begin(unsigned long, uint16_t) {} | ^~~~~ C:\Users\jcgel\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.13\cores\arduino/HardwareSerial.h:71:18: note: candidate expects 2 arguments, 4 provided

exit status 1

Compilation error: exit status 1

peterpolidoro commented 8 months ago

What happen when you run your code with the unmodified TMC2209 library files? You should be already setting the alternate pins in your code, you should not need to do that inside the TMC2209 library code as well.

Oegtsgeest commented 8 months ago

With stepper_driver.setup(serial_stream); unmodified library ; serial2, I dont see signal on the scope (stays LOW).

peterpolidoro commented 8 months ago

I will have to read about sercom to know more about how to fix it.

Oegtsgeest commented 8 months ago

Thank you again for your quick answer.

In the meantime, I am enjoying your library on serial 1. A very nice and good product.

Quite complicated chip with all its possibilities! Digging in the datasheet now!

On the sercom: I did test the serial2 while configured with the sercom coding. That worked on A2,A3. Tested with oscilloscope.

I am sorry but I mislead you in my last mail saying it did not compile.

I restarted the IDE and used the program below on the Adafruit M4 Express. Now it compiles, but there is no output on the (sercom pins) A2 and A3. (Oscilloscope)

The IDE says : “stepper not communicating”.

Joost

The sketch:

include

include

include "wiring_private.h"

// Serial2 on SERCOM4

define PIN_SERIAL2_RX A3 // RX

define PIN_SERIAL2_TX A2 // Tx

define PAD_SERIAL2_RX (SERCOM_RX_PAD_1)

define PAD_SERIAL2_TX (UART_TX_PAD_0)

const long SERIAL_BAUD_RATE = 19200;

Uart Serial2(&sercom4, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);

//Uart Serial3(&sercom0, PIN_SERIAL3_RX, PIN_SERIAL3_TX, PAD_SERIAL3_RX, PAD_SERIAL3_TX);

// Instantiate TMC2209

TMC2209 stepper_driver;

HardwareSerial & serial_stream = Serial2; // this should connect the Rx and Tx pins to the library

void SERCOM4_0_Handler()

{

Serial2.IrqHandler();

}

void SERCOM4_1_Handler()

{

Serial2.IrqHandler();

}

void SERCOM4_2_Handler()

{

Serial2.IrqHandler();

}

void SERCOM4_3_Handler()

{

Serial2.IrqHandler();

}

void setup()

{

//Serial.begin(115200);

Serial.begin(115200);

while (!Serial) {}

Serial.println("Begin set-up");

Serial2.begin(115200);

// Assign pins 12 & 13 SERCOM functionality

pinPeripheral(PIN_SERIAL2_RX, PIO_SERCOM_ALT);

pinPeripheral(PIN_SERIAL2_TX, PIO_SERCOM_ALT);

//stepper_driver.setup(serial_stream, SERIAL_BAUD_RATE, TMC2209::SERIAL_ADDRESS_0, PIN_SERIAL2_RX, PIN_SERIAL2_TX);

stepper_driver.setup(serial_stream);

Serial.println("Set-up completed");

}

void loop()

{

Serial.println("start loop");

if (stepper_driver.isSetupAndCommunicating())

{

Serial.println("Stepper driver is setup and communicating!");

Serial.println("Try turning driver power off to see what happens.");

}

else if (stepper_driver.isCommunicatingButNotSetup())

{

Serial.println("Stepper driver is communicating but not setup!");

Serial.println("Running setup again...");

stepper_driver.setup(serial_stream);

}

else

{

Serial.println("Stepper driver is not communicating!");

Serial.println("Try turning driver power on to see what happens.");

}

Serial.println();

stepper_driver.getVersion();

stepper_driver.getSettings();

delay(2000);

}

From: Peter Polidoro @.> Sent: Friday, 15 March 2024 00:14 To: janelia-arduino/TMC2209 @.> Cc: Oegtsgeest @.>; State change @.> Subject: Re: [janelia-arduino/TMC2209] Bigtree and M4 Express; communication not working (Issue #59)

I will have to read about sercom to know more about how to fix it.

— Reply to this email directly, view it on GitHub https://github.com/janelia-arduino/TMC2209/issues/59#issuecomment-1998628274 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZKROZI6J4GSMXSQPU4OYTYYIVMJAVCNFSM6AAAAABES6ZBJOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJYGYZDQMRXGQ . You are receiving this because you modified the open/close state. https://github.com/notifications/beacon/ACZKROY2LQTDHOIJWHLHUNLYYIVMJA5CNFSM6AAAAABES6ZBJOWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTTXECS3E.gif Message ID: @. @.> >

-- This email has been checked for viruses by AVG antivirus software. www.avg.com