Overview | Stand-alone programming AVRs using CircuitPython

Originalartikel

Backup

<html> <div class=„page-content all-page-view-content“ readability=„54“> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/49926“><img class=„49926-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/049/926/medium260/hacks_icon_t.png?1515373660 260w, https://cdn-learn.adafruit.com/assets/assets/000/049/926/medium640/hacks_icon_t.png?1515373660 640w, https://cdn-learn.adafruit.com/assets/assets/000/049/926/medium800/hacks_icon_t.png?1515373660 800w, https://cdn-learn.adafruit.com/assets/assets/000/049/926/large1024/hacks_icon_t.png?1515373660 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/926/medium800/hacks_icon_t.png?1515373660“ alt=„hacks_icon_t.png“/></a></div> <div class=„row-fluid build-text“ readability=„44“> <p>If you've ever wanted a <em>stand alone</em> AVR programmer, that is super easy to use, you've come to the right place!</p> <p>This guide will show you how to turn any CircuitPython powered board with 4+ GPIO pins into an AVR progammer all on its own. <strong>No software like avrdude is needed</strong>, this software will program the chip all on its own, just drag the HEX file onto the CircuitPython disk drive.</p> <p>Perfect to putting bootloaders on empty chips, or field-reprogramming a project!</p> </div> <div class=„row-fluid build-text“ readability=„44“> <p>In theory, any and all AVR chips with SPI-programming interfaces are supported. However, we only have examples for <strong>ATmega328P</strong> chips (used in Arduino compatibles), <strong>ATtiny85</strong> (used in original Trinket/Gemma), and <strong>ATmega2560</strong> (Arduino Mega compatibles)</p> <p>To program other chips, you'll need to find out the signature, size of the flash, and the flash-page size. You can find this in the datasheet or in

avrdude.conf

</p> </div> <p>This code only supports SPI-based programming, not JTAG, SWD or parallel!</p> </div><div class=„page-content all-page-view-content“ readability=„64“> <div class=„row-fluid build-text“ readability=„47“> <p>Nearly all AVRs have a 'serial' programming interface, that's what we'll be using to program them. If your chip requires SWD, JTAG or parallel, this software won't work!</p> <p>In this example we'll show how to wire up an existing Arduino 328P compatible or raw 328P chip to a Feather M0 for programming</p> <p>For other chips, the wiring is similar, but you'll need to look up which pins are Power, Ground, Reset, and SCK/MOSI/MISO</p> </div> <div class=„row-fluid build-text“ readability=„31“> <h2>Power Pins</h2> <p>Do these pins first because they're easy to forget!</p> <ul><li>If connecting to a Arduino-compatible: connect <strong>GND</strong> on the Arduino to <strong>GND</strong> on the Feather. Then either plug the Arduino into USB, or connect the Arduino <strong>5V</strong> to Feather <strong>USB</strong></li> <li>If connecting to a bare chip: connect both <strong>GND</strong> pins together and to the Feather <strong>GND</strong>. Connect <strong>AVCC</strong> to <strong>VCC</strong> to the Feather <strong>3V</strong> pin</li> </ul></div> <p>If you're breadboarding a bare ATMega328 chip, don't forget there are *two* power pins and *two* ground pins</p> <div class=„row-fluid build-text“ readability=„38“> <ul><li>Connect the <strong>CircuitPython</strong> <strong>SCK</strong> pin to the target <strong>SCK</strong> (on Uno/Atmega328 this is also known as Digital #13)</li> <li>Connect the <strong>CircuitPython</strong> <strong>MISO</strong> pin to the target <strong>MISO</strong> (on Uno/Atmega328 this is also known as Digital #12)</li> <li>Connect the <strong>CircuitPython</strong> <strong>MOSI</strong> pin to the target <strong>MOSI</strong> (on Uno/Atmega328 this is also known as Digital #11)</li> <li>Connect <strong>CircuitPython D5</strong> (or any digital pin, as long as you change the code too) to the target <strong>RESET</strong></li> </ul><p>If you are breadboarding a chip, it may need a clock or crystal and it needs to be there to program the chip! If your board has a crystal or oscillator already, skip this. If you're programming a 'raw' ATmega328, you'll want to add it:</p> <ul><li>Connect CircuitPython D9 (or any digital pin with PWM out, as long as you change the code to) to the target <strong>XTAL1</strong></li> </ul></div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/49998“><img class=„49998-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/049/998/medium260/hacks_rawchip.png?1515718660 260w, https://cdn-learn.adafruit.com/assets/assets/000/049/998/medium640/hacks_rawchip.png?1515718660 640w, https://cdn-learn.adafruit.com/assets/assets/000/049/998/medium800/hacks_rawchip.png?1515718660 800w, https://cdn-learn.adafruit.com/assets/assets/000/049/998/large1024/hacks_rawchip.png?1515718660 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/998/medium800/hacks_rawchip.png?1515718660“ alt=„hacks_rawchip.png“/></a></div> <p><a href=„https://cdn-learn.adafruit.com/assets/assets/000/050/008/original/featherchip.fzz?1515724984“ class=„btn btn-large btn-block btn-primary“ target=„_self“ type=„button“>Fritzing for diagram</a></p> <table class=„build-table“ readability=„1“><tr class=„build-row“ readability=„3“><td class=„side-images“> <ul><li class=„medium-side“><a class=„large-side-image-link“ href=„https://learn.adafruit.com/assets/49999“><img class=„49999-asset medium-side-image img-responsive“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/999/medium640/hacks_328p_pins.png?1515718791“ alt=„hacks_328p_pins.png“/></a></li> </ul></td> <td class=„side-text“ readability=„28“> <div class=„text“ readability=„31“> <ul><li><strong>VCC</strong> lines are <strong>Red</strong></li> <li><strong>Ground/GND</strong> lines are <strong>Black</strong></li> <li><strong>SCK</strong> is green</li> <li><strong>MOSI</strong> is blue</li> <li><strong>MISO</strong> is yellow</li> <li><strong>RESET</strong> is purple</li> <li><strong>XTAL</strong> is grey</li> </ul><p>Notice that the notch on the chip is to the <em>right</em> - away from the Feather!</p> </div> </td> </tr></table> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/50000“><img class=„50000-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/050/000/medium260/hacks_unoprog.png?1515719126 260w, https://cdn-learn.adafruit.com/assets/assets/000/050/000/medium640/hacks_unoprog.png?1515719126 640w, https://cdn-learn.adafruit.com/assets/assets/000/050/000/medium800/hacks_unoprog.png?1515719126 800w, https://cdn-learn.adafruit.com/assets/assets/000/050/000/large1024/hacks_unoprog.png?1515719126 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/050/000/medium800/hacks_unoprog.png?1515719126“ alt=„hacks_unoprog.png“/></a></div> <p><a href=„https://cdn-learn.adafruit.com/assets/assets/000/050/009/original/featheruno.fzz?1515725024“ class=„btn btn-large btn-block btn-primary“ target=„_self“ type=„button“>Fritzing for diagram</a></p> <div class=„row-fluid build-text“ readability=„40“> <p>For Arduino UNO and compatibles, we recommend powering from USB or DC power. Then connect <strong>GND</strong> pins together, and wire up&#160;<strong>Reset</strong>, <strong>SCK</strong>, <strong>MOSI</strong>, and <strong>MISO</strong> as seen above.</p> <p><strong>XTAL</strong> pin is not required, Arduinos have on-board crystals.</p> </div> </div><div class=„page-content all-page-view-content“ readability=„80“> <div class=„row-fluid build-text“ readability=„35“> <p>To use the AVR programming library you'll need to install the&#160;<a href=„https://github.com/adafruit/Adafruit_CircuitPython_AVRprog“>Adafruit CircuitPython AVRprog</a> library on your CircuitPython board.</p> <p>First make sure you are running the&#160;<a href=„https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython“>latest version of Adafruit CircuitPython</a>&#160;for your board.</p> <p>Next you'll need to install the necessary libraries&#160;to use the hardware–carefully follow the steps to find and install these libraries from&#160;<a href=„https://github.com/adafruit/Adafruit_CircuitPython_Bundle“>Adafruit's CircuitPython library bundle</a>.&#160; Our introduction guide has&#160;<a href=„https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries“>a great page on how to install the library bundle</a>&#160;for both express and non-express boards.</p> <p>Remember for non-express boards like the, you'll need to manually install the necessary library from the bundle:</p> <ul><li><strong>adafruit_avrprog.mpy</strong></li> </ul><p>You can also download the&#160;<strong>adafruit_avrprog.mpy</strong>&#160;from&#160;<a href=„https://github.com/adafruit/Adafruit_CircuitPython_AVRprog/releases“>its releases page on Github</a>.</p> <p>Before continuing make sure your board's lib folder or root filesystem has the&#160;<strong>adafruit_avrprog.mpy</strong> file copied over.</p> </div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/49927“><img class=„49927-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/049/927/medium260/hacks_lib.png?1515373870 260w, https://cdn-learn.adafruit.com/assets/assets/000/049/927/medium640/hacks_lib.png?1515373870 640w, https://cdn-learn.adafruit.com/assets/assets/000/049/927/medium800/hacks_lib.png?1515373870 800w, https://cdn-learn.adafruit.com/assets/assets/000/049/927/large1024/hacks_lib.png?1515373870 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/927/medium800/hacks_lib.png?1515373870“ alt=„hacks_lib.png“/></a></div> <div class=„row-fluid build-text“ readability=„18“> <p>Next&#160;<a href=„https://learn.adafruit.com/welcome-to-circuitpython/the-repl“>connect to the board's serial REPL&#160;</a>so you are at the CircuitPython&#160;<strong>&gt;&gt;&gt;</strong>&#160;prompt.</p> </div> <p>For this simple example, we're assuming you don't need a clock-driving pin here, if you do, see the full example at the end of the page!</p> <div class=„row-fluid build-text“ readability=„31“> <h2>Imports</h2> <p>You'll need to import a few libraries</p> <ul><li>

board

- for assigning hardware pins</li> <li>

busio

- we use SPI bus to talk to the target device</li> <li>

adafruit_avrprog

- the library that we're using!</li> </ul></div> <div class=„build-code code-element“ readability=„7“> <pre class=„code-text-only c4“> &gt;&gt;&gt; import board &gt;&gt;&gt; import busio &gt;&gt;&gt; import adafruit_avrprog </pre> <pre class=„prettyprint linenums“> &gt;&gt;&gt; import board &gt;&gt;&gt; import busio &gt;&gt;&gt; import adafruit_avrprog </pre></div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/49928“><img class=„49928-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/049/928/medium260/hacks_imports.png?1515374293 260w, https://cdn-learn.adafruit.com/assets/assets/000/049/928/medium640/hacks_imports.png?1515374293 640w, https://cdn-learn.adafruit.com/assets/assets/000/049/928/medium800/hacks_imports.png?1515374293 800w, https://cdn-learn.adafruit.com/assets/assets/000/049/928/large1024/hacks_imports.png?1515374293 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/928/medium800/hacks_imports.png?1515374293“ alt=„hacks_imports.png“/></a></div> <div class=„row-fluid build-text“ readability=„36“> <h2>Initialize hardware</h2> <p>Next, create the hardware interface, you'll need an SPI port and one extra pin for the reset line. We'll use

board.D5

to match our diagrams on the previous page, but it can be <em>any</em> pin you like!</p> </div> <div class=„build-code code-element“ readability=„15“> <pre class=„code-text-only c4“> &gt;&gt;&gt; spi = busio.SPI(board.SCK, board.MOSI, board.MISO) &gt;&gt;&gt; avrprog = adafruit_avrprog.AVRprog() &gt;&gt;&gt; avrprog.init(spi, board.D5) </pre> <pre class=„prettyprint linenums“> &gt;&gt;&gt; spi = busio.SPI(board.SCK, board.MOSI, board.MISO) &gt;&gt;&gt; avrprog = adafruit_avrprog.AVRprog() &gt;&gt;&gt; avrprog.init(spi, board.D5) </pre></div> <div class=„row-fluid build-text“ readability=„34“> <h2>Communication / Signature Check</h2> <p>Next we'll verify that we can talk to the chip, once that works we are best off crafting our programmer into a full <strong>main.py</strong> project but at least we can quickly determine if things worked out.</p> <ol><li>Start by initializing the programming interface with

avrprog.begin()

which will pull the <strong>reset</strong> line low and send some commands to get the chip to listen.</li> <li>Then read the signature, you'll get an array of numbers - its probably best to turn this into hex values before printing since they're referred to as hex values in datasheets.</li> <li>Finally, call

avrprog.end()

</li> </ol></div> <div class=„build-code code-element“ readability=„13“> <pre class=„code-text-only c4“> &gt;&gt;&gt; avrprog.begin() &gt;&gt;&gt; [hex(i) for i in avrprog.read_signature()] ['0x1e', '0x95', '0xf'] &gt;&gt;&gt; avrprog.end() </pre> <pre class=„prettyprint linenums“> &gt;&gt;&gt; avrprog.begin() &gt;&gt;&gt; [hex(i) for i in avrprog.read_signature()] ['0x1e', '0x95', '0xf'] &gt;&gt;&gt; avrprog.end() </pre></div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/49930“><img class=„49930-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/049/930/medium260/hacks_sigtest.png?1515374642 260w, https://cdn-learn.adafruit.com/assets/assets/000/049/930/medium640/hacks_sigtest.png?1515374642 640w, https://cdn-learn.adafruit.com/assets/assets/000/049/930/medium800/hacks_sigtest.png?1515374642 800w, https://cdn-learn.adafruit.com/assets/assets/000/049/930/large1024/hacks_sigtest.png?1515374642 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/049/930/medium800/hacks_sigtest.png?1515374642“ alt=„hacks_sigtest.png“/></a></div> <div class=„row-fluid build-text“ readability=„32“> <p>You can see here we have a 0x1E950F chip attached, also known at an <strong>ATmega328P</strong></p> </div> <div class=„row-fluid build-text“ readability=„34“> <p>You can save this code to <strong>main.py</strong> and use the REPL to see the signature data, it also includes the code for setting up the crystal-driving PWM output</p> </div> <div class=„build-code code-element“ readability=„29“> <pre class=„code-text-only c4“> „“„ Read Signature Test - All this does is read the signature from the chip to check connectivity! “„“ import board import busio import pulseio import adafruit_avrprog spi = busio.SPI(board.SCK, board.MOSI, board.MISO) avrprog = adafruit_avrprog.AVRprog() avrprog.init(spi, board.D5) # we can generate an 6 MHz clock for driving bare chips too! clock_pwm = pulseio.PWMOut(board.D9, frequency=6000000, duty_cycle=655362) avrprog.begin() print(„Signature bytes: “, [hex(i) for i in avrprog.read_signature()]) avrprog.end() </pre> <pre class=„prettyprint linenums“> „“„ Read Signature Test - All this does is read the signature from the chip to check connectivity! “„“ import board import busio import pulseio import adafruit_avrprog spi = busio.SPI(board.SCK, board.MOSI, board.MISO) avrprog = adafruit_avrprog.AVRprog() avrprog.init(spi, board.D5) # we can generate an 6 MHz clock for driving bare chips too! clock_pwm = pulseio.PWMOut(board.D9, frequency=6000000, duty_cycle=655362) avrprog.begin() print(„Signature bytes: “, [hex(i) for i in avrprog.read_signature()]) avrprog.end() </pre></div> <div class=„row-fluid build-text“ readability=„36“> <h2>SPI / Wiring Errors</h2> <p>If something went wrong, you'll get an

SPI transaction failed

exception. Check your wiring! Also, sometimes the chip doesn't quite hear us, try connecting again.</p> <p><strong>Common problems:</strong></p> <ul><li>The target isn't powered - make sure it is powered via USB or via the CircuitPython board. A shared Ground wire is <em>required</em></li> <li>Make sure you have the reset pin on the target connected to whatever pin you setup when you created the

avrprog

object</li> <li>On ATmega2560, MOSI and MISO are connected opposite than the way you think. Either way, its OK to try swapping those two wires, see if that helps!</li> <li>The target is expecting a crystal but you don't have one, for example the UNO bootloader requires that the chip have a crystal or oscillator connected up, it's not optional!</li> </ul></div> </div><div class=„page-content all-page-view-content“ readability=„52“> <div class=„row-fluid build-text“ readability=„28“> <p>OK now that you've read the signature, you can write some code!</p> <p><a href=„https://github.com/adafruit/Adafruit_CircuitPython_AVRprog/tree/master/examples“>We have a few examples available you can use 'out of the box' - all are available here</a>. You can <a href=„https://github.com/adafruit/Adafruit_CircuitPython_AVRprog/archive/master.zip“>download the library zip to get all the files</a>. For each programming demo, we also have a matching 'hex' file, that's a requirement - it's the file you'll be programming into the chip!</p> <p>Copy the programming sketch into <strong>main.py</strong> and also grab the matching hex file. For example:</p> </div> <div class=„build-code code-element“ readability=„23“> <pre class=„code-text-only c4“> „“„ UNO Optiboot programming example, be sure you have the UNO wired up so:

UNO Ground to CircuitPython GND
UNO 5V to CircuitPython USB or make sure the UNO is powered by USB
UNO Pin 13 -&gt; CircuitPython SCK
UNO Pin 12 -&gt; CircuitPython MISO
UNO Pin 11 -&gt; CircuitPython MOSI
UNO RESET  -&gt; CircuitPython D5 (or change the init() below to change it!)

Drag „optiboot_atmega328.hex“ onto the CircuitPython disk drive, then open REPL! “„“ </pre> <pre class=„prettyprint linenums“> „“„ UNO Optiboot programming example, be sure you have the UNO wired up so:

UNO Ground to CircuitPython GND
UNO 5V to CircuitPython USB or make sure the UNO is powered by USB
UNO Pin 13 -&gt; CircuitPython SCK
UNO Pin 12 -&gt; CircuitPython MISO
UNO Pin 11 -&gt; CircuitPython MOSI
UNO RESET  -&gt; CircuitPython D5 (or change the init() below to change it!)

Drag „optiboot_atmega328.hex“ onto the CircuitPython disk drive, then open REPL! “„“ </pre></div> <div class=„row-fluid build-text“ readability=„31“> <p>Indicates you need <strong>optiboot_atmega328.hex</strong></p> </div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/50007“><img class=„50007-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/050/007/medium260/hacks_files.png?1515722077 260w, https://cdn-learn.adafruit.com/assets/assets/000/050/007/medium640/hacks_files.png?1515722077 640w, https://cdn-learn.adafruit.com/assets/assets/000/050/007/medium800/hacks_files.png?1515722077 800w, https://cdn-learn.adafruit.com/assets/assets/000/050/007/large1024/hacks_files.png?1515722077 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/050/007/medium800/hacks_files.png?1515722077“ alt=„hacks_files.png“/></a></div> <div class=„row-fluid build-text“ readability=„38“> <p>Then run the REPL and look for the

Ready to GO, type 'G' here to start &gt;

prompt and type the letter G into the REPL. You should see the code begin by checking the identity of the chip (the signature), erasing the chip, then programming it.</p> </div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/50001“><img class=„50001-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/050/001/medium260/hacks_flash_start.png?1515720290 260w, https://cdn-learn.adafruit.com/assets/assets/000/050/001/medium640/hacks_flash_start.png?1515720290 640w, https://cdn-learn.adafruit.com/assets/assets/000/050/001/medium800/hacks_flash_start.png?1515720290 800w, https://cdn-learn.adafruit.com/assets/assets/000/050/001/large1024/hacks_flash_start.png?1515720290 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/050/001/medium800/hacks_flash_start.png?1515720290“ alt=„hacks_flash_start.png“/></a></div> <div class=„row-fluid build-text“ readability=„33“> <p>It will skip most of the flash 'pages' because they're empty. At the end you'll get to the pages that are flashed and verified:</p> </div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/50002“><img class=„50002-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/050/002/medium260/hacks_flashdone_verified.png?1515720332 260w, https://cdn-learn.adafruit.com/assets/assets/000/050/002/medium640/hacks_flashdone_verified.png?1515720332 640w, https://cdn-learn.adafruit.com/assets/assets/000/050/002/medium800/hacks_flashdone_verified.png?1515720332 800w, https://cdn-learn.adafruit.com/assets/assets/000/050/002/large1024/hacks_flashdone_verified.png?1515720332 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/050/002/medium800/hacks_flashdone_verified.png?1515720332“ alt=„hacks_flashdone_verified.png“/></a></div> <div class=„row-fluid build-text“ readability=„33“> <p>It's very very rare for something to go wrong during verification. But if it does you'll see something like this. Just start over by hitting ^C and ^D in the REPL to begin again.</p> </div> <div class=„row-fluid build-image“><a href=„https://learn.adafruit.com/assets/50003“><img class=„50003-asset img-responsive“ srcset=„https://cdn-learn.adafruit.com/assets/assets/000/050/003/medium260/hacks_verifyfail.png?1515720410 260w, https://cdn-learn.adafruit.com/assets/assets/000/050/003/medium640/hacks_verifyfail.png?1515720410 640w, https://cdn-learn.adafruit.com/assets/assets/000/050/003/medium800/hacks_verifyfail.png?1515720410 800w, https://cdn-learn.adafruit.com/assets/assets/000/050/003/large1024/hacks_verifyfail.png?1515720410 1024w“ sizes=„(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px“ src=„https://cdn-learn.adafruit.com/assets/assets/000/050/003/medium800/hacks_verifyfail.png?1515720410“ alt=„hacks_verifyfail.png“/></a></div> <div class=„row-fluid build-text“ readability=„32“> <p>That's it! You've programmed the chip. For more details, keep reading.</p> </div> </div><div class=„page-content all-page-view-content“ readability=„97“> <div class=„row-fluid build-text“ readability=„55“> <p>Before you can really do anything you need to tell AVRprog library what the chip is. We'll use a python dict for that. Define <strong>name</strong> (that's for your information and printing errors), <strong>sig</strong> - a list of the three-byte signature, <strong>flash_size</strong> - the size of the flash memory in <em>bytes</em>, <strong>page_size</strong> - the size of each flash memory <em>page in</em> bytes, and <strong>fuse_mask</strong> - a list of the four fuses in a list

[low, high, ext, lock]

</p> <p>Fuse mask is the oddest one, but basically it defines which bits are actually used in each fuse. For example, the ext fuse is often only the bottom three bits, so its <strong>0x07</strong>. If you're not sure, you can set all four to <strong>0xFF</strong> and then when you burn fuses, set all the high bits to 1.</p> <p>Here are some chip examples:</p> </div> <div class=„build-code code-element“ readability=„19“> <pre class=„code-text-only c4“> attiny85 = {'name': „ATtiny85“} attiny85['sig'] = [0x1E, 0x93, 0x0B] attiny85['flash_size'] = 8192 attiny85['page_size'] = 64 attiny85['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre> <pre class=„prettyprint linenums“> attiny85 = {'name': „ATtiny85“} attiny85['sig'] = [0x1E, 0x93, 0x0B] attiny85['flash_size'] = 8192 attiny85['page_size'] = 64 attiny85['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre></div> <div class=„build-code code-element“ readability=„19“> <pre class=„code-text-only c4“> atmega328p = {'name': „ATmega328P“} atmega328p['sig'] = [0x1E, 0x95, 0x0F] atmega328p['flash_size'] = 32768 atmega328p['page_size'] = 128 atmega328p['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre> <pre class=„prettyprint linenums“> atmega328p = {'name': „ATmega328P“} atmega328p['sig'] = [0x1E, 0x95, 0x0F] atmega328p['flash_size'] = 32768 atmega328p['page_size'] = 128 atmega328p['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre></div> <div class=„build-code code-element“ readability=„19“> <pre class=„code-text-only c4“> atmega2560 = {'name': „ATmega2560“} atmega2560['sig'] = [0x1E, 0x98, 0x01] atmega2560['flash_size'] = 262144 atmega2560['page_size'] = 256 atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre> <pre class=„prettyprint linenums“> atmega2560 = {'name': „ATmega2560“} atmega2560['sig'] = [0x1E, 0x98, 0x01] atmega2560['flash_size'] = 262144 atmega2560['page_size'] = 256 atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F) </pre></div> <div class=„row-fluid build-text“ readability=„37“> <p>

avrprog.verify_sig(chip_dict, verbose=True)

</p> <p>We suggest calling this first, you can call it whenever you like, and it will return True/False.

chip_dict

is that dictionary you made above</p> </div> <div class=„row-fluid build-text“ readability=„39“> <p>This one is easy, just call

avrprog.erase_chip()

- the chip erase command is the same for all chips. It may take a second on bigger chips. <strong>You must do this before programming new firmware!</strong></p> <p>Also, if your chip has the lock-firmware-fuse set, you may have to erase the flash before you can change the lock fuse.</p> </div> <div class=„row-fluid build-text“ readability=„53“> <p>You can read, write and verify fuses.</p> <p><strong>Read fuses</strong> with</p> <p>

avrprog.read_fuses(chip_dict)

</p> <p>Which will return a list of the four fuses [low, high, ext, lock]</p> <p><strong>Write fuses</strong> with</p> <p>

avrprog.write_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)

</p> <p>Only arguments that are passed in will be written, so you can choose to write one fuse, or all 4.</p> <p><strong>Verify fuses</strong> with</p> <p>

avrprog.verify_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)

</p> <p>Only arguments that are passed in will be verified, so you can choose to verify one fuse, or all 4.</p> </div> <div class=„row-fluid build-text“ readability=„60“> <p>OK this is the good part, here's how you can write and verify flash memory. Reading memory to disk is not supported yet!</p> <p>

avrprog.program_file(chip_dict, "filename.hex", verbose=True, verify=True)

</p> <p>This function does all the work really, give it the chip information dictionary, and the name of a file (full path is OK). If

verify

is True, it will verify each page manually after writing. This is way faster than writing the whole file and then verifying the whole file so we recommend it.</p> <p>If you really want, you can also verify against a file with:</p> <p>

verify_file(chip_dict, "filename.hex", verbose=True)

</p> <p><strong>But</strong> it will check every single byte of the flash chip, so for example, if its a sparse hex file, like most bootloaders are where only a small portion of flash is data and the rest is empty, the empty parts are still checked. So it's very slow!</p> </div> <div class=„row-fluid build-text“ readability=„31“> <p>Not supported at this time!</p> </div> </div> </html>