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 };
00033 enum ftdi_break_type { BREAK_OFF=0, BREAK_ON=1 };
00034
00036 enum ftdi_mpsse_mode {
00037 BITMODE_RESET = 0x00,
00038 BITMODE_BITBANG= 0x01,
00039 BITMODE_MPSSE = 0x02,
00040 BITMODE_SYNCBB = 0x04,
00041 BITMODE_MCU = 0x08,
00042
00043 BITMODE_OPTO = 0x10,
00044 BITMODE_CBUS = 0x20
00045 };
00046
00048 enum ftdi_interface {
00049 INTERFACE_ANY = 0,
00050 INTERFACE_A = 1,
00051 INTERFACE_B = 2
00052 };
00053
00054
00055 #define MPSSE_WRITE_NEG 0x01
00056 #define MPSSE_BITMODE 0x02
00057 #define MPSSE_READ_NEG 0x04
00058 #define MPSSE_LSB 0x08
00059 #define MPSSE_DO_WRITE 0x10
00060 #define MPSSE_DO_READ 0x20
00061 #define MPSSE_WRITE_TMS 0x40
00062
00063
00064 #define SET_BITS_LOW 0x80
00065
00066
00067 #define SET_BITS_HIGH 0x82
00068
00069
00070 #define GET_BITS_LOW 0x81
00071 #define GET_BITS_HIGH 0x83
00072 #define LOOPBACK_START 0x84
00073 #define LOOPBACK_END 0x85
00074 #define TCK_DIVISOR 0x86
00075
00076
00077 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00078
00079
00080 #define SEND_IMMEDIATE 0x87
00081 #define WAIT_ON_HIGH 0x88
00082 #define WAIT_ON_LOW 0x89
00083
00084
00085 #define READ_SHORT 0x90
00086
00087 #define READ_EXTENDED 0x91
00088
00089
00090 #define WRITE_SHORT 0x92
00091
00092 #define WRITE_EXTENDED 0x93
00093
00094
00095
00096
00097 #define SIO_RESET 0
00098 #define SIO_MODEM_CTRL 1
00099 #define SIO_SET_FLOW_CTRL 2
00100 #define SIO_SET_BAUD_RATE 3
00101 #define SIO_SET_DATA 4
00102
00103 #define SIO_RESET_REQUEST_TYPE 0x40
00104 #define SIO_RESET_REQUEST SIO_RESET
00105 #define SIO_RESET_SIO 0
00106 #define SIO_RESET_PURGE_RX 1
00107 #define SIO_RESET_PURGE_TX 2
00108
00109 #define SIO_SET_BAUDRATE_REQUEST_TYPE 0x40
00110 #define SIO_SET_BAUDRATE_REQUEST SIO_SET_BAUD_RATE
00111
00112 #define SIO_SET_DATA_REQUEST_TYPE 0x40
00113 #define SIO_SET_DATA_REQUEST SIO_SET_DATA
00114
00115 #define SIO_SET_FLOW_CTRL_REQUEST SIO_SET_FLOW_CTRL
00116 #define SIO_SET_FLOW_CTRL_REQUEST_TYPE 0x40
00117
00118 #define SIO_DISABLE_FLOW_CTRL 0x0
00119 #define SIO_RTS_CTS_HS (0x1 << 8)
00120 #define SIO_DTR_DSR_HS (0x2 << 8)
00121 #define SIO_XON_XOFF_HS (0x4 << 8)
00122
00123 #define SIO_SET_MODEM_CTRL_REQUEST_TYPE 0x40
00124 #define SIO_SET_MODEM_CTRL_REQUEST SIO_MODEM_CTRL
00125
00126 #define SIO_SET_DTR_MASK 0x1
00127 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK << 8))
00128 #define SIO_SET_DTR_LOW ( 0 | ( SIO_SET_DTR_MASK << 8))
00129 #define SIO_SET_RTS_MASK 0x2
00130 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00131 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00132
00133 #define SIO_RTS_CTS_HS (0x1 << 8)
00134
00135
00136
00137 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00138
00144 struct ftdi_context {
00145
00147 struct usb_dev_handle *usb_dev;
00149 int usb_read_timeout;
00151 int usb_write_timeout;
00152
00153
00155 enum ftdi_chip_type type;
00157 int baudrate;
00159 unsigned char bitbang_enabled;
00161 unsigned char *readbuffer;
00163 unsigned int readbuffer_offset;
00165 unsigned int readbuffer_remaining;
00167 unsigned int readbuffer_chunksize;
00169 unsigned int writebuffer_chunksize;
00170
00171
00173 int interface;
00175 int index;
00176
00178 int in_ep;
00179 int out_ep;
00180
00182 unsigned char bitbang_mode;
00183
00185 int eeprom_size;
00186
00188 char *error_str;
00189
00191 char *async_usb_buffer;
00193 unsigned int async_usb_buffer_size;
00194 };
00195
00199 struct ftdi_device_list {
00201 struct ftdi_device_list *next;
00203 struct usb_device *dev;
00204 };
00205
00209 struct ftdi_eeprom {
00211 int vendor_id;
00213 int product_id;
00214
00216 int self_powered;
00218 int remote_wakeup;
00220 int BM_type_chip;
00221
00223 int in_is_isochronous;
00225 int out_is_isochronous;
00227 int suspend_pull_downs;
00228
00230 int use_serial;
00232 int change_usb_version;
00234 int usb_version;
00236 int max_power;
00237
00239 char *manufacturer;
00241 char *product;
00243 char *serial;
00244
00247 int size;
00248 };
00249
00250 #ifdef __cplusplus
00251 extern "C" {
00252 #endif
00253
00254 int ftdi_init(struct ftdi_context *ftdi);
00255 struct ftdi_context *ftdi_new();
00256 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00257
00258 void ftdi_deinit(struct ftdi_context *ftdi);
00259 void ftdi_free(struct ftdi_context *ftdi);
00260 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00261
00262 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00263 int vendor, int product);
00264 void ftdi_list_free(struct ftdi_device_list **devlist);
00265 void ftdi_list_free2(struct ftdi_device_list *devlist);
00266 int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00267 char * manufacturer, int mnf_len,
00268 char * description, int desc_len,
00269 char * serial, int serial_len);
00270
00271 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00272 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00273 const char* description, const char* serial);
00274 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00275
00276 int ftdi_usb_close(struct ftdi_context *ftdi);
00277 int ftdi_usb_reset(struct ftdi_context *ftdi);
00278 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00279 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00280 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00281
00282 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00283 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00284 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00285 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00286 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00287 enum ftdi_break_type break_type);
00288
00289 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00290 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00291 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00292
00293 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00294 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00295 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00296
00297 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00298 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00299
00300 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask);
00301 int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00302 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00303 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00304
00305 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00306 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00307
00308 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00309
00310
00311 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00312 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00313 int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00314 int ftdi_setrts(struct ftdi_context *ftdi, int state);
00315
00316 int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00317 int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00318
00319
00320 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00321
00322
00323 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00324 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00325
00326
00327
00328 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00329 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00330 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00331 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00332 int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00333
00334 char *ftdi_get_error_string(struct ftdi_context *ftdi);
00335
00336 #ifdef __cplusplus
00337 }
00338 #endif
00339
00340 #endif