Macでブートローダーを書き込む(その2)

ArduinoのArduino playground – OSXISPMKIIを参考にfinkをインストールして、fink install libusb avrdude でlibusb, avrdudeをインストールした。

さらに下に書いてある、
Finally, create a file named “burn-bootloader-usb.command” (or whatever) in $IDIR and make it executable.

#!/bin/sh
IPATH=`echo $0 | sed s/[^\/]*$//`;
CMD="$IPATH/bin/avrdude" 
OPTS="-p m8 -b 115200 -P usb -c avrispmkII" 

# Erase chip write lock and fuses 
$CMD $OPTS -e -U lock:w:0x3f:m -U lfuse:w:0xdf:m -U hfuse:w:0xca:m  

# Upload bootloader code 
$CMD $OPTS -D -U flash:w:ATmegaBOOT.hex:i 

# Lock boot section 
$CMD $OPTS -U lock:w:0x0f:m

のファイルを作って試してみると次のようなエラーが出た。
(hexファイルは /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/bootloaders/ 以下にいろいろあったので、同じ名前の bt/ATmegaBOOT_168.hex をfinkでavrdudeをインストールした /sw/bin/ 内にコピーしました。)

avrdude: stk500v2_recv_mk2: error in USB receive

ググったらソースコードにパッチを当ててビルドする必要がありそう。
finkでソースコードにパッチを当ててビルドする方法がわかってないので、Mac用のAVR開発環境 (CrossPack for AVR® Development) があったことを思い出してサイトを見に行ったら新しいの (CrossPack-AVR-20120217.dmg) がリリースされていたのでそちらを使うことにした。

インストーラー・パッケージになっているので、dmgファイルをマウントしてパッケージをダブルクリックすれば後はインストーラーに答えていけばOK。

/usr/local/CrossPack-AVR/(実際には/usr/local/CrossPack-AVR-20120217/へのシンボリックリンク)にインストールされます。

今度はCrossPackのディレクトリーにhexファイルとスクリプトをコピーして動かしてみるとAVRISP mkIIのLEDがオレンジ色になって書き込みしているみたいだけど、Arduinoに差し替えても書き込みができない。
次のメッセージが怪しいかなと調べてみると。

avrdude: Device signature = 0x1e950f
avrdude: Expected signature for ATMEGA8 is 1E 93 07
         Double check chip, or use -F to override this check.

書き込もうとしているのはATmega168PなんだけどATMEGA8となっている。

OPTS="-p m8 -b 115200 -P usb -c avrispmkII"

この"m8"が怪しい。
で、hexファイルを探す時に見つけた /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/boards.txt を見ると、スクリプトの他のおかしい点も見つけた。

最終的に次のように書き換えたらうまく書き込めた。

PATH=`echo $0 | sed s/[^\/]*$//`;
CMD="/usr/local/CrossPack-AVR/bin/avrdude" 
OPTS="-p m168p -b 19200 -P usb -c avrispmkII" 

# Erase chip write lock and fuses 
$CMD $OPTS -e -U lock:w:0x3f:m -U lfuse:w:0xff:m -U hfuse:w:0xdd:m -U efuse:w:0x00:m

# Upload bootloader code 
$CMD $OPTS -D -U flash:w:ATmegaBOOT_168_diecimila.hex:i 

# Lock boot section 
$CMD $OPTS -U lock:w:0x0f:m

もちろん、hexファイルも atmega/ATmegaBOOT_168_diecimila.hex/usr/local/CrossPack-AVR/ にコピーしました。

実際に動かした時のログです。

MACHINENAME:CrossPack-AVR username$ ./burn-boot.cmd 

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e940b
avrdude: erasing chip
avrdude: reading input file "0x3f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3f:
avrdude: load data lock data from input file 0x3f:
avrdude: input file 0x3f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0xff"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xff:
avrdude: load data lfuse data from input file 0xff:
avrdude: input file 0xff contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude: reading input file "0xdd"
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xdd:
avrdude: load data hfuse data from input file 0xdd:
avrdude: input file 0xdd contains 1 bytes
avrdude: reading on-chip hfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: reading input file "0x00"
avrdude: writing efuse (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0x00:
avrdude: load data efuse data from input file 0x00:
avrdude: input file 0x00 contains 1 bytes
avrdude: reading on-chip efuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of efuse verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.


avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e940b
avrdude: reading input file "ATmegaBOOT_168_diecimila.hex"
avrdude: writing flash (16294 bytes):

Writing | ################################################## | 100% 0.61s

avrdude: 16294 bytes of flash written
avrdude: verifying flash memory against ATmegaBOOT_168_diecimila.hex:
avrdude: load data flash data from input file ATmegaBOOT_168_diecimila.hex:
avrdude: input file ATmegaBOOT_168_diecimila.hex contains 16294 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 4.80s

avrdude: verifying ...
avrdude: 16294 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.


avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e940b
avrdude: reading input file "0x0f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0f:
avrdude: load data lock data from input file 0x0f:
avrdude: input file 0x0f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

MACHINENAME:CrossPack-AVR username$ pwd
/usr/local/CrossPack-AVR

ここまでやって気が付いたんだけど、さらにスケッチも書き込めるといいなぁ。
Arduinoのチップを何回も差し替えたりしているとバカになりそうだし。

カテゴリー: Arduino, AVR, ソフトウェア, 作業 タグ: , , , , , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA