00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
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
00053 #define MPSSE_WRITE_NEG 0x01
00054 #define MPSSE_BITMODE 0x02
00055 #define MPSSE_READ_NEG 0x04
00056 #define MPSSE_LSB 0x08
00057 #define MPSSE_DO_WRITE 0x10
00058 #define MPSSE_DO_READ 0x20
00059 #define MPSSE_WRITE_TMS 0x40
00060
00061
00062 #define SET_BITS_LOW 0x80
00063
00064
00065 #define SET_BITS_HIGH 0x82
00066
00067
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
00074
00075 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00076
00077
00078 #define SEND_IMMEDIATE 0x87
00079 #define WAIT_ON_HIGH 0x88
00080 #define WAIT_ON_LOW 0x89
00081
00082
00083 #define READ_SHORT 0x90
00084
00085 #define READ_EXTENDED 0x91
00086
00087
00088 #define WRITE_SHORT 0x92
00089
00090 #define WRITE_EXTENDED 0x93
00091
00092
00093
00094
00095 #define SIO_MODEM_CTRL 1
00096 #define SIO_SET_FLOW_CTRL 2
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
00119
00120 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00121
00127 struct ftdi_context {
00128
00130 struct usb_dev_handle *usb_dev;
00132 int usb_read_timeout;
00134 int usb_write_timeout;
00135
00136
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
00156 int interface;
00158 int index;
00159
00161 int in_ep;
00162 int out_ep;
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_rx_buffer(struct ftdi_context *ftdi);
00262 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00263 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00264
00265 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00266 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00267 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00268
00269 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00270 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00271 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00272
00273 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00274 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00275 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00276
00277 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00278 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00279
00280 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask);
00281 int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00282 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00283 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00284
00285 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00286 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00287
00288 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00289
00290 int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00291 int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00292
00293
00294 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00295
00296
00297 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00298 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00299
00300
00301
00302 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00303 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00304 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00305 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00306 int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00307
00308 char *ftdi_get_error_string(struct ftdi_context *ftdi);
00309
00310
00311 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00312 int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00313 int ftdi_setrts(struct ftdi_context *ftdi, int state);
00314
00315 #ifdef __cplusplus
00316 }
00317 #endif
00318
00319 #endif