MBoot Interfaces

Module implementing the Mboot communication protocol.

MBoot UART

Module for serial communication with a target device using MBoot protocol.

spsdk.mboot.interfaces.uart.scan_uart(port=None, baudrate=None, timeout=None)

Scan connected serial ports.

Returns list of serial ports with devices that respond to PING command. If ‘port’ is specified, only that serial port is checked If no devices are found, return an empty list.

Parameters
  • port (Optional[str]) – name of preferred serial port, defaults to None

  • baudrate (Optional[int]) – speed of the UART interface, defaults to 56700

  • timeout (Optional[int]) – timeout in milliseconds, defaults to 5000

Return type

List[Uart]

Returns

list of interfaces responding to the PING command

spsdk.mboot.interfaces.uart.calc_crc(data)

Calculate CRC from the data.

Parameters

data (bytes) – data to calculate CRC from

Return type

int

Returns

calculated CRC

spsdk.mboot.interfaces.uart.to_int(data, little_endian=True)

Convert bytes into single integer.

Parameters
  • data (bytes) – bytes to convert

  • little_endian (bool) – indicate byte ordering in data, defaults to True

Return type

int

Returns

integer

class spsdk.mboot.interfaces.uart.PingResponse(version: int, options: int, crc: int)

Bases: tuple

Special type of response for Ping Command.

Create new instance of PingResponse(version, options, crc)

property version

Alias for field number 0

property options

Alias for field number 1

property crc

Alias for field number 2

classmethod parse(data)

Parse raw data into PingResponse object.

Parameters

data (bytes) – bytes to be unpacked to PingResponse object 4B version, 2B data, 2B CRC16

Raises

McuBootConnectionError – Received invalid ping response

Return type

PingResponse

Returns

PingResponse

class spsdk.mboot.interfaces.uart.FPType

Bases: spsdk.utils.easy_enum.Enum

Type of frames used in serial communication.

ACK = 161
NACK = 162
ABORT = 163
CMD = 164
DATA = 165
PING = 166
PINGR = 167
class spsdk.mboot.interfaces.uart.Uart(port=None, baudrate=57600, timeout=5000)

Bases: spsdk.mboot.interfaces.base.MBootInterface

UART interface.

Initialize the UART interface.

Parameters
  • port (Optional[str]) – name of the serial port, defaults to None

  • baudrate (int) – baudrate of the serial port, defaults to 57600

  • timeout (int) – read/write timeout in milliseconds, defaults to 5000

Raises

McuBootConnectionError – when the port could not be opened

FRAME_START_BYTE = 90
FRAME_START_BYTE_NOT_READY = 0
property is_opened: bool

Return True if device is open, False otherwise.

Return type

bool

open()

Open the UART interface.

Raises

McuBootConnectionError – In any case of fail of UART open operation.

Return type

None

close()

Close the UART interface.

Raises

McuBootConnectionError – In any case of fail of UART close operation.

Return type

None

info()

Return information about the UART interface.

Return type

str

Returns

Description of UART interface

Raises

McuBootConnectionError – When no port is available

read()

Read data from device.

Return type

Union[CmdResponse, bytes]

Returns

read data

Raises
write(packet)

Write data to the device; data might be in form of ‘CmdPacket’ or bytes.

Parameters

packet (Union[CmdPacket, bytes]) – Packet to send

Raises

AttributeError – frame type is incorrect

Return type

None

ping()

Ping the target device, retrieve protocol version.

Raises
Return type

None

ping_timeout(timeout=500)

Context manager for changing UART’s timeout.

Parameters

timeout (int) – New temporary timeout in milliseconds, defaults to PING_TIMEOUT_MS (500ms)

Return type

Generator[None, None, None]

Returns

Generator[None, None, None]

MBoot USB

Module for serial communication with a target device using MBoot protocol.

spsdk.mboot.interfaces.usb.scan_usb(device_id=None)

Scan connected USB devices.

Parameters

device_id (Optional[str]) – see USBDeviceFilter classes constructor for usb_id specification

Return type

List[RawHid]

Returns

list of matching RawHid devices

class spsdk.mboot.interfaces.usb.RawHid

Bases: spsdk.mboot.interfaces.base.MBootInterface

Base class for OS specific RAW HID Interface classes.

Initialize the USB interface object.

property name: str

Get the name of the device.

Return type

str

Returns

Name of the device.

property is_opened: bool

Indicates whether device is open.

Return type

bool

Returns

True if device is open, False othervise.

info()

Return information about the USB interface.

Return type

str

open()

Open the interface.

Raises
Return type

None

close()

Close the interface.

Raises
Return type

None

write(packet)

Write data on the OUT endpoint associated to the HID interfaces.

Parameters

packet (Union[CmdPacket, bytes]) – Data to send

Raises
Return type

None

read()

Read data on the IN endpoint associated to the HID interface.

Return type

Union[CmdResponse, bytes]

Returns

Return CmdResponse object.

Raises
static enumerate(usb_device_filter)

Get list of all connected devices matching the USBDeviceFilter object.

Parameters

usb_device_filter (USBDeviceFilter) – USBDeviceFilter object

Return type

List[RawHid]

Returns

List of interfaces found

MBoot USBSIO

Module for USB-SIO communication with a target device using MBoot protocol.

spsdk.mboot.interfaces.usbsio.get_usbsio_devices(config=None)

Returns list of ports indexes of USBSIO devices.

It could be filtered by standard SPSDK USB filters.

Parameters

config (Optional[str]) – Could contain USB filter configuration, defaults to None

Return type

List[int]

Returns

List of port indexes of founded USBSIO device

spsdk.mboot.interfaces.usbsio.scan_usbsio(config=None, timeout=5000)

Scan connected USB-SIO bridge devices.

Parameters
  • config (Optional[str]) – Configuration string identifying spi or i2c SIO interface and could filter out USB devices

  • timeout (int) – Read timeout in milliseconds, defaults to 5000

Return type

List[UsbSio]

Returns

List of matching UsbSio devices

Raises
  • SPSDKError – When libusbsio library error or if no bridge device found

  • SPSDKValueError – Invalid configuration detected.

spsdk.mboot.interfaces.usbsio.scan_usbsio_i2c(config=None, timeout=5000)

Scan connected USB-SIO bridge devices and return just I2C devices.

Parameters
  • config (Optional[str]) – Configuration string identifying spi or i2c SIO interface and could filter out USB devices.

  • timeout (int) – Read timeout in milliseconds, defaults to 5000

Return type

List[UsbSioI2C]

Returns

List of matching UsbSioI2C devices only

spsdk.mboot.interfaces.usbsio.scan_usbsio_spi(config=None, timeout=5000)

Scan connected USB-SIO bridge devices and return just SPI devices.

Parameters
  • config (Optional[str]) – Configuration string identifying spi or i2c SIO interface and could filter out USB devices.

  • timeout (int) – Read timeout in milliseconds, defaults to 5000

Return type

List[UsbSioSPI]

Returns

List of matching UsbSioSPI devices only

class spsdk.mboot.interfaces.usbsio.UsbSio(dev=0, config=None, timeout=5000)

Bases: spsdk.mboot.interfaces.uart.Uart

USBSIO general interface, base class for SPI or I2C communication over LIBUSBSIO.

This class inherits from Uart communication as the SPI/I2C protocol is the same. The Uart’s read and write methods are leveraged. The low-level _read and _write methods are overridden.

Initialize the Interface object.

Parameters
  • dev (int) – device index to be used, default is set to 0

  • config (Optional[str]) – configuration string identifying spi or i2c SIO interface

  • timeout (int) – read timeout in milliseconds, defaults to 5000

Raises

SPSDKError – When LIBUSBSIO device is not opened.

property is_opened: bool

Indicates whether interface is open.

Return type

bool

info()

Return string containing information about the interface.

Return type

str

static get_interface_cfg(config, interface)

Return part of interface config.

Parameters
  • config (str) – Full config of LIBUSBSIO

  • interface (str) – Name of interface to find.

Return type

str

Returns

Part with interface config.

class spsdk.mboot.interfaces.usbsio.UsbSioSPI(config=None, dev=0, port=0, ssel_port=0, ssel_pin=15, speed_khz=1000, cpol=1, cpha=1, timeout=5000)

Bases: spsdk.mboot.interfaces.usbsio.UsbSio

USBSIO SPI interface.

Initialize the UsbSioSPI Interface object.

Parameters
  • config (Optional[str]) – configuration string passed from command line

  • dev (int) – device index to be used, default is set to 0

  • port (int) – default SPI port to be used, typically 0 as only one port is supported by LPCLink2/MCULink

  • ssel_port (int) – bridge GPIO port used to drive SPI SSEL signal

  • ssel_pin (int) – bridge GPIO pin used to drive SPI SSEL signal

  • speed_khz (int) – SPI clock speed in kHz

  • cpol (int) – SPI clock polarity mode

  • cpha (int) – SPI clock phase mode

  • timeout (int) – read timeout in milliseconds, defaults to 5000

Raises

SPSDKError – When port configuration cannot be parsed

FRAME_START_BYTE_NOT_READY = 255
open()

Open the interface.

Return type

None

close()

Close the interface.

Return type

None

class spsdk.mboot.interfaces.usbsio.UsbSioI2C(config=None, dev=0, port=0, address=16, speed_khz=100, timeout=5000)

Bases: spsdk.mboot.interfaces.usbsio.UsbSio

USBSIO I2C interface.

Initialize the UsbSioI2C Interface object.

Parameters
  • config (Optional[str]) – configuration string passed from command line

  • dev (int) – device index to be used, default is set to 0

  • port (int) – default I2C port to be used, typically 0 as only one port is supported by LPCLink2/MCULink

  • address (int) – I2C target device address

  • speed_khz (int) – I2C clock speed in kHz

  • timeout (int) – read timeout in milliseconds, defaults to 5000

Raises

SPSDKError – When port configuration cannot be parsed

open()

Open the interface.

Return type

None

close()

Close the interface.

Return type

None

MBoot Interface Class

Module for functionality shared across all MBoot interfaces.

spsdk.mboot.interfaces.base.Interface

alias of spsdk.mboot.interfaces.base.MBootInterface

class spsdk.mboot.interfaces.base.MBootInterface(reopen=False)

Bases: abc.ABC

Base class for all Mboot Interface classes.

Initialize the Interface object.

Parameters

reopen (bool) – Reopen the interface after reset, defaults to False

close()

Close the interface.

Return type

None

info()

Return string containing information about the interface.

Return type

str

property is_opened: bool

Indicates whether interface is open.

Return type

bool

property need_data_split: bool

Indicates whether device need to split data into smaller chunks.

Return type

bool

open()

Open the interface.

Return type

None

read()

Read data from the device.

Return type

Union[CmdResponse, bytes]

write(packet)

Write a packet to the device.

Return type

None