Skip to content

Commit 8338a09

Browse files
committed
allow two clock rates (slow/fast)
1 parent 722e7a1 commit 8338a09

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

adafruit_avrprog.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
import busio
3737
from digitalio import Direction, DigitalInOut
3838

39+
_SLOW_CLOCK = 100000
40+
_FAST_CLOCK = 2000000
41+
3942
class AVRprog:
4043
"""
4144
Program your favorite AVR chips directly from CircuitPython with this
@@ -73,7 +76,7 @@ def verify_sig(self, chip, verbose=False):
7376
Verify that the chip is connected properly, responds to commands,
7477
and has the correct signature. Returns True/False based on success
7578
"""
76-
self.begin()
79+
self.begin(clock=_SLOW_CLOCK)
7780
sig = self.read_signature()
7881
self.end()
7982
if verbose:
@@ -179,7 +182,7 @@ def read_fuses(self, chip):
179182
Each fuse is bitwise-&'s with the chip's fuse mask for simplicity
180183
"""
181184
mask = chip['fuse_mask']
182-
self.begin()
185+
self.begin(clock=_SLOW_CLOCK)
183186
low = self._transaction((0x50, 0, 0, 0))[2] & mask[0]
184187
high = self._transaction((0x58, 0x08, 0, 0))[2] & mask[1]
185188
ext = self._transaction((0x50, 0x08, 0, 0))[2] & mask[2]
@@ -193,7 +196,7 @@ def write_fuses(self, chip, low=None, high=None, ext=None, lock=None):
193196
Write any of the 4 fuses. If the kwarg low/high/ext/lock is not
194197
passed in or is None, that fuse is skipped
195198
"""
196-
self.begin()
199+
self.begin(clock=_SLOW_CLOCK)
197200
lock and self._transaction((0xAC, 0xE0, 0, lock))
198201
low and self._transaction((0xAC, 0xA0, 0, low))
199202
high and self._transaction((0xAC, 0xA8, 0, high))
@@ -221,14 +224,14 @@ def erase_chip(self):
221224
"""
222225
Fully erases the chip.
223226
"""
224-
self.begin()
227+
self.begin(clock=_SLOW_CLOCK)
225228
self._transaction((0xAC, 0x80, 0, 0))
226229
self._busy_wait()
227230
self.end()
228231

229232
#################### Mid level
230233

231-
def begin(self):
234+
def begin(self, clock=_FAST_CLOCK):
232235
"""
233236
Begin programming mode: pull reset pin low, initialize SPI, and
234237
send the initialization command to get the AVR's attention.
@@ -237,7 +240,7 @@ def begin(self):
237240
if self._spi:
238241
while self._spi and not self._spi.try_lock():
239242
pass
240-
self._spi.configure(baudrate=1000000)
243+
self._spi.configure(baudrate=clock)
241244
self._transaction((0xAC, 0x53, 0, 0))
242245

243246
def end(self):

0 commit comments

Comments
 (0)