DK6 API

This module contains support for programming DK6 devices.

Module with the DK6 commands

DK6 Device Commands.

class spsdk.dk6.commands.CmdPacket(data)

Bases: object

DK6 command packet format class.

Initialize the Command Packet object.

Parameters:

data (bytes) – Command data, defaults to None

info()

Get object info.

Return type:

str

to_bytes()

Serialize CmdPacket into bytes.

Return type:

Optional[bytes]

Returns:

Serialized object into bytes

class spsdk.dk6.commands.CmdResponse(cmd_type, raw_data)

Bases: object

DK6 response base format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

MSG_OFFSET = 1
info()

Get object info.

Return type:

str

class spsdk.dk6.commands.CommandTag

Bases: Enum

DK6 Commands.

EXECUTE = 33
GET_CHIPID = 50
MEM_BLANK_CHECK = 68
MEM_CLOSE = 74
MEM_ERASE = 66
MEM_GET_INFO = 76
MEM_OPEN = 64
MEM_READ = 70
MEM_WRITE = 72
RESET = 20
SET_BAUD = 39
UNLOCK_ISP = 78
class spsdk.dk6.commands.GenericResponse(cmd_type, raw_data)

Bases: CmdResponse

DK6 generic response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.GetChipIdResponse(cmd_type, raw_data)

Bases: CmdResponse

Chip get info response format class.

Initialize the Flash-Read-Once response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

FORMAT = '<II'
info()

Get object info.

Return type:

str

class spsdk.dk6.commands.IspUnlockResponse(cmd_type, raw_data)

Bases: CmdResponse

ISP Unlock response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemBlankCheckResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory open response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemCloseResponse(cmd_type, raw_data)

Bases: CmdResponse

DK6 memory close response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemEraseResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory open response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemGetInfoResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory get info response format class.

Initialize the Flash-Read-Once response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

FORMAT = '<BIIIBB'
MEM_NAME_OFFSET = 15
info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemOpenResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory open response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

FORMAT = '<B'
info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemReadResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory open response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemWriteResponse(cmd_type, raw_data)

Bases: CmdResponse

Memory open response format class.

Initialize the Command Response object.

Parameters:
  • header – Header for the response

  • raw_data (bytes) – Response data

info()

Get object info.

Return type:

str

class spsdk.dk6.commands.MemoryAccessValues

Bases: Enum

DK6 Memory Access Values.

ALL = 15
BLANK_CHECK_ENABLED = 4
ERASE = 2
ERASE_ALL = 3
READ = 0
WRITE = 1
class spsdk.dk6.commands.MemoryId

Bases: Enum

DK6 Memory IDs.

Config = 3
EFUSE = 4
FLASH = 0
PSECT = 1
RAM0 = 6
RAM1 = 7
ROM = 5
pFLASH = 2
class spsdk.dk6.commands.MemoryType

Bases: Enum

DK6 Memory Types.

EFUSE = 5
FLASH = 1
RAM = 2
ROM = 0
class spsdk.dk6.commands.ResponseTag

Bases: Enum

DK6 Responses to Commands.

EXECUTE = 34
GET_CHIPID = 51
MEM_BLANK_CHECK = 69
MEM_CLOSE = 75
MEM_ERASE = 67
MEM_GET_INFO = 77
MEM_OPEN = 65
MEM_READ = 71
MEM_WRITE = 73
RESET = 21
SET_BAUD = 40
UNLOCK_ISP = 79
class spsdk.dk6.commands.StatusCode

Bases: Enum

DK6 Command Status Codes.

ASSERT_FAIL = 251
AUTH_ERROR = 247
CRC_ERROR = 252
INVALID_RESPONSE = 253
MEMORY_ACCESS_INVALID = 243
MEMORY_BAD_STATE = 240
MEMORY_INVALID = 245
MEMORY_INVALID_MODE = 239
MEMORY_NOT_SUPPORTED = 244
MEMORY_OUT_OF_RANGE = 242
MEMORY_TOO_LONG = 241
NOT_SUPPORTED = 255
NO_RESPONSE = 246
OK = 0
READ_FAIL = 249
TEST_ERROR = 248
USER_INTERRUPT = 250
WRITE_FAIL = 254
spsdk.dk6.commands.parse_cmd_response(data, frame_type)

Parse command response.

Parameters:
  • data (bytes) – Input data in bytes

  • frame_type (int) – Frame Type

Return type:

Union[CmdResponse, GenericResponse, GetChipIdResponse, MemOpenResponse, MemEraseResponse, MemBlankCheckResponse, MemReadResponse, MemWriteResponse, MemCloseResponse, MemGetInfoResponse, IspUnlockResponse]

Returns:

De-serialized object from data

Module representing DK6 device and memories

DK6 Device high level API.

class spsdk.dk6.dk6device.DK6ChipIdInternal

Bases: Enum

DK6 Internal chip ID.

JN5189 = 2290649224
K32W041 = 318817926
K32W061 = 339789446
QN9090 = 306235014
class spsdk.dk6.dk6device.DK6Device(device)

Bases: object

Class that represents DK6 device.

It’s a high level class that encapsulates communication interface and protocol

DK6Device constructor.

Parameters:
  • device (SerialDevice) – SerialDevice that will be used for communication

  • baudrate – communication baudrate, defaults to 115200

add_memory(memory)

Add memory to the list of available memories.

Parameters:

memory (DK6Memory) – DK6Memory

Return type:

None

add_memory_from_response(memory_response)

Add memory from MemGetInfoResponse.

Parameters:

memory_response (MemGetInfoResponse) – MemGetInfoResponse

Return type:

None

close()

Close UART device.

Raises:

SPSDKError: When the device cannot be closed

Return type:

None

erase_memory(memory_id, address, length, access=15, progress_callback=None, relative=False, verify=False)

Erase memory of DK6 device.

# 1. Make a validation of the read request # 2. Open memory in given access mode # 4. Erase data # 5. Optionally verify with blank check # 6. Close memory

Parameters:
  • memory_id (MemoryId) – MemoryID of the memory to be used

  • address (int) – start address

  • length (int) – length of data

  • access (MemoryAccessValues) – memory access value, defaults to MemoryAccessValues.WRITE

  • progress_callback (Optional[Callable[[int, int], None]]) – progress callback used in CLI, defaults to None

  • relative (bool) – True if address is relative to the memory base address

  • verify (bool) – True for erase verification by memory blank check

Raises:
  • SPSDKError – Memory ID is not supported

  • SPSDKError – Access is not allowed

  • SPSDKError – Invalid range

  • SPSDKError – No response from device

Return type:

None

get_mac_str()

Get MAC address in string format.

Return type:

str

Returns:

string containing MAC address FF:FF..

get_memory(memory_id)

Get DK6Memory based on memory_id.

Parameters:

memory_id (MemoryId) – MemoryId of the desired memory

Raises:

SPSDKError – When the memory cannot be fetched

Return type:

DK6Memory

Returns:

DK6Memory

init()

Initialize DK6 device for communication. :rtype: None

  1. Unlock ISP default

  2. Get device information

  3. Unlock ISP with default key

  4. Get info about memories

read_memory(memory_id, address, length, access=1, progress_callback=None, relative=False)

Read memory from the DK6 device.

  1. Make a validation of the read request

  2. Open memory in given access mode

  3. Split read request to chunks of max(MAX_PAYLOAD_SIZE, requested_len)

  4. Read data

  5. Close memory

Parameters:
  • memory_id (MemoryId) – MemoryID of the memory to be used

  • address (int) – start address

  • length (int) – length of data

  • access (MemoryAccessValues) – memory access value, defaults to MemoryAccessValues.WRITE

  • progress_callback (Optional[Callable[[int, int], None]]) – progress callback used in CLI, defaults to None

  • relative (bool) – True if address is relative to the memory base address

Raises:
  • SPSDKError – Memory ID is not supported

  • SPSDKError – Access is not allowed

  • SPSDKError – Invalid range

Return type:

bytes

Returns:

Read data

reset()

Resets device.

Return type:

None

write_memory(memory_id, address, length, data, access=15, progress_callback=None, relative=False)

Write memory to the DK6 device.

  1. Make a validation of the read request

  2. Open memory in given access mode

  3. Split write request to chunks of max(MAX_PAYLOAD_SIZE, requested_len)

  4. Write data

  5. Close memory

Parameters:
  • memory_id (MemoryId) – MemoryID of the memory to be used

  • address (int) – start address

  • length (int) – length of data

  • data (bytes) – data to be written

  • access (MemoryAccessValues) – memory access value, defaults to MemoryAccessValues.WRITE

  • progress_callback (Optional[Callable[[int, int], None]]) – progress callback used in CLI, defaults to None

  • relative (bool) – True if address is relative to the memory base address

Raises:
  • SPSDKError – Memory ID is not supported

  • SPSDKError – Access is not allowed

  • SPSDKError – Invalid range

  • SPSDKError – No response from device

Return type:

None

class spsdk.dk6.dk6device.DK6DeviceId

Bases: Enum

DK6 Device IDs.

JN5188 = 5188
JN5189 = 5189
K32W041 = 32041
K32W061 = 32061
QN9030 = 9030
QN9090 = 9090
UNKNOWN = 0
class spsdk.dk6.dk6device.DK6Memory(base_address, length, sector_size, mem_type, mem_name, mem_id, access)

Bases: object

Class that holds information about the DK6 devices memory.

DK6Memory Constructor.

Parameters:
  • base_address (int) – Memory base address

  • length (int) – Memory length

  • sector_size (int) – Memory sector size

  • mem_type (MemoryType) – Memory type

  • mem_name (str) – Memory name

  • mem_id (MemoryId) – Memory ID

  • access (MemoryAccessValues) – Memory access

property end_address: int

End address of Memory.

Returns:

End address

spsdk.dk6.dk6device.check_memory(memory, access, length, relative, address)

Check memory range and return sanitized address value.

Parameters:
  • memory (DK6Memory) – DK6Memory

  • access (MemoryAccessValues) – access type

  • length (int) – length of data

  • relative (bool) – true if address is relative to base address

  • address (int) – memory address

Raises:
  • SPSDKError – if memory ID is not supported

  • SPSDKError – if access is not allowed

  • SPSDKError – if the memory range is invalid

Return type:

int

Returns:

Sanitized memory address

Module representing drivers for communication

DK6 Drivers backend interface.

class spsdk.dk6.driver.Backend(value)

Bases: Enum

Backend selection.

FTD2xx = 3
PYFTDI = 1
PYLIBFTDI = 2
PYSERIAL = 4
class spsdk.dk6.driver.DeviceInfo(device_id, vid, pid, sn, description, address, backend)

Bases: object

Device info class.

Contains information about the connected device

Device info constructor.

Parameters:
  • device_id (Union[str, int]) – Device ID

  • vid (int) – USB VID

  • pid (int) – USB PID

  • sn (str) – Serial number

  • description (str) – description

  • address (int) – device address

  • backend (Backend) – backend

class spsdk.dk6.driver.DriverInterface(backend)

Bases: object

Interface to FTDI backends.

Supported backends are: pyftdi, pylibftdi and ftdi2xx

Initialize driver interface and serial interface based on backend.

Parameters:

backend (Backend) – supported backend name

get_serial()

Return serial device.

Raises:

SPSDKError – if serial device is not initialized

Return type:

SerialDevice

Returns:

Serial device based on backend

go_to_isp(device_id)

Send a sequence that goes to ISP mode using FTDI bitbang device.

Return type:

None

init_serial(device_id, baudrate=115200, timeout=5000)

Initialize serial device.

Parameters:
  • device_id (str) – device ID

  • baudrate (int) – UART baudrate, defaults to 115200

  • timeout (int) – read and write timeout, defaults to 5000 ms

Raises:

SPSDKError – if invalid device_id is provided

Return type:

None

list_devices()

Returns a list of devices that are connected for selected backend.

Return type:

List[DeviceInfo]

Returns:

List of devices

spsdk.dk6.driver.generate_pyftdi_url(serial_number, address=1)

Generates URL for PyFTDI from serial number.

Parameters:
  • serial_number (str) – serial number from USBDeviceDescriptor

  • address (int) – interface number

Return type:

str

Returns:

url for PyFTDI

Module representing communication interface

DK6 UART communication interface.

class spsdk.dk6.interface.Uart(device)

Bases: object

UART interface for DK6 devices.

Initialize the UART interface.

Parameters:

port – name of the serial port, defaults to None

Raises:

McuBootConnectionError – when the port could not be opened

CHECKSUM_SIZE = 4
FLAG_SIZE = 1
FRAME_START_BYTE = 0
FRAME_TYPE_SIZE = 1
HEADER_SIZE = 8
LENGTH_SIZE = 2
static calc_frame_crc(data, frame_type)

Calculate the CRC of a frame.

Parameters:
  • data (Optional[bytes]) – frame data

  • frame_type (Union[int, CommandTag]) – frame type

Return type:

int

Returns:

calculated CRC

close()

Close the UART interface.

Raises:

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

Return type:

None

static create_frame(data, frame_type)

Encapsulate data into frame.

Parameters:
  • data (Optional[bytes]) – payload data

  • frame_type (Union[int, CommandTag]) – frame type

Return type:

bytes

Returns:

frame

property is_opened: bool

Return True if device is open, False otherwise.

open()

Open the UART interface.

Raises:

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

Return type:

None

read()

Read data from device.

Return type:

Any

Returns:

CmdResponse

Raises:
  • SPSDKError – Did not receive correct frame start byte

  • SPSDKError – When received invalid CRC

write(frame_type, packet)

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

Parameters:
  • frame_type (CommandTag) – CommandTag

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

Raises:

AttributeError – frame type is incorrect

Return type:

None

spsdk.dk6.interface.calc_crc(data)

Calculate CRC from the data.

Parameters:

data (bytes) – data to calculate CRC from

Return type:

int

Returns:

calculated CRC

spsdk.dk6.interface.to_int(data, little_endian=False)

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

Module representing communication protocol

DK6 Communication protocol.

class spsdk.dk6.protocol.DK6Protocol(device)

Bases: object

Class implementing communication protocol for the DK6 devices.

DK6Protocol constructor.

Parameters:

device (Uart) – serial device that will be used for communication.

MAX_PAYLOAD_SIZE = 512
execute(address)

This command executes (runs) code in flash or RAM.

The response is sent before execution jumps to the provided address.

Parameters:

address (int) – Memory address to start execution from

Return type:

GenericResponse

Returns:

GenericResponse

get_device_information()

Get device information.

Return type:

GetChipIdResponse

Returns:

GetChipIdResponse containing chip ID and chip (ROM) version

mem_blank_check(address, length, handle=0, mode=0)

This command checks if a region of the selected memory has been erased.

Parameters:
  • address (int) – start address

  • length (int) – number of bytes to be erased

  • handle (int) – handle returned by open memory command, defaults to 0

  • mode (int) – erase mode, defaults to 0

Return type:

MemBlankCheckResponse

Returns:

MemEraseResponse

mem_close(handle=0)

Close the memory. Finalize writing of the memory.

Parameters:

handle (int) – handle returned by open memory command, defaults to 0

Return type:

MemCloseResponse

Returns:

MemCloseResponse

mem_erase(address, length, handle=0, mode=0)

This command erases a region of the selected memory.

Parameters:
  • address (int) – start address

  • length (int) – number of bytes to be erased

  • handle (int) – handle returned by open memory command, defaults to 0

  • mode (int) – erase mode, defaults to 0

Return type:

MemEraseResponse

Returns:

MemEraseResponse

mem_get_info(memory_id=0)

Get memory info about specified memory ID.

Parameters:

memory_id (Union[MemoryId, int]) – memory ID, defaults to MemoryId.FLASH

Return type:

MemGetInfoResponse

Returns:

MemGetInfoResponse containing information like size, length etc.

mem_open(memory_id=0, access=1)

Open given memory in the specified accesss mode.

Parameters:
  • memory_id (MemoryId) – memory that will be opened, defaults to MemoryId.FLASH

  • access (MemoryAccessValues) – access mode, defaults to MemoryAccessValues.READ

Return type:

MemOpenResponse

Returns:

MemOpenResponse containing handle

mem_read(address, length, handle=0, mode=0)

Read from memory.

Parameters:
  • address (int) – start address

  • length (int) – length of data to be read in bytes

  • handle (int) – handle that was returned by mem_open, defaults to 0

  • mode (int) – Read mode, defaults to 0

Return type:

MemReadResponse

Returns:

MemReadResponse containing read data

mem_write(address, length, data, handle=0, mode=0)

Write to memory.

Parameters:
  • address (int) – start address

  • length (int) – number of bytes to be written

  • data (bytes) – data to be written

  • handle (int) – handle returned by open memory command, defaults to 0

  • mode (int) – write mode, defaults to 0

Return type:

MemWriteResponse

Returns:

MemWriteResponse

reset()

Resets device.

Return type:

GenericResponse

Returns:

GenericResponse

set_baud_rate(baudrate)

Sets baudrate.

Parameters:

baudrate (int) – int value of baudrate to be set

Return type:

GenericResponse

Returns:

GenericResponse

unlock_isp(mode=1, key=b'\\x11"3DUfw\\x88\\x11"3DUfw\\x88')

Unlocks ISP with the key.

If the key is not provided, default will be used.

Parameters:
  • mode (IspMode) – Unlock ISP mode, defaults to IspMode.START_ISP

  • key (bytes) – default key or signed unlock key, defaults to DEFAULT_KEY

Return type:

IspUnlockResponse

Returns:

IspUnlockResponse

unlock_isp_default()

Sends unlock ISP sequence in default mode.

It means that only Get device info command will work.

Return type:

IspUnlockResponse

Returns:

IspUnlockResponse

class spsdk.dk6.protocol.IspMode

Bases: Enum

DK6 ISP modes.

DEFAULT = 0
EXTENDED_ISP_UNLOCK = 128
START_ISP = 1
UNLOCK_DEVICE = 127