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 (
SerialDevice
) – Serial device
- NEW_LINE = '\r\n'#
- RC_SLEEP = 0.05#
- START_SYNC = '?'#
- SYNC_STRING = 'Synchronized\r\n'#
- SYNC_VERIFIED_STRING = 'OK'#
- 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, retries=10)#
Synchronize connection.
- Parameters:
frequency (
int
) – Frequency of the crystalretries (
int
) – Number of retries for synchronization (10 default)
- Return type:
None
Send ? to get baud rate
Receive “Synchronized” message
Send “Synchronized” message
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#
- assert_rc(status)#
Assert status.
- Return type:
None
- 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.
- Parameters:
start_sector (
int
) – start sectorend_sector (
int
) – end sectorprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if blank
- static calc_crc(data)#
Calculate CRC from the data.
- Parameters:
data (
bytes
) – data to calculate CRC from- Return type:
int
- Returns:
calculated CRC
- calculate_page_count(data)#
Calculate number of pages needed for writing the data.
- Return type:
int
- calculate_sector_count(data)#
Calculate number of sectors needed for writing the data.
- Return type:
int
- close()#
Close the interface.
- Return type:
None
- compare(dst_address, src_address, length, print_status=True)#
This command is used to compare the memory contents at two locations.
- Parameters:
dst_address (
int
) – destination addresssrc_address (
int
) – source addresslength (
int
) – length of data to compareprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if content is same
- copy_ram_to_flash(flash_address, ram_address, length, print_status=True)#
This command is used to program the flash memory.
- Parameters:
flash_address (
int
) – address in flashram_address (
int
) – address in RAMlength (
int
) – length of data to copyprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if copied
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.
- 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.
- Parameters:
start_page (
int
) – start pageend_page (
int
) – end pageprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if erased
- 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.
- Parameters:
start_sector (
int
) – start sectorend_sector (
int
) – end sectorprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if erased
This command only allows erasure of all user sectors when the code read protection is enabled.
- get_crp_level()#
Read CRP level from offset 0x2FC and decode it.
- Return type:
- Returns:
CRP level
- get_device()#
Get LPCDevice if defined or read it from part ID.
- Return type:
LPCDevice
- Returns:
LPCDevice
- get_info()#
Returns info about the device.
Part ID
UID
Boot code version
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.
- Parameters:
address (
int
) – address in RAM or flashthumb_mode (
bool
) – thumb mode
- Return type:
None
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.
- prepare_sectors_for_write(start_sector, end_sector, print_status=True)#
Prepare sectors for write.
- Parameters:
start_sector (
int
) – start sectorend_sector (
int
) – end sectorprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if prepared
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.
- print_status(status)#
Print status from the status code.
- Parameters:
status (
Optional
[StatusCode
]) – StatusCode- Return type:
None
- program_flash(bin_data, start_sector=0, start_page=None, progress_callback=None, print_status=True, erase=True, verify=True)#
This command is used for programming the flash memory.
- Parameters:
bin_data (
bytes
) – binary data to be writtenstart_sector (
int
) – start sector to writestart_page (
Optional
[int
]) – start page to writeprogress_callback (
Optional
[Callable
[[int
,int
],None
]]) – callback function for progressprint_status (
bool
) – print statuserase (
bool
) – erase the sector before writingverify (
bool
) – verify the written data
- Return type:
bool
Erase the first sector to make the image un-bootable and prevent bricking
Optionally write the checksum to the image vector table
Write the image in reverse order
- program_flash_page(data, page_index, verify=False, erase=True)#
This command is used for programming a single flash page.
- Parameters:
data (
bytes
) – data to be writtenpage_index (
int
) – index of the page to writeverify (
bool
) – verify the written dataerase (
bool
) – erase the page before writing
- Return type:
bool
Approach for writing a single page: 1) Write data to RAM 2) Prepare sector for writing 3) Erase page (if required) 4) Again prepare sector 5) Copy RAM to flash
- program_flash_sector(data, sector, verify=False, erase=True)#
This command is used for programming the flash sector.
- Parameters:
data (
bytes
) – data to be writtensector (
int
) – sector to writeverify (
bool
) – verify the written dataerase (
bool
) – erase the sector before writing
- Return type:
bool
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
- 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.
- Parameters:
address (
int
) – address in RAM or flashlength (
int
) – length of data
- Return type:
Optional
[int
]- Returns:
CRC checksum
This command is blocked when code read protection is enabled.
- 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.
- Parameters:
start_address (
int
) – start addressend_address (
int
) – end addresswait_states (
int
) – wait statesmode (
int
) – mode
- Return type:
int
- Returns:
flash signature
- read_memory(address, length, binary=None, progress_callback=None)#
This command is used to read data from RAM or flash memory.
- Parameters:
address (
int
) – address in RAM or flashlength (
int
) – length of data to readbinary (
Optional
[str
]) – binary file to write the dataprogress_callback (
Optional
[Callable
[[int
,int
],None
]]) – progress callback
- Return type:
bytes
- Returns:
read data
This command is blocked when code read protection is enabled.
- 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
- return_status(status)#
Return True if status is SUCCESS. Otherwise False.
- Return type:
bool
- send_command(command, print_status=False, expect_rc=True)#
Send command.
- Parameters:
command (
str
) – command to sendprint_status (
bool
) – print statusexpect_rc (
bool
) – expect return code
- Return type:
Optional
[StatusCode
]- Returns:
status code
- set_baud_rate(baud_rate, stop_bits=1, print_status=True)#
This command is used to change the baud rate.
- Parameters:
baud_rate (
int
) – new baud ratestop_bits (
int
) – number of stop bitsprint_status (
bool
) – print status
- Return type:
bool
The new baud rate is effective after the command handler sends the CMD_SUCCESS return code.
- set_echo(echo, print_status=True)#
The default setting for echo command is ON.
- Parameters:
echo (
bool
) – echoprint_status (
bool
) – print status
- Return type:
bool
- Returns:
True if echo is set
When ON the ISP command handler sends the received serial data back to the host.
- sync_connection(frequency, retries=10)#
Synchronize connection.
- Parameters:
frequency (
int
) – frequency of the crystalretries (
int
) – number of retries
- Return type:
bool
- Returns:
True if synchronized
Send ? to get baud rate
Receive “Synchronized” message
Send “Synchronized” message
Receive “OK” message
- unlock(print_status=True)#
This command is used to unlock Flash Write, Erase, and Go commands.
- Parameters:
print_status (
bool
) – print status- Return type:
bool
- Returns:
True if unlocked
- write_ram(address, data)#
This command is used to download data to RAM.
- Parameters:
address (
int
) – address in RAMdata (
bytes
) – data to write
- Return type:
bool
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.