DK6 API

Contents

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(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Commands.

EXECUTE = (33, 'ExecuteCommand', 'Execute Command')#
GET_CHIPID = (50, 'GetChipIdCommand', 'Get Chip ID Command')#
MEM_BLANK_CHECK = (68, 'MemBlankCheckCommand', 'Memory Blank Check Command')#
MEM_CLOSE = (74, 'MemoryCloseCommand', 'Memory Close Command')#
MEM_ERASE = (66, 'MemEraseCommand', 'Memory Erase Command')#
MEM_GET_INFO = (76, 'MemoryGetInfoCommand', 'Memory Get Info Command')#
MEM_OPEN = (64, 'MemOpenCommand', 'Memory Open Command')#
MEM_READ = (70, 'MemReadCommand', 'Memory Read Command')#
MEM_WRITE = (72, 'MemoryWriteCommand', 'Memory Write Command')#
RESET = (20, 'ResetCommand', 'Reset Command')#
SET_BAUD = (39, 'SetBaudCommand', 'Set Baud Command')#
UNLOCK_ISP = (78, 'UnlockISPCommand', 'Unlock ISP Command')#
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(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Memory Access Values.

ALL = (15, 'All is available')#
BLANK_CHECK_ENABLED = (4, 'Blank Check Enabled')#
ERASE = (2, 'Erase Enabled')#
ERASE_ALL = (3, 'Erase All Enabled')#
READ = (0, 'Read Enabled')#
WRITE = (1, 'Write Enabled')#
class spsdk.dk6.commands.MemoryId(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Memory IDs.

Config = (3, 'Config')#
EFUSE = (4, 'EFUSE')#
FLASH = (0, 'FLASH')#
PSECT = (1, 'PSECT')#
RAM0 = (6, 'RAM0')#
RAM1 = (7, 'RAM1')#
ROM = (5, 'ROM')#
pFLASH = (2, 'pFLASH')#
class spsdk.dk6.commands.MemoryType(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Memory Types.

EFUSE = (5, 'EFUSE (OTP)', 'EFUSE (OTP)')#
FLASH = (1, 'FLASH', 'FLASH memory')#
RAM = (2, 'RAM', 'RAM')#
ROM = (0, 'ROM', 'Read only memory')#
class spsdk.dk6.commands.ResponseTag(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Responses to Commands.

EXECUTE = (34, 'ExecuteResponse', 'Execute Response')#
GET_CHIPID = (51, 'GetChipIdResponse', 'Get Chip ID Response')#
MEM_BLANK_CHECK = (69, 'MemBlankCheckResponse', 'Memory Blank Check Response')#
MEM_CLOSE = (75, 'MemoryCloseResponse', 'Memory Close Response')#
MEM_ERASE = (67, 'MemEraseResponse', 'Memory Erase Response')#
MEM_GET_INFO = (77, 'MemoryGetInfoResponse', 'Memory Get Info Response')#
MEM_OPEN = (65, 'MemOpenResponse', 'Memory Open Response')#
MEM_READ = (71, 'MemReadResponse', 'Memory Read Response')#
MEM_WRITE = (73, 'MemoryWriteResponse', 'Memory Write Response')#
RESET = (21, 'ResetResponse', 'Reset Response')#
SET_BAUD = (40, 'SetBaudResponse', 'Set Baud Response')#
UNLOCK_ISP = (79, 'UnlockISPResponse', 'Unlock ISP Response')#
class spsdk.dk6.commands.StatusCode(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Command Status Codes.

ASSERT_FAIL = (251, 'ASSERT_FAIL', 'Assertion fail')#
AUTH_ERROR = (247, 'AUTH_ERROR', 'Not authorized')#
CRC_ERROR = (252, 'CRC_ERROR', 'CRC Error')#
INVALID_RESPONSE = (253, 'INVALID_RESPONSE', 'Invalid response')#
MEMORY_ACCESS_INVALID = (243, 'MEMORY_ACCESS_INVALID', 'Memory access invalid')#
MEMORY_BAD_STATE = (240, 'MEMORY_BAD_STATE', 'Memory bad state')#
MEMORY_INVALID = (245, 'MEMORY_INVALID', 'Memory invalid')#
MEMORY_INVALID_MODE = (239, 'MEMORY_INVALID_MODE', 'Memory invalid mode')#
MEMORY_NOT_SUPPORTED = (244, 'MEMORY_NOT_SUPPORTED', 'Memory not supported')#
MEMORY_OUT_OF_RANGE = (242, 'MEMORY_OUT_OF_RANGE', 'Memory out of range')#
MEMORY_TOO_LONG = (241, 'MEMORY_TOO_LONG', 'Memory too long')#
NOT_SUPPORTED = (255, 'NOT_SUPPORTED', 'Not supported')#
NO_RESPONSE = (246, 'NO_RESPONSE', 'No response')#
OK = (0, 'OK', 'Success')#
READ_FAIL = (249, 'READ_FAIL', 'Read fail')#
TEST_ERROR = (248, 'TEST_ERROR', 'Test Error')#
USER_INTERRUPT = (250, 'USER_INTERRUPT', 'User interrupt')#
WRITE_FAIL = (254, 'WRITE_FAIL', 'Write fail')#
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(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Internal chip ID.

JN5189 = (2290649224, 'JN5189', 'JN5189 ESx')#
K32W041 = (318817926, 'K32W041', 'K32W041')#
K32W061 = (339789446, 'K32W061', 'K32W061')#
QN9090 = (306235014, 'QN9090', 'QN9090')#
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=MemoryAccessValues.ALL, 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:
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=MemoryAccessValues.WRITE, 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:
Return type:

bytes

Returns:

Read data

reset()#

Resets device.

Return type:

None

set_baud_rate(baudrate)#

Set baud rate.

Return type:

None

write_memory(memory_id, address, length, data, access=MemoryAccessValues.ALL, 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:
Return type:

None

class spsdk.dk6.dk6device.DK6DeviceId(tag, label, description=None)#

Bases: SpsdkEnum

DK6 Device IDs.

JN5188 = (5188, 'JN5188')#
JN5189 = (5189, 'JN5189')#
K32W041 = (32041, 'K32W041')#
K32W061 = (32061, 'K32W061')#
QN9030 = (9030, 'QN9030')#
QN9090 = (9090, 'QN9090')#
UNKNOWN = (0, 'UNKNOWN')#
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:
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

set_baud_rate(baudrate)#

Set baud rate.

Parameters:

baudrate (int) – UART baudrate

Return type:

None

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:
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=MemoryId.FLASH)#

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=MemoryId.FLASH, access=MemoryAccessValues.WRITE)#

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:

None

unlock_isp(mode=IspMode.START_ISP, 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(tag, label, description=None)#

Bases: SpsdkEnum

DK6 ISP modes.

DEFAULT = (0, 'default', 'Default')#
EXTENDED_ISP_UNLOCK = (128, 'extend_unlock', 'Extended unlock')#
START_ISP = (1, 'start_isp', 'Start ISP Functionality')#
UNLOCK_DEVICE = (127, 'unlock_device', 'Unlock device')#