libftdi  0.19
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 };
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 /* Value Low */
00088 /* Value HIGH */ /*rate is 12000000/((1+value)*2) */
00089 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00090 
00091 /* Commands in MPSSE and Host Emulation Mode */
00092 #define SEND_IMMEDIATE 0x87
00093 #define WAIT_ON_HIGH   0x88
00094 #define WAIT_ON_LOW    0x89
00095 
00096 /* Commands in Host Emulation Mode */
00097 #define READ_SHORT     0x90
00098 /* Address_Low */
00099 #define READ_EXTENDED  0x91
00100 /* Address High */
00101 /* Address Low  */
00102 #define WRITE_SHORT    0x92
00103 /* Address_Low */
00104 #define WRITE_EXTENDED 0x93
00105 /* Address High */
00106 /* Address Low  */
00107 
00108 /* Definitions for flow control */
00109 #define SIO_RESET          0 /* Reset the port */
00110 #define SIO_MODEM_CTRL     1 /* Set the modem control register */
00111 #define SIO_SET_FLOW_CTRL  2 /* Set flow control register */
00112 #define SIO_SET_BAUD_RATE  3 /* Set baud rate */
00113 #define SIO_SET_DATA       4 /* Set the data characteristics of the port */
00114 
00115 #define FTDI_DEVICE_OUT_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT)
00116 #define FTDI_DEVICE_IN_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN)
00117 
00118 /* Requests */
00119 #define SIO_RESET_REQUEST             SIO_RESET
00120 #define SIO_SET_BAUDRATE_REQUEST      SIO_SET_BAUD_RATE
00121 #define SIO_SET_DATA_REQUEST          SIO_SET_DATA
00122 #define SIO_SET_FLOW_CTRL_REQUEST     SIO_SET_FLOW_CTRL
00123 #define SIO_SET_MODEM_CTRL_REQUEST    SIO_MODEM_CTRL
00124 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
00125 #define SIO_SET_EVENT_CHAR_REQUEST    0x06
00126 #define SIO_SET_ERROR_CHAR_REQUEST    0x07
00127 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
00128 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
00129 #define SIO_SET_BITMODE_REQUEST       0x0B
00130 #define SIO_READ_PINS_REQUEST         0x0C
00131 #define SIO_READ_EEPROM_REQUEST       0x90
00132 #define SIO_WRITE_EEPROM_REQUEST      0x91
00133 #define SIO_ERASE_EEPROM_REQUEST      0x92
00134 
00135 
00136 #define SIO_RESET_SIO 0
00137 #define SIO_RESET_PURGE_RX 1
00138 #define SIO_RESET_PURGE_TX 2
00139 
00140 #define SIO_DISABLE_FLOW_CTRL 0x0
00141 #define SIO_RTS_CTS_HS (0x1 << 8)
00142 #define SIO_DTR_DSR_HS (0x2 << 8)
00143 #define SIO_XON_XOFF_HS (0x4 << 8)
00144 
00145 #define SIO_SET_DTR_MASK 0x1
00146 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK  << 8))
00147 #define SIO_SET_DTR_LOW  ( 0 | ( SIO_SET_DTR_MASK  << 8))
00148 #define SIO_SET_RTS_MASK 0x2
00149 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00150 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00151 
00152 #define SIO_RTS_CTS_HS (0x1 << 8)
00153 
00154 /* marker for unused usb urb structures
00155    (taken from libusb) */
00156 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00157 
00158 #ifdef __GNUC__
00159     #define DEPRECATED(func) func __attribute__ ((deprecated))
00160 #elif defined(_MSC_VER)
00161     #define DEPRECATED(func) __declspec(deprecated) func
00162 #else
00163     #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
00164     #define DEPRECATED(func) func
00165 #endif
00166 
00167 
00173 struct ftdi_context
00174 {
00175     /* USB specific */
00177     struct usb_dev_handle *usb_dev;
00179     int usb_read_timeout;
00181     int usb_write_timeout;
00182 
00183     /* FTDI specific */
00185     enum ftdi_chip_type type;
00187     int baudrate;
00189     unsigned char bitbang_enabled;
00191     unsigned char *readbuffer;
00193     unsigned int readbuffer_offset;
00195     unsigned int readbuffer_remaining;
00197     unsigned int readbuffer_chunksize;
00199     unsigned int writebuffer_chunksize;
00201     unsigned int max_packet_size;
00202 
00203     /* FTDI FT2232C requirecments */
00205     int interface;   /* 0 or 1 */
00207     int index;       /* 1 or 2 */
00208     /* Endpoints */
00210     int in_ep;
00211     int out_ep;      /* 1 or 2 */
00212 
00214     unsigned char bitbang_mode;
00215 
00217     int eeprom_size;
00218 
00220     char *error_str;
00221 
00223     char *async_usb_buffer;
00225     unsigned int async_usb_buffer_size;
00226 
00228     enum ftdi_module_detach_mode module_detach_mode;
00229 };
00230 
00234 struct ftdi_device_list
00235 {
00237     struct ftdi_device_list *next;
00239     struct usb_device *dev;
00240 };
00241 
00242 
00244 #define CBUS_TXDEN 0
00245 
00246 #define CBUS_PWREN 1
00247 
00248 #define CBUS_RXLED 2
00249 
00250 #define CBUS_TXLED 3
00251 
00252 #define CBUS_TXRXLED 4
00253 
00254 #define CBUS_SLEEP 5
00255 
00256 #define CBUS_CLK48 6
00257 
00258 #define CBUS_CLK24 7
00259 
00260 #define CBUS_CLK12 8
00261 
00262 #define CBUS_CLK6 9
00263 
00264 #define CBUS_IOMODE 10
00265 
00266 #define CBUS_BB_WR 11
00267 
00268 #define CBUS_BB_RD 12
00269 
00270 
00272 #define INVERT_TXD 0x01
00273 
00274 #define INVERT_RXD 0x02
00275 
00276 #define INVERT_RTS 0x04
00277 
00278 #define INVERT_CTS 0x08
00279 
00280 #define INVERT_DTR 0x10
00281 
00282 #define INVERT_DSR 0x20
00283 
00284 #define INVERT_DCD 0x40
00285 
00286 #define INVERT_RI  0x80
00287 
00289 #define HIGH_CURRENT_DRIVE 0x04
00290 
00294 struct ftdi_eeprom
00295 {
00297     int vendor_id;
00299     int product_id;
00300 
00302     int self_powered;
00304     int remote_wakeup;
00306     int chip_type;
00307 
00309     int in_is_isochronous;
00311     int out_is_isochronous;
00313     int suspend_pull_downs;
00314 
00316     int use_serial;
00318     int change_usb_version;
00320     int usb_version;
00322     int max_power;
00323 
00325     char *manufacturer;
00327     char *product;
00329     char *serial;
00330 
00331     /* Special function of FT232R devices (and possibly others as well) */
00333     int cbus_function[5];
00335     int high_current;
00337     int invert;
00338 
00341     int size;
00342 };
00343 
00344 #ifdef __cplusplus
00345 extern "C"
00346 {
00347 #endif
00348 
00349     int ftdi_init(struct ftdi_context *ftdi);
00350     struct ftdi_context *ftdi_new(void);
00351     int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00352 
00353     void ftdi_deinit(struct ftdi_context *ftdi);
00354     void ftdi_free(struct ftdi_context *ftdi);
00355     void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00356 
00357     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00358                           int vendor, int product);
00359     void ftdi_list_free(struct ftdi_device_list **devlist);
00360     void ftdi_list_free2(struct ftdi_device_list *devlist);
00361     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00362                              char * manufacturer, int mnf_len,
00363                              char * description, int desc_len,
00364                              char * serial, int serial_len);
00365 
00366     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00367     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00368                            const char* description, const char* serial);
00369     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00370                            const char* description, const char* serial, unsigned int index);
00371     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00372     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
00373 
00374     int ftdi_usb_close(struct ftdi_context *ftdi);
00375     int ftdi_usb_reset(struct ftdi_context *ftdi);
00376     int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00377     int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00378     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00379 
00380     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00381     int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00382                                enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00383     int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00384                                 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00385                                 enum ftdi_break_type break_type);
00386 
00387     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00388     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00389     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00390 
00391     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00392     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00393     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00394 
00395     int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00396     void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00397 
00398     int DEPRECATED(ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask));
00399     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00400     int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00401     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00402 
00403     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00404     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00405 
00406     int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00407 
00408     /* flow control */
00409     int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00410     int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00411     int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00412     int ftdi_setrts(struct ftdi_context *ftdi, int state);
00413 
00414     int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00415     int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00416 
00417     /* set eeprom size */
00418     void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00419 
00420     /* init and build eeprom from ftdi_eeprom structure */
00421     void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00422     void ftdi_eeprom_free(struct ftdi_eeprom *eeprom);
00423     int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00424     int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *output, int size);
00425 
00426     /* "eeprom" needs to be valid 128 byte eeprom (generated by the eeprom generator)
00427        the checksum of the eeprom is valided */
00428     int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00429     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00430     int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00431     int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00432     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00433 
00434     int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
00435     int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
00436 
00437     char *ftdi_get_error_string(struct ftdi_context *ftdi);
00438 
00439 #ifdef __cplusplus
00440 }
00441 #endif
00442 
00443 #endif /* __libftdi_h__ */