AVRISP mkII(ATMEL純正のAVRマイコンライター)を使って、ブートローダーを書き込むテストをしました。
ATmega32U4はArduino Leonardoにも搭載されており、Arduino IDEからAVRISP mkII経由でブートローダーを書き込んでみました。
すると、Arduino IDEでは何度やってもエラーが出て書き込みに失敗するので、Arduino IDE上で実行しているコマンドを抜き出して、コマンドプロンプトからブートローダー書き込みができたので記録として残します。
この作業は、失敗すると2度とボードが使えなくなったりする可能性がありますので、作業は自己責任でお願いいたします。
書込みコマンド(avrdude)
avrdudeというコマンド(avrdude.exe)は、Arduino IDEをインストールしたディレクトリ配下の”hardware\tools\avr\bin\”に入っています。
avrdudeのコマンドラインオプションは以下のようになります。
avrdude: unknown option -- -
Usage: avrdude [options]
Options:
-p <partno> Required. Specify AVR device.
-b <baudrate> Override RS-232 baud rate.
-B <bitclock> Specify JTAG/STK500v2 bit clock period (us).
-C <config-file> Specify location of configuration file.
-c <programmer> Specify programmer type.
-D Disable auto erase for flash memory
-i <delay> ISP Clock Delay [in microseconds]
-P <port> Specify connection port.
-F Override invalid signature check.
-e Perform a chip erase.
-O Perform RC oscillator calibration (see AVR053).
-U <memtype>:r|w|v:<filename>[:format]
Memory operation specification.
Multiple -U options are allowed, each request
is performed in the order specified.
-n Do not write anything to the device.
-V Do not verify.
-u Disable safemode, default when running from a script.
-s Silent safemode operation, will not ask you if
fuses should be changed back.
-t Enter terminal mode.
-E <exitspec>[,<exitspec>] List programmer exit specifications.
-x <extended_param> Pass <extended_param> to programmer.
-y Count # erase cycles in EEPROM.
-Y <number> Initialize erase cycle # in EEPROM.
-v Verbose output. -v -v for more.
-q Quell progress output. -q -q for less.
-l logfile Use logfile rather than stderr for diagnostics.
-? Display this usage.
avrdude version 6.3, URL: <http://savannah.nongnu.org/projects/avrdude/>
ブートローダー書込みに必要なファイル
avrdude.conf・・・Arduino IDEをインストールしたフォルダ配下の”hardware\tools\avr\etc\”に置かれているファイルで、avrdudeで使用する設定ファイル。
今回はArduino Leonardo用のブートローダー書込みのみですので、特に変更などはありませんがavrdudeが実行中に参照します。
Caterina-Leonardo.hex・・・Arduino IDEをインストールしたフォルダ配下の”hardware\arduino\avr\bootloaders\caterina\”に置かれているファイルで、Arduino Leonardo用ブートローダー本体のhexファイル。
コマンドライン
実際のコマンドは2つあります。
1. FUSE設定書込み
2. ブートローダー書込み
実行するのは、avrdudeというコマンド1つです。
avrdudeのコマンドラインオプションの組合せで、上記の2つの書き込みが実現されます。
FUSE設定書込み
FUSE設定書込みのコマンドは下記の通り
"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe" -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -v -patmega32u4 -cstk500v2 -Pusb -e -Ulock:w:0x3F:m -Uefuse:w:0xcb:m -Uhfuse:w:0xd8:m -Ulfuse:w:0xff:m
横2行では判りづらいので、オプションごとに改行するとこうなります。
"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe"
-C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"
-v
-patmega32u4
-cstk500v2
-Pusb
-e
-Ulock:w:0x3F:m
-Uefuse:w:0xcb:m
-Uhfuse:w:0xd8:m
-Ulfuse:w:0xff:m
コマンドラインオプションを順に並べるとこうなります。
-C <config-file> Specify location of configuration file.
どの設定ファイルを使用するか(このコマンドの場合、”C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf”)
-v Verbose output. -v -v for more.
出力するかどうか(このコマンドの場合、「出力する」)
-p <partno> Required. Specify AVR device.
AVRデバイスの指定(このコマンドの場合、”atmega32u4″)
-c <programmer> Specify programmer type.
プログラマのタイプ指定(このコマンドの場合、”stk500v2″)
-P <port> Specify connection port.
接続ポート指定(このコマンドの場合、”usb”)
-e Perform a chip erase.
チップ消去実行有無(このコマンドの場合、「チップ消去を実行する」)
-U <memtype>:r|w|v:<filename>[:format]
Memory operation specification.
Multiple -U options are allowed, each request
is performed in the order specified.
メモリ動作仕様
複数の-Uオプションは、各要求を許可。指定された順序で実行されます。
(このコマンドの場合、”lock:w:0x3F:m”、”efuse:w:0xcb:m”、”hfuse:w:0xd8:m”、”lfuse:w:0xff:m”)
ブートローダー書込み
ブートローダー書込みのコマンドは下記の通り
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe" -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega32u4 -cstk500v2 -Pusb -Uflash:w:"C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/caterina/Caterina-Leonardo.hex":i -Ulock:w:0x2F:m
こちらもオプションごとに改行するとこうなります。
"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe"
-C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"
-v
-patmega32u4
-cstk500v2
-Pusb
-Uflash:w:"C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\caterina\Caterina-Leonardo.hex":i
-Ulock:w:0x2F:m
-C <config-file> Specify location of configuration file.
どの設定ファイルを使用するか(このコマンドの場合、”C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf”)
-v Verbose output. -v -v for more.
出力するかどうか(このコマンドの場合、「出力する」)
-p <partno> Required. Specify AVR device.
AVRデバイスの指定(このコマンドの場合、”atmega32u4″)
-c <programmer> Specify programmer type.
プログラマのタイプ指定(このコマンドの場合、”stk500v2″)
-P <port> Specify connection port.
接続ポート指定(このコマンドの場合、”usb”)
-U <memtype>:r|w|v:<filename>[:format]
Memory operation specification.
Multiple -U options are allowed, each request
is performed in the order specified.
メモリ動作仕様
複数の-Uオプションは、各要求を許可。指定された順序で実行されます。
(このコマンドの場合、”flash:w:”C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\caterina\Caterina-Leonardo.hex”:i”、”lock:w:0x2F:m “)
この2つのコマンドですが、実行するときにもう一つコツがあります(Arduino IDEでブートローダー書込みに失敗した理由もこれだった?)。
それは、この2つのコマンドは「ちょっとだけ時間を空けて実行する必要性がある」ということです。
間をおかずに実行すると、ブートローダー書込みのコマンド実行時に失敗してしまうので、そのままではまったくArduino Leonardoが使えなくなります。

