API

Is a Python Class for updating the firmware of Arduino boards that use Atmel AVR CPUs. For example Arduino Nano, Uno, Mega and more.

The module implements the essential parts that Avrdude uses for the arduino and wiring protocols. In turn, they are a subset of the STK500 V1 and V2 protocols respectively.

arduinobootloader.AVR_ATMEL_CPUS = {2003718: ['ATmega8515', 64, 128], 2003719: ['ATmega8', 64, 128], 2003720: ['ATmega8535', 64, 128], 2003722: ['ATmega88', 64, 128], 2003970: ['ATmega163', 128, 128], 2003971: ['ATmega16', 128, 128], 2003972: ['ATmega162', 128, 128], 2003973: ['ATmega169', 128, 128], 2003974: ['ATmega168', 128, 256], 2004226: ['ATmega32', 128, 256], 2004239: ['ATmega328P', 128, 256], 2004244: ['ATmega328', 128, 256], 2004482: ['ATmega64', 256, 256], 2004488: ['ATmega640', 256, 1024], 2004738: ['ATmega128', 256, 512], 2004739: ['ATmega1280', 256, 512], 2004740: ['ATmega1281', 256, 512], 2004741: ['ATmega1284P', 256, 512], 2004866: ['AT90USB1287', 256, 512], 2004993: ['ATmega2560', 256, 1024], 2004994: ['ATmega2561', 256, 1024]}

Dictionary with the list of Atmel AVR 8 CPUs used by Arduino boards. Contains the size in bytes and the number of pages in flash memory. The key is the processor signature which is made up of SIG1, SIG2 and SIG3.

class arduinobootloader.ArduinoBootloader(*args, **kwargs)

Bases: object

Contains the two inner classes that support the Stk500 V1 and V2 protocols for comunicate with arduino bootloaders.

class Stk500v1(ab)

Bases: object

It encapsulates the communication protocol that Arduino uses for the first versions of bootoloader, which can write up to 128 K bytes of flash memory. For example: Nano, Uno, etc. The older version (ATmegaBOOT_168.c) works at 57600 baudios, the new version (OptiBoot) at 115200

board_request()

Get the firmware and hardware version of the bootloader.

Returns:True when success.
Return type:bool
close()

Close the communication port.

cpu_signature()

Get CPU information: name, size and count of the flash memory pages

Returns:True when success.
Return type:bool
get_sync()

Send the sync command whose function is to discard the reception buffers of both serial units. Set the receive unit timeout to 500mS and send the sync command up to 5 times to eliminate noise from the line.

Returns:True when success.
Return type:bool
leave_bootloader()

Leave programming mode and start executing the stored firmware

Returns:True when success.
Return type:bool
open(port=None, speed=57600)

Find and open the communication port where the Arduino is connected. Generate the reset sequence with the DTR / RTS pins. Send the sync command to verify that there is a valid bootloader.

Parameters:
  • port (str) – serial port identifier (example: ttyUSB0 or COM1). None for automatic board search.
  • speed (int) – comunication baurate, for older bootloader use 57600.
Returns:

True when the serial port was opened and the connection to the board was established.

Return type:

bool

read_memory(address, count, flash=True)

Read the memory from requested address.

Parameters:
  • address (int) – memory address of the first byte to read. (16 bits).
  • count (int) – bytes to read.
  • flash (bool) – eeprom supported only by the older version of bootloader.
Returns:

the buffer read or None when there is error.

Return type:

bytearray

write_memory(buffer, address, flash=True)

Write the buffer to the requested address of memory.

Parameters:
  • buffer (bytearray) – data to write.
  • address (int) – memory address of the first byte (16 bits).
  • flash (bool) – for old bootloader version can be flash or eeprom.
Returns:

True the buffer was successfully written.

Return type:

bool

class Stk500v2(ab)

Bases: object

It encapsulates the communication protocol that Arduino uses in bootloaders with more than 128K bytes of flash memory. For example: Mega 2560 etc

board_request()

Get the firmware and hardware version of the bootloader.

Returns:True when success.
Return type:bool
close()

Close the communication port.

cpu_signature()

Get CPU information: name, size and count of the flash memory pages

Returns:True when success.
Return type:bool
get_sync()

Send the sync command

Returns:True when success.
Return type:bool
leave_bootloader()

Leave programming mode and start executing the stored firmware

Returns:True when success.
Return type:bool
open(port=None, speed=115200)

Find and open the communication port where the Arduino is connected. Generate the reset sequence with the DTR / RTS pins. Send the sync command to verify that there is a valid bootloader.

Parameters:
  • port (str) – serial port identifier (example: ttyUSB0 or COM1). None for automatic board search.
  • speed (int) – comunication baurate (115200).
Returns:

True when the serial port was opened and the connection to the board was established.

Return type:

bool

read_memory(address, count, flash=True)

Read the memory from requested address.

Parameters:
  • address (int) – memory address of the first byte to read. (32 bits).
  • count (int) – bytes to read.
  • flash (bool) – stk500v2 version only supports flash.
Returns:

the buffer read or None when there is error.

Return type:

bytearray

write_memory(buffer, address, flash=True)

Write the buffer to the requested address of memory.

Parameters:
  • buffer (bytearray) – data to write.
  • address (int) – memory address of the first byte (32 bits).
  • flash (bool) – stk500v2 version only supports flash.
Returns:

True the buffer was successfully written.

Return type:

bool

close()

Close the serial communication port.

cpu_name

Dictionary cpu name

Setter:name
Type:str
cpu_page_size

CPU flash page size in bytes, not words.

Setter:size
Type:int
cpu_pages

CPU flash pages

Setter:pages
Type:int
hw_version

bootloader hardware version

Setter:version
Type:int
open(port=None, speed=115200)

Find and open the communication port where the Arduino is connected. Generate the reset sequence with the DTR / RTS pins. Send the sync command to verify that there is a valid bootloader.

Parameters:
  • port (str) – serial port identifier (example: ttyUSB0 or COM1). None for automatic board search.
  • speed (int) – comunication baurate.
Returns:

True when the serial port was opened and the connection to the board was established.

Return type:

bool

programmer_name

Name given by Atmel to its programmers, for example (ISP_V2). Optiboot returns an empty string to decrease the footprint of the bootloader.

Setter:name
Type:str
select_programmer(protocol)

Select the communication protocol to connect with the Arduino bootloader.

Parameters:protocol (str) – arduino bootloader can be: Stk500v1 or Stk500v2
Returns:None for unknow protocol
Return type:object
sw_version

botloader sotware version

Setter:version
Type:str
arduinobootloader.CMD_GET_PARAMETER = 3

Bootloader information of the Stk500v2 Protocol

arduinobootloader.CMD_LEAVE_PROGMODE_ISP = 17

Leave the programmer mode of the Stk500v2 Protocol

arduinobootloader.CMD_LOAD_ADDRESS = 6

Set the flash adddress of the Stk500v2 Protocol

arduinobootloader.CMD_PROGRAM_FLASH_ISP = 19

Write the flash of the Stk500v2 Protocol

arduinobootloader.CMD_READ_FLASH_ISP = 20

Read the flash of the Stk500v2 Protocol

arduinobootloader.CMD_SIGN_ON = 1

Synchronize the communication of the Stk500v2 Protocol

arduinobootloader.CMD_SPI_MULTI = 29

Cpu information of the Stk500v2 Protocol

arduinobootloader.CPU_SIG1 = 0

Cpu signature part 1

arduinobootloader.CPU_SIG2 = 1

Cpu signature part 2

arduinobootloader.CPU_SIG3 = 2

Cpu signature part 3

arduinobootloader.MESSAGE_START = 27

Start message of the Stk500v2 header (ESC = 27 decimal)

arduinobootloader.OPT_HW_VERSION = b'\x90'

Hardware version of the bootloader

arduinobootloader.OPT_SW_MAJOR = b'\x91'

Major software bootloader version

arduinobootloader.OPT_SW_MINOR = b'\x92'

Minor software bootloader version

arduinobootloader.RESP_STK_IN_SYNC = 20

Start message of the Stk500v1

arduinobootloader.RESP_STK_OK = 16

End message of the Stk500v1

arduinobootloader.STATUS_CMD_OK = 0

The command was successful

arduinobootloader.TOKEN = 14

End message of the Stk500v2 header (ESC = 27 decimal)