From c51a352272800c1fc9b1d128d7a48190b4c25c2f Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 7 Jan 2018 18:04:49 -0500 Subject: [PATCH] updated comments, linted mega2560 example --- adafruit_avrprog.py | 26 ++++++++++++++++---------- examples/program_mega2560.py | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/adafruit_avrprog.py b/adafruit_avrprog.py index 4a4a72b..d471ce5 100644 --- a/adafruit_avrprog.py +++ b/adafruit_avrprog.py @@ -49,9 +49,9 @@ class AVRprog: def init(self, spi_bus, rst_pin): """ - Initialize the programmer with SPI pins that will be used to - communicate with the chip. Currently only hardware-SPI pins are - supported! + Initialize the programmer with an SPI port that will be used to + communicate with the chip. Make sure your SPI supports 'write_readinto' + Also pass in a reset pin that will be used to get into programming mode """ self._spi = spi_bus self._rst = DigitalInOut(rst_pin) @@ -323,13 +323,19 @@ class AVRprog: def read_hex_page(file_state, page_addr, page_size, page_buffer): """ - Helper function that does the Intel Hex parsing. Given an open file - 'hexfile' and our desired buffer address start (page_addr), size - (page_size) and an allocated bytearray. This function will try to - read the file and fill the page_buffer. If the next line has data - that is beyond the size of the page_address, it will return without - changing the buffer, so pre-fill it with 0xFF (for sparsely-defined - HEX files. + Helper function that does the Intel Hex parsing. Takes in a dictionary + that contains the file 'state'. The dictionary should have file_state['f'] + be the file stream object (returned by open), the file_state['line'] which + tracks the line number of the file for better debug messages. This function + will update 'line' as it reads lines. It will set 'eof' when the file has + completed reading. It will also store the 'extended address' state in + file_state['ext_addr'] + In addition to the file, it takes the desired buffer address start + (page_addr), size (page_size) and an allocated bytearray. + This function will try to read the file and fill the page_buffer. + If the next line has data that is beyond the size of the page_address, + it will return without changing the buffer, so pre-fill it with 0xFF + before calling, for sparsely-defined HEX files. Returns False if the file has no more data to read. Returns True if we've done the best job we can with filling the buffer and the next line does not contain any more data we can use. diff --git a/examples/program_mega2560.py b/examples/program_mega2560.py index 2b8b18e..6d6b8f4 100644 --- a/examples/program_mega2560.py +++ b/examples/program_mega2560.py @@ -1,4 +1,13 @@ -# Mega STK500 Bootloader programming example +""" +Arduino Mega 2560 programming example, be sure you have the Mega/2560 wired up so: + Mega Ground to CircuitPython GND + Mega 5V to CircuitPythong USB or make sure the Trinket is powered by USB + Pin 52 -> CircuitPython SCK + Pin 50 -> CircuitPython MISO - Note this is backwards from what you expect + Pin 51 -> CircuitPython MOSI - Note this is backwards from what you expect + RESET -> CircuitPython D5 (or change the init() below to change it) +Drag "stk500boot_v2_mega2560.hex" onto the CircuitPython disk drive, then open REPL +""" import board import busio @@ -15,13 +24,16 @@ atmega2560['flash_size'] = 262144 atmega2560['page_size'] = 256 atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) -# Helper to print out errors for us def error(err): + """ Helper to print out errors for us and then halt """ print("ERROR: "+err) avrprog.end() while True: pass +while input("Ready to GO, type 'G' here to start> ") != 'G': + pass + if not avrprog.verify_sig(atmega2560, verbose=True): error("Signature read failure") print("Found", atmega2560['name']) @@ -37,7 +49,7 @@ print("Programming flash from file") avrprog.program_file(atmega2560, "stk500boot_v2_mega2560.hex", verbose=True, verify=True) avrprog.write_fuses(atmega2560, lock=0x0F) -if not avrprog.verify_fuses(atmega2560,lock=0x0F): +if not avrprog.verify_fuses(atmega2560, lock=0x0F): error("Failure verifying fuses!") print("Done!")