As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com.

Difference between revisions of "CW1173 ChipWhisperer-Lite/AVR Programmer"

From ChipWhisperer Wiki
Jump to: navigation, search
 
Line 1: Line 1:
 +
{{Warningbox|This section was recently updated for ChipWhisperer 5. The old version can be found here: https://wiki.newae.com/V4:CW1173_ChipWhisperer-Lite/AVR_Programmer.}}
 
The CW1173 and CW1200 have built-in support for programming either Atmel AVR or Atmel XMEGA device. This is designed to allow you to program our target boards (including the built-in XMEGA target).
 
The CW1173 and CW1200 have built-in support for programming either Atmel AVR or Atmel XMEGA device. This is designed to allow you to program our target boards (including the built-in XMEGA target).
  
Line 5: Line 6:
 
The AVR device programmer requires four connections to the target: RESET, MOSI, MISO, SCK. See [[#20-Pin_Connector]] for details of AVR programming pin connections.
 
The AVR device programmer requires four connections to the target: RESET, MOSI, MISO, SCK. See [[#20-Pin_Connector]] for details of AVR programming pin connections.
  
==== Accessing the Programming ====
+
==== Accessing the Programmer ====
  
To access the AVR Programmer, select the "CW-Lite AVR Programmer" from the pull-down Tools menu:
+
First, setup the ChipWhisperer as usual:
 +
<syntaxhighlight lang=python>
 +
import chipwhisperer as cw
 +
scope = cw.scope()
 +
target = cw.target(scope)
 +
</syntaxhighlight>
  
[[File:avrprog_menu.png|image]]
+
Next, the AVR programmer can be accessed through the cw.programmers.AVRProgrammer object:
  
Which should give you the AVR Programmer Window.
+
<syntaxhighlight lang=python>
 +
prog = cw.programmers.AVRProgrammer
 +
</syntaxhighlight>
 +
==== Programming the Flash ====
  
==== Clock Source Selection ====
+
Note to use the AVR programmer you may require a valid clock source for the AVR. It is suggested to use the default ChipWhisperer setup, which will generate a 7.37MHz clock:
  
Note to use the AVR programmer you may require a valid clock source for the AVR. It is suggested to select one of the setup scripts (such as <code>ChipWhisperer-Lite: AES Simple-Serial on ATMega328P</code>) which will generate a 7.37 MHz clock.
+
<syntaxhighlight lang=python>
 +
scope.default_setup()
 +
</syntaxhighlight>
  
Check if the device is found by pressing the &quot;Check Signature&quot; button. The status window will show the detected device based on the signature.
+
Note that NewAE's Notduino targets have the transmit and receive pins swapped compared to the usual targets. If you're using one of these targets, swap them using the API:
  
[[File:avrprog_sigok.png|image]]
+
<syntaxhighlight lang=python>
 +
scope.io.tio1 = "serial_tx"
 +
scope.io.tio2 = "serial_rx"
 +
</syntaxhighlight>
  
If this fails, double-check connections, and ensure the clock source to the AVR is suitable. Note some errors will appear as part of the main window log:
+
Next, try programming the device by calling <code>cw.program_target()</code>:
  
[[File:avrprog_fail.png|image]]
+
<syntaxhighlight lang=python>
 +
cw.program_target(scope, prog, "<path to hex file")
 +
</syntaxhighlight>
  
 
The default SPI data rate for the programmer is too fast for devices which are running slower than 2 MHz. If programming a device with a clock source slower than 2 MHz, you will need to enable the &quot;Slow Clock Mode&quot;. In &quot;Slow Clock Mode&quot; the entire SAM3U and FPGA clock is changed from 96 MHz to 12 MHz. Note the default fuse bytes for a virgin ATMega328P result in a 1 MHz clock, so you will need to use &quot;slow clock mode&quot; to program the correct fuse bytes, after which point you will not need to use &quot;slow clock mode&quot;.
 
The default SPI data rate for the programmer is too fast for devices which are running slower than 2 MHz. If programming a device with a clock source slower than 2 MHz, you will need to enable the &quot;Slow Clock Mode&quot;. In &quot;Slow Clock Mode&quot; the entire SAM3U and FPGA clock is changed from 96 MHz to 12 MHz. Note the default fuse bytes for a virgin ATMega328P result in a 1 MHz clock, so you will need to use &quot;slow clock mode&quot; to program the correct fuse bytes, after which point you will not need to use &quot;slow clock mode&quot;.
 +
 +
To enable slow clock mode, call <code>program_target</code> with an additional <code>slow_clock = True</code> parameter:
 +
 +
<syntaxhighlight lang=python>
 +
cw.program_target(scope, prog, "<path to hex file", slow_clock=True)
 +
</syntaxhighlight>
  
 
<blockquote>'''note'''
 
<blockquote>'''note'''
  
The 'slow clock mode' is used to provide a slower SPI clock than would otherwise be possible. When switching into 'slow clock mode' it will cause all DCM blocks in the FPGA to become unlocked. You will need to reset the DCM blocks, or simply restart the CW-Capture software and run the setup script.
+
The 'slow clock mode' is used to provide a slower SPI clock than would otherwise be possible. When switching into 'slow clock mode' it will cause all DCM blocks in the FPGA to become unlocked. You will need to reset the DCM blocks, or simply call <code>default_setup()</code> again (this will reset the serial pins, so you'll have to swap them again if you're using a Notduino target).
 
</blockquote>
 
</blockquote>
 
==== Programming the Fuses ====
 
==== Programming the Fuses ====
  
By default the AVR programmer allows you to modify the LOW fuse byte only, as this byte controls the clock source selection. To change the value of the fuse byte:
+
By default the AVR programmer allows you to modify the LOW fuse byte only, as this byte controls the clock source selection. To change the value of the fuse byte, you'll need to manually setup the programmer:
  
# Press the &quot;Read Fuses&quot; button, and the values should be populated
+
<syntaxhighlight lang=python>
# Specify the new low fuse value
+
avr_programmer = prog(slow_clock=False) #call with slow_clock = True to enable the slow clock
# Hit &quot;Write Fuses&quot;
+
avr_programmer.scope = scope
 +
avr_programmer.open()
 +
avr_programmer.find()
 +
</syntaxhighlight>
 +
 
 +
Once that's accomplished, the readFuse() and writeFuse() methods can be used to read and write the fuse bits, respectively:
 +
 
 +
<syntaxhighlight lang=python>
 +
fuse_vals = avr_programmer.readFuse(<value>)
 +
avr_programmer.writeFuse(<value>, <lfuse>)
 +
</syntaxhighlight>
 +
 
 +
with <value> corresponding to one of the following: "low", "high", or "extended".
  
 
See [http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega328p an Online Fuse Calculator] to better understand what the values mean.
 
See [http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega328p an Online Fuse Calculator] to better understand what the values mean.
Line 47: Line 81:
 
The low fuse byte must be changed to <code>D0</code> to use the external clock provided by the ChipWhisperer toolchain.
 
The low fuse byte must be changed to <code>D0</code> to use the external clock provided by the ChipWhisperer toolchain.
 
</blockquote>
 
</blockquote>
==== Programming the Flash ====
+
==== Troubleshooting ====
 
+
Programming the flash is accomplished by selecting the new .hex file in the &quot;Find&quot; menu, and pressing the &quot;Erase/Program/Verify FLASH&quot; button. The &quot;Status&quot; line will show the following information:
+
  
* File programmed into device
+
If you run into issues when programming the AVR, try the following troubleshooting methods:
* Time file was last modified (very useful to confirm you are using changed file when doing development)
+
* Status of verification, and number of bytes programmed/verified
+
  
[[File:avrprog_progok.png|image]]
+
* Ensure the ChipWhisperer is outputting a clock if the target requires one.
 +
* Ensure you have the serial pins the right way around
 +
* Ensure the fuse bits are set correctly.

Latest revision as of 12:05, 12 March 2020

This section was recently updated for ChipWhisperer 5. The old version can be found here: https://wiki.newae.com/V4:CW1173_ChipWhisperer-Lite/AVR_Programmer.

The CW1173 and CW1200 have built-in support for programming either Atmel AVR or Atmel XMEGA device. This is designed to allow you to program our target boards (including the built-in XMEGA target).

Note this programmer is fairly simple, and does not provide all the features of a stand-alone programmer.

The AVR device programmer requires four connections to the target: RESET, MOSI, MISO, SCK. See #20-Pin_Connector for details of AVR programming pin connections.

Accessing the Programmer

First, setup the ChipWhisperer as usual:

import chipwhisperer as cw
scope = cw.scope()
target = cw.target(scope)

Next, the AVR programmer can be accessed through the cw.programmers.AVRProgrammer object:

prog = cw.programmers.AVRProgrammer

Programming the Flash

Note to use the AVR programmer you may require a valid clock source for the AVR. It is suggested to use the default ChipWhisperer setup, which will generate a 7.37MHz clock:

scope.default_setup()

Note that NewAE's Notduino targets have the transmit and receive pins swapped compared to the usual targets. If you're using one of these targets, swap them using the API:

scope.io.tio1 = "serial_tx"
scope.io.tio2 = "serial_rx"

Next, try programming the device by calling cw.program_target():

cw.program_target(scope, prog, "<path to hex file")

The default SPI data rate for the programmer is too fast for devices which are running slower than 2 MHz. If programming a device with a clock source slower than 2 MHz, you will need to enable the "Slow Clock Mode". In "Slow Clock Mode" the entire SAM3U and FPGA clock is changed from 96 MHz to 12 MHz. Note the default fuse bytes for a virgin ATMega328P result in a 1 MHz clock, so you will need to use "slow clock mode" to program the correct fuse bytes, after which point you will not need to use "slow clock mode".

To enable slow clock mode, call program_target with an additional slow_clock = True parameter:

cw.program_target(scope, prog, "<path to hex file", slow_clock=True)
note

The 'slow clock mode' is used to provide a slower SPI clock than would otherwise be possible. When switching into 'slow clock mode' it will cause all DCM blocks in the FPGA to become unlocked. You will need to reset the DCM blocks, or simply call default_setup() again (this will reset the serial pins, so you'll have to swap them again if you're using a Notduino target).

Programming the Fuses

By default the AVR programmer allows you to modify the LOW fuse byte only, as this byte controls the clock source selection. To change the value of the fuse byte, you'll need to manually setup the programmer:

avr_programmer = prog(slow_clock=False) #call with slow_clock = True to enable the slow clock
avr_programmer.scope = scope
avr_programmer.open()
avr_programmer.find()

Once that's accomplished, the readFuse() and writeFuse() methods can be used to read and write the fuse bits, respectively:

fuse_vals = avr_programmer.readFuse(<value>)
avr_programmer.writeFuse(<value>, <lfuse>)

with <value> corresponding to one of the following: "low", "high", or "extended".

See an Online Fuse Calculator to better understand what the values mean.

tip
If programming a virgin ATMega328P device, the default low-fuse value of 62 results in the internal 8 MHz oscillator being divided down to 1 MHz. Any external clock is ignored.

The low fuse byte must be changed to D0 to use the external clock provided by the ChipWhisperer toolchain.

Troubleshooting

If you run into issues when programming the AVR, try the following troubleshooting methods:

  • Ensure the ChipWhisperer is outputting a clock if the target requires one.
  • Ensure you have the serial pins the right way around
  • Ensure the fuse bits are set correctly.