Wednesday, January 29, 2020

Setting up TMC2209 in UART mode on MRR ESPA

Note: This post is specifically for the MRR ESPA and MRR ESPE boards. While the principle may be similar, it is not guaranteed to work for other boards. Please consult your manufacturer for support. Also, this post was written in January 2020 and may not apply to the latest version of Marlin.

The MRR ESPA is a 3D printer control board based on the ESP32 microcontroller. Marlin 2.0 now has support for ESP3D web interface on the ESP32, which means that 3D printers controlled by ESP32 can run Marlin with a tested web interface.

However, one of the issues with the ESP32 is the lack of GPIO pins. And given that each TMC2208 stepper driver needs 2 pins (RX + TX) to run in UART mode, I decided not to design jumpers for configuring TMC2208 drivers. Instead, only jumper configuration for TMC2130 in SPI mode was added.

But the new TMC2209 driver allows configuration of a slave address (up to 4 separate addresses) for each driver. What this means is that up to 4 x TMC2209 drivers can share a single set of RX/TX serial port. And the ESP32 has 3 hardware serial ports (one is used for the USB, which leaves another two unused) so technically, the MRR ESPA should be able to support 4 x TMC2209 stepper drivers in UART mode if slave addresses are used.

For those who don't like to read, you can watch this simple video, but I think reading the blog post should give more information.


Preparation
- Remove the DIAG and INDEX pins on the TMC2209 driver. Use a soldering iron, put it between the two pins to melt the solder of both pins together, then pull out both pins from the other side.
- Make the cable required to connect the serial port (RX and TX) to the PDN pin on the TMC2209.
- Make the cable to connect CLK on the TMC2209 to GND. The TMC2209 allows the use of an external clock signal, and the CLK pin should be connected to GND if the external clock is not used. This is basically a split wire, with a single wire being split into as many ends as needed (depends on number of stepper drivers using TMC2209). Update: After a close look at the TMC2209 schematic, there is no need for the cable to connect CLK pin to GND.

Preparing the firmware
- In Configuration.h, set the driver type for each axis using TMC2209. For example:
  #define X_DRIVER_TYPE  TMC2209
  #define Y_DRIVER_TYPE  TMC2209
  #define Z_DRIVER_TYPE  TMC2209
  #define E0_DRIVER_TYPE  TMC2209
- Also in Configuration.h, you can switch the motor direction. For example:
  #define INVERT_X_DIR true
  to
  #define INVERT_X_DIR false
  Do this for each axis using TMC2209. This is optional. You can also just flip the motor connector around, which is what I usually do.
- In Configuration_adv.h, configure the slave addresses. For example:
  #define  X_SLAVE_ADDRESS 0
  #define  Y_SLAVE_ADDRESS 1
  #define  Z_SLAVE_ADDRESS 2
  #define  E0_SLAVE_ADDRESS 3
- Also in Configuration_adv.h, set up the serial port to use by adding the below (just below the slave addresses):
  #define X_HARDWARE_SERIAL Serial1
  #define Y_HARDWARE_SERIAL Serial1
  #define Z_HARDWARE_SERIAL Serial1
  #define E0_HARDWARE_SERIAL Serial1
  This tells Marlin that the TMC2209 on X, Y, Z, and E0 are using the second hardware serial port Serial1. (Update 2020/2/22: Marlin has some issues recognizing hardware serial defines for extruders. See this issue on how it can be worked around.)
- Finally, also in Configuration_adv.h, set up the serial port parameters to use by adding the below (just below the slave addresses and the ?_HARDWARE_SERIAL lines):
  #define TMC_BAUD_RATE 115200, SERIAL_8N1, 21, 22
  This sets the serial port(s) for TMC UART mode to run at a baud rate of 115200, 8N1 protocol, and use GPIO21 for RX, and GPIO22 for TX. It is a quick and dirty hack that only works if one hardware serial port (which supports up to 4 x TMC2209 drivers) is used.
- Done! Build the firmware, then flash it to the board via USB or OTA. (Note: In Configuration_adv.h, there are many other options for Trinamic drivers. You can read the comments for more information.)

Installing the stepper drivers
- Remove all jumpers for the stepper motors (MS1, MS2, MS3, RST). The RST/SLP jumper is totally not used. MS1 and MS2 will be used to set the slave address (next step). MS3 jumper can be used, but this is a bit difficult to explain and I will do so separately.

- Use MS1 and MS2 to set the slave address for each axis that will be using TMC2209.
  Address 0: MS1 and MS2 unconnected
  Address 1: MS1 connected to VCC, MS2 unconnected
  Address 2: MS1 unconnected, MS2 connected to VCC
  Address 3: MS1 and MS2 connected to VCC
  The photo below shows slave addresses configured as X=0, Y=1, Z=2, E0=3.

- Connect the PDN cable. The wire with a 1K resistor should be connected to the pin being used for TX. For the MRR ESPA in this example, this would probably be GPIO22, which is labeled SCL. The other wire is for RX, which is GPIO21 labeled SDA on the board. The remaining wires connect to the PDN pin on the TMC2209. (Note: Check your TMC2209 schematic to see which is the PDN pin. For the BIGTREETECH TMC2209 V1.2 driver that I am using, it is in the same row as the EN pin, pin number 4 in that row with pin 1 being the EN pin. If PDN is on pin4, then MS3 jumper can be used to route the PDN pin to the CS_PIN header. See further below for how to use this.)
- Connect the CLK pins on the TMC2209 to GND on the MRR ESPA using the other cable that was prepared.

- Finally, connect the motors, then power up the board!

After powering up the board, sending M122 via serial terminal should give the status of the drivers if TMC_DEBUG was enabled.

For the MRR ESPE, the process should be similar. However, unused pins like GPIO2 and GPIO4 can also be used for the third hardware serial to support the 5th stepper driver on the MRR ESPE. This requires a bit more work to set up the third hardware serial port Serial2 to use those pins, though. For example, something like the below line will need to be added somewhere
  Serial2.begin(115200, SERIAL_8N1, 2, 4);
(Untested because I don't have enough TMC2209 drivers.)

Okay, a bit about MS3. If the MS3 jumper is connected to the "TMC" side, it will route the MS3 pin (which is the same position as the PDN pin on my TMC2209 driver board) to the respective CS_PIN on the board. This means the RX/TX/PDN cable can be connected to the CS_PIN header instead. This is especially helpful if the PDN pin is not exposed to the top side of the stepper driver board.

For more information on the MRR ESPA:
Facebook page
Facebook group for users
GitHub repository
MRR ESPA available here
 
Note (added January 23, 2021): I found that someone has actually posted the design of the MRR ESPE in his name on PCBWay. This is the page. Whoever this Oleg person is, he is in no way related to me nor this project and does not have the rights to post the design on PCBWay. I made the project open source but open source is not free, and it definitely does not mean you can take credit for something you did not create. It means you can use it if you give proper credit to the creators of the work.

6 comments:

kirchnet said...

In Configuration.h do you also need to set
#define SERIAL_PORT_2 0
or another number?
I don't seem to get the M122 gcode command to recognize the TMC2209. No matter what combination of setting I always get "Error: All Low"
Maybe I am using the wrong TMC library? I use TMCStepper 0.7.1
https://github.com/teemuatlut/TMCStepper

Teck said...

Usually, the "Error: All Low" message comes out when the TMC stepper drivers are not powered. Are you running on the main PSU (12V to 24V)? If it is powered, then the next issue is with serial port connection or initialization. It is likely to do with the physical connections (double check that) but
#define SERIAL_PORT_2 -1
may help (or not). I left it commented out, though.
I have not tested newer versions of Marlin actually, it has been a while since I played around with the firmware. One option is to go back to an older release (from around May 2020) and see if that works. There has been a lot of work on Marlin since then, and I have not tested to see if any of the improvements made to other parts of Marlin has broken ESP32 support.

Teck said...

TMC drivers
This is the documentation for TMC stepper drivers on Marlin's page. It has a troubleshooting section, you may want to see if that provides any hints.

Another link that may be helpful.

Rmdhn said...

I know this is a long shot to reply here, but would it say be possible, for there to be a TMC2208 on address 0 and TMC2209 on address 1?

Teck said...

You can always give it a try, since the TMC2208 defaults to address 0 anyway. But why not just use TMC2209 for all required stepper motor drivers? The price difference shouldn't be that great now. And probably save you a bit of headache if things don't work and you need to troubleshoot.

Rmdhn said...

Eh, i just had TMC2208, Bought them when the price difference was pretty substantial. Sorta regretted that but oh well, prolly just gonna buy 3 2209 then