Monday, October 31, 2022

Connecting the Raspberry Pi to the Robin Nano via GPIO pins

Since the Robin Nano board is mounted such that the USB port is only accessible outside the printer, I had to snake the USB cable out through one of the holes and around the printer to reach the port. For a clean installation, I need a way to let the Raspberry Pi connect to the nano using the GPIO pins.

USB port can only be reached from the outside

Currently, however, most of the easily accessible pins are already taken on my board as part of enabling SPI communication on the TMC2209 drivers.

Most easy to access pins are used
So I went hunting for pins that I could use and it turns out most of the pins on the TF connector can at least be used for SPI freeing up the pins on the WIFI connector that are most commonly used for RPI communication. So the plan now is to rewire the 5 SPI wires into a single 9-pin JST-XH connector and then use the WIFI pins for RPI.



Establishing Common Grounds

Before we can hookup I/O pins between separate devices, we need to ensure that they all have a common I/O ground. This is different from grounding the printer (which you should do as well). What I mean is that the negative terminal on the DC side of all the power supplies should be connected together. This ensures that the 24v line from the 24v PSU is actually 24 volts above any of the DC grounds and the 5v line from the 5v PSU is actually 5 volts above the same ground lines. When I had the USB cable connected, there was a ground wire that joined the PSU's together. Although, I could have added a ground wire as part of the wires connecting the RPI with the Robin Nano, its usually better practice to join the PSUs directly to minimize any current going over the ground GPIO pins.

In the end, I connected the GPIO pin14/15 (which are pins for a serial port) on the RPi4 to pins PA10/PA9 on the Robin Nano, respectively. Note that you must cross the wires so that the TX from one boards goes to the RX pin on the other board and vice versa. 

Klipper/RPi Configuration

Next, I had to build a new klipper firmware so that it would know to listen on the correct pins.
I ran through make menuconfig as per the klipper installation instructions being sure to use the same settings described in the top of my printer.cfg regarding booloader size, startup pins, etc. The difference this time, is I picked the serial connection to match the new pins we're using.




After this I: 
  1. saved the config
  2. ran "make" to build the image
  3. used the mks script to convert the image to one compatible with robin nano
  4. used Filezilla to get the file onto my computer
  5. copied image to sd card
  6. booted robin nano with sd card (the image file will get renamed with a *.CUR extension)
Next I had to configure printer.cfg to use the correct serial port. Klipper docs don't document this correctly. They assume you are using USB. Instead of using their command, use "ls /dev/serial*" but at this point the serial ports are not configured correctly. This doc proved helpful: https://docs.google.com/document/d/1kFasg5H_YDpIwc0PZpKph6RTX4BPROkNgTojQ0RWks8/edit

Basically, I first need to configure RPi to disable bluetooth, enable serial ports, disable shell on serial ports. This was done with a combination of editing /boot/config.txt and running raspi-config.
After all was set and done, I had 2 serial interfaces /dev/serial0 and /dev/serial1 with serial1 mapped to /dev/ttyS0 which is the one that corresponds to the GPIO pin 14/15.

Finally, in printer.cfg under the [mcu] section, I set the "serial" to "/dev/ttyS0". Restarted everything and then it all worked.