API Reference#
A Python wrapper for bossac.
Functions#
- bossac.upload(filename, *, port, offset=None, erase=True, verify=True, boot=True, reset=True, touch=True, usb=None, log_level=logging.INFO)#
Upload firmware to a device.
The firmware file is checked first. Then, unless
touchis False, the device is put into its SAM-BA bootloader with a 1200 baud touch. The flash is then erased, written and verified, the device is set to boot from flash and finally reset to start the firmware.- Parameters:
filename (
PathLike|str) – Path to the firmware file: a raw binary (.bin) or an Intel HEX file (.hex).port (
str) – Port of the device.offset (
int|None, default:None) – Flash offset at which to write a binary file - required for one. Must be aligned to a flash page, and must not overlap the bootloader - see the notes below. For an Intel HEX file, the offset is determined from the file’s addresses and the device’s flash address - specifying it raises aValueError.erase (
bool, default:True) – Erase the flash before writing.verify (
bool, default:True) – Verify the flash’s contents after writing.boot (
bool, default:True) – Set the device to boot from flash, if supported.reset (
bool, default:True) – Reset the device after the upload, if supported.touch (
bool, default:True) – Put the device into its bootloader with a 1200 baud touch first.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False), e.g. the Arduino Due’s programming port. Detected by bossac if not given.log_level (
int, default:logging.INFO) – Log level for bossac’s status messages,logging.NOTSETto disable. Errors are always logged as warnings.
- Raises:
FileNotFoundError – If the firmware file does not exist, or if
touchis True and the port can’t be found.IsADirectoryError – If the firmware path is a directory.
ValueError – If the firmware file has an unsupported extension, can’t be loaded or doesn’t fit into the flash, or if the offset is missing or invalid.
TimeoutError – If the bootloader’s port doesn’t appear after the 1200 baud touch, or if bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails, e.g. if the verification fails.
- Return type:
None
Notes
A binary file doesn’t record where in the flash it belongs, so its offset has to be given, matching the board’s bootloader. Offsets are relative to the start of the flash:
Arduino Due and other SAM3/SAM4 boards: 0, as their bootloader is in ROM
SAMD21 boards with an Arduino-style bootloader (Arduino Zero/MKR, SparkFun, Adafruit M0):
0x2000, after the 8 KiB bootloaderSAMD51 boards with a UF2 bootloader (Adafruit M4):
0x4000, after the 16 KiB bootloader
An offset of 0 on a board whose bootloader is in flash overwrites the bootloader. Intel HEX files carry absolute addresses, so they are flashed at the right offset on any board - prefer them where a build produces them.
Examples
Upload an Intel HEX file, which is flashed at the offset of its addresses:
>>> bossac.upload('blink.hex', port='/dev/ttyACM0')Upload a binary file to a SAMD21 board, after its 8 KiB bootloader:
>>> bossac.upload('blink.bin', port='/dev/ttyACM0', offset=0x2000)Upload a binary file via the programming port of an Arduino Due:
>>> bossac.upload('blink.bin', port='/dev/ttyACM0', offset=0, usb=False)
- bossac.verify(filename, *, port, offset=None, usb=None, log_level=logging.INFO)#
Verify that a device’s flash matches a firmware file.
The device must already be in its SAM-BA bootloader. There is deliberately no
touchoption: a 1200 baud touch usually destroys the very firmware that is to be checked. Ways into the bootloader that keep the firmware intact:double-tapping the reset button, on SAMD boards with an Arduino-style bootloader
a command of the firmware itself that resets into the bootloader without erasing anything, where the firmware offers one
none on the Arduino Due, whose ROM bootloader is only reached by erasing the flash
- Parameters:
filename (
PathLike|str) – Path to the firmware file: a raw binary (.bin) or an Intel HEX file (.hex).port (
str) – Port of the device.offset (
int|None, default:None) – Flash offset of a binary file - required for one, seeupload()for typical values. For an Intel HEX file, the offset is determined from the file’s addresses and the device’s flash address - specifying it raises aValueError.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False). Detected by bossac if not given.log_level (
int, default:logging.INFO) – Log level for bossac’s status messages,logging.NOTSETto disable. Errors are always logged as warnings.
- Raises:
FileNotFoundError – If the firmware file does not exist.
IsADirectoryError – If the firmware path is a directory.
ValueError – If the firmware file has an unsupported extension, can’t be loaded or doesn’t fit into the flash, or if the offset is missing or invalid.
TimeoutError – If bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails or the flash doesn’t match the firmware file.
- Return type:
None
Examples
Check the firmware of the device on a specific port:
>>> bossac.verify('blink.bin', port='/dev/ttyACM0')
- bossac.read(filename, *, port, size=None, offset=0, usb=None, log_level=logging.INFO)#
Read a device’s flash into a binary file.
An existing file is overwritten.
The device must already be in its SAM-BA bootloader. There is deliberately no
touchoption: a 1200 baud touch usually destroys the very firmware that is to be read. Ways into the bootloader that keep the firmware intact:double-tapping the reset button, on SAMD boards with an Arduino-style bootloader
a command of the firmware itself that resets into the bootloader without erasing anything, where the firmware offers one
none on the Arduino Due, whose ROM bootloader is only reached by erasing the flash
- Parameters:
filename (
PathLike|str) – Path of the binary file (.bin) to write.port (
str) – Port of the device.size (
int|None, default:None) – Number of bytes to read, the entire flash if not given.offset (
int, default:0) – Flash offset at which to start reading. Must be aligned to a flash page.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False). Detected by bossac if not given.log_level (
int, default:logging.INFO) – Log level for bossac’s status messages,logging.NOTSETto disable. Errors are always logged as warnings.
- Raises:
FileNotFoundError – If the file’s directory does not exist.
IsADirectoryError – If the path is a directory.
ValueError – If the file doesn’t have the extension
.bin.TimeoutError – If bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails.
- Return type:
None
Examples
Back up the first 64 KiB of the device’s flash:
>>> bossac.read('backup.bin', port='/dev/ttyACM0', size=0x10000)
- bossac.erase(*, port, offset, touch=False, usb=None, log_level=logging.INFO)#
Erase a device’s flash.
The device must be in its SAM-BA bootloader, unless
touchis True.- Parameters:
port (
str) – Port of the device.offset (
int) – Flash offset at which to start erasing. Must be aligned to a flash page. On boards whose bootloader is in flash, it must not overlap the bootloader - see the notes ofupload()for typical values.touch (
bool, default:False) – Put the device into its bootloader with a 1200 baud touch first.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False). Detected by bossac if not given.log_level (
int, default:logging.INFO) – Log level for bossac’s status messages,logging.NOTSETto disable. Errors are always logged as warnings.
- Raises:
FileNotFoundError – If
touchis True and the port can’t be found.TimeoutError – If the bootloader’s port doesn’t appear after the 1200 baud touch, or if bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails.
- Return type:
None
Warning
With an offset of 0, the entire flash is erased - including the bootloader of boards that keep it in flash, such as SAMD21 and SAMD51 boards. Such a board can’t be programmed over USB anymore until its bootloader is restored with a debugger.
Examples
Erase the firmware of a SAMD21 board, keeping its 8 KiB bootloader:
>>> bossac.erase(port='/dev/ttyACM0', offset=0x2000, touch=True)Erase the entire flash of an Arduino Due, whose bootloader is in ROM:
>>> bossac.erase(port='/dev/ttyACM0', offset=0, touch=True)
- bossac.reset(*, port, usb=None, log_level=logging.INFO)#
Reset a device to start its firmware.
The device must be in its SAM-BA bootloader.
- Parameters:
port (
str) – Port of the device.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False). Detected by bossac if not given.log_level (
int, default:logging.INFO) – Log level for bossac’s status messages,logging.NOTSETto disable. Errors are always logged as warnings.
- Raises:
TimeoutError – If bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails.
- Return type:
None
Examples
Leave the bootloader of a device:
>>> bossac.reset(port='/dev/ttyACM0')
- bossac.info(*, port, touch=False, usb=None)#
Return a device’s details.
The device must be in its SAM-BA bootloader, unless
touchis True. SeeDeviceInfofor the details reported.- Parameters:
port (
str) – Port of the device.touch (
bool, default:False) – Put the device into its bootloader with a 1200 baud touch first. This usually destroys the firmware, which then has to be uploaded again: the Arduino Due erases its flash, and SAMD boards running Arduino firmware erase its first flash row.usb (
bool|None, default:None) – Whether the port is a native USB port (True) or a USB-to-serial bridge (False). Detected by bossac if not given.
- Returns:
Details of the device.
- Return type:
- Raises:
FileNotFoundError – If
touchis True and the port can’t be found.TimeoutError – If the bootloader’s port doesn’t appear after the 1200 baud touch, or if bossac stops responding, e.g. because the device isn’t in its bootloader.
RuntimeError – If bossac’s output can’t be parsed.
BossacError – If bossac fails.
Examples
Get the flash size of a device in its bootloader:
>>> bossac.info(port='/dev/ttyACM0')['total_size'] 524288
- bossac.version()#
Return the version of the bundled bossac binary.
- Returns:
Version of bossac, e.g.
'1.9.1'.- Return type:
- Raises:
RuntimeError – If the version can’t be determined from bossac’s output.
TimeoutError – If bossac stops responding, e.g. because the device isn’t in its bootloader.
BossacError – If bossac fails.
Examples
Check the version of the bundled bossac binary:
>>> bossac.version() '1.9.1'
Exceptions#
- exception bossac.BossacError#
Bases:
CalledProcessErrorRaised when bossac exits with an error.
- classmethod __new__(*args, **kwargs)#
- __init__(returncode, cmd, output=None, stderr=None)#