Package: arduinobootloader
PyArduinoFlashPyArduinoFlash is an open source library in Python for updating the firmware
of Arduino boards that use theATmegaBOOT_168or ArduinoStk500V2bootloader, for exampleArduino NanoorArduino UnoorArduino Mega 2560and many more.The intention is to have a class that can be imported into any python project to update the Arduinos through the serial port.It implements a subset of Atmel's STK-500V1 and STK500V2 protocol, using as reference the protocols implemented byAvrdudein thearduino.candwiring.cmodules.For Arduino's using Atmel AVR8 processors there are three versions of the bootoloader available. For boards that have less than 128 Kbytes of Flash memory, for example Nano or Uno using the Atmega328P, etc that are marked in the Arduino IDE as older you have to use STK500-V1 at 57600 baud. And for the new ones (they implement the Optiboot bootloader) you have to use STK500-V1 at 115200 baud.
For boards that have processors of more than 128 Kbytes, for example the Mega 2560, STK500-V2 must be used at 115200 baud.As an example of use, there is an APP inKivyMdandKivythat exposes through a GUI all the methods required to update and verify the firmware.The first example shows the upgrade of a Nano board with a new bootolader. Select STK500-V1 at 115200 baud.The second example shows the upgrade of a Mega board with 2560 processor. STK500-V2 must be selected at 115200 baud.InstallationInstall using pip or pip3 (recommended, no separate download required):pip install arduinobootloaderDocumentation and ExamplesfromintelheximportIntelHexfromarduinobootloaderimportArduinoBootloaderih=IntelHex()ab=ArduinoBootloader()prg=ab.select_programmer("Stk500v1")ifprg.open():ifnotprg.board_request():prg.close()returnprint("botloader name:{}version:{}hardware:{}".format(ab.programmer_name,ab.sw_version,ab.hw_version))ifnotprg.cpu_signature():prg.close()returnprint("cpu name:{}".format(ab.cpu_name))ih.fromfile("firmware_file.hex",format='hex')foraddressinrange(0,ih.maxaddr(),ab.cpu_page_size):buffer=ih.tobinarray(start=address,size=ab.cpu_page_size)ifnotprg.write_memory(buffer,address):print("Write error")prg.leave_bootloader()prg.close()returnforaddressinrange(0,ih.maxaddr(),ab.cpu_page_size):buffer=ih.tobinarray(start=address,size=ab.cpu_page_size)read_buffer=prg.read_memory(address,ab.cpu_page_size)ifnotlen(read_buffer):print("Read error")breakifbuffer!=read_buffer:print("File not match")breakprg.leave_prg_mode()prg.close()The parsing of the file inIntel hexadecimal formatis done with theIntelHexlibrary.To have an instance of the class useab = ArduinoBootloader()The next step is to select the programmer protocolprg = ab.select_protocol("Stk500v1"). To establish the connection with the bootloader of the Arduino board useprg.open()that returnsTruewhen it is successful.As the library needs the information of the CPU to know the size of the page, it is necessary to use the functionsprg.board_request()andprg.cpu_signature()If the previous functions were executed successfully (they returnTrue), now you have to open the hexadecimal file with theih.fromfile("firmware_file.hex", format='hex')function. If there are errors in the format or the file path is invalid, exceptions are thrown.To obtain the page that corresponds to the current address, use theih.tobinarray(start=address, size=ab.cpu_page_size).For the bootloader to write it, use the functionprg.write_memory(buffer, address)which take the buffer and the current address as parameters. ReturnsTruewhen completed successfully.The reading to verify the recording is done in the same way, with the exception that the function returns the buffer read. If it isNoneit indicates that there were problems.The bootloader begins the execution of the firmware after a period of time without receiving communication; nevertheless it is convenient to execute the functionprg.leave_bootloader().Finally, to release the serial port, you have to execute the functionprg.close().ScriptsThe Script folder contains arduinoflash.py file that allows update or read the firmware of Arduino boards.One of the purposes is to show the use of the PyArduinoBootloader library in conjunction with theIntelHexlibrary to process hexadecimal files.Use theargparselibrary, to read the command line (file and options).And to indicate the progress theprogressbar2library.usage:arduinoflash.py[-h][--version][-r|-u]filename

arduinoflashutility

positionalarguments:filenamefilenameinhexadecimalIntelformat

optionalarguments:-h,--helpshowthishelpmessageandexit--versionscriptversion-bBAUDRATE,--baudrateBAUDRATEoldbootolader(57600)Optiboot(115200)-pPROGRAMMER,--programmerPROGRAMMERprogrammerversion-Nano(Stk500v1)Mega(Stk500v2)-r,--readreadthecpuflashmemory-u,--updateupdatecpuflashmemoryThe following capture shows the reading of the flash memory of an Arduino Nano board.And the next shows the firmware update of an Arduino Nano board.The following capture shows the reading of the flash memory of an Arduino Mega 2560 board.And the next shows the firmware update of an Arduino Mega 2560 board.SupportIf you need assistance, contact me:Email      :juanschiavoni@gmail.comContributingLicensesPyArduinoFlash is released under the terms of the MIT License. Please refer to the
LICENSE file.