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 };
00047 
00049 enum ftdi_interface
00050 {
00051     INTERFACE_ANY = 0,
00052     INTERFACE_A   = 1,
00053     INTERFACE_B   = 2,
00054     INTERFACE_C   = 3,
00055     INTERFACE_D   = 4
00056 };
00057 
00058 /* Shifting commands IN MPSSE Mode*/
00059 #define MPSSE_WRITE_NEG 0x01   /* Write TDI/DO on negative TCK/SK edge*/
00060 #define MPSSE_BITMODE   0x02   /* Write bits, not bytes */
00061 #define MPSSE_READ_NEG  0x04   /* Sample TDO/DI on negative TCK/SK edge */
00062 #define MPSSE_LSB       0x08   /* LSB first */
00063 #define MPSSE_DO_WRITE  0x10   /* Write TDI/DO */
00064 #define MPSSE_DO_READ   0x20   /* Read TDO/DI */
00065 #define MPSSE_WRITE_TMS 0x40   /* Write TMS/CS */
00066 
00067 /* FTDI MPSSE commands */
00068 #define SET_BITS_LOW   0x80
00069 /*BYTE DATA*/
00070 /*BYTE Direction*/
00071 #define SET_BITS_HIGH  0x82
00072 /*BYTE DATA*/
00073 /*BYTE Direction*/
00074 #define GET_BITS_LOW   0x81
00075 #define GET_BITS_HIGH  0x83
00076 #define LOOPBACK_START 0x84
00077 #define LOOPBACK_END   0x85
00078 #define TCK_DIVISOR    0x86
00079 /* Value Low */
00080 /* Value HIGH */ /*rate is 12000000/((1+value)*2) */
00081 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00082 
00083 /* Commands in MPSSE and Host Emulation Mode */
00084 #define SEND_IMMEDIATE 0x87
00085 #define WAIT_ON_HIGH   0x88
00086 #define WAIT_ON_LOW    0x89
00087 
00088 /* Commands in Host Emulation Mode */
00089 #define READ_SHORT     0x90
00090 /* Address_Low */
00091 #define READ_EXTENDED  0x91
00092 /* Address High */
00093 /* Address Low  */
00094 #define WRITE_SHORT    0x92
00095 /* Address_Low */
00096 #define WRITE_EXTENDED 0x93
00097 /* Address High */
00098 /* Address Low  */
00099 
00100 /* Definitions for flow control */
00101 #define SIO_RESET          0 /* Reset the port */
00102 #define SIO_MODEM_CTRL     1 /* Set the modem control register */
00103 #define SIO_SET_FLOW_CTRL  2 /* Set flow control register */
00104 #define SIO_SET_BAUD_RATE  3 /* Set baud rate */
00105 #define SIO_SET_DATA       4 /* Set the data characteristics of the port */
00106 
00107 #define FTDI_DEVICE_OUT_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT)
00108 #define FTDI_DEVICE_IN_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN)
00109 
00110 /* Requests */
00111 #define SIO_RESET_REQUEST             SIO_RESET
00112 #define SIO_SET_BAUDRATE_REQUEST      SIO_SET_BAUD_RATE
00113 #define SIO_SET_DATA_REQUEST          SIO_SET_DATA
00114 #define SIO_SET_FLOW_CTRL_REQUEST     SIO_SET_FLOW_CTRL
00115 #define SIO_SET_MODEM_CTRL_REQUEST    SIO_MODEM_CTRL
00116 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
00117 #define SIO_SET_EVENT_CHAR_REQUEST    0x06
00118 #define SIO_SET_ERROR_CHAR_REQUEST    0x07
00119 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
00120 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
00121 #define SIO_SET_BITMODE_REQUEST       0x0B
00122 #define SIO_READ_PINS_REQUEST         0x0C
00123 #define SIO_READ_EEPROM_REQUEST       0x90
00124 #define SIO_WRITE_EEPROM_REQUEST      0x91
00125 #define SIO_ERASE_EEPROM_REQUEST      0x92
00126 
00127 
00128 #define SIO_RESET_SIO 0
00129 #define SIO_RESET_PURGE_RX 1
00130 #define SIO_RESET_PURGE_TX 2
00131 
00132 #define SIO_DISABLE_FLOW_CTRL 0x0
00133 #define SIO_RTS_CTS_HS (0x1 << 8)
00134 #define SIO_DTR_DSR_HS (0x2 << 8)
00135 #define SIO_XON_XOFF_HS (0x4 << 8)
00136 
00137 #define SIO_SET_DTR_MASK 0x1
00138 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK  << 8))
00139 #define SIO_SET_DTR_LOW  ( 0 | ( SIO_SET_DTR_MASK  << 8))
00140 #define SIO_SET_RTS_MASK 0x2
00141 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00142 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00143 
00144 #define SIO_RTS_CTS_HS (0x1 << 8)
00145 
00146 /* marker for unused usb urb structures
00147    (taken from libusb) */
00148 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00149 
00155 struct ftdi_context
00156 {
00157     /* USB specific */
00159     struct usb_dev_handle *usb_dev;
00161     int usb_read_timeout;
00163     int usb_write_timeout;
00164 
00165     /* FTDI specific */
00167     enum ftdi_chip_type type;
00169     int baudrate;
00171     unsigned char bitbang_enabled;
00173     unsigned char *readbuffer;
00175     unsigned int readbuffer_offset;
00177     unsigned int readbuffer_remaining;
00179     unsigned int readbuffer_chunksize;
00181     unsigned int writebuffer_chunksize;
00182 
00183     /* FTDI FT2232C requirecments */
00185     int interface;   /* 0 or 1 */
00187     int index;       /* 1 or 2 */
00188     /* Endpoints */
00190     int in_ep;
00191     int out_ep;      /* 1 or 2 */
00192 
00194     unsigned char bitbang_mode;
00195 
00197     int eeprom_size;
00198 
00200     char *error_str;
00201 
00203     char *async_usb_buffer;
00205     unsigned int async_usb_buffer_size;
00206 };
00207 
00211 struct ftdi_device_list
00212 {
00214     struct ftdi_device_list *next;
00216     struct usb_device *dev;
00217 };
00218 
00222 struct ftdi_eeprom
00223 {
00225     int vendor_id;
00227     int product_id;
00228 
00230     int self_powered;
00232     int remote_wakeup;
00234     int BM_type_chip;
00235 
00237     int in_is_isochronous;
00239     int out_is_isochronous;
00241     int suspend_pull_downs;
00242 
00244     int use_serial;
00246     int change_usb_version;
00248     int usb_version;
00250     int max_power;
00251 
00253     char *manufacturer;
00255     char *product;
00257     char *serial;
00258 
00261     int size;
00262 };
00263 
00264 #ifdef __cplusplus
00265 extern "C"
00266 {
00267 #endif
00268 
00269     int ftdi_init(struct ftdi_context *ftdi);
00270     struct ftdi_context *ftdi_new();
00271     int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00272 
00273     void ftdi_deinit(struct ftdi_context *ftdi);
00274     void ftdi_free(struct ftdi_context *ftdi);
00275     void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00276 
00277     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00278                           int vendor, int product);
00279     void ftdi_list_free(struct ftdi_device_list **devlist);
00280     void ftdi_list_free2(struct ftdi_device_list *devlist);
00281     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00282                              char * manufacturer, int mnf_len,
00283                              char * description, int desc_len,
00284                              char * serial, int serial_len);
00285 
00286     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00287     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00288                            const char* description, const char* serial);
00289     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00290 
00291     int ftdi_usb_close(struct ftdi_context *ftdi);
00292     int ftdi_usb_reset(struct ftdi_context *ftdi);
00293     int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00294     int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00295     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00296 
00297     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00298     int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00299                                enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00300     int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00301                                 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00302                                 enum ftdi_break_type break_type);
00303 
00304     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00305     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00306     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00307 
00308     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00309     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00310     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00311 
00312     int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00313     void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00314 
00315     int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask);
00316     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00317     int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00318     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00319 
00320     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00321     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00322 
00323     int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00324 
00325     /* flow control */
00326     int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00327     int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00328     int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00329     int ftdi_setrts(struct ftdi_context *ftdi, int state);
00330 
00331     int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00332     int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00333 
00334     /* set eeprom size */
00335     void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00336 
00337     /* init and build eeprom from ftdi_eeprom structure */
00338     void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00339     int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00340     int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *output, int size);
00341 
00342     /* "eeprom" needs to be valid 128 byte eeprom (generated by the eeprom generator)
00343        the checksum of the eeprom is valided */
00344     int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00345     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00346     int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00347     int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00348     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00349 
00350     char *ftdi_get_error_string(struct ftdi_context *ftdi);
00351  
00352 #ifdef __cplusplus
00353 }
00354 #endif
00355 
00356 #endif /* __libftdi_h__ */

Generated on Tue Apr 24 12:24:09 2012 for libftdi by  doxygen 1.4.7