#!/bin/bash

# Flashes a LPC based boards over the serial line connected to the GPIO headers of a RPI. 
# In addition to the TX/RX lines you will also need to connect the BOOT and RESET lines.
# This configuration uses GPIO6 for RESET and GPIO12 for BOOT. Pin numbering is using 
# the wiring standard.

pin_reset=22
pin_boot=26
firmware=$HOME/klipper/out/klipper.bin

sudo systemctl stop klipper

# enter boot bootloader sequence

# configure reset and boot GPIOs as outputs
gpio mode $pin_reset OUT
gpio mode $pin_boot  OUT

# hold reset and bootloader down
gpio write $pin_reset 0
gpio write $pin_boot 0

# go back to high-z and let skr pullups set the lines to high
gpio mode $pin_reset IN
gpio mode $pin_boot IN

lpc21isp -wipe -bin $firmware /dev/ttyS0 230400 12000

sudo systemctl start klipper