Skip to content

Add ATtiny13a support #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions adafruit_avrprog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions examples/attiny13a_blink.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:1000000009C00EC00DC00CC00BC00AC009C008C09A
:1000100007C006C011241FBECFE9CDBF02D012C059
:10002000EFCF81E087BB18BA98E088B3892788BBF7
:100030002FEF35EA8EE0215030408040E1F700C0DC
:080040000000F3CFF894FFCF9C
:00000001FF
20 changes: 13 additions & 7 deletions examples/avrprog_program_mega2560.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 """
Expand Down
44 changes: 44 additions & 0 deletions examples/avrprog_program_tiny13a.py
Original file line number Diff line number Diff line change
@@ -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!")
6 changes: 1 addition & 5 deletions examples/avrprog_program_trinket85.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
6 changes: 1 addition & 5 deletions examples/avrprog_program_uno328.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down