lpcprog

Contents

lpcprog#

This module contains support for programming LPC devices.

Module representing communication interface#

LPCxxx ISP UART communication interface.

class spsdk.lpcprog.interface.LPCProgInterface(device)#

Bases: object

LPCxxx ISP UART communication interface.

Initialize the LPCProgInterface.

Parameters:

device (Serial) – Serial device

NEW_LINE = '\r\n'#
RC_SLEEP = 0.05#
START_SYNC = '?'#
SYNC_STRING = 'Synchronized\r\n'#
SYNC_VERIFIED_STRING = 'OK\r\n'#
clear_serial()#

Flush buffers.

Return type:

None

close()#

Close the UART interface.

Raises:

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

Return type:

None

open()#

Open the UART interface.

Raises:

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

Return type:

None

read_all()#

Read all data from the device.

Raises:
  • SPSDKError – When read fails

  • TimeoutError – Time-out

Return type:

bytes

Returns:

Data read from the device

read_line(decode=True)#

Read line from the device.

Raises:
  • SPSDKError – When read fails

  • TimeoutError – Time-out

Return type:

str

Returns:

Data read from the device

send_command(command, expect_rc=True)#

Writes command.

Return type:

Optional[int]

sync_connection(frequency)#

Synchronize connection. :rtype: None

  1. Send ? to get baud rate

  2. Receive “Synchronized” message

  3. Send “Synchronized” message

  4. Receive “OK” message

write(data)#

Send data to device.

Parameters:

data (bytes) – Data to send

Raises:

SPSDKError – When sending the data fails

Return type:

None

write_string(data)#

Write string to serial device.

Parameters:

data (str) – string data

Return type:

None

Module representing communication protocol#

ISP Communication protocol for LPC devices.

class spsdk.lpcprog.protocol.LPCProgCRPLevels(tag, label, description=None)#

Bases: SpsdkEnum

LPC CRP Levels.

Code Read Protection is a mechanism that allows the user to enable different levels of security in the system so that access to the on-chip flash and use of the ISP can be restricted. When needed, CRP is invoked by programming a specific pattern in the flash image at offset 0x0000 02FC. IAP commands are not affected by the code read protection.

CRP1 = (1499702940, 'CRP1', 'SWD Disabled, ISP commands restricted. Cannot write sector 0 and read memory.')#
CRP2 = (2520082890, 'CRP2', 'SWD Disabled, ISP only allows erase of all sectors')#
CRP3 = (1666817190, 'CRP3', 'SWD Disabled, ISP disabled.')#
NO_CRP = (4294967295, 'NO_CRP', 'All USART ISP commands are supported.')#
NO_ISP = (1399499925, 'NO_ISP', 'Access to the chip via the SWD pins is enabled, ISP pins disabled')#
class spsdk.lpcprog.protocol.LPCProgProtocol(interface, print_func, device=None)#

Bases: object

LPCProg protocol.

Initialize the LPCProgProtocol.

ALLOWED_BAUD_RATES = [9600, 19200, 38400, 57600, 115200, 230400, 460800]#
ALLOWED_BAUD_RATES_STR = ['9600', '19200', '38400', '57600', '115200', '230400', '460800']#
CRC_VECT_TABLE_OFFSET = 28#
CRP_LENGTH = 4#
CRP_OFFSET = 764#
PAGE_SIZE = 64#
SECTOR_SIZE = 1024#
UNLOCK_CODE = 23130#
VECT_TABLE_SIZE = 32#
blank_check_sectors(start_sector, end_sector, print_status=True)#

This command is used to blank check one or more sectors of on-chip flash memory.

Return type:

bool

static calc_crc(data)#

Calculate CRC from the data.

Parameters:

data (bytes) – data to calculate CRC from

Return type:

int

Returns:

calculated CRC

calculate_sector_count(data)#

Calculate number of sectors needed for writing the data.

Return type:

int

compare(dst_address, src_address, length, print_status=True)#

This command is used to compare the memory contents at two locations.

Return type:

bool

copy_ram_to_flash(flash_address, ram_address, length, print_status=True)#

This command is used to program the flash memory.

The “Prepare Sector(s) for Write Operation” command should precede this command. The affected sectors are automatically protected again once the copy command is successfully executed. This command is blocked when code read protection is enabled.

Return type:

None

decode_part_id(part_id)#

Return decoded part ID from the database.

Return type:

Optional[str]

erase_page(start_page, end_page, print_status=True)#

This command is used to erase one or more page(s) of on-chip flash memory.

Return type:

None

erase_sector(start_sector, end_sector, print_status=True)#

This command is used to erase one or more sector(s) of on-chip flash memory.

This command only allows erasure of all user sectors when the code read protection is enabled.

Return type:

None

get_crp_level()#

Read CRP level from offset 0x2FC and decode it.

Return type:

LPCProgCRPLevels

get_device()#

Get LPCDevice if defined or read it from part ID.

Return type:

LPCDevice

Returns:

LPCDevice

get_info()#

Returns info about the device.

  1. Part ID

  2. UID

  3. Boot code version

  4. CRP level

Return type:

str

Returns:

string containing description

get_latest_status()#

Get latest status.

Return type:

str

static get_supported_families()#

Get the list of supported families by LPCProg.

Return type:

list[str]

Returns:

List of supported families.

go(address, thumb_mode=False)#

This command is used to execute a program residing in RAM or flash memory.

It may not be possible to return to the ISP command handler once this command is successfully executed. This command is blocked when code read protection is enabled.

Return type:

None

make_image_bootable(data)#

Make the image bootable by inserting the CRC checksum in the correct place.

Parameters:

data (bytes) – image data

Return type:

bytes

Returns:

image data with correct CRC checksum

prepare_sectors_for_write(start_sector, end_sector, print_status=True)#

Prepare sectors for write.

This command must be executed before executing “Copy RAM to flash” or “Erase Sector(s)”, or “Erase Pages” command.

Successful execution of the “Copy RAM to flash” or “Erase Sector(s)” or “Erase Pages” command causes relevant sectors to be protected again. To prepare a single sector use the same “Start” and “End” sector numbers.

Return type:

None

print_status(status)#

Print status from the status code.

Parameters:

status (Optional[StatusCode]) – StatusCode

Return type:

None

program_flash(bin_data, start_sector=0, progress_callback=None, print_status=True)#

This command is used for programming the flash memory. :rtype: None

  1. Erase the first sector to make the image un-bootable and prevent bricking

  2. Optionally write the checksum to the image vector table

  3. Write the image in reverse order

program_flash_sector(data, sector, verify=False)#

This command is used for programming the flash sector.

Approach for writing the sector 1) Write data to RAM 2) Prepare sector for writing 3) Erase sector 4) Again prepare sector 5) Copy RAM to flash

Return type:

None

read_boot_code_version()#

Read boot code version.

Return type:

str

read_crc_checksum(address, length)#

This command is used to read the CRC checksum of a block of RAM or flash memory.

This command is blocked when code read protection is enabled.

Return type:

Optional[int]

read_flash_signature(start_address, end_address, wait_states=2, mode=0)#

This command is used to read the flash signature generated by the flash controller.

Return type:

int

read_memory(address, length, binary=None, progress_callback=None)#

This command is used to read data from RAM or flash memory.

This command is blocked when code read protection is enabled.

Return type:

bytes

read_part_id()#

This command is used to read the part identification number.

Return type:

str

read_uid()#

This command is used to read the unique ID.

Return type:

str

send_command(command, print_status=False, expect_rc=True)#

Send command.

Return type:

Optional[StatusCode]

set_baud_rate(baud_rate, stop_bits=1, print_status=True)#

This command is used to change the baud rate.

The new baud rate is effective after the command handler sends the CMD_SUCCESS return code.

Return type:

None

set_echo(echo, print_status=True)#

The default setting for echo command is ON.

When ON the ISP command handler sends the received serial data back to the host.

Return type:

None

sync_connection(frequency)#

Synchronize connection. :rtype: None

  1. Send ? to get baud rate

  2. Receive “Synchronized” message

  3. Send “Synchronized” message

  4. Receive “OK” message

unlock(print_status=True)#

This command is used to unlock Flash Write, Erase, and Go commands.

Return type:

None

write_ram(address, data)#

This command is used to download data to RAM.

This command is blocked when code read protection levels 2 or 3 are enabled. Writing to addresses below 0x1000 0600 is disabled for CRP1.

The host should send the plain binary code after receiving the CMD_SUCCESS return code. This ISP command handler responds with “OK<CR><LF>” when the transfer has finished.

Return type:

None