kuroの覚え書き

96の個人的覚え書き

Arduino (Leonardo互換)のHEX書き込み

ずいぶん前に入手したまま放置していたCJMCU beatle (Leonardo互換の Arduinoボード)にADBキーボードのファームウェアを入れてみようと思う。
http://d.hatena.ne.jp/k-kuro/20151119

さて、とりあえずすることはArduinoIDEからボードへ書き込みし、内部で行われているAVRdudeのコマンドを確認すること。
ArduinoIDEの環境設定で「より詳細な情報を表示する。」の コンパイル 書き込み にチェックを入れて

マイコンボードに書き込む」ボタンを押すと

下のカラムに実行されたコマンドが表示されるが

avrdudeの文字が現れる部分を探し、その内容をコピーする。


/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega32u4 -cavr109 -P/dev/cu.usbmodem411 -b57600 -D -Uflash:w:/var/folders/wt/b27s5_f920q6xsm1jkj867n00000gn/T/build1944791337978728544.tmp/Blink.cpp.hex:i

すごく長い行だ。/Application/Arduino.appのパッケージの中に avrdudeは入っており、コンパイルされたBlink.hexが書き込まれている。

そこで、任意のhexファイルをこのw:以下に入れてやれば書き込みができるはずなんだが、どうもすんなりいかない。

avrdude: Version 6.0.1, compiled on Apr  3 2014 at 22:00:33
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/xxxxx/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/cu.usbmodem411
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .avrdude: Send: . [1b] 
avrdude: Send: S [53] 
avrdude: ser_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding

こんな表示でエラーが出る。



ArduinoIDEの動作を確認してみると

シリアルポートを1200bpsで開閉する事により、リセットを行なっています。/dev/cu.usbmodem411
PORTS {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/cu.usbmodem411, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, /dev/tty.usbmodem411, } / {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } => {}
PORTS {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } / {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } => {}
PORTS {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } / {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } => {}
PORTS {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, } / {/dev/cu.Bluetooth-Incoming-Port, /dev/cu.iPhone-WirelessiAP, /dev/cu.usbmodem411, /dev/tty.Bluetooth-Incoming-Port, /dev/tty.iPhone-WirelessiAP, /dev/tty.usbmodem411, } => {/dev/cu.usbmodem411, /dev/tty.usbmodem411, }
Found upload port: /dev/cu.usbmodem411

こんな表示が出ている。



つまり書き込む前にリセットをかけているようだ。
いろいろネット検索してみるとやはり書き込む前にリセットボタンを押してやる必要があるらしい。
今回使用しているボードはボタンも何もないがリセットピンはGNDピンの隣にあるので、このピンをGNDにショートしてやればリセットがかかるはず。
ということで、リセットをかけてBlink.hexの書き込みを試してみると

avrdude: Version 6.0.1, compiled on Apr  3 2014 at 22:00:33
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/xxxxx/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/cu.usbmodem411
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled

〜〜中略〜〜

	 Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .avrdude: Send: . [1b] 
avrdude: Send: S [53] 
avrdude: Recv: C [43] 

avrdude: Recv: A [41] T [54] E [45] R [52] I [49] N [4e] 
avrdude: Send: V [56] 
avrdude: Recv: 1 [31] 0 [30] 
avrdude: Send: v [76] 
avrdude: Recv: ? [3f] 
avrdude: Send: p [70] 
avrdude: Recv: S [53] 
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
avrdude: Send: a [61] 
avrdude: Recv: Y [59] 
Programmer supports auto addr increment.
avrdude: Send: b [62] 
avrdude: Recv: Y [59] 
avrdude: Recv: . [00] 
avrdude: Recv: . [80] 
Programmer supports buffered memory access with buffersize=128 bytes.
avrdude: Send: t [74] 

Programmer supports the following devices:
avrdude: Recv: D [44] 
    Device code: 0x44
avrdude: Recv: . [00] 

avrdude: Send: T [54] D [44] 
avrdude: Recv: . [0d] 
avrdude: devcode selected: 0x44
avrdude: Send: P [50] 
avrdude: Recv: . [0d] 
avrdude: AVR device initialized and ready to accept instructions

Reading |                                                    | 0% 0.00savrdude: Send: s [73] 
avrdude: Recv: . [87] . [95] . [1e] 
Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587
avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 1, lfuse value: ff
avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 2, lfuse value: ff
avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 3, lfuse value: ff
avrdude: safemode: lfuse reads as FF
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 1, hfuse value: d8
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 2, hfuse value: d8
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 3, hfuse value: d8
avrdude: safemode: hfuse reads as D8
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 1, efuse value: cb
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 2, efuse value: cb
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 3, efuse value: cb
avrdude: safemode: efuse reads as CB
avrdude: reading input file "/var/folders/wt/b27s5_f920q6xsm1jkj867n00000gn/T/build1944791337978728544.tmp/Blink.cpp.hex"
avrdude: writing flash (8384 bytes):

Writing |                                                    | 0% 0.00savrdude: Send: A [41] . [00] . [00] 
avrdude: Recv: . [0d] 
avrdude: Send: B [42] . [00] . [80] F [46] . [0c] . [94] h [68] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [f9] . [09] . [0c] . [94] / [2f] . [09] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [b8] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] 
avrdude: Recv: . [0d] 

〜〜中略〜〜

avrdude: Recv: . [0d] 
avrdude: Send: B [42] . [00] . [80] F [46] . [00] . [e1] . [00] . [00] . [00] . [00] . [00] . [00] . [01] . [01] . [00] . [00] . [00] . [00] . [93] . [0c] . [b4] . [0c] . [1a] . [0c] Z [5a] . [0c] 9 [39] . [0c] . [84] . [0c] . [00] . [00] . [00] . [00] . [e7] . [0f] . [ee] . [0f] . [e7] . [0f] . [e7] . [0f] . [e7] . [0f] . [e7] . [0f] . [00] . [00] . [00] . [00] . [86] . [0f] . [ee] . [0f] . [fe] . [0d] . [c7] . [0e] T [54] . [0f] . [00] . [00] . [00] . [00] . [e7] . [0f] . [ee] . [0f] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] 
avrdude: Recv: . [0d] 
Writing | ################################################## | 100% 0.74s

avrdude: 8384 bytes of flash written
avrdude: verifying flash memory against /var/folders/wt/b27s5_f920q6xsm1jkj867n00000gn/T/build1944791337978728544.tmp/Blink.cpp.hex:
avrdude: load data flash data from input file /var/folders/wt/b27s5_f920q6xsm1jkj867n00000gn/T/build1944791337978728544.tmp/Blink.cpp.hex:
avrdude: input file /var/folders/wt/b27s5_f920q6xsm1jkj867n00000gn/T/build1944791337978728544.tmp/Blink.cpp.hex contains 8384 bytes
avrdude: reading on-chip flash data:

Reading |                                                    | 0% 0.00savrdude: Send: A [41] . [00] . [00] 
avrdude: Recv: . [0d] 
avrdude: Send: g [67] . [00] . [80] F [46] 
avrdude: Recv: . [0c] . [94] h [68] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [f9] . [09] . [0c] . [94] / [2f] . [09] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [b8] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] . [0c] . [94] . [90] . [01] 

〜〜中略〜〜

avrdude: Recv: . [0d] 
avrdude: Send: g [67] . [00] . [80] F [46] 
avrdude: Recv: . [00] . [e1] . [00] . [00] . [00] . [00] . [00] . [00] . [01] . [01] . [00] . [00] . [00] . [00] . [93] . [0c] . [b4] . [0c] . [1a] . [0c] Z [5a] . [0c] 9 [39] . [0c] . [84] . [0c] . [00] . [00] . [00] . [00] . [e7] . [0f] . [ee] . [0f] . [e7] . [0f] . [e7] . [0f] . [e7] . [0f] . [e7] . [0f] . [00] . [00] . [00] . [00] . [86] . [0f] . [ee] . [0f] . [fe] . [0d] . [c7] . [0e] T [54] . [0f] . [00] . [00] . [00] . [00] . [e7] . [0f] . [ee] . [0f] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] . [ff] 
Reading | ################################################## | 100% 0.20s

avrdude: avr_read(): skipping page 66: no interesting data
avrdude: avr_read(): skipping page 67: no interesting data

〜〜中略〜〜

avrdude: avr_read(): skipping page 255: no interesting data
avrdude: verifying ...
avrdude: 8384 bytes of flash verified

avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 1, lfuse value: ff
avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 2, lfuse value: ff
avrdude: Send: F [46] 
avrdude: Recv: . [ff] 
avrdude: safemode read 3, lfuse value: ff
avrdude: safemode: lfuse reads as FF
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 1, hfuse value: d8
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 2, hfuse value: d8
avrdude: Send: N [4e] 
avrdude: Recv: . [d8] 
avrdude: safemode read 3, hfuse value: d8
avrdude: safemode: hfuse reads as D8
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 1, efuse value: cb
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 2, efuse value: cb
avrdude: Send: Q [51] 
avrdude: Recv: . [cb] 
avrdude: safemode read 3, efuse value: cb
avrdude: safemode: efuse reads as CB
avrdude: safemode: Fuses OK (H:CB, E:D8, L:FF)
avrdude: Send: L [4c] 
avrdude: Recv: . [0d] 
avrdude: Send: E [45] 
avrdude: Recv: . [0d] 

avrdude done.  Thank you.

とちゃんと書き込めた。



そこでキーボードのファームウェア
https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb
からダウンロードし、ボードのAVR(32U4)に合わせてrev1のmakefile
$make -f Makefile.rev1
とmakeし、同様に書き込みを試みたところ、書き込みが終わったと同時に

できた。

ハードウェア配線は
https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb
ここの通りに。