4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

libmcp2221を使ってみた

Last updated at Posted at 2018-07-12

秋月電子で入手できるMCP2221AのライブラリがgithubにあったのでMac OS X El Capitanで試してみた。

このライブラリをMac OS Xで使うには以下のHIDライブラリが必要です。

とりあえずmac/hid.cとhidapi/hidapi.hがあれば良いようです。

libmcp2221ディレクトリにファイルをコピーしてコンパイルしたところ問題なくコンパイル通りました。

以前作ったI2C赤外線リモコンを制御してみます。examples/i2c/main.cをちょっと修正しただけです。

main.c
/*
 * Project: MCP2221 HID Library
 * Author: Zak Kemble, contact@zakkemble.co.uk
 * Copyright: (C) 2015 by Zak Kemble
 * License: GNU GPL v3 (see License.txt)
 * Web: http://blog.zakkemble.co.uk/mcp2221-hid-library/
 */

# ifndef _WIN32
	#define _BSD_SOURCE
	#include <unistd.h>
	#define Sleep(ms) usleep(ms * 1000)
# endif

# include <stdio.h>
# include "../libmcp2221/win/win.h"
# include "../libmcp2221/libmcp2221.h"
# include "../libmcp2221/hidapi.h"

int main(void)
{
	puts("Starting!");

	mcp2221_init();
	
	// Get list of MCP2221s
	printf("Looking for devices... ");
	int count = mcp2221_find(MCP2221_DEFAULT_VID, MCP2221_DEFAULT_PID, NULL, NULL, NULL);
	printf("found %d devices\n", count);

	// Open whatever device was found first
	printf("Opening device... ");
	mcp2221_t* myDev = mcp2221_open();

	if(!myDev)
	{
		mcp2221_exit();
		puts("No MCP2221s found");
		getchar();
		return 0;
	}
	puts("done");

	mcp2221_error res;
	
	while(1)
	{
		Sleep(500);

		uint8_t buff[2];
		mcp2221_i2c_state_t state = MCP2221_I2C_IDLE;
		
		// Stop any transfers
		mcp2221_i2cState(myDev, &state);
		if(state != MCP2221_I2C_IDLE)
			mcp2221_i2cCancel(myDev);

		// Set divider from 12MHz
		mcp2221_i2cDivider(myDev, 26);

		// Write IR Pattern
		puts("Writing...");
		uint8_t tmp[3];
		// Sony CD Play
		tmp[0] = 0x33;
		tmp[1] = 0x4d;
		tmp[2] = 0x10;
		mcp2221_i2cWrite(myDev, 4, &tmp, 3, MCP2221_I2CRW_NORMAL);
		puts("Write complete");

		// Wait for completion
		while(1)
		{
			if(mcp2221_i2cState(myDev, &state) != MCP2221_SUCCESS)
				puts("ERROR");
			printf("State: %hhu\n", state);
			if(state == MCP2221_I2C_IDLE)
				break;
		}

		break;
	}

	switch(res)
	{
		case MCP2221_SUCCESS:
			puts("No error");
			break;
		case MCP2221_ERROR:
			puts("General error");
			break;
		case MCP2221_INVALID_ARG:
			puts("Invalid argument, probably null pointer");
			break;
		case MCP2221_ERROR_HID:
			printf("USB HID Error: %ls\n", hid_error(myDev->handle));
			break;
		default:
			printf("Unknown error %d\n", res);
			break;
	}
	
	mcp2221_exit();
	
	return 0;
}

以下でビルドします。

cc -o main hid.c libmcp2221.c main.c -framework IOKit -framework CoreFoundation

写真(2018-07-12 19.29).jpg

信号でました。

MCP2221はシリアルでも使えるしI2Cでも使えて一粒で二度美味しいです。

以前はこういう類は、FTDIのチップが便利だったのだが、AppleがFTDI用の勝手kextを入れちゃって、そのままではBitBangが使えないので、こっちの方がいいかもしれない。これってAppleのFTDIへの嫌がらせなのでは。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?