Skip to content

Commit c51a352

Browse files
committed
updated comments, linted mega2560 example
1 parent 2bef50c commit c51a352

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

adafruit_avrprog.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class AVRprog:
4949

5050
def init(self, spi_bus, rst_pin):
5151
"""
52-
Initialize the programmer with SPI pins that will be used to
53-
communicate with the chip. Currently only hardware-SPI pins are
54-
supported!
52+
Initialize the programmer with an SPI port that will be used to
53+
communicate with the chip. Make sure your SPI supports 'write_readinto'
54+
Also pass in a reset pin that will be used to get into programming mode
5555
"""
5656
self._spi = spi_bus
5757
self._rst = DigitalInOut(rst_pin)
@@ -323,13 +323,19 @@ def _busy_wait(self):
323323

324324
def read_hex_page(file_state, page_addr, page_size, page_buffer):
325325
"""
326-
Helper function that does the Intel Hex parsing. Given an open file
327-
'hexfile' and our desired buffer address start (page_addr), size
328-
(page_size) and an allocated bytearray. This function will try to
329-
read the file and fill the page_buffer. If the next line has data
330-
that is beyond the size of the page_address, it will return without
331-
changing the buffer, so pre-fill it with 0xFF (for sparsely-defined
332-
HEX files.
326+
Helper function that does the Intel Hex parsing. Takes in a dictionary
327+
that contains the file 'state'. The dictionary should have file_state['f']
328+
be the file stream object (returned by open), the file_state['line'] which
329+
tracks the line number of the file for better debug messages. This function
330+
will update 'line' as it reads lines. It will set 'eof' when the file has
331+
completed reading. It will also store the 'extended address' state in
332+
file_state['ext_addr']
333+
In addition to the file, it takes the desired buffer address start
334+
(page_addr), size (page_size) and an allocated bytearray.
335+
This function will try to read the file and fill the page_buffer.
336+
If the next line has data that is beyond the size of the page_address,
337+
it will return without changing the buffer, so pre-fill it with 0xFF
338+
before calling, for sparsely-defined HEX files.
333339
Returns False if the file has no more data to read. Returns True if
334340
we've done the best job we can with filling the buffer and the next
335341
line does not contain any more data we can use.

examples/program_mega2560.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# Mega STK500 Bootloader programming example
1+
"""
2+
Arduino Mega 2560 programming example, be sure you have the Mega/2560 wired up so:
3+
Mega Ground to CircuitPython GND
4+
Mega 5V to CircuitPythong USB or make sure the Trinket is powered by USB
5+
Pin 52 -> CircuitPython SCK
6+
Pin 50 -> CircuitPython MISO - Note this is backwards from what you expect
7+
Pin 51 -> CircuitPython MOSI - Note this is backwards from what you expect
8+
RESET -> CircuitPython D5 (or change the init() below to change it)
9+
Drag "stk500boot_v2_mega2560.hex" onto the CircuitPython disk drive, then open REPL
10+
"""
211

312
import board
413
import busio
@@ -15,13 +24,16 @@
1524
atmega2560['page_size'] = 256
1625
atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
1726

18-
# Helper to print out errors for us
1927
def error(err):
28+
""" Helper to print out errors for us and then halt """
2029
print("ERROR: "+err)
2130
avrprog.end()
2231
while True:
2332
pass
2433

34+
while input("Ready to GO, type 'G' here to start> ") != 'G':
35+
pass
36+
2537
if not avrprog.verify_sig(atmega2560, verbose=True):
2638
error("Signature read failure")
2739
print("Found", atmega2560['name'])
@@ -37,7 +49,7 @@ def error(err):
3749
avrprog.program_file(atmega2560, "stk500boot_v2_mega2560.hex", verbose=True, verify=True)
3850

3951
avrprog.write_fuses(atmega2560, lock=0x0F)
40-
if not avrprog.verify_fuses(atmega2560,lock=0x0F):
52+
if not avrprog.verify_fuses(atmega2560, lock=0x0F):
4153
error("Failure verifying fuses!")
4254

4355
print("Done!")

0 commit comments

Comments
 (0)