updated comments, linted mega2560 example
This commit is contained in:
parent
2bef50c500
commit
c51a352272
2 changed files with 31 additions and 13 deletions
|
|
@ -49,9 +49,9 @@ class AVRprog:
|
||||||
|
|
||||||
def init(self, spi_bus, rst_pin):
|
def init(self, spi_bus, rst_pin):
|
||||||
"""
|
"""
|
||||||
Initialize the programmer with SPI pins that will be used to
|
Initialize the programmer with an SPI port that will be used to
|
||||||
communicate with the chip. Currently only hardware-SPI pins are
|
communicate with the chip. Make sure your SPI supports 'write_readinto'
|
||||||
supported!
|
Also pass in a reset pin that will be used to get into programming mode
|
||||||
"""
|
"""
|
||||||
self._spi = spi_bus
|
self._spi = spi_bus
|
||||||
self._rst = DigitalInOut(rst_pin)
|
self._rst = DigitalInOut(rst_pin)
|
||||||
|
|
@ -323,13 +323,19 @@ class AVRprog:
|
||||||
|
|
||||||
def read_hex_page(file_state, page_addr, page_size, page_buffer):
|
def read_hex_page(file_state, page_addr, page_size, page_buffer):
|
||||||
"""
|
"""
|
||||||
Helper function that does the Intel Hex parsing. Given an open file
|
Helper function that does the Intel Hex parsing. Takes in a dictionary
|
||||||
'hexfile' and our desired buffer address start (page_addr), size
|
that contains the file 'state'. The dictionary should have file_state['f']
|
||||||
(page_size) and an allocated bytearray. This function will try to
|
be the file stream object (returned by open), the file_state['line'] which
|
||||||
read the file and fill the page_buffer. If the next line has data
|
tracks the line number of the file for better debug messages. This function
|
||||||
that is beyond the size of the page_address, it will return without
|
will update 'line' as it reads lines. It will set 'eof' when the file has
|
||||||
changing the buffer, so pre-fill it with 0xFF (for sparsely-defined
|
completed reading. It will also store the 'extended address' state in
|
||||||
HEX files.
|
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
|
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
|
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.
|
line does not contain any more data we can use.
|
||||||
|
|
|
||||||
|
|
@ -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 board
|
||||||
import busio
|
import busio
|
||||||
|
|
@ -15,13 +24,16 @@ atmega2560['flash_size'] = 262144
|
||||||
atmega2560['page_size'] = 256
|
atmega2560['page_size'] = 256
|
||||||
atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
|
atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
|
||||||
|
|
||||||
# Helper to print out errors for us
|
|
||||||
def error(err):
|
def error(err):
|
||||||
|
""" Helper to print out errors for us and then halt """
|
||||||
print("ERROR: "+err)
|
print("ERROR: "+err)
|
||||||
avrprog.end()
|
avrprog.end()
|
||||||
while True:
|
while True:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
while input("Ready to GO, type 'G' here to start> ") != 'G':
|
||||||
|
pass
|
||||||
|
|
||||||
if not avrprog.verify_sig(atmega2560, verbose=True):
|
if not avrprog.verify_sig(atmega2560, verbose=True):
|
||||||
error("Signature read failure")
|
error("Signature read failure")
|
||||||
print("Found", atmega2560['name'])
|
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.program_file(atmega2560, "stk500boot_v2_mega2560.hex", verbose=True, verify=True)
|
||||||
|
|
||||||
avrprog.write_fuses(atmega2560, lock=0x0F)
|
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!")
|
error("Failure verifying fuses!")
|
||||||
|
|
||||||
print("Done!")
|
print("Done!")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue