2011/02/06 からのアクセス回数 6456
ARMマイコン で、「ARMマイコンパーフェクト学習基板」のUSB HIDの作り方が紹介されて いましたので、オープンソースのHIDライブラリであるlibhidを 使ってオリジナルのHIDにアクセスする方法を紹介します。
ARMマイコン では、HID作成のポイントとなるソースは公開されていますが、 全ソースがありません。
スタートポイントは、LPC Xpressoに付属するLPC13xxのサンプル プログラムから、usbhid_romプロジェクトをインポートします。
このusbhidrom_main.cを以下のように変更します。
ARMマイコン に解説されているとおり、オリジナルのHIDを作る場合、変更する関数は
の2個です。 そのソースも非常に簡単です。
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では、そのままではARMマイコンパーエフェクト基板に書き込むための、 バイナリファイルが作成されません。
これが本当のやり方とは思いませんが、私の方法を紹介します。
arm-none-eabi-size ${BuildArtifactFileName};  arm-none-eabi-objcopy 
-O binary ${BuildArtifactFileName} ${BuildArtifactFileBaseName}.bin; lpcrc 
${BuildArtifactFileBaseName}.bin
できあがった、binファイルをARMマイコンパーフェクト学習基板にコピーします。
usbhid_romサンプルには、確認のためのデモプログラム(HID Demonstration.exe)が入っています。 これを起動し、LED1をチェックすると基板のLEDが点灯し、INPUTnの色が緑に変わって 変化すればOKです。
libhidのページでは、SVNからのインストール方法が紹介されていますが、configure等一部のファイルが含まれておらず、コンパイルすることができません。
そこで、Aliothのダウンロードページから
をダウンロードします。
今回は、C以外の言語を使わないので、swigを外しました。 コンパイルとインストールは以下のように行います。
$ ./configure --disable-swig $ make 正常にコンパイルできたら $ sudo make install
コンパイルが完了したら、testディレクトリの
を使ってARMマイコンパーフェクト学習基板を認識するか試してみます(マウスなど他のHID機器でも構いません)。
test_libhid.cの43行を以下のように修正します。
  HIDInterfaceMatcher matcher = { 0x1FC9, 0x0003, NULL, NULL, 0 };
makeコマンドでtest_libhidを作り直し、実行してみます。
$ 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を出力します。 上記の例では、
path: 0xff000001.0xff000001; type: 0x80 path: 0xff000001.0xff000001; type: 0x90
このパスを使って、libhidのhid_set_output_report, hid_get_input_report関数を呼び出すとHID機器と通信することができる仕組みになっています。
最後にtest_libhidを使ってARMARMマイコンパーフェクト学習基板にアクセスする例を示します。
test_libhid.cの191行目に以下のコードを挿入します。
	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が点滅し、出力の
NOTICE: hid_get_input_report(): successfully retrieved report from USB device 036/008[0]. hid_get_input_report successed val=110
のvalの値がだんだん増えていくのが確認できるはずです。
修正したソースは、以下の通りです。
この記事は、
皆様のご意見、ご希望をお待ちしております。