Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 2LabsToGo-Eco-Firmware/2labstogo_programmer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
programmer
id = "2LabsToGo";
desc = "Use the Linux GPIO interface to bitbang GPIO lines (Bookworm compatible)";
type = "linuxgpio";
prog_modes = PM_ISP;
reset = 6;
sck = 11;
sdo = 10;
sdi = 9;
;
54 changes: 51 additions & 3 deletions 2LabsToGo-Eco-Firmware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,60 @@ The built firmware.hex file is then saved in 2LabsToGo-Eco-Firmware/2LabsToGo-Ec
<b>However, building the firmware is only required, if the Marlin codes have been modified.</b><a>

A pre-built firmware file (firmware_2LabsToGo-Eco.hex) is available in this folder, including files for setting the fuses and flashing the bootloader.
To flash the firmware from the Raspberry Pi onto the Arduino chip of the 2LabsToGo-Eco mainboard,
use the bash script <b>flash_firmware.sh</b>.

## Flashing Firmware on Raspberry Pi OS Bookworm

The firmware flashing process has been updated to work with Raspberry Pi OS Bookworm Lite, which uses the new `gpiod` interface instead of the legacy `sysfs` GPIO.

### First-time Setup (Bookworm)

Before flashing firmware for the first time on Bookworm, run the setup script:

```bash
sudo bash setup_bookworm.sh
```

This script will:
- Install required packages (avrdude with gpiod support)
- Configure GPIO access permissions
- Set up the system for firmware flashing

**Note:** After running the setup script, you may need to log out and back in for group permission changes to take effect.

### Flashing the Firmware

To flash the firmware from the Raspberry Pi onto the Arduino chip of the 2LabsToGo-Eco mainboard, use the bash script:

```bash
sudo bash flash_firmware.sh
```

### Compatibility Notes

- **Bookworm (recommended)**: Uses `linuxgpiod` driver - fully supported
- **Bullseye and older**: Uses `linuxgpio` driver - legacy support

If you're using an older Raspberry Pi OS version, the original configuration will still work.

For more details on flashing, consult the 2LabsToGo-Eco Assembly guide
(see reference 1 in this [README](https://github.com/OfficeChromatography/2LabsToGo-Eco/blob/main/README.md)).

## After firmware update
<b>After a firmware update, both PID tunings must be performed again to safe the settings in the firmware.<br>
Consult the 2LabsToGo-Eco Assembly guide.</b>
Consult the 2LabsToGo-Eco Assembly guide.</b>

## Troubleshooting

### GPIO Access Issues
If you encounter "Permission denied" errors:
1. Ensure you're running with `sudo`
2. Check that `/dev/gpiochip0` exists: `ls -l /dev/gpiochip*`
3. Verify GPIO group membership: `groups`

### avrdude Version Issues
If flashing fails, check your avrdude version:
```bash
avrdude -? 2>&1 | grep version
```

Version 7.0 or higher is required for Bookworm compatibility. The setup script will attempt to install a compatible version.
4 changes: 2 additions & 2 deletions 2LabsToGo-Eco-Firmware/avrdude_gpio.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15833,11 +15833,11 @@ part parent ".avr8x_mega"
;
;

# Linux GPIO configuration for avrdude.
# Linux GPIO configuration for avrdude
# Change the lines below to the GPIO pins connected to the AVR.
programmer
id = "2LabsToGo";
desc = "Use the Linux sysfs interface to bitbang GPIO lines";
desc = "Use the Linux GPIO interface to bitbang GPIO lines (Bookworm compatible)";
type = "linuxgpio";
reset = 6;
sck = 11;
Expand Down
87 changes: 78 additions & 9 deletions 2LabsToGo-Eco-Firmware/flash_firmware.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
#!/bin/bash
#read -p "Enter your username: " user
#echo "You entered $user"

#sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U lfuse:w:0xff:m -U hfuse:w:0xd8:m -U efuse:w:0xfd:m
sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U lfuse:w:0xff:m -U hfuse:w:0xd8:m -U efuse:w:0xfd:m
#sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U flash:w:ArduinoISP.ino.hex:i
sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U flash:w:ArduinoISP.ino.hex:i
#sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U flash:w:firmware_2LabsToGo.hex:i
sudo avrdude -p atmega2560 -C avrdude_gpio.conf -c 2LabsToGo -v -U flash:w:firmware_2LabsToGo-Eco.hex:i

# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (use sudo)"
exit 1
fi

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$SCRIPT_DIR/2labstogo_programmer.conf"

# Use the correct avrdude path
AVRDUDE="/usr/local/bin/avrdude"

# GPIO chip device (typically gpiochip0 on Raspberry Pi)
GPIO_DEVICE="/dev/gpiochip0"

# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Configuration file not found at $CONFIG_FILE"
exit 1
fi

# Check if GPIO device exists
if [ ! -e "$GPIO_DEVICE" ]; then
echo "Error: GPIO device $GPIO_DEVICE not found"
exit 1
fi

# Check if avrdude is installed
if [ ! -f "$AVRDUDE" ]; then
echo "avrdude is not installed at $AVRDUDE. Please run setup_bookworm.sh first"
exit 1
fi

# Check if linuxgpio programmer is available
if ! "$AVRDUDE" -c ?type 2>&1 | grep -q "linuxgpio"; then
echo "Error: avrdude doesn't support linuxgpio programmer"
echo "Please run: sudo bash setup_bookworm.sh"
exit 1
fi

echo "Using avrdude: $AVRDUDE"
echo "Using avrdude version: $("$AVRDUDE" -? 2>&1 | grep -oP 'version \K[0-9]+\.[0-9]+' | head -1)"
echo "Using config file: $CONFIG_FILE"
echo "Using GPIO device: $GPIO_DEVICE"

# Verify the programmer is defined in the config
if ! grep -q 'id = "2LabsToGo"' "$CONFIG_FILE"; then
echo "Error: Programmer '2LabsToGo' not found in config file"
exit 1
fi

echo "Setting fuses..."
"$AVRDUDE" -p atmega2560 -C "+$CONFIG_FILE" -c 2LabsToGo -i 50 -P "$GPIO_DEVICE" -v -U lfuse:w:0xff:m -U hfuse:w:0xd8:m -U efuse:w:0xfd:m

if [ $? -ne 0 ]; then
echo "Error: Failed to set fuses"
exit 1
fi

echo "Flashing bootloader..."
"$AVRDUDE" -p atmega2560 -C "+$CONFIG_FILE" -c 2LabsToGo -i 50 -P "$GPIO_DEVICE" -v -U flash:w:ArduinoISP.ino.hex:i

if [ $? -ne 0 ]; then
echo "Error: Failed to flash bootloader"
exit 1
fi

echo "Flashing firmware..."
"$AVRDUDE" -p atmega2560 -C "+$CONFIG_FILE" -c 2LabsToGo -i 50 -P "$GPIO_DEVICE" -v -U flash:w:firmware_2LabsToGo-Eco.hex:i

if [ $? -eq 0 ]; then
echo "Firmware flashed successfully!"
else
echo "Error: Failed to flash firmware"
exit 1
fi
128 changes: 128 additions & 0 deletions 2LabsToGo-Eco-Firmware/setup_bookworm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/bin/bash

# Setup script for Raspberry Pi OS Bookworm Lite compatibility
# This script prepares the system for firmware flashing

set -e

echo "=== 2LabsToGo-Eco Firmware Setup for Bookworm ==="

# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root: sudo bash setup_bookworm.sh"
exit 1
fi

# Update package list
echo "Updating package list..."
apt update

# Install required packages
echo "Installing required packages..."
apt install -y libgpiod2 gpiod libgpiod-dev

# Check if avrdude supports linuxgpiod
echo "Checking avrdude version and linuxgpiod support..."
if command -v avrdude &> /dev/null && avrdude -c ? 2>&1 | grep -q "linuxgpio"; then
echo "✓ avrdude with linuxgpiod support detected"
else
echo "Building avrdude from source with gpiod support..."

# Install build dependencies
apt install -y build-essential git cmake libelf-dev libusb-dev \
libhidapi-dev libftdi1-dev libreadline-dev flex bison

# Remove old avrdude if installed
apt remove -y avrdude || true

echo "Cloning avrdude repository..."
cd /tmp
if [ -d "avrdude" ]; then
rm -rf avrdude
fi

git clone https://github.com/avrdudes/avrdude.git
cd avrdude
git checkout v8.1

echo "Building avrdude..."
mkdir -p build
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D HAVE_LINUXGPIO=1 \
-D CMAKE_INSTALL_PREFIX=/usr/local \
..

# Check if cmake found libgpiod
if grep -q "HAVE_LINUXGPIO:BOOL=1" CMakeCache.txt; then
echo "✓ CMake detected linuxgpio support"
else
echo "✗ Error: CMake did not enable linuxgpio support"
echo "CMake cache content:"
grep -i "gpiod\|gpio" CMakeCache.txt || echo "No GPIO-related entries found"
exit 1
fi

make -j$(nproc)
make install

# Update library cache
ldconfig

# Remove any old avrdude binaries from standard paths
rm -f /usr/bin/avrdude

echo "✓ avrdude installed from source"

# Verify installation
echo "Verifying avrdude installation..."
AVRDUDE_PATH=$(which avrdude)
echo "Using avrdude at: $AVRDUDE_PATH"

# Test linuxgpio support
if /usr/local/bin/avrdude -c ?type 2>&1 | grep -q "linuxgpio"; then
echo "✓ linuxgpio programmer support confirmed"
else
echo "✗ Error: linuxgpio support not found"
echo ""
echo "Available programmer types:"
/usr/local/bin/avrdude -c ?type 2>&1 | grep -i gpio || echo "No GPIO programmers found"
exit 1
fi

# Verify libgpiod linkage
if ldd /usr/local/bin/avrdude | grep -q libgpiod; then
echo "✓ avrdude is linked against libgpiod library"
else
echo "✗ Error: avrdude is NOT linked against libgpiod library"
exit 1
fi
fi

# Verify GPIO chip exists
if [ ! -e /dev/gpiochip0 ]; then
echo "⚠ Warning: /dev/gpiochip0 not found"
echo "GPIO device may not be accessible"
else
echo "✓ GPIO device found"
fi

# Add current user to gpio group (if not root)
if [ -n "$SUDO_USER" ]; then
usermod -a -G gpio "$SUDO_USER"
echo "✓ User $SUDO_USER added to gpio group"
echo " (You may need to log out and back in for this to take effect)"
fi

# Create udev rule for GPIO access
echo 'SUBSYSTEM=="gpio", KERNEL=="gpiochip*", GROUP="gpio", MODE="0660"' > /etc/udev/rules.d/90-gpio.rules
udevadm control --reload-rules
udevadm trigger

echo ""
echo "=== Setup Complete ==="
echo ""
echo "Next steps:"
echo "1. If you're not root, log out and back in for group changes to take effect"
echo "2. Run: sudo bash flash_firmware.sh"
echo ""
14 changes: 8 additions & 6 deletions 2LabsToGo-Eco-Software/Operational_qualification/application.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#!/usr/bin/env python3
#application.py
# application.py

import serial
import time


def command(ser, command):
ser.write(str.encode(command))
time.sleep(1)
ser.write(str.encode(command))
time.sleep(1)


ser = serial.Serial('/dev/ttyAMA1', 115200)
ser = serial.Serial("/dev/ttyAMA3", 115200)
time.sleep(1)

command(ser, "G0Y10F3000\r\n") #needle is in vial 1!!
command(ser, "G0Y10F3000\r\n") # needle is in vial 1!!
time.sleep(2)

#first band
# first band
command(ser, "G0X30\r\n")
command(ser, "G98F1200\r\n")
command(ser, "G0X30.5F3000\r\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python3
#backlight_off.py
# backlight_off.py

import serial
import time


def command(ser, command):
ser.write(str.encode(command))
time.sleep(1)
ser.write(str.encode(command))
time.sleep(1)


ser = serial.Serial('/dev/ttyAMA1', 115200)
ser = serial.Serial("/dev/ttyAMA3", 115200)
time.sleep(1)

command(ser, "M42P13S0\r\n")
Expand Down
10 changes: 6 additions & 4 deletions 2LabsToGo-Eco-Software/Operational_qualification/backlight_on.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python3
#backlight_on.py
# backlight_on.py

import serial
import time


def command(ser, command):
ser.write(str.encode(command))
time.sleep(1)
ser.write(str.encode(command))
time.sleep(1)


ser = serial.Serial('/dev/ttyAMA1', 115200)
ser = serial.Serial("/dev/ttyAMA3", 115200)
time.sleep(1)

command(ser, "M42P13S255\r\n")
Expand Down
Loading