libftdi  0.20
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 by Intra2net AG
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 <usb.h>
00021 
00022 #define FTDI_DEFAULT_EEPROM_SIZE 128
00023 
00025 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 };
00027 enum ftdi_parity_type { NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4 };
00029 enum ftdi_stopbits_type { STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2 };
00031 enum ftdi_bits_type { BITS_7=7, BITS_8=8 };
00033 enum ftdi_break_type { BREAK_OFF=0, BREAK_ON=1 };
00034 
00036 enum ftdi_mpsse_mode
00037 {
00038     BITMODE_RESET  = 0x00,    
00039     BITMODE_BITBANG= 0x01,    
00040     BITMODE_MPSSE  = 0x02,    
00041     BITMODE_SYNCBB = 0x04,    
00042     BITMODE_MCU    = 0x08,    
00043                               /* CPU-style fifo mode gets set via EEPROM */
00044     BITMODE_OPTO   = 0x10,    
00045     BITMODE_CBUS   = 0x20,    
00046     BITMODE_SYNCFF = 0x40,    
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 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT)
00131 #define FTDI_DEVICE_IN_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_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 
00188 struct ftdi_context
00189 {
00190     /* USB specific */
00192     struct usb_dev_handle *usb_dev;
00194     int usb_read_timeout;
00196     int usb_write_timeout;
00197 
00198     /* FTDI specific */
00200     enum ftdi_chip_type type;
00202     int baudrate;
00204     unsigned char bitbang_enabled;
00206     unsigned char *readbuffer;
00208     unsigned int readbuffer_offset;
00210     unsigned int readbuffer_remaining;
00212     unsigned int readbuffer_chunksize;
00214     unsigned int writebuffer_chunksize;
00216     unsigned int max_packet_size;
00217 
00218     /* FTDI FT2232C requirecments */
00220     int interface;   /* 0 or 1 */
00222     int index;       /* 1 or 2 */
00223     /* Endpoints */
00225     int in_ep;
00226     int out_ep;      /* 1 or 2 */
00227 
00229     unsigned char bitbang_mode;
00230 
00232     int eeprom_size;
00233 
00235     char *error_str;
00236 
00238     char *async_usb_buffer;
00240     unsigned int async_usb_buffer_size;
00241 
00243     enum ftdi_module_detach_mode module_detach_mode;
00244 };
00245 
00249 struct ftdi_device_list
00250 {
00252     struct ftdi_device_list *next;
00254     struct usb_device *dev;
00255 };
00256 
00257 
00259 #define CBUS_TXDEN 0
00260 
00261 #define CBUS_PWREN 1
00262 
00263 #define CBUS_RXLED 2
00264 
00265 #define CBUS_TXLED 3
00266 
00267 #define CBUS_TXRXLED 4
00268 
00269 #define CBUS_SLEEP 5
00270 
00271 #define CBUS_CLK48 6
00272 
00273 #define CBUS_CLK24 7
00274 
00275 #define CBUS_CLK12 8
00276 
00277 #define CBUS_CLK6 9
00278 
00279 #define CBUS_IOMODE 10
00280 
00281 #define CBUS_BB_WR 11
00282 
00283 #define CBUS_BB_RD 12
00284 
00285 
00287 #define INVERT_TXD 0x01
00288 
00289 #define INVERT_RXD 0x02
00290 
00291 #define INVERT_RTS 0x04
00292 
00293 #define INVERT_CTS 0x08
00294 
00295 #define INVERT_DTR 0x10
00296 
00297 #define INVERT_DSR 0x20
00298 
00299 #define INVERT_DCD 0x40
00300 
00301 #define INVERT_RI  0x80
00302 
00304 #define HIGH_CURRENT_DRIVE 0x04
00305 
00309 struct ftdi_eeprom
00310 {
00312     int vendor_id;
00314     int product_id;
00315 
00317     int self_powered;
00319     int remote_wakeup;
00321     int chip_type;
00322 
00324     int in_is_isochronous;
00326     int out_is_isochronous;
00328     int suspend_pull_downs;
00329 
00331     int use_serial;
00333     int change_usb_version;
00335     int usb_version;
00337     int max_power;
00338 
00340     char *manufacturer;
00342     char *product;
00344     char *serial;
00345 
00346     /* Special function of FT232R devices (and possibly others as well) */
00348     int cbus_function[5];
00350     int high_current;
00352     int invert;
00353 
00356     int size;
00357 };
00358 
00359 #ifdef __cplusplus
00360 extern "C"
00361 {
00362 #endif
00363 
00364     int ftdi_init(struct ftdi_context *ftdi);
00365     struct ftdi_context *ftdi_new(void);
00366     int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00367 
00368     void ftdi_deinit(struct ftdi_context *ftdi);
00369     void ftdi_free(struct ftdi_context *ftdi);
00370     void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00371 
00372     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00373                           int vendor, int product);
00374     void ftdi_list_free(struct ftdi_device_list **devlist);
00375     void ftdi_list_free2(struct ftdi_device_list *devlist);
00376     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00377                              char * manufacturer, int mnf_len,
00378                              char * description, int desc_len,
00379                              char * serial, int serial_len);
00380 
00381     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00382     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00383                            const char* description, const char* serial);
00384     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00385                            const char* description, const char* serial, unsigned int index);
00386     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00387     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
00388 
00389     int ftdi_usb_close(struct ftdi_context *ftdi);
00390     int ftdi_usb_reset(struct ftdi_context *ftdi);
00391     int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00392     int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00393     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00394 
00395     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00396     int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00397                                enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00398     int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00399                                 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00400                                 enum ftdi_break_type break_type);
00401 
00402     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00403     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00404     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00405 
00406     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00407     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00408     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00409 
00410     int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00411     void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00412 
00413     int DEPRECATED(ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask));
00414     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00415     int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00416     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00417 
00418     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00419     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00420 
00421     int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00422 
00423     /* flow control */
00424     int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00425     int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00426     int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00427     int ftdi_setrts(struct ftdi_context *ftdi, int state);
00428 
00429     int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00430     int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00431 
00432     /* set eeprom size */
00433     void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00434 
00435     /* init and build eeprom from ftdi_eeprom structure */
00436     void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00437     void ftdi_eeprom_free(struct ftdi_eeprom *eeprom);
00438     int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00439     int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *output, int size);
00440 
00441     /* "eeprom" needs to be valid 128 byte eeprom (generated by the eeprom generator)
00442        the checksum of the eeprom is valided */
00443     int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00444     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00445     int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00446     int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00447     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00448 
00449     int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
00450     int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
00451 
00452     char *ftdi_get_error_string(struct ftdi_context *ftdi);
00453 
00454 #ifdef __cplusplus
00455 }
00456 #endif
00457 
00458 #endif /* __libftdi_h__ */