Flashing Firmware with Python
v0.3.7
The
tweliter
module is currently in beta.Overview
The tweliter
module is a Python module for flashing firmware to TWELITE devices.
It can be installed from PyPI, used as a command-line tool, or controlled from Python scripts.
Installation
Install it from PyPI:
pip install tweliter
With Poetry:
poetry add tweliter
Additional Notes for Linux
udev
Rule Configuration
You may need to grant permissions using udev
.
- Create
/etc/udev/rules.d/99-ftdi.rules
:
# TWELITE R / MONOSTICK (FT232R / 0403:6001)
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0666"
# TWELITE R2 / R3 (FT230X / 0403:6015)
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", MODE="0666"
- Reload the udev rules:
sudo udevadm control --reload-rules
sudo udevadm trigger
While the TWELITE STAGE App requires unloading the ftdi_sio
module, it needs to be loaded when using Python:
modprobe usbserial
modprobe ftdi_sio
Additional Notes for Windows
Applying the libusb
Driver
If you get an error like No backend available
, try applying the libusb-win32
driver using Zadig. Select the TWELITE R or MONOSTICK device and apply libusb-win32
(not WinUSB
).
Reference: Installation — PyFtdi documentation (uses the
pyftdi
module)
Examples
As a Command-Line Tool
Specify a .bin
binary file after tweliter
:
tweliter dir/SomeApp_BLUE.bin
Be careful not to confuse BLUE series with RED series.
tweliter
Command-Line Options
tweliter -h
usage: tweliter [-h] [--url URL] [--verify] [--startmsg STARTMSG]
[--retry RETRY]
file
Write TWELITE BLUE/RED firmware
positional arguments:
file Firmware file to write
options:
-h, --help show this help message and exit
--url URL Device URL starting with ftdi://
--verify Verify firmware after writing
--startmsg STARTMSG Prefix for startup message to check
--retry RETRY Retry count in case of firmware writing failure
url
: Specify the URL scheme for FTDI device- If omitted, it auto-selects if one device is found, or prompts selection if multiple
verify
: Verifies firmware after flashing if specifiedstartmsg
: Displays the startup message after the specified prefix- Default is
!INF MONO WIRELESS
- Default is
retry
: Retries the write process if it fails- Defaults to 2 retries
From a Python Script
from pathlib import Path
from tweliter import Tweliter
file = Path('dir/SomeApp_BLUE.bin')
try:
with Tweliter(url="ftdi://:ft-x:/1") as liter:
# Get serial interface
ser = liter.get_serial_instance()
# Write firmware
liter.write(ser, file, verify=True)
# Show startup message
print(liter.get_startup_message_after(ser, "!INF MONO WIRELESS"))
except IOError as e:
print(f"Cannot connect {e}")
except RuntimeError as e:
print(f"Failed to write {e}")