Skip to content

Instantly share code, notes, and snippets.

@SuperMarioSF
Last active February 28, 2024 01:27
Show Gist options
  • Save SuperMarioSF/baea9fc13fc320e5a002c58cf107bd10 to your computer and use it in GitHub Desktop.
Save SuperMarioSF/baea9fc13fc320e5a002c58cf107bd10 to your computer and use it in GitHub Desktop.
ClockworkPi uConsole A06 Core LTE Module systemd service for automatic startup

Systemd unit file for starting up LTE module for uConsole A06

WARNING!

Please note this file may only work on A06 (and prehaps A04 also) models. CM4 models is using a different script so this systemd unit file need to adjusted accordingly to work on CM4 model.

What is this?

uConsole is a nice machine that made by ClockworkPi, which is having an optional LTE module. It came with a shell script pre-installed to manage LTE module's power. But it requires user action to enable, everytime. This thing is the solution for LTE module automatic start on boot.

What's inside?

uConsoleLTE.service is a systemd unit file that being translated from the included shell script uconsole-4g in the uConsole A06 system image. This unit file can power on and safe power down the LTE module inside uConsole A06 model.

Provided ModemManager.service is a modified service unit file of Modem Manager, which added BindsTo directive to allow Modem Manager to start and stop with LTE module service.

Why Modem Manager is involved?

If LTE module powered on before Modem Manager started, most of time Modem Manager will have issue detecting the modem. The reason is simple: The USB UART of LTE module came up earlier than the LTE module became ready, and Modem Manager detected the port early, so Modem Manager timed out on port detection, and it just give up and never try again. A service restart is required to work. So this BindTo can make sure Modem Manager is only started if LTE module is enabled, and the Before directive in uConsoleLTE.service make sure Modem Manager is started after LTE Module is powered on and ready.

How to use this?

  1. Put those files in /etc/systemd/system/ directory with their filename accordingly.
  2. run sudo systemctl daemon-reload.
  3. run sudo systemctl start uConsoleLTE.service to start modem.
  4. If you want it automatically start on boot, run systemctl enable uConsoleLTE.service.

Downsides?

This service unit can delay the shutdown up to about 30s.

Because systemd doesn't have a way to ignore the service stop on shutdown, every time system goes shutdown it will safely power off the modem first, which is actually a good thing I guess.

License?

Dude, this is a such simple thing... Just copy already!

If you insist on one, fine, WTFPL 2.0. WTFPL

[Unit]
Description=Modem Manager
After=polkit.service
Requires=polkit.service
ConditionVirtualization=!container
BindsTo=uConsoleLTE.service
[Service]
Type=dbus
BusName=org.freedesktop.ModemManager1
ExecStart=/usr/sbin/ModemManager
StandardError=null
Restart=on-abort
CapabilityBoundingSet=CAP_SYS_ADMIN CAP_NET_ADMIN
ProtectSystem=true
ProtectHome=true
PrivateTmp=true
RestrictAddressFamilies=AF_NETLINK AF_UNIX AF_QIPCRTR
NoNewPrivileges=true
User=root
[Install]
WantedBy=multi-user.target
[Unit]
Description=uConsole LTE Module
After=polkit.service
Requires=polkit.service
Before=ModemManager.service
PropagatesReloadTo=ModemManager.service
[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutStartSec=20
TimeoutStopSec=35
# Setup PWRKEY pin
ExecStartPre=/usr/bin/gpio mode 34 out
# Setup RESET pin
ExecStartPre=/usr/bin/gpio mode 33 out
# Set RESET pin to high
ExecStartPre=/usr/bin/gpio write 33 1
# Set PWRKEY pin to high
ExecStartPre=/usr/bin/gpio write 34 1
# Delay for module to properly reseted
ExecStartPre=/usr/bin/sleep 2
# Set RESET pin to low
ExecStartPre=/usr/bin/gpio write 33 0
# Wait for module to boot up
ExecStart=/usr/bin/sleep 13
# Wait for ModemManager to properly shutdown.
ExecStop=/usr/bin/sleep 5
# Setup PWRKEY pin
ExecStop=/usr/bin/gpio mode 34 out
# Set PWRKEY pin to low
ExecStop=/usr/bin/gpio write 34 0
# Set PWRKEY pin to high
ExecStop=/usr/bin/gpio write 34 1
# Delay for module to properly send power off signal
ExecStop=/usr/bin/sleep 3
# Set PWRKEY pin to low
ExecStop=/usr/bin/gpio write 34 0
# Delay for module to power off
ExecStop=/usr/bin/sleep 20
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment