STM32 API

This API is for STM32 experimental daughter boards.

class scaffold.stm32.STM32(scaffold)

Class for instrumenting STM32 devices using Scaffold board and API. The following Scaffold IOs are used:

  • D0: STM32 UART MCU RX pin, Scaffold UART TX

  • D1: STM32 UART MCU TX pin, Scaffold UART RX

  • D2: STM32 NRST pin for reset

  • D6: STM32 BOOT0 pin

  • D7: STM32 BOOT1 pin

The uart0 peripheral of Scaffold board is used for serial communication with the ST bootloader.

This class can communicate with the ST bootloader via USART1. This allows programming the Flash memory and then execute the loaded code.

__init__(scaffold)
Parameters:

scaffold – An instance of scaffold.Scaffold which will be configured and used to communicate with STM32 daughter board.

assert_device()

Raise a RuntimeError is device is unknown (None).

checksum(data)

Calculate the checksum of some data, according to the STM32 bootloader protocol.

Parameters:

data – Input bytes.

Returns:

Checksum byte. This is the XOR of all input bytes.

command(index)

Send a command and return the response.

Parameters:

index – Command index.

Returns:

Response bytes.

extended_erase()

Execute the Extended Erase command to erase all the Flash memory of the device.

get()

Execute the Get command of the bootloader, which returns the version and the supported commands.

get_id()

Execute the Get ID command. The result is interpreted and the class will try to find information if the ID matches a known device.

global_erase()

This function works with a chip that has a bootloader version lower than 3.0 like STM32 F100xx. Unlike a chip that has a bootloader version higher or equal to 3.0, the command to erase the Flash memory is x43/xbc instead of x44/xbb. The function is based on the translation of the diagram in the documentation “USART protocol used in the STM32” in the category “Erase Memory command: host side”. Written by Hugo LE GUEN, intern at IETR.

go(address, trigger=0)

Execute the Go command.

Parameters:
  • address – Jump address.

  • trigger – 1 to enable trigger on command transmission.

read_memory(address, length, trigger=0)

Tries to read some memory from the device. If requested size is larger than 256 bytes, many Read Memory commands are sent.

Parameters:
  • address – Memory address to be read.

  • size – Number of bytes to be read.

  • trigger – 1 to enable trigger on command transmission.

read_option_bytes()

Read the option bytes of the device. The method get_id must have been called previously for device identification.

Returns:

Memory content of ‘option_bytes’ section.

readout_protect()

Execute the Readout Unprotect command.

readout_unprotect()

Execute the Readout Unprotect command. If the device is locked, it will perform mass flash erase, which can be very very long.

startup_bootloader()

Power-cycle and reset target device in bootloader mode (boot on System Memory) and initiate serial communication. The byte 0x7f is sent and the response byte 0x79 (ACK) is expected. If the device does not respond, a Timeout exception is thrown by Scaffold. The device will not respond if it is locked in RDP2 state (Readout Protection level 2).

startup_flash()

Power-cycle and reset target device and boot from user Flash memory.

wait_ack(tag=None)

Wait for ACK byte.

Parameters:

tag – Tag which is set when NACKError are thrown. Useful for error diagnostic.

wait_ack_or_nack()

Wait for ACK or NACK byte.

Returns:

True if ACK has been received, False if NACK has been received.

write_memory(address, data, trigger=0)

Write data to device memory. If target address is Flash memory, this function DOES NOT erase Flash memory prior to writing. If data size is larger than 256 bytes, many Write Memory commands are sent.

Parameters:
  • address – Address.

  • data – Data to be written. bytes or bytearray.

  • trigger – 1 to enable trigger on each command transmission.

write_protect(sectors: List[int])

Execute Write Protect command.

Parameters:

sectors – List of sectors to be write protected.

write_unprotect()

Execute Write Unprotect command.