Skip to content

Instantly share code, notes, and snippets.

Last active June 2, 2024 03:44
Show Gist options
  • Save EEParker/d91fab4227c5ce4d88ce8a0e4c2df75e to your computer and use it in GitHub Desktop.
Save EEParker/d91fab4227c5ce4d88ce8a0e4c2df75e to your computer and use it in GitHub Desktop.
PI Zero 2 W UART configuration for Marlin

PI Zero 2 W UART configuration for Marlin

Install Prerequisites

sudo apt update
sudo apt install picocom -y

Enable Serial Hardware

2022-08-12 09_35_30-pi@qzero_ ~ 2022-08-12 09_35_39-pi@qzero_ ~ 2022-08-12 09_35_42-pi@qzero_ ~ 2022-08-12 09_35_45-pi@qzero_ ~

Moving The Serial Ports

By default, the Raspberry Pi 3 Model B (and Zero 2 W) assigns ttyS0 to GPIO14:15 while ttyAMA0 serves the Bluetooth module. As the mini UART is not a full featured UART, you may want to use ttyAMA0 on GPIO14:15 instead as it is a full featured UART. Fortunately, there are a couple of device tree overlays that will accomplish this.

  • pi3-miniuart-bt: This overlay flip flops the UARTs by assigning ttyAMA0 to GPIO14:15 while assigning ttyS0 to the Bluetooth module.
  • pi3-disable-bt: This overlay assigns ttyAMA0 to GPIO14:15 while disabling Bluetooth altogether.

In general, the Mini-UART has one big pitfall. It doesn't have its own clock source, so the UART bitrate depends on the CPU clock. Which means you have to set a fixed CPU clock for reliable communication.

This guide will use pi3-disable-bt to ensure compatibility with Marlin uart.

Modify The Boot Files

# FILE /boot/config.txt

# ...


Check configuration

Reboot the PI to apply configuration.

sudo reboot now

Once rebooted, check the configuration. /dev/ttyAMA0 should now be assigned to /dev/ttyS0.

pi@raspberry:~ $ ls -l /dev | grep ttyAMA0 && ls -l /dev | grep ttyS0
lrwxrwxrwx  1 root root           7 Jul 10 14:35 serial0 -> ttyAMA0
crw-rw----  1 root dialout 204,  64 Jul 10 14:35 ttyAMA0
lrwxrwxrwx  1 root root           5 Jul 10 14:35 serial1 -> ttyS0
crw-rw----  1 root dialout   4,  64 Jul 10 14:35 ttyS0

Grant permissions to ports for pi user.

sudo usermod -a -G dialout pi
sudo usermod -a -G tty pi

Test permissions and marlin connection

picocom /dev/ttyAMA0 -b 115200 --imap lfcrlf --echo

Send command M115 to marlin to test the connection.

Port Aliasing

Some 3D printing host applications require a ttyUSBXX port ID. To alias the serial port, you need to add a udev rule. Here we will alias ttyAMA[X] to ttyUSB[X]. Note, this could interfere with USB devices. In our case we are not using any so there is no issue.

Find the block for ttyAMA0, and in the SYMLINK section, add ttyUSB%c inside the quotes, with a space after serial%c.

sudo nano /etc/udev/rules.d/99-com.rules
# FILE: /etc/udev/rules.d/99-com.rules
KERNEL=="ttyAMA0", PROGRAM="/bin/sh -c '\
        ALIASES=/proc/device-tree/aliases; \
        if cmp -s $$ALIASES/uart0 $$ALIASES/serial0; then \
                echo 0;\
        elif cmp -s $$ALIASES/uart0 $$ALIASES/serial1; then \
                echo 1; \
        else \
                exit 1; \
'", SYMLINK+="serial%c ttyUSB%c"
# Add new symlink here ^

You can reload the udev rules with following command to test. However, your application will probably require a reboot to pick up the new port.

sudo udevadm control --reload-rules && sudo udevadm trigger



PI Zero 2 W Docker and Memory Tuning

Thie PI Zero 2 W is very memory constrained, the following settings will increase the swap and allow us to run more memory applications.

Enable cgroups for Docker

sudo nano /boot/cmdline.txt

Add cgroup_enable=memory cgroup_enable=cpu cgroup_memory=1

Increase the swap

Disable swap

sudo dphys-swapfile swapoff

As root, edit the file /etc/dphys-swapfile and modify the variable CONF_SWAPSIZE:

sudo nano /etc/dphys-swapfile

Edit the line and enter decide swap size in MB


Apply the new swap configuration.

sudo dphys-swapfile setup

Enable swap

sudo dphys-swapfile swapon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment