From 5a85c1667a80cc2dda9861980c64d99f8da0163e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 14 Aug 2017 11:44:06 -0400 Subject: [PATCH] pru: Update installation and flash scripts Update the scripts used to install and "flash" the pru micro-controller code. Also, add a "flash" script for the linux micro-controller code. This makes it easier to install Klipper on a Beaglebone board that uses a Replicape. Signed-off-by: Kevin O'Connor --- scripts/flash-linux.sh | 20 +++++++ scripts/flash-pru.sh | 18 ++---- scripts/install-beaglebone.sh | 10 +++- scripts/klipper-pru-start.sh | 103 +++++++++++++++++++++++++++++----- src/linux/Makefile | 4 ++ 5 files changed, 126 insertions(+), 29 deletions(-) create mode 100755 scripts/flash-linux.sh diff --git a/scripts/flash-linux.sh b/scripts/flash-linux.sh new file mode 100755 index 00000000..ea2e3070 --- /dev/null +++ b/scripts/flash-linux.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# This script installs the Linux MCU code to /usr/local/bin/ + +if [ "$EUID" -ne 0 ]; then + echo "This script must be run as root" + exit -1 +fi +set -e + +# Install new micro-controller code +echo "Installing mirco-controller code to /usr/local/bin/" +rm -f /usr/local/bin/klipper_mcu +cp out/klipper.elf /usr/local/bin/klipper_mcu +sync + +# Restart (if system install script present) +if [ -f /etc/init.d/klipper_pru ]; then + echo "Attempting host MCU restart..." + service klipper_pru restart +fi diff --git a/scripts/flash-pru.sh b/scripts/flash-pru.sh index c0fee3e5..c508606c 100755 --- a/scripts/flash-pru.sh +++ b/scripts/flash-pru.sh @@ -13,18 +13,8 @@ cp out/pru0.elf /lib/firmware/am335x-pru0-fw cp out/pru1.elf /lib/firmware/am335x-pru1-fw sync -# Shutdown existing Klipper instance (if applicable). The goal is to -# put the GPIO pins in a safe state. -if [ -c /dev/rpmsg_pru30 ]; then - echo "Attempting to shutdown existing firmware..." - ( echo "FORCE_SHUTDOWN" > /dev/rpmsg_pru30 ) 2> /dev/null || ( echo "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) - sleep 1 - ( echo "FORCE_SHUTDOWN" > /dev/rpmsg_pru30 ) 2> /dev/null || ( echo "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) - sleep 1 +# Restart (if system install script present) +if [ -f /etc/init.d/klipper_pru ]; then + echo "Attempting PRU restart..." + service klipper_pru restart fi -set +e - -# Restart the PRU -echo "Restarting pru_rproc module" -rmmod -f pru_rproc -modprobe pru_rproc diff --git a/scripts/install-beaglebone.sh b/scripts/install-beaglebone.sh index 209f6948..695e9f15 100755 --- a/scripts/install-beaglebone.sh +++ b/scripts/install-beaglebone.sh @@ -33,10 +33,17 @@ install_udev() { report_status "Installing pru udev rule..." sudo /bin/sh -c "cat > /etc/udev/rules.d/pru.rules" < /dev/rpmsg_pru30 ) 2> /dev/null || ( log_action_msg "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) + sleep 1 + ( echo "FORCE_SHUTDOWN" > /dev/rpmsg_pru30 ) 2> /dev/null || ( log_action_msg "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) + sleep 1 + set +e + fi + + log_daemon_msg "Stopping pru" + echo 4a334000.pru0 > /sys/bus/platform/drivers/pru-rproc/unbind + echo 4a338000.pru1 > /sys/bus/platform/drivers/pru-rproc/unbind +} + +pru_start() +{ + if [ -c /dev/rpmsg_pru30 ]; then + pru_stop + sleep 1 + fi + + log_daemon_msg "Starting pru" + echo 4a334000.pru0 > /sys/bus/platform/drivers/pru-rproc/bind + echo 4a338000.pru1 > /sys/bus/platform/drivers/pru-rproc/bind +} + +mcu_host_stop() +{ + # Shutdown existing Klipper instance (if applicable). The goal is to + # put the GPIO pins in a safe state. + if [ -c /tmp/klipper_host_mcu ]; then + log_daemon_msg "Attempting to shutdown host mcu..." + set -e + ( echo "FORCE_SHUTDOWN" > /tmp/klipper_host_mcu ) 2> /dev/null || ( log_action_msg "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) + sleep 1 + ( echo "FORCE_SHUTDOWN" > /tmp/klipper_host_mcu ) 2> /dev/null || ( log_action_msg "Firmware busy! Please shutdown Klipper and then retry." && exit 1 ) + sleep 1 + set +e + fi + + log_daemon_msg "Stopping klipper host mcu" $NAME + killproc -p $PIDFILE $KLIPPER_HOST_MCU +} + +mcu_host_start() +{ + [ -x $KLIPPER_HOST_MCU ] || return + + if [ -c /tmp/klipper_host_mcu ]; then + mcu_host_stop + fi + + log_daemon_msg "Starting klipper MCU" $NAME + start-stop-daemon --start --quiet --exec $KLIPPER_HOST_MCU \ + --background --pidfile $PIDFILE --make-pidfile \ + -- $KLIPPER_HOST_ARGS + log_end_msg $? +} + case "$1" in -start) log_daemon_msg "Starting klipper_pru" $NAME - # Enable ADC module - echo 'BB-ADC' > /sys/devices/platform/bone_capemgr/slots - # Start PRU firmware - if [ ! -c /dev/rpmsg_pru30 ]; then - rmmod -f pru_rproc - modprobe pru_rproc - fi - ;; -stop|restart|reload|force-reload|status) - ;; -*) log_action_msg "Usage: /etc/init.d/klipper_pru {start|stop|status|restart|reload|force-reload}" - exit 2 - ;; +start) + pru_start + mcu_host_start + ;; +stop) + pru_stop + mcu_host_stop + ;; +restart) + $0 stop + $0 start + ;; +reload|force-reload) + log_daemon_msg "Reloading configuration not supported" $NAME + log_end_msg 1 + ;; +status) + status_of_proc -p $PIDFILE $KLIPPER_HOST_MCU $NAME && exit 0 || exit $? + ;; +*) log_action_msg "Usage: /etc/init.d/klipper {start|stop|status|restart|reload|force-reload}" + exit 2 + ;; esac exit 0 diff --git a/src/linux/Makefile b/src/linux/Makefile index 5ab3fc27..6df57f44 100644 --- a/src/linux/Makefile +++ b/src/linux/Makefile @@ -7,3 +7,7 @@ src-y += linux/pca9685.c linux/spidev.c linux/analog.c src-y += generic/crc16_ccitt.c generic/alloc.c CFLAGS_klipper.elf += -lutil + +flash: $(OUT)klipper.elf + @echo " Flashing" + $(Q)sudo ./scripts/flash-linux.sh