00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __libftdi_h__
00018 #define __libftdi_h__
00019
00020 #include <stdint.h>
00021 #ifndef _WIN32
00022 #include <sys/time.h>
00023 #endif
00024
00025
00026
00027
00028
00029
00030 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
00031 #include <windows.h>
00032 #if defined(interface)
00033 #undef interface
00034 #endif
00035 #endif
00036
00038 enum ftdi_chip_type
00039 {
00040 TYPE_AM=0,
00041 TYPE_BM=1,
00042 TYPE_2232C=2,
00043 TYPE_R=3,
00044 TYPE_2232H=4,
00045 TYPE_4232H=5,
00046 TYPE_232H=6,
00047 TYPE_230X=7,
00048 };
00050 enum ftdi_parity_type { NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4 };
00052 enum ftdi_stopbits_type { STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2 };
00054 enum ftdi_bits_type { BITS_7=7, BITS_8=8 };
00056 enum ftdi_break_type { BREAK_OFF=0, BREAK_ON=1 };
00057
00059 enum ftdi_mpsse_mode
00060 {
00061 BITMODE_RESET = 0x00,
00062 BITMODE_BITBANG= 0x01,
00063 BITMODE_MPSSE = 0x02,
00064 BITMODE_SYNCBB = 0x04,
00065 BITMODE_MCU = 0x08,
00066
00067 BITMODE_OPTO = 0x10,
00068 BITMODE_CBUS = 0x20,
00069 BITMODE_SYNCFF = 0x40,
00070 BITMODE_FT1284 = 0x80,
00071 };
00072
00074 enum ftdi_interface
00075 {
00076 INTERFACE_ANY = 0,
00077 INTERFACE_A = 1,
00078 INTERFACE_B = 2,
00079 INTERFACE_C = 3,
00080 INTERFACE_D = 4
00081 };
00082
00084 enum ftdi_module_detach_mode
00085 {
00086 AUTO_DETACH_SIO_MODULE = 0,
00087 DONT_DETACH_SIO_MODULE = 1
00088 };
00089
00090
00091 #define MPSSE_WRITE_NEG 0x01
00092 #define MPSSE_BITMODE 0x02
00093 #define MPSSE_READ_NEG 0x04
00094 #define MPSSE_LSB 0x08
00095 #define MPSSE_DO_WRITE 0x10
00096 #define MPSSE_DO_READ 0x20
00097 #define MPSSE_WRITE_TMS 0x40
00098
00099
00100 #define SET_BITS_LOW 0x80
00101
00102
00103 #define SET_BITS_HIGH 0x82
00104
00105
00106 #define GET_BITS_LOW 0x81
00107 #define GET_BITS_HIGH 0x83
00108 #define LOOPBACK_START 0x84
00109 #define LOOPBACK_END 0x85
00110 #define TCK_DIVISOR 0x86
00111
00112 #define DIS_DIV_5 0x8a
00113 #define EN_DIV_5 0x8b
00114 #define EN_3_PHASE 0x8c
00115 #define DIS_3_PHASE 0x8d
00116 #define CLK_BITS 0x8e
00117 #define CLK_BYTES 0x8f
00118 #define CLK_WAIT_HIGH 0x94
00119 #define CLK_WAIT_LOW 0x95
00120 #define EN_ADAPTIVE 0x96
00121 #define DIS_ADAPTIVE 0x97
00122 #define CLK_BYTES_OR_HIGH 0x9c
00123 #define CLK_BYTES_OR_LOW 0x9d
00124
00125 #define DRIVE_OPEN_COLLECTOR 0x9e
00126
00127
00128 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00129
00130
00131 #define SEND_IMMEDIATE 0x87
00132 #define WAIT_ON_HIGH 0x88
00133 #define WAIT_ON_LOW 0x89
00134
00135
00136 #define READ_SHORT 0x90
00137
00138 #define READ_EXTENDED 0x91
00139
00140
00141 #define WRITE_SHORT 0x92
00142
00143 #define WRITE_EXTENDED 0x93
00144
00145
00146
00147
00148 #define SIO_RESET 0
00149 #define SIO_MODEM_CTRL 1
00150 #define SIO_SET_FLOW_CTRL 2
00151 #define SIO_SET_BAUD_RATE 3
00152 #define SIO_SET_DATA 4
00153
00154 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
00155 #define FTDI_DEVICE_IN_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
00156
00157
00158 #define SIO_RESET_REQUEST SIO_RESET
00159 #define SIO_SET_BAUDRATE_REQUEST SIO_SET_BAUD_RATE
00160 #define SIO_SET_DATA_REQUEST SIO_SET_DATA
00161 #define SIO_SET_FLOW_CTRL_REQUEST SIO_SET_FLOW_CTRL
00162 #define SIO_SET_MODEM_CTRL_REQUEST SIO_MODEM_CTRL
00163 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
00164 #define SIO_SET_EVENT_CHAR_REQUEST 0x06
00165 #define SIO_SET_ERROR_CHAR_REQUEST 0x07
00166 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
00167 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
00168 #define SIO_SET_BITMODE_REQUEST 0x0B
00169 #define SIO_READ_PINS_REQUEST 0x0C
00170 #define SIO_READ_EEPROM_REQUEST 0x90
00171 #define SIO_WRITE_EEPROM_REQUEST 0x91
00172 #define SIO_ERASE_EEPROM_REQUEST 0x92
00173
00174
00175 #define SIO_RESET_SIO 0
00176 #define SIO_RESET_PURGE_RX 1
00177 #define SIO_RESET_PURGE_TX 2
00178
00179 #define SIO_DISABLE_FLOW_CTRL 0x0
00180 #define SIO_RTS_CTS_HS (0x1 << 8)
00181 #define SIO_DTR_DSR_HS (0x2 << 8)
00182 #define SIO_XON_XOFF_HS (0x4 << 8)
00183
00184 #define SIO_SET_DTR_MASK 0x1
00185 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK << 8))
00186 #define SIO_SET_DTR_LOW ( 0 | ( SIO_SET_DTR_MASK << 8))
00187 #define SIO_SET_RTS_MASK 0x2
00188 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00189 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00190
00191 #define SIO_RTS_CTS_HS (0x1 << 8)
00192
00193
00194
00195 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00196
00197 #ifdef __GNUC__
00198 #define DEPRECATED(func) func __attribute__ ((deprecated))
00199 #elif defined(_MSC_VER)
00200 #define DEPRECATED(func) __declspec(deprecated) func
00201 #else
00202 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
00203 #define DEPRECATED(func) func
00204 #endif
00205
00206 struct ftdi_transfer_control
00207 {
00208 int completed;
00209 unsigned char *buf;
00210 int size;
00211 int offset;
00212 struct ftdi_context *ftdi;
00213 struct libusb_transfer *transfer;
00214 };
00215
00221 struct ftdi_context
00222 {
00223
00225 struct libusb_context *usb_ctx;
00227 struct libusb_device_handle *usb_dev;
00229 int usb_read_timeout;
00231 int usb_write_timeout;
00232
00233
00235 enum ftdi_chip_type type;
00237 int baudrate;
00239 unsigned char bitbang_enabled;
00241 unsigned char *readbuffer;
00243 unsigned int readbuffer_offset;
00245 unsigned int readbuffer_remaining;
00247 unsigned int readbuffer_chunksize;
00249 unsigned int writebuffer_chunksize;
00251 unsigned int max_packet_size;
00252
00253
00255 int interface;
00257 int index;
00258
00260 int in_ep;
00261 int out_ep;
00262
00264 unsigned char bitbang_mode;
00265
00267 struct ftdi_eeprom *eeprom;
00268
00270 const char *error_str;
00271
00273 enum ftdi_module_detach_mode module_detach_mode;
00274 };
00275
00279 enum ftdi_eeprom_value
00280 {
00281 VENDOR_ID = 0,
00282 PRODUCT_ID = 1,
00283 SELF_POWERED = 2,
00284 REMOTE_WAKEUP = 3,
00285 IS_NOT_PNP = 4,
00286 SUSPEND_DBUS7 = 5,
00287 IN_IS_ISOCHRONOUS = 6,
00288 OUT_IS_ISOCHRONOUS = 7,
00289 SUSPEND_PULL_DOWNS = 8,
00290 USE_SERIAL = 9,
00291 USB_VERSION = 10,
00292 USE_USB_VERSION = 11,
00293 MAX_POWER = 12,
00294 CHANNEL_A_TYPE = 13,
00295 CHANNEL_B_TYPE = 14,
00296 CHANNEL_A_DRIVER = 15,
00297 CHANNEL_B_DRIVER = 16,
00298 CBUS_FUNCTION_0 = 17,
00299 CBUS_FUNCTION_1 = 18,
00300 CBUS_FUNCTION_2 = 19,
00301 CBUS_FUNCTION_3 = 20,
00302 CBUS_FUNCTION_4 = 21,
00303 CBUS_FUNCTION_5 = 22,
00304 CBUS_FUNCTION_6 = 23,
00305 CBUS_FUNCTION_7 = 24,
00306 CBUS_FUNCTION_8 = 25,
00307 CBUS_FUNCTION_9 = 26,
00308 HIGH_CURRENT = 27,
00309 HIGH_CURRENT_A = 28,
00310 HIGH_CURRENT_B = 29,
00311 INVERT = 30,
00312 GROUP0_DRIVE = 31,
00313 GROUP0_SCHMITT = 32,
00314 GROUP0_SLEW = 33,
00315 GROUP1_DRIVE = 34,
00316 GROUP1_SCHMITT = 35,
00317 GROUP1_SLEW = 36,
00318 GROUP2_DRIVE = 37,
00319 GROUP2_SCHMITT = 38,
00320 GROUP2_SLEW = 39,
00321 GROUP3_DRIVE = 40,
00322 GROUP3_SCHMITT = 41,
00323 GROUP3_SLEW = 42,
00324 CHIP_SIZE = 43,
00325 CHIP_TYPE = 44,
00326 POWER_SAVE = 45,
00327 CLOCK_POLARITY = 46,
00328 DATA_ORDER = 47,
00329 FLOW_CONTROL = 48,
00330 CHANNEL_C_DRIVER = 49,
00331 CHANNEL_D_DRIVER = 50,
00332 CHANNEL_A_RS485 = 51,
00333 CHANNEL_B_RS485 = 52,
00334 CHANNEL_C_RS485 = 53,
00335 CHANNEL_D_RS485 = 54,
00336 RELEASE_NUMBER = 55,
00337 EXTERNAL_OSCILLATOR= 56,
00338 USER_DATA_ADDR = 57,
00339 };
00340
00344 struct ftdi_device_list
00345 {
00347 struct ftdi_device_list *next;
00349 struct libusb_device *dev;
00350 };
00351 #define FT1284_CLK_IDLE_STATE 0x01
00352 #define FT1284_DATA_LSB 0x02
00353 #define FT1284_FLOW_CONTROL 0x04
00354 #define POWER_SAVE_DISABLE_H 0x80
00355
00356 #define USE_SERIAL_NUM 0x08
00357 enum ftdi_cbus_func
00358 {
00359 CBUS_TXDEN = 0, CBUS_PWREN = 1, CBUS_RXLED = 2, CBUS_TXLED = 3, CBUS_TXRXLED = 4,
00360 CBUS_SLEEP = 5, CBUS_CLK48 = 6, CBUS_CLK24 = 7, CBUS_CLK12 = 8, CBUS_CLK6 = 9,
00361 CBUS_IOMODE = 0xa, CBUS_BB_WR = 0xb, CBUS_BB_RD = 0xc
00362 };
00363
00364 enum ftdi_cbush_func
00365 {
00366 CBUSH_TRISTATE = 0, CBUSH_TXLED = 1, CBUSH_RXLED = 2, CBUSH_TXRXLED = 3, CBUSH_PWREN = 4,
00367 CBUSH_SLEEP = 5, CBUSH_DRIVE_0 = 6, CBUSH_DRIVE1 = 7, CBUSH_IOMODE = 8, CBUSH_TXDEN = 9,
00368 CBUSH_CLK30 = 10, CBUSH_CLK15 = 11, CBUSH_CLK7_5 = 12
00369 };
00370
00371 enum ftdi_cbusx_func
00372 {
00373 CBUSX_TRISTATE = 0, CBUSX_TXLED = 1, CBUSX_RXLED = 2, CBUSX_TXRXLED = 3, CBUSX_PWREN = 4,
00374 CBUSX_SLEEP = 5, CBUSX_DRIVE_0 = 6, CBUSX_DRIVE1 = 7, CBUSX_IOMODE = 8, CBUSX_TXDEN = 9,
00375 CBUSX_CLK24 = 10, CBUSX_CLK12 = 11, CBUSX_CLK6 = 12, CBUSX_BAT_DETECT = 13,
00376 CBUSX_BAT_DETECT_NEG = 14, CBUSX_I2C_TXE = 15, CBUSX_I2C_RXF = 16, CBUSX_VBUS_SENSE = 17,
00377 CBUSX_BB_WR = 18, CBUSX_BB_RD = 19, CBUSX_TIME_STAMP = 20, CBUSX_AWAKE = 21
00378 };
00379
00381 #define INVERT_TXD 0x01
00382
00383 #define INVERT_RXD 0x02
00384
00385 #define INVERT_RTS 0x04
00386
00387 #define INVERT_CTS 0x08
00388
00389 #define INVERT_DTR 0x10
00390
00391 #define INVERT_DSR 0x20
00392
00393 #define INVERT_DCD 0x40
00394
00395 #define INVERT_RI 0x80
00396
00398 #define CHANNEL_IS_UART 0x0
00399 #define CHANNEL_IS_FIFO 0x1
00400 #define CHANNEL_IS_OPTO 0x2
00401 #define CHANNEL_IS_CPU 0x4
00402 #define CHANNEL_IS_FT1284 0x8
00403
00404 #define CHANNEL_IS_RS485 0x10
00405
00406 #define DRIVE_4MA 0
00407 #define DRIVE_8MA 1
00408 #define DRIVE_12MA 2
00409 #define DRIVE_16MA 3
00410 #define SLOW_SLEW 4
00411 #define IS_SCHMITT 8
00412
00414 #define DRIVER_VCP 0x08
00415 #define DRIVER_VCPH 0x10
00416
00417 #define USE_USB_VERSION_BIT 0x10
00418
00419 #define SUSPEND_DBUS7_BIT 0x80
00420
00422 #define HIGH_CURRENT_DRIVE 0x10
00423 #define HIGH_CURRENT_DRIVE_R 0x04
00424
00428 struct size_and_time
00429 {
00430 uint64_t totalBytes;
00431 struct timeval time;
00432 };
00433
00434 typedef struct
00435 {
00436 struct size_and_time first;
00437 struct size_and_time prev;
00438 struct size_and_time current;
00439 double totalTime;
00440 double totalRate;
00441 double currentRate;
00442 } FTDIProgressInfo;
00443
00444 typedef int (FTDIStreamCallback)(uint8_t *buffer, int length,
00445 FTDIProgressInfo *progress, void *userdata);
00446
00455 struct ftdi_version_info
00456 {
00457 int major;
00458 int minor;
00459 int micro;
00460 const char *version_str;
00461 const char *snapshot_str;
00462 };
00463
00464
00465 #ifdef __cplusplus
00466 extern "C"
00467 {
00468 #endif
00469
00470 int ftdi_init(struct ftdi_context *ftdi);
00471 struct ftdi_context *ftdi_new(void);
00472 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00473
00474 void ftdi_deinit(struct ftdi_context *ftdi);
00475 void ftdi_free(struct ftdi_context *ftdi);
00476 void ftdi_set_usbdev (struct ftdi_context *ftdi, struct libusb_device_handle *usbdev);
00477
00478 struct ftdi_version_info ftdi_get_library_version(void);
00479
00480 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00481 int vendor, int product);
00482 void ftdi_list_free(struct ftdi_device_list **devlist);
00483 void ftdi_list_free2(struct ftdi_device_list *devlist);
00484 int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
00485 char *manufacturer, int mnf_len,
00486 char *description, int desc_len,
00487 char *serial, int serial_len);
00488 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
00489 char *manufacturer, int mnf_len,
00490 char *description, int desc_len,
00491 char *serial, int serial_len);
00492
00493 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
00494 char *manufacturer, int mnf_len,
00495 char *product, int prod_len,
00496 char *serial, int serial_len);
00497 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
00498 char * product, char * serial);
00499
00500 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00501 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00502 const char* description, const char* serial);
00503 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00504 const char* description, const char* serial, unsigned int index);
00505 int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr);
00506 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev);
00507 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
00508
00509 int ftdi_usb_close(struct ftdi_context *ftdi);
00510 int ftdi_usb_reset(struct ftdi_context *ftdi);
00511 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00512 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00513 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00514
00515 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00516 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00517 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00518 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00519 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00520 enum ftdi_break_type break_type);
00521
00522 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00523 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00524 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00525
00526 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size);
00527 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00528 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00529
00530 int ftdi_readstream(struct ftdi_context *ftdi, FTDIStreamCallback *callback,
00531 void *userdata, int packetsPerTransfer, int numTransfers);
00532 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
00533
00534 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
00535 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc);
00536 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, struct timeval * to);
00537
00538 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00539 int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00540 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00541
00542 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00543 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00544
00545 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00546
00547
00548 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00549 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00550 int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00551 int ftdi_setrts(struct ftdi_context *ftdi, int state);
00552
00553 int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00554 int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00555
00556
00557 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi,
00558 char * manufacturer, char *product,
00559 char * serial);
00560 int ftdi_eeprom_build(struct ftdi_context *ftdi);
00561 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose);
00562
00563 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
00564 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value);
00565
00566 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
00567 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size);
00568
00569 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size);
00570
00571 int ftdi_read_eeprom(struct ftdi_context *ftdi);
00572 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00573 int ftdi_write_eeprom(struct ftdi_context *ftdi);
00574 int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00575
00576 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
00577 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
00578
00579 const char *ftdi_get_error_string(struct ftdi_context *ftdi);
00580
00581 #ifdef __cplusplus
00582 }
00583 #endif
00584
00585 #endif