#freeze
[[FrontPage]]

2011/02/06 からのアクセス回数 &counter;

#contents

** はじめに [#g593bfb6]
[[ARMマイコン>http://www.amazon.co.jp/dp/B004AX9QWG/]]
で、「ARMマイコンパーフェクト学習基板」のUSB HIDの作り方が紹介されて
いましたので、オープンソースのHIDライブラリであるlibhidを
使ってオリジナルのHIDにアクセスする方法を紹介します。

** ARMマイコンパーフェクト学習基板のHIDを作成 [#j794d453]
[[ARMマイコン>http://www.amazon.co.jp/dp/B004AX9QWG/]]
では、HID作成のポイントとなるソースは公開されていますが、
全ソースがありません。

*** LPC Xpressoの例題を使う [#c6f310b0]
スタートポイントは、LPC Xpressoに付属するLPC13xxのサンプル
プログラムから、usbhid_romプロジェクトをインポートします。

このusbhidrom_main.cを以下のように変更します。

- &ref(usbhidrom_main.c);

*** HID依存のソースはとても短い [#rdebadbf]
[[ARMマイコン>http://www.amazon.co.jp/dp/B004AX9QWG/]]
に解説されているとおり、オリジナルのHIDを作る場合、変更する関数は
- GetInReport (ホストにデータを送る関数)
- SetOutReport(USBにデータを送る関数)

の2個です。
そのソースも非常に簡単です。
#pre{{
uint8_t PCInReportData = 0;
int	incount = 0;
void GetInReport (uint8_t src[], uint32_t length)
{
	incount++;
	if (incount >= 50) {
		incount = 0;
		PCInReportData++;
	}
	src[0] = PCInReportData;
}

void SetOutReport (uint8_t dst[], uint32_t length)
{
	uint8_t PCOutReportData = dst[0];
	if(PCOutReportData& 0x01)
		LPC_GPIO[LED_PORT]->MASKED_ACCESS[1<<LED_BIT] = 0xff;
	else
		LPC_GPIO[LED_PORT]->MASKED_ACCESS[1<<LED_BIT] = 0;
}
}}


*** LPC Xpressoでバイナリファイルを作成する方法 [#u3801e73]
LPC Xpressoでは、そのままではARMマイコンパーエフェクト基板に書き込むための、
バイナリファイルが作成されません。

これが本当のやり方とは思いませんが、私の方法を紹介します。
- CodeBaseに付いてきたlpcrc.exeをLPC Xpressoのbinフォルダにコピー
- プロジェクトのPropertiesのC/C++ BuildのSettingを選択し、Build Stepsタグの
Post-build stepsに以下のコマンドをセット(実際には1行で指定)

#pre{{
arm-none-eabi-size ${BuildArtifactFileName};  arm-none-eabi-objcopy 
-O binary ${BuildArtifactFileName} ${BuildArtifactFileBaseName}.bin; lpcrc 
${BuildArtifactFileBaseName}.bin
}}

できあがった、binファイルをARMマイコンパーフェクト学習基板にコピーします。

*** 動作確認 [#nf97b040]
usbhid_romサンプルには、確認のためのデモプログラム(HID Demonstration.exe)が入っています。
これを起動し、LED1をチェックすると基板のLEDが点灯し、INPUTnの色が緑に変わって
変化すればOKです。

&ref(Demo_prog.png);

** libhidのインストール [#lf6e0cf1]
*** ソースのダウンロード [#y0727248]
[[libhidのページ>http://libhid.alioth.debian.org/]]では、SVNからのインストール方法が紹介されていますが、configure等一部のファイルが含まれておらず、コンパイルすることができません。

そこで、[[Aliothのダウンロードページ>http://alioth.debian.org/frs/?group_id=30451]]から
- &ref(libhid-0.2.16.tar.gz);

をダウンロードします。

*** インストール [#o9e3bb00]
今回は、C以外の言語を使わないので、swigを外しました。
コンパイルとインストールは以下のように行います。
#pre{{
$ ./configure --disable-swig
$ make
正常にコンパイルできたら
$ sudo make install
}}

*** 動作確認 [#xc185edf]
コンパイルが完了したら、testディレクトリの
- test_libhid.c

を使ってARMマイコンパーフェクト学習基板を認識するか試してみます(マウスなど他のHID機器でも構いません)。

test_libhid.cの43行を以下のように修正します。

#pre{{
  HIDInterfaceMatcher matcher = { 0x1FC9, 0x0003, NULL, NULL, 0 };
}}

makeコマンドでtest_libhidを作り直し、実行してみます。
#pre{{
$ make
$ ./test_libhid
 NOTICE: hid_init(): libhid 0.2.16.0.0 is being initialized.
  TRACE: hid_init(): initialising USB subsystem...
  TRACE: hid_init(): scanning for USB busses...
  TRACE: hid_init(): scanning for USB devices...
 NOTICE: hid_init(): successfully initialised HID library.
  TRACE: hid_new_HIDInterface(): creating a new HIDInterface instance...
  TRACE: hid_force_open(): forcefully opening a device interface according to matching criteria...
  TRACE: hid_get_usb_handle(): acquiring handle for a USB device...
途中省略
 NOTICE: hid_prepare_parser(): successfully set up the HID parser for USB device 036/008[0].
 NOTICE: hid_force_open(): successfully opened USB device 036/008[0].
device identification of HIDInterface 036/008[0]:
  dev_handle:    0x10018fda0
  device:        0x100838e00
  location:      036/008
  manufacturer:  NXP SEMICOND 
  product:       NXP LPC13XX HID    
  serial number: DEMO00000000
  TRACE: hid_reset_parser(): resetting the HID parser for USB device 036/008[0]...
  TRACE: hid_dump_tree(): iterating the parse tree for USB device 036/008[0]...
parse tree of HIDInterface 036/008[0]:
  path: 0xff000001.0xff000001; type: 0x80
  path: 0xff000001.0xff000001; type: 0x90
  TRACE: hid_reset_parser(): resetting the HID parser for USB device 036/008[0]...
  TRACE: hid_close(): closing USB device 036/008[0]...
  TRACE: hid_close(): closing handle of USB device 036/008[0]...
 NOTICE: hid_close(): successfully closed USB device 036/008[0].
  TRACE: hid_reset_parser(): resetting the HID parser for USB device 036/008[0]...
  TRACE: hid_close(): freeing memory allocated for HID parser...
  TRACE: hid_close(): resetting HIDInterface...
 NOTICE: hid_cleanup(): successfully deinitialised HID library.

}}

test_libhidは、指定されたHIDデバイスを検索し、そのpathを出力します。
上記の例では、
#pre{{
  path: 0xff000001.0xff000001; type: 0x80
  path: 0xff000001.0xff000001; type: 0x90
}}
このパスを使って、libhidのhid_set_output_report, hid_get_input_report関数を呼び出すとHID機器と通信することができる仕組みになっています。

** ARMマイコンパーフェクト学習基板にアクセス [#eb6cb5d4]
最後にtest_libhidを使ってARMARMマイコンパーフェクト学習基板にアクセスする例を示します。

test_libhid.cの191行目に以下のコードを挿入します。

#pre{{
	unsigned char	PATHLEN = 2;
	int const		PATH_IN[] = { 0xffa00001, 0xffa00001 };
	int const		PATH_OUT[] = { 0xffa00001, 0xffa00001 };
	
	unsigned char	SEND_PACKET_LEN = 1;
	unsigned char	RECV_PACKET_LEN = 1;
	char	PACKET[] = { 0x1 };
	char	packet[RECV_PACKET_LEN];

	int j;
	for (j = 0; j < 100000; j++) {
		if (j % 500 == 0)
			PACKET[0] =0;
		if (j % 1000 == 0)
			PACKET[0] =1;
		ret = hid_set_output_report(hid, PATH_IN, PATHLEN, PACKET, SEND_PACKET_LEN);
		if (ret != HID_RET_SUCCESS) {
			fprintf(stderr, "hid_set_output_report failed with return code %d¥n", ret);
		}
		ret = hid_get_input_report(hid, PATH_OUT, PATHLEN, packet, RECV_PACKET_LEN);
		if (ret != HID_RET_SUCCESS) {
			fprintf(stderr, "hid_get_input_report failed with return code %d¥n", ret);
		}
		else {
			fprintf(stderr, "hid_get_input_report successed val=%d\n", (int)packet[0]);
		}
	}
}}

ARMマイコンパーフェクト学習基板のLEDが点滅し、出力の
#pre{{
 NOTICE: hid_get_input_report(): successfully retrieved report from USB device 036/008[0].
hid_get_input_report successed val=110
}}
のvalの値がだんだん増えていくのが確認できるはずです。

修正したソースは、以下の通りです。
- &ref(test_libhid.c);

** コメント [#wa396b55]
この記事は、

#vote(おもしろかった[0],そうでもない[0],わかりずらい[0])

皆様のご意見、ご希望をお待ちしております。
- 例題を[[arm/JavaからUSB HIDデバイスを制御する]]にアップしました。 -- [[竹本 浩]] &new{2011-09-03 (土) 16:02:20};

#comment_kcaptcha

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
SmartDoc