LoginSignup
2
2

More than 5 years have passed since last update.

IOUSBのサンプルプログラム

Last updated at Posted at 2017-08-06

Mac OS XのIOUSBFamilyのサンプルプログラムにUSBNotification Exampleというものがあります。

このアプリは2003年くらいに作られて、EZ-USB(FX2じゃないオリジナル)をターゲットにしたプログラムです。

EZ-USBは初期のUSB内蔵8051系ワンチップマイコンで、今みたいにフラッシュは無くて、機能させるにはUSBからプログラムをRAMにダウンロードして実行するかI2CのEEPROMにプログラムを入れておいてRAMにロードするかのどちらかになりました。RAMにロードしたプログラムは電源が切れると消えるので、再度ダウンロードする必要があります。

このサンプルプログラムではUSBのデバイスの認識とファームウエアの転送して、ファームウエアのデバイスを再認識して、ファームウエアへのデータ転送をおこなっています。

手元にちょうど通信用ケーブルで使われていたEEPROMをはがしたEZ-USBがあったので試してみました。

写真(2017-08-06 13.17).jpg

Mac OS XのユーザランドのUSBアプリケーションはlibusbを使っている物が多いですが、素のIOUSBってどんな物か確認してみたいと思ったのも動機の一つです。

そのままでは動かなかったので、プログラムの修正は以下の通りです。

  • kOurProductIDを8193から8497に変更
  • RawDeviceAdded()のGetDeviceReleaseNumber()のチェックを外す

これでビルドしてコマンドラインで実行してEZ-USBを接続すると以下のようなログがでます。

bash-3.2$ ./USBNotificationExample 
Looking for devices matching vendor ID=1351 and product ID=8497
Raw device added.
Raw device removed.
Bulk test device added.
Interface found.
Interface class 255, subclass 0
Interface has 14 endpoints.
pipeRef 1: direction out, transfer type bulk, maxPacketSize 64
pipeRef 2: direction out, transfer type bulk, maxPacketSize 64
pipeRef 3: direction out, transfer type bulk, maxPacketSize 64
pipeRef 4: direction out, transfer type bulk, maxPacketSize 64
pipeRef 5: direction out, transfer type bulk, maxPacketSize 64
pipeRef 6: direction out, transfer type bulk, maxPacketSize 64
pipeRef 7: direction out, transfer type bulk, maxPacketSize 64
pipeRef 8: direction in, transfer type interrupt, maxPacketSize 64
pipeRef 9: direction in, transfer type bulk, maxPacketSize 64
pipeRef 10: direction in, transfer type bulk, maxPacketSize 64
pipeRef 11: direction in, transfer type bulk, maxPacketSize 64
pipeRef 12: direction in, transfer type bulk, maxPacketSize 64
pipeRef 13: direction in, transfer type bulk, maxPacketSize 64
pipeRef 14: direction in, transfer type bulk, maxPacketSize 64
Async event source added to run loop.
Async write complete.
Wrote "Bulk I/O Test" (13 bytes) to bulk endpoint
Async bulk read complete.
Read "Bulk I/O Test" (13 bytes) from bulk endpoint

EZ-USBのファームウエアはbulktest.cの中に入っていておそらくCypressのSDKに入っていたバイナリをそのまま使っていると思われます。このファームウエアはエンドポイント0x02(pipeRef:2)で受け取ったデータを、バイト毎にビット反転して、エンドポイント0x82(pipeRef:9)で返してきます。

USB Probeでファームが書き込まれてディスクリプタが変わるのが確認できます。

同期と非同期のbulkへの書き込み読み込みコードが入っていてUSE_ASYNC_IOをdefineすると非同期での処理になります。

同じくEZ-USBをターゲットにしたファーム書き込みコードもサンプルで用意されていますが、使い勝手がわるかったので、ちょっといじってここに置いてあります。FX2対応も入っています。

FX2用の類似のテストプログラムはCypressからAN74505として提供されています。

AN74505をlibusbからIOUSBに書き換えてみました。ファームのダウンロード機能も入れてあります。雪豹で確認しました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2