= [http://www.ftdichip.com/Products/ICs/FT260.html FTDI FT260]で実験 = FT260をBBBに繋いで使い方の実験してみる。 == パッケージインストール == 始めにpythonで実験するために、hidapi周りのパッケージをインストールする。 {{{ root@beaglebone:~# apt-get install libhidapi-libusb0 libudev-dev libusb root@beaglebone:~# pip install --trusted-host pypi.python.org Cython hidapi }}} == デバイスの接続 == 開発ボードにはI2C接続のEEPROMが搭載されていて、defaultではジャンパで接続されている。[[br]] なるべく余計な接続をしない状態で実験したかったのでEEPROMなしの状態にジャンパを設定する。[[br]] デバイスを接続すると、{{{musb-hdrc musb-hdrc.1.auto: Babble}}}というメッセージが大量に出力されるが、一応認識されて繋がりはするようだ。 {{{ [ 6891.320644] musb-hdrc: 28/31 max ep, 16384/16384 memory [ 6891.659553] musb-hdrc musb-hdrc.1.auto: Babble [ 6891.664416] musb-hdrc musb-hdrc.1.auto: Babble [ 6891.668943] musb-hdrc: setup fifo_mode 4 [ 6891.668996] musb-hdrc: 28/31 max ep, 16384/16384 memory [ 6892.066437] usb 1-1: new full-speed USB device number 27 using musb-hdrc [ 6892.199679] usb 1-1: New USB device found, idVendor=0403, idProduct=6030 [ 6892.199741] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0 [ 6892.199773] usb 1-1: Product: FT260 [ 6892.199803] usb 1-1: Manufacturer: FTDI [ 6902.262497] hid-generic 0003:0403:6030.0005: usb_submit_urb(ctrl) failed: -1 [ 6902.276074] hid-generic 0003:0403:6030.0005: timeout initializing reports [ 6902.284785] hid-generic 0003:0403:6030.0005: hidraw0: USB HID v1.11 Device [FTDI FT260] on usb-musb-hdrc.1.auto-1/input0 [ 6912.310492] hid-generic 0003:0403:6030.0006: usb_submit_urb(ctrl) failed: -1 [ 6912.322341] hid-generic 0003:0403:6030.0006: timeout initializing reports [ 6912.330402] hid-generic 0003:0403:6030.0006: hidraw1: USB HID v1.11 Device [FTDI FT260] on usb-musb-hdrc.1.auto-1/input1 }}} HIDデバイスが2つ認識されているが、[http://www.ftdichip.com/Support/Documents/ProgramGuides/AN_394_User_Guide_for_FT260.pdf FT260のアプリケーションノート]の6ページ目{{{1.2 FT260 HID Interfaces and Endpoints}}}をみると、I2CとUARTがそれぞれ別のHIDとして認識されるようだ。[[br]] {{{hidraw0}}}がI2C、{{{hidraw1}}}がUARTらしい。 ジャンパを設定し直して、I2Cのみにしてみると、 {{{ [ 7774.216655] musb-hdrc: 28/31 max ep, 16384/16384 memory [ 7774.555547] musb-hdrc musb-hdrc.1.auto: Babble [ 7774.560500] musb-hdrc musb-hdrc.1.auto: Babble [ 7774.565029] musb-hdrc: setup fifo_mode 4 [ 7774.565083] musb-hdrc: 28/31 max ep, 16384/16384 memory [ 7774.962288] usb 1-1: new full-speed USB device number 31 using musb-hdrc [ 7775.093398] usb 1-1: New USB device found, idVendor=0403, idProduct=6030 [ 7775.093436] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0 [ 7775.093452] usb 1-1: Product: FT260 [ 7775.093467] usb 1-1: Manufacturer: FTDI [ 7785.130492] hid-generic 0003:0403:6030.0007: usb_submit_urb(ctrl) failed: -1 [ 7785.144095] hid-generic 0003:0403:6030.0007: timeout initializing reports [ 7785.153751] hid-generic 0003:0403:6030.0007: hidraw0: USB HID v1.11 Device [FTDI FT260] on usb-musb-hdrc.1.auto-1/input0 }}} となり、I2Cのみになるので、これで実験することにする。 == pythonでのテスト == 以前、帯名さんがHIDデバイスのテストをした時のドキュメントを参考にしてみる。[[br]] まずは、FT260の設定を取得してみる。 {{{ oot@beaglebone:~# ipython --nosep Python 2.7.9 (default, Aug 13 2016, 17:56:53) Type "copyright", "credits" or "license" for more information. IPython 2.3.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import hid In [2]: h = hid.device() In [3]: In [3]: h.open(0x0403, 0x6030) In [4]: h.set_nonblocking(1) Out[4]: 0 In [5]: h.write([0xA1] + [0]*63) Out[5]: 64 In [6]: h.read(64) Out[6]: [] In [7]: h.write([0xA0] + [0]*63) Out[7]: 64 In [8]: h.write([0xA0] + [0]*63) Out[8]: 64 In [9]: h.read(64) Out[9]: [] In [10]: h.write([0xA0] + [0]*11) Out[10]: -1 In [11]: h.write([0xA0] + [0]*63) Out[11]: -1 In [12]: h.read(64) Out[12]: [] In [13]: h.read(64) Out[13]: [] In [14]: h.write([0xA0] + [0]*63) Out[14]: -1 In [15]: h.write([0xC0] + [0]*63) Out[15]: -1 In [16]: h.close() }}} うまくいかない、、、[[br]] 色々見ていったところ、取得するデータによって使用する関数が違うらしい。[[br]] [http://www.ftdichip.com/Support/Documents/ProgramGuides/AN_394_User_Guide_for_FT260.pdf FT260のアプリケーションノート]を見てみると、15ページの{{{4.3 FT260 Report ID List}}}には、 || Report ID || Type || description || || 0xA0 || Feature || Chip code || || 0xA1 || Feature || System Setting || || 0xB0 || Feature || GPIO || || 0xB1 || Input || Interrupt Status (from UART interface) || || 0xC0 || Feature || I2C Status || || 0xC2 || Output || I2C Read Request || || 0xD0 ~ 0xDE || Input, Output|| I2C Report || || 0xE0 || Feature || UART Status || || 0xE2 || Feature || UART RI and DCD Status || || 0xF0 ~0xFE ||Input, Output || UART Report || と書いてあり、{{{Feature}}}へのアクセスは{{{send_feature_report, get_feature_report}}}で行い、{{{INPUT,OUTPUT}}}は{{{write,read}}}で行うらしい。 {{{ root@beaglebone:~# ipython --nosep Python 2.7.9 (default, Aug 13 2016, 17:56:53) Type "copyright", "credits" or "license" for more information. IPython 2.3.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import hid In [2]: h = hid.device() In [3]: h.open(0x0403, 0x6030) In [4]: h.get_feature_report(0xA0,13) Out[4]: [160, 2, 96, 2, 0, 2, 96, 1, 0, 1, 1, 3, 0] In [5]: h.get_feature_report(0xA1,64) Out[5]: [161, 1, 2, 0, 1, 1, 0, 0, 1, 3, 6, 1, 1, 12, 1, 0, 0, 0, 0, 0, 172, 0, 188, 0, 204] }}} これでやっと、FT260にアクセスできることが確認できた。[[br]] 次にI2C上のスレーブデバイスにアクセス。[[br]] 接続しているI2Cデバイスは、[http://www.analog.com/jp/products/analog-to-digital-converters/integrated-special-purpose-converters/digital-temperature-sensors/adt7410.html#product-overview ADT7410]を基板化した、[http://akizukidenshi.com/catalog/g/gM-06675/ 秋月電子 ADT7410使用 高精度・高分解能 I2C・16Bit 温度センサモジュール]。 FT260開発ボードの3.3V電源と、IO0,IO1を接続した。 {{{ FT260 <-> ADT7410 3.3V (JP6-1) VDD IO0 (JP6-11) SCL IO1 (JP6-10) SDA GND GND }}} 温度センサのI2Cアドレスは0x48としておく。 {{{ In [1]: import hid In [2]: h = hid.device() In [3]: h.open(0x0403, 0x6030) In [4]: h.set_nonblocking(1) Out[4]: 0 In [5]: h.write([0xC2, 0x48, 0x06, 0x04, 0x00]) Out[5]: 5 In [6]: h.read(64) Out[6]: [208, 4, 13, 88, 128, 0, 50, 0] }}} 先頭2byteはFT260のHIDコマンドヘッダなので、{{{[13, 88, 128, 0, 50, 0]}}}がデータ。[[br]] 取得するデータ数は4byteだけど、6byteデータが出てくる。多分、HIDヘッダも含めて4byte単位でデータが出てくるのだろう。[[br]] ADT7410のデータシートから、{{{[13, 88]}}}が温度データ、次の{{{[128]}}}がStatus、{{{[0]}}}がConfigurationとなっているようだ。[[br]] Configurationの7bit目が1なら16bit,0なら13bitデータになる。[[br]] 13bit時のデータは[15:3]が有効なデータなので、計算式は、 {{{ ADC_DEC = ((HSB<<8) | LSB)>>3 Positive_Temp[℃] = ADC_DEC /16.0 Negative_Temp[℃] = (ADC_DEC - 8192)/16 }}} となるらしい。[[br]] この計算式に当てはめると、{{{LSB=88,HSB=13}}}の場合、{{{26.6875[℃]}}}となる。[[br]] 今回は16bitで使いたいので、Configration Registorの7bit目を1にする必要がある。[[br]]