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 };
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 };
00032 
00034 enum ftdi_mpsse_mode {
00035     BITMODE_RESET  = 0x00,
00036     BITMODE_BITBANG= 0x01,
00037     BITMODE_MPSSE  = 0x02,
00038     BITMODE_SYNCBB = 0x04,
00039     BITMODE_MCU    = 0x08,
00040     // CPU-style fifo mode gets set via EEPROM
00041     BITMODE_OPTO   = 0x10,
00042     BITMODE_CBUS   = 0x20
00043 };
00044 
00046 enum ftdi_interface {
00047     INTERFACE_ANY = 0,
00048     INTERFACE_A   = 1,
00049     INTERFACE_B   = 2
00050 };
00051 
00052 /* Shifting commands IN MPSSE Mode*/
00053 #define MPSSE_WRITE_NEG 0x01   /* Write TDI/DO on negative TCK/SK edge*/
00054 #define MPSSE_BITMODE   0x02   /* Write bits, not bytes */
00055 #define MPSSE_READ_NEG  0x04   /* Sample TDO/DI on negative TCK/SK edge */
00056 #define MPSSE_LSB       0x08   /* LSB first */
00057 #define MPSSE_DO_WRITE  0x10   /* Write TDI/DO */
00058 #define MPSSE_DO_READ   0x20   /* Read TDO/DI */
00059 #define MPSSE_WRITE_TMS 0x40   /* Write TMS/CS */
00060 
00061 /* FTDI MPSSE commands */
00062 #define SET_BITS_LOW   0x80
00063 /*BYTE DATA*/
00064 /*BYTE Direction*/
00065 #define SET_BITS_HIGH  0x82
00066 /*BYTE DATA*/
00067 /*BYTE Direction*/
00068 #define GET_BITS_LOW   0x81
00069 #define GET_BITS_HIGH  0x83
00070 #define LOOPBACK_START 0x84
00071 #define LOOPBACK_END   0x85
00072 #define TCK_DIVISOR    0x86
00073 /* Value Low */
00074 /* Value HIGH */ /*rate is 12000000/((1+value)*2) */
00075 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00076 
00077 /* Commands in MPSSE and Host Emulation Mode */
00078 #define SEND_IMMEDIATE 0x87 
00079 #define WAIT_ON_HIGH   0x88
00080 #define WAIT_ON_LOW    0x89
00081 
00082 /* Commands in Host Emulation Mode */
00083 #define READ_SHORT     0x90
00084 /* Address_Low */
00085 #define READ_EXTENDED  0x91
00086 /* Address High */
00087 /* Address Low  */
00088 #define WRITE_SHORT    0x92
00089 /* Address_Low */
00090 #define WRITE_EXTENDED 0x93
00091 /* Address High */
00092 /* Address Low  */
00093 
00094 /* Definitions for flow control */
00095 #define SIO_MODEM_CTRL     1 /* Set the modem control register */
00096 #define SIO_SET_FLOW_CTRL  2 /* Set flow control register */
00097 
00098 #define SIO_SET_FLOW_CTRL_REQUEST_TYPE 0x40
00099 #define SIO_SET_FLOW_CTRL_REQUEST SIO_SET_FLOW_CTRL
00100 
00101 #define SIO_DISABLE_FLOW_CTRL 0x0 
00102 #define SIO_RTS_CTS_HS (0x1 << 8)
00103 #define SIO_DTR_DSR_HS (0x2 << 8)
00104 #define SIO_XON_XOFF_HS (0x4 << 8)
00105 
00106 #define SIO_SET_MODEM_CTRL_REQUEST_TYPE 0x40
00107 #define SIO_SET_MODEM_CTRL_REQUEST SIO_MODEM_CTRL
00108 
00109 #define SIO_SET_DTR_MASK 0x1
00110 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK  << 8))
00111 #define SIO_SET_DTR_LOW  ( 0 | ( SIO_SET_DTR_MASK  << 8))
00112 #define SIO_SET_RTS_MASK 0x2
00113 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00114 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00115 
00116 #define SIO_RTS_CTS_HS (0x1 << 8)
00117 
00118 /* marker for unused usb urb structures
00119    (taken from libusb) */
00120 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00121 
00127 struct ftdi_context {
00128     // USB specific
00130     struct usb_dev_handle *usb_dev;
00132     int usb_read_timeout;
00134     int usb_write_timeout;
00135 
00136     // FTDI specific
00138     enum ftdi_chip_type type;
00140     int baudrate;
00142     unsigned char bitbang_enabled;
00144     unsigned char *readbuffer;
00146     unsigned int readbuffer_offset;
00148     unsigned int readbuffer_remaining;
00150     unsigned int readbuffer_chunksize;
00152     unsigned int writebuffer_chunksize;
00153 
00154     // FTDI FT2232C requirecments
00156     int interface;   // 0 or 1
00158     int index;       // 1 or 2
00159     // Endpoints
00161     int in_ep;
00162     int out_ep;      // 1 or 2
00163 
00165     unsigned char bitbang_mode;
00166 
00168     int eeprom_size;
00169 
00171     char *error_str;
00172 
00174     char *async_usb_buffer;
00176     unsigned int async_usb_buffer_size;
00177 };
00178 
00182 struct ftdi_device_list {
00184     struct ftdi_device_list *next;
00186     struct usb_device *dev;
00187 };
00188 
00192 struct ftdi_eeprom {
00194     int vendor_id;
00196     int product_id;
00197 
00199     int self_powered;
00201     int remote_wakeup;
00203     int BM_type_chip;
00204 
00206     int in_is_isochronous;
00208     int out_is_isochronous;
00210     int suspend_pull_downs;
00211 
00213     int use_serial;
00215     int change_usb_version;
00217     int usb_version;
00219     int max_power;
00220 
00222     char *manufacturer;
00224     char *product;
00226     char *serial;
00227 
00230   int size;
00231 };
00232 
00233 #ifdef __cplusplus
00234 extern "C" {
00235 #endif
00236 
00237     int ftdi_init(struct ftdi_context *ftdi);
00238     struct ftdi_context *ftdi_new();
00239     int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00240 
00241     void ftdi_deinit(struct ftdi_context *ftdi);
00242     void ftdi_free(struct ftdi_context *ftdi);
00243     void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00244 
00245     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00246                           int vendor, int product);
00247     void ftdi_list_free(struct ftdi_device_list **devlist);
00248     void ftdi_list_free2(struct ftdi_device_list *devlist);
00249     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00250                              char * manufacturer, int mnf_len,
00251                              char * description, int desc_len,
00252                              char * serial, int serial_len);
00253 
00254     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00255     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00256                            const char* description, const char* serial);
00257     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00258 
00259     int ftdi_usb_close(struct ftdi_context *ftdi);
00260     int ftdi_usb_reset(struct ftdi_context *ftdi);
00261     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00262 
00263     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00264     int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00265                                enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00266 
00267     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00268     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00269     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00270 
00271     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00272     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00273     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00274 
00275     int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00276     void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00277 
00278     int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask);
00279     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00280     int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00281     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00282 
00283     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00284     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00285 
00286     // set eeprom size
00287     void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00288 
00289     // init and build eeprom from ftdi_eeprom structure
00290     void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00291     int  ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00292 
00293     // "eeprom" needs to be valid 128 byte eeprom (generated by the eeprom generator)
00294     // the checksum of the eeprom is valided
00295     int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00296     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00297     int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00298     int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00299     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00300 
00301     char *ftdi_get_error_string(struct ftdi_context *ftdi);
00302 
00303     // flow control
00304     int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00305     int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00306     int ftdi_setrts(struct ftdi_context *ftdi, int state);
00307 
00308 #ifdef __cplusplus
00309 }
00310 #endif
00311 
00312 #endif /* __libftdi_h__ */

Generated on Wed Mar 12 07:35:50 2008 for libftdi by  doxygen 1.5.1