diff --git a/adafruit_avrprog.py b/adafruit_avrprog.py index 7d71679..42670b4 100644 --- a/adafruit_avrprog.py +++ b/adafruit_avrprog.py @@ -58,6 +58,41 @@ class AVRprog: """ Helper class used to program AVR chips from CircuitPython. """ + class Boards: + """ + Some well known board definitions. + """ + # pylint: disable=too-few-public-methods + ATtiny13a = { + 'name': "ATtiny13a", + 'sig': [0x1E, 0x90, 0x07], + 'flash_size': 1024, + 'page_size': 32, + 'fuse_mask': (0xFF, 0xFF, 0x00, 0x03), + 'clock_speed': 100000 + } + ATtiny85 = { + 'name': "ATtiny85", + 'sig': [0x1E, 0x93, 0x0B], + 'flash_size': 8192, + 'page_size': 64, + 'fuse_mask': (0xFF, 0xFF, 0x07, 0x3F) + } + ATmega328p = { + 'name': "ATmega328p", + 'sig': [0x1E, 0x95, 0x0F], + 'flash_size': 32768, + 'page_size': 128, + 'fuse_mask': (0xFF, 0xFF, 0x07, 0x3F), + } + ATmega2560 = { + 'name': "ATmega2560", + 'sig': [0x1E, 0x98, 0x01], + 'flash_size': 262144, + 'page_size': 256, + 'fuse_mask': (0xFF, 0xFF, 0x07, 0x3F) + } + _spi = None _rst = None @@ -100,7 +135,8 @@ def program_file(self, chip, file_name, verbose=False, verify=True): print("Erasing chip....") self.erase_chip() - self.begin() + clock_speed = getattr(chip, 'clock_speed', _FAST_CLOCK) + self.begin(clock=clock_speed) # create a file state dictionary file_state = {'line': 0, 'ext_addr': 0, 'eof': False} @@ -164,7 +200,8 @@ def verify_file(self, chip, file_name, verbose=False): file_state['f'] = open(file_name, 'r') page_size = chip['page_size'] - self.begin() + clock_speed = getattr(chip, 'clock_speed', _FAST_CLOCK) + self.begin(clock=clock_speed) for page_addr in range(0x0, chip['flash_size'], page_size): page_buffer = bytearray(page_size) for b in range(page_size): diff --git a/examples/attiny13a_blink.hex b/examples/attiny13a_blink.hex new file mode 100644 index 0000000..33c6aca --- /dev/null +++ b/examples/attiny13a_blink.hex @@ -0,0 +1,6 @@ +:1000000009C00EC00DC00CC00BC00AC009C008C09A +:1000100007C006C011241FBECFE9CDBF02D012C059 +:10002000EFCF81E087BB18BA98E088B3892788BBF7 +:100030002FEF35EA8EE0215030408040E1F700C0DC +:080040000000F3CFF894FFCF9C +:00000001FF diff --git a/examples/avrprog_program_mega2560.py b/examples/avrprog_program_mega2560.py index 6d6b8f4..2b83736 100644 --- a/examples/avrprog_program_mega2560.py +++ b/examples/avrprog_program_mega2560.py @@ -1,7 +1,7 @@ """ 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 + Mega 5V to CircuitPython 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 @@ -17,12 +17,18 @@ avrprog = adafruit_avrprog.AVRprog() avrprog.init(spi, board.D5) -# Each chip has to have a definition so the script knows how to find it -atmega2560 = {'name': "ATmega2560"} -atmega2560['sig'] = [0x1E, 0x98, 0x01] -atmega2560['flash_size'] = 262144 -atmega2560['page_size'] = 256 -atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) +# To program a chip, you'll need to find out the signature, size of the flash, +# flash-page size and fuse mask. You can find this in the datasheet or in +# avrdude.conf located at: +# http://svn.savannah.nongnu.org/viewvc/*checkout*/avrdude/trunk/avrdude/avrdude.conf.in +# You can also use the predefined values in AVRprog.Boards +atmega2560 = { + 'name': "ATmega2560", + 'sig': [0x1E, 0x98, 0x01], + 'flash_size': 262144, + 'page_size': 256, + 'fuse_mask': (0xFF, 0xFF, 0x07, 0x3F) +} def error(err): """ Helper to print out errors for us and then halt """ diff --git a/examples/avrprog_program_tiny13a.py b/examples/avrprog_program_tiny13a.py new file mode 100644 index 0000000..d8e90d5 --- /dev/null +++ b/examples/avrprog_program_tiny13a.py @@ -0,0 +1,44 @@ +""" +ATtiny13a programming example, be sure you have the '13a wired up so: + ATtiny13a GND to CircuitPython GND + ATtiny13a VCC to CircuitPython USB + Pin 2 -> CircuitPython SCK + Pin 1 -> CircuitPython MISO + Pin 0 -> CircuitPython MOSI + RESET -> CircuitPython D5 (or change the init() below to change it!) +Drag "attiny13a_blink.hex" onto the CircuitPython disk drive, then open REPL! +""" + +import board +import busio +import adafruit_avrprog + +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +avrprog = adafruit_avrprog.AVRprog() +avrprog.init(spi, board.D5) + +# Each chip has to have a definition so the script knows how to find it +attiny13 = avrprog.Boards.ATtiny13a + +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(attiny13, verbose=True): + error("Signature read failure") +print("Found", attiny13['name']) + +avrprog.write_fuses(attiny13, low=0x7A, high=0xFF) +if not avrprog.verify_fuses(attiny13, low=0x7A, high=0xFF): + error("Failure verifying fuses!") + +print("Programming flash from file") +avrprog.program_file(attiny13, "attiny13a_blink.hex", verbose=True, verify=True) + +print("Done!") diff --git a/examples/avrprog_program_trinket85.py b/examples/avrprog_program_trinket85.py index 31d6bfc..693b9bd 100644 --- a/examples/avrprog_program_trinket85.py +++ b/examples/avrprog_program_trinket85.py @@ -18,11 +18,7 @@ avrprog.init(spi, board.D5) # Each chip has to have a definition so the script knows how to find it -attiny85 = {'name': "ATtiny85"} -attiny85['sig'] = [0x1E, 0x93, 0x0B] -attiny85['flash_size'] = 8192 -attiny85['page_size'] = 64 -attiny85['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) +attiny85 = avrprog.Boards.ATtiny85 def error(err): """ Helper to print out errors for us and then halt """ diff --git a/examples/avrprog_program_uno328.py b/examples/avrprog_program_uno328.py index a888b2c..f939519 100644 --- a/examples/avrprog_program_uno328.py +++ b/examples/avrprog_program_uno328.py @@ -24,11 +24,7 @@ #pylint: enable-msg=no-member # Each chip has to have a definition so the script knows how to find it -atmega328p = {'name': "ATmega328P"} -atmega328p['sig'] = [0x1E, 0x95, 0x0F] -atmega328p['flash_size'] = 32768 -atmega328p['page_size'] = 128 -atmega328p['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) +atmega328p = avrprog.Boards.ATmega328p def error(err): """ Helper to print out errors for us and then halt """