{"id":29228958,"url":"https://github.com/jjsch-dev/pyarduinoflash","last_synced_at":"2025-07-03T11:02:23.079Z","repository":{"id":57411360,"uuid":"299062416","full_name":"jjsch-dev/PyArduinoFlash","owner":"jjsch-dev","description":"Python Class for updating the firmware of Arduino boards","archived":false,"fork":false,"pushed_at":"2022-09-06T12:29:04.000Z","size":4136,"stargazers_count":6,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T05:02:36.499Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jjsch-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-27T15:34:56.000Z","updated_at":"2023-11-27T13:06:54.000Z","dependencies_parsed_at":"2022-08-27T17:11:06.179Z","dependency_job_id":null,"html_url":"https://github.com/jjsch-dev/PyArduinoFlash","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jjsch-dev/PyArduinoFlash","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjsch-dev%2FPyArduinoFlash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjsch-dev%2FPyArduinoFlash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjsch-dev%2FPyArduinoFlash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjsch-dev%2FPyArduinoFlash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jjsch-dev","download_url":"https://codeload.github.com/jjsch-dev/PyArduinoFlash/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjsch-dev%2FPyArduinoFlash/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263314092,"owners_count":23447289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-03T11:02:22.194Z","updated_at":"2025-07-03T11:02:23.004Z","avatar_url":"https://github.com/jjsch-dev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"PyArduinoFlash\n====\n\n\nPyArduinoFlash is an open source library in Python for updating the firmware \nof Arduino boards that use the [ATmegaBOOT_168](https://github.com/arduino/ArduinoCore-avr/tree/master/bootloaders/atmega) or Arduino [Stk500V2](https://github.com/arduino/Arduino-stk500v2-bootloader) bootloader, for example [Arduino Nano](https://store.arduino.cc/usa/arduino-nano) or [Arduino Uno](https://store.arduino.cc/usa/arduino-uno-rev3) or [Arduino Mega 2560](https://store.arduino.cc/usa/mega-2560-r3) and many more.\n\nThe intention is to have a class that can be imported into any python project to update the Arduinos through the serial port.\n\nIt implements a subset of Atmel's STK-500V1 and STK500V2 protocol, using as reference the protocols implemented by [Avrdude](http://savannah.nongnu.org/projects/avrdude) in the ```arduino.c``` and ```wiring.c``` modules.\n\nFor 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. \nFor boards that have processors of more than 128 Kbytes, for example the Mega 2560, the STK500-V2 protocol must be used at 115200 baud.\n\n\nAs an example of use, there is an APP in [KivyMd](https://gitlab.com/kivymd/KivyMD) and [Kivy](http://kivy.org) that exposes through a GUI all the methods required to update and verify the firmware.\n\nThe first example shows the upgrade of a Nano board with a new bootolader. Select STK500-V1 at 115200 baud.\n\n![](images/arduino_kivy_stk500v1.gif)\n\nThe second example shows the upgrade of a Mega board with 2560 processor. STK500-V2 must be selected at 115200 baud.\n\n![](images/arduino_kivy_stk500v2.gif)\n\nInstallation\n------------\nInstall using pip or pip3 (recommended, no separate download required):\n\n``pip install arduinobootloader`` \n\nDocumentation and Examples\n----------------------------------------\n```python\n    from intelhex import IntelHex\n    from arduinobootloader import ArduinoBootloader\n\n    def update(self):\n        ih = IntelHex()\n        ab = ArduinoBootloader()\n        prg = ab.select_programmer(\"Stk500v1\")\n\n        if prg.open(speed=115200):\n            if not prg.board_request():\n                prg.close()\n                return\n\n            print(\"botloader name: {} version: {} hardware: {}\".format(ab.programmer_name,\n                                                                       ab.sw_version,\n                                                                       ab.hw_version))\n\n            if not prg.cpu_signature():\n                prg.close()\n                return\n\n            print(\"cpu name: {}\".format(ab.cpu_name) )\n\n            try:\n                ih.fromfile(\"filename.hex\", format='hex')\n            except (FileNotFoundError, AddressOverlapError, HexRecordError):\n                return\n\n            for address in range(0, ih.maxaddr(), ab.cpu_page_size):\n                buffer = ih.tobinarray(start=address, size=ab.cpu_page_size)\n                if not prg.write_memory(buffer, address):\n                   print(\"Write error\")\n                   prg.leave_bootloader()\n                   prg.close()\n                   return\n\n            for address in range(0, ih.maxaddr(), ab.cpu_page_size):\n                buffer = ih.tobinarray(start=address, size=ab.cpu_page_size)\n                read_buffer = prg.read_memory(address, ab.cpu_page_size)\n                if read_buffer is None:\n                   print(\"Read error\")\n                   break\n\n                if buffer != read_buffer:\n                   print(\"File not match\")\n                   break\n\n            prg.leave_bootloader()\n            prg.close()\n\n```\n\nThe parsing of the file in [Intel hexadecimal format](https://en.wikipedia.org/wiki/Intel_HEX) is done with the [IntelHex](https://github.com/python-intelhex/intelhex) library.\n\nTo have an instance of the class use ``ab = ArduinoBootloader()``\nThen select the protocol of the programmer ``prg = ab.select_protocol(\"Stk500v1\") ``. To establish the connection with the bootloader of the Arduino board use ``prg.open()`` that returns ``True`` when it is successful.\n\nAs the library needs the information of the CPU to know the size and count of the flash page, use the method ``prg.board_request()`` and ``prg.cpu_signature()``\n\nIf the previous was successfully (they return ``True``), now open the hexadecimal file with the ``ih.fromfile(\"firmware_file.hex\", format='hex')`` function. If there are errors in the format or the file path is invalid, exceptions are thrown.\n\nTo obtain the page of the current address, use the  ``ih.tobinarray(start=address, size=ab.cpu_page_size)`` .\n\nFor write it in the flash memory, use the method ``prg.write_memory(buffer, address)`` which take the buffer and the address as parameters. Returns ``True`` when success.\n\nThe read to verify is done in the same way, with the exception that the method returns the read buffer. If it is ``None`` it indicates that there were problems.\n\nThe bootloader begins the execution of the firmware after a period of time without receiving communication; nevertheless it is convenient to execute the function ``prg.leave_bootloader()``.\n\nCall the method ``prg.close()`` to release the serial port.\n\nScripts\n-------\nThe Script folder contains arduinoflash.py file that allows update or read the firmware of Arduino boards.\n\nOne of the purposes is to show the use of the PyArduinoBootloader library in conjunction with the [IntelHex](https://github.com/python-intelhex/intelhex) library to process hexadecimal files.\n\nUse the [argparse](https://docs.python.org/3/library/argparse.html#module-argparse) library, to read the command line (file and options). \n\nAnd to indicate the progress the [progressbar2](https://pypi.org/project/progressbar2/) library.\n\n```shell script: usage: arduinoflash.py [-h] [--version] [-r | -u] filename\nusage: arduinoflash.py [-h] [--version] [-r | -u] filename\n\narduino flash utility\n\npositional arguments:\n  filename      filename in hexadecimal Intel format\n\noptional arguments:\n  -h, --help    show this help message and exit\n  --version     script version\n  -b BAUDRATE, --baudrate BAUDRATE\n                        old bootolader (57600) Optiboot (115200)\n  -p PROGRAMMER, --programmer PROGRAMMER\n                        programmer version - Nano (Stk500v1) Mega (Stk500v2)\n  -r, --read            read the cpu flash memory\n  -u, --update          update cpu flash memory\n\n```\nThe following capture shows the reading of the flash memory of an Arduino Nano board.\n\n![](images/arduinoflash_read_stk500v1.gif)\n\nAnd the next shows the firmware update of an Arduino Nano board.\n\n![](images/arduinoflash_update_stk500v1.gif)\n\nThe following capture shows the reading of the flash memory of an Arduino Mega 2560 board.\n\n![](images/arduinoflash_read_stk500v2.gif)\n\nAnd the next shows the firmware update of an Arduino Mega 2560 board.\n\n![](images/arduinoflash_update_stk500v2.gif)\n\nSupport\n-------\n\nIf you need assistance, contact me:\n\n* Email      : juanschiavoni@gmail.com\n\n\nContributing\n------------\n\n\nLicenses\n--------\n\n- PyArduinoFlash is released under the terms of the MIT License. Please refer to the\n  LICENSE file.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjsch-dev%2Fpyarduinoflash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjjsch-dev%2Fpyarduinoflash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjsch-dev%2Fpyarduinoflash/lists"}