前回、Arduino LeonardoではUSBキーボードに関するHID Report Descriptorではふつうの日本語キーボード等とは違い、キーの上限を101に制限していることがわかった。これが原因で日本語キーボードの[¥]や[ろ]に対応するコードを送っても無視されていた。
 このHID Report Descriptorの記述はArduino IDE1.0.1のソースにあるHID.cppの中で定義されてある。これはコンパイル済みなのでいじれない。そこでArduino IDEが生成する、マイコンに書き込むイメージファイルである.HEXファイルにパッチを当てることにした。

 Arduino IDE1.0.1でコンパイル時に生成される.hexファイルの所在は以下の方法で知ることが出来る。
Arduino開発環境が使えなくなってハマった件

例えばこのようなディレクトリの中に生成される。(MacOSX)
/var/folders/r5/h8nsfjps4kzdltsm9t4p0qr00000gn/T/build2978712088924621678.tmp/sketch_oct06a.cpp.hex

.hexファイルはひとつしかないので、これをコピーして以下のパッチを当てる。
25行目、
:100180008103950675081500256505071900296581
 ↓
:10018000810395067508150025FE0507190029FE4F

 パッチ当て後はチェックサムも変更しなければならないのでawkで作ったツールで確認した。
[awk]インテルHEXフォーマットのチェッカ

 こうやって作った.hexファイルを書き込む。色々試行錯誤して、結局AVRISPmkIIを使った。
 
$ /Applications/Arduino1.0.1.app/Contents/Resources/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino1.0.1.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega32u4 -cavrispmkII -Pusb -b57600 -e -Uflash:w:ok.hex:i

書き込み用ツールavrdudeはArduinoIDE内のものを、confファイルも同様。ここでは-eでフラッシュメモリを消去してから書き込んでいる。-Uオプションで消去をスキップしたらverifyでエラーが出たため。

以下、ログ
avrdude: Version 5.11, compiled on Sep  2 2011 at 18:52:52
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

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

         Using Port                    : usb
         Using Programmer              : avrispmkII
         Overriding Baud Rate          : 57600
avrdude: usbdev_open(): Found AVRISP mkII, serno: 000200081404
         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    10     8    0 no       1024    8      0  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel AVR ISP mkII
         Programmer Model: AVRISP mkII
         Hardware Version: 1
         Firmware Version Master : 1.10
         Vtarget         : 5.0 V
         SCK period      : 8.00 us

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9587
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CB
avrdude: erasing chip
avrdude: reading input file "ok.hex"
avrdude: writing flash (6676 bytes):

Writing | ################################################## | 100% 2.75s

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

Reading | ################################################## | 100% 2.01s

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

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CB
avrdude: safemode: Fuses OK

avrdude done.  Thank you.


 これで書き込んだLeonardo(Davinci)をPCに接続し、前回のキー入力テストを行うと、ちゃんと¥マークが入力できた。成功!

 なお、Arduino IDEのソース中ではLEDの状態を受信する部分は定義されていないので、USB経由の指示で、接続したPS/2キーボードのLEDを制御することができないようだ。