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, 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
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
00059
00060 #define MPSSE_WRITE_NEG 0x01
00061 #define MPSSE_BITMODE 0x02
00062 #define MPSSE_READ_NEG 0x04
00063 #define MPSSE_LSB 0x08
00064 #define MPSSE_DO_WRITE 0x10
00065 #define MPSSE_DO_READ 0x20
00066 #define MPSSE_WRITE_TMS 0x40
00067
00068
00069 #define SET_BITS_LOW 0x80
00070
00071
00072 #define SET_BITS_HIGH 0x82
00073
00074
00075 #define GET_BITS_LOW 0x81
00076 #define GET_BITS_HIGH 0x83
00077 #define LOOPBACK_START 0x84
00078 #define LOOPBACK_END 0x85
00079 #define TCK_DIVISOR 0x86
00080
00081
00082 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
00083
00084
00085 #define SEND_IMMEDIATE 0x87
00086 #define WAIT_ON_HIGH 0x88
00087 #define WAIT_ON_LOW 0x89
00088
00089
00090 #define READ_SHORT 0x90
00091
00092 #define READ_EXTENDED 0x91
00093
00094
00095 #define WRITE_SHORT 0x92
00096
00097 #define WRITE_EXTENDED 0x93
00098
00099
00100
00101
00102 #define SIO_RESET 0
00103 #define SIO_MODEM_CTRL 1
00104 #define SIO_SET_FLOW_CTRL 2
00105 #define SIO_SET_BAUD_RATE 3
00106 #define SIO_SET_DATA 4
00107
00108 #define FTDI_DEVICE_OUT_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT)
00109 #define FTDI_DEVICE_IN_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN)
00110
00111
00112 #define SIO_RESET_REQUEST SIO_RESET
00113 #define SIO_SET_BAUDRATE_REQUEST SIO_SET_BAUD_RATE
00114 #define SIO_SET_DATA_REQUEST SIO_SET_DATA
00115 #define SIO_SET_FLOW_CTRL_REQUEST SIO_SET_FLOW_CTRL
00116 #define SIO_SET_MODEM_CTRL_REQUEST SIO_MODEM_CTRL
00117 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
00118 #define SIO_SET_EVENT_CHAR_REQUEST 0x06
00119 #define SIO_SET_ERROR_CHAR_REQUEST 0x07
00120 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
00121 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
00122 #define SIO_SET_BITMODE_REQUEST 0x0B
00123 #define SIO_READ_PINS_REQUEST 0x0C
00124 #define SIO_READ_EEPROM_REQUEST 0x90
00125 #define SIO_WRITE_EEPROM_REQUEST 0x91
00126 #define SIO_ERASE_EEPROM_REQUEST 0x92
00127
00128
00129 #define SIO_RESET_SIO 0
00130 #define SIO_RESET_PURGE_RX 1
00131 #define SIO_RESET_PURGE_TX 2
00132
00133 #define SIO_DISABLE_FLOW_CTRL 0x0
00134 #define SIO_RTS_CTS_HS (0x1 << 8)
00135 #define SIO_DTR_DSR_HS (0x2 << 8)
00136 #define SIO_XON_XOFF_HS (0x4 << 8)
00137
00138 #define SIO_SET_DTR_MASK 0x1
00139 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK << 8))
00140 #define SIO_SET_DTR_LOW ( 0 | ( SIO_SET_DTR_MASK << 8))
00141 #define SIO_SET_RTS_MASK 0x2
00142 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
00143 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
00144
00145 #define SIO_RTS_CTS_HS (0x1 << 8)
00146
00147
00148
00149 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
00150
00151 #ifdef __GNUC__
00152 #define DEPRECATED(func) func __attribute__ ((deprecated))
00153 #elif defined(_MSC_VER)
00154 #define DEPRECATED(func) __declspec(deprecated) func
00155 #else
00156 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
00157 #define DEPRECATED(func) func
00158 #endif
00159
00160
00166 struct ftdi_context
00167 {
00168
00170 struct usb_dev_handle *usb_dev;
00172 int usb_read_timeout;
00174 int usb_write_timeout;
00175
00176
00178 enum ftdi_chip_type type;
00180 int baudrate;
00182 unsigned char bitbang_enabled;
00184 unsigned char *readbuffer;
00186 unsigned int readbuffer_offset;
00188 unsigned int readbuffer_remaining;
00190 unsigned int readbuffer_chunksize;
00192 unsigned int writebuffer_chunksize;
00194 unsigned int max_packet_size;
00195
00196
00198 int interface;
00200 int index;
00201
00203 int in_ep;
00204 int out_ep;
00205
00207 unsigned char bitbang_mode;
00208
00210 int eeprom_size;
00211
00213 char *error_str;
00214
00216 char *async_usb_buffer;
00218 unsigned int async_usb_buffer_size;
00219 };
00220
00224 struct ftdi_device_list
00225 {
00227 struct ftdi_device_list *next;
00229 struct usb_device *dev;
00230 };
00231
00235 struct ftdi_eeprom
00236 {
00238 int vendor_id;
00240 int product_id;
00241
00243 int self_powered;
00245 int remote_wakeup;
00247 int BM_type_chip;
00248
00250 int in_is_isochronous;
00252 int out_is_isochronous;
00254 int suspend_pull_downs;
00255
00257 int use_serial;
00259 int change_usb_version;
00261 int usb_version;
00263 int max_power;
00264
00266 char *manufacturer;
00268 char *product;
00270 char *serial;
00271
00274 int size;
00275 };
00276
00277 #ifdef __cplusplus
00278 extern "C"
00279 {
00280 #endif
00281
00282 int ftdi_init(struct ftdi_context *ftdi);
00283 struct ftdi_context *ftdi_new(void);
00284 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface);
00285
00286 void ftdi_deinit(struct ftdi_context *ftdi);
00287 void ftdi_free(struct ftdi_context *ftdi);
00288 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usbdev);
00289
00290 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
00291 int vendor, int product);
00292 void ftdi_list_free(struct ftdi_device_list **devlist);
00293 void ftdi_list_free2(struct ftdi_device_list *devlist);
00294 int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct usb_device *dev,
00295 char * manufacturer, int mnf_len,
00296 char * description, int desc_len,
00297 char * serial, int serial_len);
00298
00299 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
00300 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00301 const char* description, const char* serial);
00302 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00303 const char* description, const char* serial, unsigned int index);
00304 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
00305 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
00306
00307 int ftdi_usb_close(struct ftdi_context *ftdi);
00308 int ftdi_usb_reset(struct ftdi_context *ftdi);
00309 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
00310 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
00311 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
00312
00313 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
00314 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00315 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00316 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
00317 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
00318 enum ftdi_break_type break_type);
00319
00320 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00321 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00322 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00323
00324 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
00325 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
00326 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
00327
00328 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
00329 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
00330
00331 int DEPRECATED(ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask));
00332 int ftdi_disable_bitbang(struct ftdi_context *ftdi);
00333 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
00334 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
00335
00336 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
00337 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
00338
00339 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
00340
00341
00342 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
00343 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
00344 int ftdi_setdtr(struct ftdi_context *ftdi, int state);
00345 int ftdi_setrts(struct ftdi_context *ftdi, int state);
00346
00347 int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
00348 int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
00349
00350
00351 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size);
00352
00353
00354 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
00355 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
00356 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *output, int size);
00357
00358
00359
00360 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00361 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
00362 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize);
00363 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
00364 int ftdi_erase_eeprom(struct ftdi_context *ftdi);
00365
00366 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
00367 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
00368
00369 char *ftdi_get_error_string(struct ftdi_context *ftdi);
00370
00371 #ifdef __cplusplus
00372 }
00373 #endif
00374
00375 #endif