libftdi1  1.0
ftdi.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           ftdi.h  -  description
00003                              -------------------
00004     begin                : Fri Apr 4 2003
00005     copyright            : (C) 2003-2013 by Intra2net AG and the libftdi developers
00006     email                : opensource@intra2net.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU Lesser General Public License           *
00013  *   version 2.1 as published by the Free Software Foundation;             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #ifndef __libftdi_h__
00018 #define __libftdi_h__
00019 
00020 #include <stdint.h>
00021 #include <sys/time.h>
00022 
00024 enum ftdi_chip_type { TYPE_AM=0, TYPE_BM=1, TYPE_2232C=2, TYPE_R=3, TYPE_2232H=4, TYPE_4232H=5, TYPE_232H=6 };
00026 enum ftdi_parity_type { NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4 };
00028 enum ftdi_stopbits_type { STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2 };
00030 enum ftdi_bits_type { BITS_7=7, BITS_8=8 };
00032 enum ftdi_break_type { BREAK_OFF=0, BREAK_ON=1 };
00033 
00035 enum ftdi_mpsse_mode
00036 {
00037     BITMODE_RESET  = 0x00,    
00038     BITMODE_BITBANG= 0x01,    
00039     BITMODE_MPSSE  = 0x02,    
00040     BITMODE_SYNCBB = 0x04,    
00041     BITMODE_MCU    = 0x08,    
00042                               /* CPU-style fifo mode gets set via EEPROM */
00043     BITMODE_OPTO   = 0x10,    
00044     BITMODE_CBUS   = 0x20,    
00045     BITMODE_SYNCFF = 0x40,    
00046     BITMODE_FT1284 = 0x80,    
00047 };
00048 
00050 enum ftdi_interface
00051 {
00052     INTERFACE_ANY = 0,
00053     INTERFACE_A   = 1,
00054     INTERFACE_B   = 2,
00055     INTERFACE_C   = 3,
00056     INTERFACE_D   = 4
00057 };
00058 
00060 enum ftdi_module_detach_mode
00061 {
00062     AUTO_DETACH_SIO_MODULE = 0,
00063     DONT_DETACH_SIO_MODULE = 1
00064 };
00065 
00066 /* Shifting commands IN MPSSE Mode*/
00067 #define MPSSE_WRITE_NEG 0x01   /* Write TDI/DO on negative TCK/SK edge*/
00068 #define MPSSE_BITMODE   0x02   /* Write bits, not bytes */
00069 #define MPSSE_READ_NEG  0x04   /* Sample TDO/DI on negative TCK/SK edge */
00070 #define MPSSE_LSB       0x08   /* LSB first */
00071 #define MPSSE_DO_WRITE  0x10   /* Write TDI/DO */
00072 #define MPSSE_DO_READ   0x20   /* Read TDO/DI */
00073 #define MPSSE_WRITE_TMS 0x40   /* Write TMS/CS */
00074 
00075 /* FTDI MPSSE commands */
00076 #define SET_BITS_LOW   0x80
00077 /*BYTE DATA*/
00078 /*BYTE Direction*/
00079 #define SET_BITS_HIGH  0x82
00080 /*BYTE DATA*/
00081 /*BYTE Direction*/
00082 #define GET_BITS_LOW   0x81
00083 #define GET_BITS_HIGH  0x83
00084 #define LOOPBACK_START 0x84
00085 #define LOOPBACK_END   0x85
00086 #define TCK_DIVISOR    0x86
00087 /* H Type specific commands */
00088 #define DIS_DIV_5       0x8a
00089 #define EN_DIV_5        0x8b
00090 #define EN_3_PHASE      0x8c
00091 #define DIS_3_PHASE     0x8d
00092 #define CLK_BITS        0x8e
00093 #define CLK_BYTES       0x8f
00094 #define CLK_WAIT_HIGH   0x94
00095 #define CLK_WAIT_LOW    0x95
00096 #define EN_ADAPTIVE     0x96
00097 #define DIS_ADAPTIVE    0x97
00098 #define CLK_BYTES_OR_HIGH 0x9c
00099 #define CLK_BYTES_OR_LOW  0x0d
00100 /*FT232H specific commands */
00101 #define DRIVE_OPEN_COLLECTOR 0x9e
00102 /* Value Low */
00103 /* Value HIGH */ /*rate is 12000000/((1+value)*2) */
00104 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00105 
00106 /* Commands in MPSSE and Host Emulation Mode */
00107 #define SEND_IMMEDIATE 0x87
00108 #define WAIT_ON_HIGH   0x88
00109 #define WAIT_ON_LOW    0x89
00110 
00111 /* Commands in Host Emulation Mode */
00112 #define READ_SHORT     0x90
00113 /* Address_Low */
00114 #define READ_EXTENDED  0x91
00115 /* Address High */
00116 /* Address Low  */
00117 #define WRITE_SHORT    0x92
00118 /* Address_Low */
00119 #define WRITE_EXTENDED 0x93
00120 /* Address High */
00121 /* Address Low  */
00122 
00123 /* Definitions for flow control */
00124 #define SIO_RESET          0 /* Reset the port */
00125 #define SIO_MODEM_CTRL     1 /* Set the modem control register */
00126 #define SIO_SET_FLOW_CTRL  2 /* Set flow control register */
00127 #define SIO_SET_BAUD_RATE  3 /* Set baud rate */
00128 #define SIO_SET_DATA       4 /* Set the data characteristics of the port */
00129 
00130 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
00131 #define FTDI_DEVICE_IN_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
00132 
00133 /* Requests */
00134 #define SIO_RESET_REQUEST             SIO_RESET
00135 #define SIO_SET_BAUDRATE_REQUEST      SIO_SET_BAUD_RATE
00136 #define SIO_SET_DATA_REQUEST          SIO_SET_DATA
00137 #define SIO_SET_FLOW_CTRL_REQUEST     SIO_SET_FLOW_CTRL
00138 #define SIO_SET_MODEM_CTRL_REQUEST    SIO_MODEM_CTRL
00139 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
00140 #define SIO_SET_EVENT_CHAR_REQUEST    0x06
00141 #define SIO_SET_ERROR_CHAR_REQUEST    0x07
00142 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
00143 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
00144 #define SIO_SET_BITMODE_REQUEST       0x0B
00145 #define SIO_READ_PINS_REQUEST         0x0C
00146 #define SIO_READ_EEPROM_REQUEST       0x90
00147 #define SIO_WRITE_EEPROM_REQUEST      0x91
00148 #define SIO_ERASE_EEPROM_REQUEST      0x92
00149 
00150 
00151 #define SIO_RESET_SIO 0
00152 #define SIO_RESET_PURGE_RX 1
00153 #define SIO_RESET_PURGE_TX 2
00154 
00155 #define SIO_DISABLE_FLOW_CTRL 0x0
00156 #define SIO_RTS_CTS_HS (0x1 << 8)
00157 #define SIO_DTR_DSR_HS (0x2 << 8)
00158 #define SIO_XON_XOFF_HS (0x4 << 8)
00159 
00160 #define SIO_SET_DTR_MASK 0x1
00161 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK  << 8))
00162 #define SIO_SET_DTR_LOW  ( 0 | ( SIO_SET_DTR_MASK  << 8))
00163 #define SIO_SET_RTS_MASK 0x2
00164 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00165 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00166 
00167 #define SIO_RTS_CTS_HS (0x1 << 8)
00168 
00169 /* marker for unused usb urb structures
00170    (taken from libusb) */
00171 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00172 
00173 #ifdef __GNUC__
00174     #define DEPRECATED(func) func __attribute__ ((deprecated))
00175 #elif defined(_MSC_VER)
00176     #define DEPRECATED(func) __declspec(deprecated) func
00177 #else
00178     #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
00179     #define DEPRECATED(func) func
00180 #endif
00181 
00182 struct ftdi_transfer_control
00183 {
00184     int completed;
00185     unsigned char *buf;
00186     int size;
00187     int offset;
00188     struct ftdi_context *ftdi;
00189     struct libusb_transfer *transfer;
00190 };
00191 
00197 struct ftdi_context
00198 {
00199     /* USB specific */
00201     struct libusb_context *usb_ctx;
00203     struct libusb_device_handle *usb_dev;
00205     int usb_read_timeout;
00207     int usb_write_timeout;
00208 
00209     /* FTDI specific */
00211     enum ftdi_chip_type type;
00213     int baudrate;
00215     unsigned char bitbang_enabled;
00217     unsigned char *readbuffer;
00219     unsigned int readbuffer_offset;
00221     unsigned int readbuffer_remaining;
00223     unsigned int readbuffer_chunksize;
00225     unsigned int writebuffer_chunksize;
00227     unsigned int max_packet_size;
00228 
00229     /* FTDI FT2232C requirecments */
00231     int interface;   /* 0 or 1 */
00233     int index;       /* 1 or 2 */
00234     /* Endpoints */
00236     int in_ep;
00237     int out_ep;      /* 1 or 2 */
00238 
00240     unsigned char bitbang_mode;
00241 
00243     struct ftdi_eeprom *eeprom;
00244 
00246     char *error_str;
00247 
00249     enum ftdi_module_detach_mode module_detach_mode;
00250 };
00251 
00255 enum ftdi_eeprom_value
00256 {
00257     VENDOR_ID          = 0,
00258     PRODUCT_ID         = 1,
00259     SELF_POWERED       = 2,
00260     REMOTE_WAKEUP      = 3,
00261     IS_NOT_PNP         = 4,
00262     SUSPEND_DBUS7      = 5,
00263     IN_IS_ISOCHRONOUS  = 6,
00264     OUT_IS_ISOCHRONOUS = 7,
00265     SUSPEND_PULL_DOWNS = 8,
00266     USE_SERIAL         = 9,
00267     USB_VERSION        = 10,
00268     USE_USB_VERSION    = 11,
00269     MAX_POWER          = 12,
00270     CHANNEL_A_TYPE     = 13,
00271     CHANNEL_B_TYPE     = 14,
00272     CHANNEL_A_DRIVER   = 15,
00273     CHANNEL_B_DRIVER   = 16,
00274     CBUS_FUNCTION_0    = 17,
00275     CBUS_FUNCTION_1    = 18,
00276     CBUS_FUNCTION_2    = 19,
00277     CBUS_FUNCTION_3    = 20,
00278     CBUS_FUNCTION_4    = 21,
00279     CBUS_FUNCTION_5    = 22,
00280     CBUS_FUNCTION_6    = 23,
00281     CBUS_FUNCTION_7    = 24,
00282     CBUS_FUNCTION_8    = 25,
00283     CBUS_FUNCTION_9    = 26,
00284     HIGH_CURRENT       = 27,
00285     HIGH_CURRENT_A     = 28,
00286     HIGH_CURRENT_B     = 29,
00287     INVERT             = 30,
00288     GROUP0_DRIVE       = 31,
00289     GROUP0_SCHMITT     = 32,
00290     GROUP0_SLEW        = 33,
00291     GROUP1_DRIVE       = 34,
00292     GROUP1_SCHMITT     = 35,
00293     GROUP1_SLEW        = 36,
00294     GROUP2_DRIVE       = 37,
00295     GROUP2_SCHMITT     = 38,
00296     GROUP2_SLEW        = 39,
00297     GROUP3_DRIVE       = 40,
00298     GROUP3_SCHMITT     = 41,
00299     GROUP3_SLEW        = 42,
00300     CHIP_SIZE          = 43,
00301     CHIP_TYPE          = 44,
00302     POWER_SAVE         = 45,
00303     CLOCK_POLARITY     = 46,
00304     DATA_ORDER         = 47,
00305     FLOW_CONTROL       = 48,
00306     CHANNEL_C_DRIVER   = 49,
00307     CHANNEL_D_DRIVER   = 50,
00308     CHANNEL_A_RS485    = 51,
00309     CHANNEL_B_RS485    = 52,
00310     CHANNEL_C_RS485    = 53,
00311     CHANNEL_D_RS485    = 54,
00312 };
00313 
00317 struct ftdi_device_list
00318 {
00320     struct ftdi_device_list *next;
00322     struct libusb_device *dev;
00323 };
00324 #define FT1284_CLK_IDLE_STATE 0x01
00325 #define FT1284_DATA_LSB       0x02 /* DS_FT232H 1.3 amd ftd2xx.h 1.0.4 disagree here*/
00326 #define FT1284_FLOW_CONTROL   0x04
00327 #define POWER_SAVE_DISABLE_H 0x80
00328 
00329 #define USE_SERIAL_NUM 0x08
00330 enum ftdi_cbus_func {/* FIXME: Recheck value, especially the last */
00331     CBUS_TXDEN = 0, CBUS_PWREN = 1, CBUS_RXLED = 2, CBUS_TXLED = 3, CBUS_TXRXLED = 4,
00332     CBUS_SLEEP = 5, CBUS_CLK48 = 6, CBUS_CLK24 = 7, CBUS_CLK12 = 8, CBUS_CLK6 =  9,
00333     CBUS_IOMODE = 0xa, CBUS_BB_WR = 0xb, CBUS_BB_RD = 0xc, CBUS_BB   = 0xd};
00334 
00335 enum ftdi_cbush_func {/* FIXME: Recheck value, especially the last */
00336     CBUSH_TRISTATE = 0, CBUSH_RXLED = 1, CBUSH_TXLED = 2, CBUSH_TXRXLED = 3, CBUSH_PWREN = 4,
00337     CBUSH_SLEEP = 5, CBUSH_DRIVE_0 = 6, CBUSG_DRIVE1 = 7, CBUSH_IOMODE = 8, CBUSH_TXDEN =  9,
00338     CBUSH_CLK30 = 0xa, CBUSH_CLK15 = 0xb, CBUSH_CLK7_5 = 0xc};
00339 
00341 #define INVERT_TXD 0x01
00342 
00343 #define INVERT_RXD 0x02
00344 
00345 #define INVERT_RTS 0x04
00346 
00347 #define INVERT_CTS 0x08
00348 
00349 #define INVERT_DTR 0x10
00350 
00351 #define INVERT_DSR 0x20
00352 
00353 #define INVERT_DCD 0x40
00354 
00355 #define INVERT_RI  0x80
00356 
00358 #define CHANNEL_IS_UART 0x0
00359 #define CHANNEL_IS_FIFO 0x1
00360 #define CHANNEL_IS_OPTO 0x2
00361 #define CHANNEL_IS_CPU  0x4
00362 #define CHANNEL_IS_FT1284 0x8
00363 
00364 #define CHANNEL_IS_RS485 0x10
00365 
00366 #define DRIVE_4MA  0
00367 #define DRIVE_8MA  1
00368 #define DRIVE_12MA 2
00369 #define DRIVE_16MA 3
00370 #define SLOW_SLEW  4
00371 #define IS_SCHMITT 8
00372 
00374 #define DRIVER_VCP 0x08
00375 #define DRIVER_VCPH 0x10 /* FT232H has moved the VCP bit */
00376 
00377 #define USE_USB_VERSION_BIT 0x10
00378 
00379 #define SUSPEND_DBUS7_BIT 0x80
00380 
00382 #define HIGH_CURRENT_DRIVE   0x10
00383 #define HIGH_CURRENT_DRIVE_R 0x04
00384 
00388 struct size_and_time
00389 {
00390         uint64_t totalBytes;
00391         struct timeval time;
00392 };
00393 
00394 typedef struct
00395 {
00396     struct size_and_time first;
00397     struct size_and_time prev;
00398     struct size_and_time current;
00399     double totalTime;
00400     double totalRate;
00401     double currentRate;
00402 } FTDIProgressInfo;
00403 
00404 typedef int (FTDIStreamCallback)(uint8_t *buffer, int length,
00405                                  FTDIProgressInfo *progress, void *userdata);
00406 
00415 struct ftdi_version_info
00416 {
00417     int major;
00418     int minor;
00419     int micro;
00420     const char *version_str;
00421     const char *snapshot_str;
00422 };
00423 
00424 
00425 #ifdef __cplusplus
00426 extern "C"
00427 {
00428 #endif
00429 
00430     int ftdi_init(struct ftdi_context *ftdi);
00431     struct ftdi_context *ftdi_new(void);
00432     int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00433 
00434     void ftdi_deinit(struct ftdi_context *ftdi);
00435     void ftdi_free(struct ftdi_context *ftdi);
00436     void ftdi_set_usbdev (struct ftdi_context *ftdi, struct libusb_device_handle *usbdev);
00437 
00438     struct ftdi_version_info ftdi_get_library_version();
00439 
00440     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00441                           int vendor, int product);
00442     void ftdi_list_free(struct ftdi_device_list **devlist);
00443     void ftdi_list_free2(struct ftdi_device_list *devlist);
00444     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
00445                              char * manufacturer, int mnf_len,
00446                              char * description, int desc_len,
00447                              char * serial, int serial_len);
00448 
00449     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00450     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00451                            const char* description, const char* serial);
00452     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00453                            const char* description, const char* serial, unsigned int index);
00454     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev);
00455     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
00456 
00457     int ftdi_usb_close(struct ftdi_context *ftdi);
00458     int ftdi_usb_reset(struct ftdi_context *ftdi);
00459     int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00460     int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00461     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00462 
00463     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00464     int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00465                                enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00466     int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00467                                 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00468                                 enum ftdi_break_type break_type);
00469 
00470     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00471     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00472     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00473 
00474     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00475     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00476     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00477 
00478     int ftdi_readstream(struct ftdi_context *ftdi, FTDIStreamCallback *callback,
00479                         void *userdata, int packetsPerTransfer, int numTransfers);
00480     struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
00481 
00482     struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
00483     int ftdi_transfer_data_done(struct ftdi_transfer_control *tc);
00484 
00485     int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00486     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00487     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00488 
00489     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00490     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00491 
00492     int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00493 
00494     /* flow control */
00495     int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00496     int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00497     int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00498     int ftdi_setrts(struct ftdi_context *ftdi, int state);
00499 
00500     int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00501     int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00502 
00503     /* init eeprom for the given FTDI type */
00504     int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, 
00505                                   char * manufacturer, char *product, 
00506                                   char * serial);
00507     int ftdi_eeprom_build(struct ftdi_context *ftdi);
00508     int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose);
00509 
00510     int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
00511     int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int  value);
00512 
00513     int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
00514     int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size);
00515 
00516     int ftdi_read_eeprom(struct ftdi_context *ftdi);
00517     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00518     int ftdi_write_eeprom(struct ftdi_context *ftdi);
00519     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00520 
00521     int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
00522     int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
00523 
00524     char *ftdi_get_error_string(struct ftdi_context *ftdi);
00525 
00526 #ifdef __cplusplus
00527 }
00528 #endif
00529 
00530 #endif /* __libftdi_h__ */