Skip to content

Instantly share code, notes, and snippets.

@cr08
Last active February 28, 2025 01:07
Show Gist options
  • Save cr08/53546199fc09623dba821dafd0f7cc2d to your computer and use it in GitHub Desktop.
Save cr08/53546199fc09623dba821dafd0f7cc2d to your computer and use it in GitHub Desktop.
Belabox install script to add service for setting physical GPIO pins 15 and 40 to OUT and LOW for specific 4G/5G hats
#!/bin/bash
####################################
#
# This script is designed for use with the official Belabox images on the supported Rockchip RK3588 boards
# in combination with the SpotPear 4G/5G M.2 modem hats:
# https://spotpear.com/index.php/shop/Raspberry-Pi-M.2-4G-5G-GSM-GPRS-EDGE.html
#
# The Belabox image, at least on the Orange Pi 5 Plus, defaults available GPIO pins to IN with a pullup.
# Physical pins 15 and 40 tie to modem POWER and RESET pins with these hats and the defaults will keep
# the modems in a reset state.
#
# This installer provides a script configured as a system service to set these pins to OUT and LOW, ensuring
# the modems are in a running state on boot.
#
# Please reach out to Vchat20 on the Belabox Discord server for any questions.
#
####################################
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Attempting to elevate privileges..."
exec sudo "$0" "$@"
fi
# Path for the GPIO script
GPIO_SCRIPT_PATH="/usr/local/bin/gpio-script.sh"
# Create the GPIO script
cat << 'EOF' > $GPIO_SCRIPT_PATH
#!/bin/bash
# Determine the board model
MODEL=$(cat /sys/firmware/devicetree/base/model)
# Initialize GPIO pins based on board model
if [[ "$MODEL" == *"Orange Pi 5 Plus"* ]]; then
GPIO_PIN1=99
GPIO_PIN2=40
elif [[ "$MODEL" == *"Radxa ROCK 5B"* || "$MODEL" == *"Banana Pi BPI-W3"* ]]; then # Matches Rock 5B/5B+ amd BPI-W3. All share same GPIO pin numbers
GPIO_PIN1=107
GPIO_PIN2=112
elif [[ "$MODEL" == *"Radxa ROCK 5A"* ]]; then
GPIO_PIN1=137
GPIO_PIN2=140
else
echo "Unknown board model: $MODEL"
exit 1
fi
echo "Running on board: $MODEL"
echo "Using GPIO pins $GPIO_PIN1 and $GPIO_PIN2"
# Export the GPIO pins
echo "$GPIO_PIN1" > /sys/class/gpio/export
echo "$GPIO_PIN2" > /sys/class/gpio/export
# Set the GPIO pins to "out" direction
echo "out" > /sys/class/gpio/gpio"$GPIO_PIN1"/direction
echo "out" > /sys/class/gpio/gpio"$GPIO_PIN2"/direction
# Set the GPIO pins to "low"
echo "0" > /sys/class/gpio/gpio"$GPIO_PIN1"/value
echo "0" > /sys/class/gpio/gpio"$GPIO_PIN2"/value
echo "GPIO pins $GPIO_PIN1 and $GPIO_PIN2 set to out and low."
exit 0
EOF
# Make the GPIO script executable
chmod +x $GPIO_SCRIPT_PATH
# Path for the service file
SERVICE_FILE_PATH="/etc/systemd/system/gpio-service.service"
# Create the service file
cat << EOF > $SERVICE_FILE_PATH
[Unit]
Description=GPIO Control Service
After=multi-user.target
[Service]
ExecStart=$GPIO_SCRIPT_PATH
Restart=no
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd manager configuration
systemctl daemon-reload
# Enable the service to start at boot
systemctl enable gpio-service.service
# Start the service immediately
systemctl start gpio-service.service
echo "Installation complete. The GPIO service is now running and will start at boot."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment