libftdi1  1.0
ftdi.h
Go to the documentation of this file.
1 /***************************************************************************
2  ftdi.h - description
3  -------------------
4  begin : Fri Apr 4 2003
5  copyright : (C) 2003-2013 by Intra2net AG and the libftdi developers
6  email : opensource@intra2net.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU Lesser General Public License *
13  * version 2.1 as published by the Free Software Foundation; *
14  * *
15  ***************************************************************************/
16 
17 #ifndef __libftdi_h__
18 #define __libftdi_h__
19 
20 #include <stdint.h>
21 #include <sys/time.h>
22 
26 enum ftdi_parity_type { NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4 };
33 
36 {
37  BITMODE_RESET = 0x00,
39  BITMODE_MPSSE = 0x02,
40  BITMODE_SYNCBB = 0x04,
41  BITMODE_MCU = 0x08,
42  /* CPU-style fifo mode gets set via EEPROM */
43  BITMODE_OPTO = 0x10,
44  BITMODE_CBUS = 0x20,
45  BITMODE_SYNCFF = 0x40,
46  BITMODE_FT1284 = 0x80,
47 };
48 
51 {
57 };
58 
61 {
64 };
65 
66 /* Shifting commands IN MPSSE Mode*/
67 #define MPSSE_WRITE_NEG 0x01 /* Write TDI/DO on negative TCK/SK edge*/
68 #define MPSSE_BITMODE 0x02 /* Write bits, not bytes */
69 #define MPSSE_READ_NEG 0x04 /* Sample TDO/DI on negative TCK/SK edge */
70 #define MPSSE_LSB 0x08 /* LSB first */
71 #define MPSSE_DO_WRITE 0x10 /* Write TDI/DO */
72 #define MPSSE_DO_READ 0x20 /* Read TDO/DI */
73 #define MPSSE_WRITE_TMS 0x40 /* Write TMS/CS */
74 
75 /* FTDI MPSSE commands */
76 #define SET_BITS_LOW 0x80
77 /*BYTE DATA*/
78 /*BYTE Direction*/
79 #define SET_BITS_HIGH 0x82
80 /*BYTE DATA*/
81 /*BYTE Direction*/
82 #define GET_BITS_LOW 0x81
83 #define GET_BITS_HIGH 0x83
84 #define LOOPBACK_START 0x84
85 #define LOOPBACK_END 0x85
86 #define TCK_DIVISOR 0x86
87 /* H Type specific commands */
88 #define DIS_DIV_5 0x8a
89 #define EN_DIV_5 0x8b
90 #define EN_3_PHASE 0x8c
91 #define DIS_3_PHASE 0x8d
92 #define CLK_BITS 0x8e
93 #define CLK_BYTES 0x8f
94 #define CLK_WAIT_HIGH 0x94
95 #define CLK_WAIT_LOW 0x95
96 #define EN_ADAPTIVE 0x96
97 #define DIS_ADAPTIVE 0x97
98 #define CLK_BYTES_OR_HIGH 0x9c
99 #define CLK_BYTES_OR_LOW 0x0d
100 /*FT232H specific commands */
101 #define DRIVE_OPEN_COLLECTOR 0x9e
102 /* Value Low */
103 /* Value HIGH */ /*rate is 12000000/((1+value)*2) */
104 #define DIV_VALUE(rate) (rate > 6000000)?0:((6000000/rate -1) > 0xffff)? 0xffff: (6000000/rate -1)
105 
106 /* Commands in MPSSE and Host Emulation Mode */
107 #define SEND_IMMEDIATE 0x87
108 #define WAIT_ON_HIGH 0x88
109 #define WAIT_ON_LOW 0x89
110 
111 /* Commands in Host Emulation Mode */
112 #define READ_SHORT 0x90
113 /* Address_Low */
114 #define READ_EXTENDED 0x91
115 /* Address High */
116 /* Address Low */
117 #define WRITE_SHORT 0x92
118 /* Address_Low */
119 #define WRITE_EXTENDED 0x93
120 /* Address High */
121 /* Address Low */
122 
123 /* Definitions for flow control */
124 #define SIO_RESET 0 /* Reset the port */
125 #define SIO_MODEM_CTRL 1 /* Set the modem control register */
126 #define SIO_SET_FLOW_CTRL 2 /* Set flow control register */
127 #define SIO_SET_BAUD_RATE 3 /* Set baud rate */
128 #define SIO_SET_DATA 4 /* Set the data characteristics of the port */
129 
130 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
131 #define FTDI_DEVICE_IN_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
132 
133 /* Requests */
134 #define SIO_RESET_REQUEST SIO_RESET
135 #define SIO_SET_BAUDRATE_REQUEST SIO_SET_BAUD_RATE
136 #define SIO_SET_DATA_REQUEST SIO_SET_DATA
137 #define SIO_SET_FLOW_CTRL_REQUEST SIO_SET_FLOW_CTRL
138 #define SIO_SET_MODEM_CTRL_REQUEST SIO_MODEM_CTRL
139 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
140 #define SIO_SET_EVENT_CHAR_REQUEST 0x06
141 #define SIO_SET_ERROR_CHAR_REQUEST 0x07
142 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
143 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
144 #define SIO_SET_BITMODE_REQUEST 0x0B
145 #define SIO_READ_PINS_REQUEST 0x0C
146 #define SIO_READ_EEPROM_REQUEST 0x90
147 #define SIO_WRITE_EEPROM_REQUEST 0x91
148 #define SIO_ERASE_EEPROM_REQUEST 0x92
149 
150 
151 #define SIO_RESET_SIO 0
152 #define SIO_RESET_PURGE_RX 1
153 #define SIO_RESET_PURGE_TX 2
154 
155 #define SIO_DISABLE_FLOW_CTRL 0x0
156 #define SIO_RTS_CTS_HS (0x1 << 8)
157 #define SIO_DTR_DSR_HS (0x2 << 8)
158 #define SIO_XON_XOFF_HS (0x4 << 8)
159 
160 #define SIO_SET_DTR_MASK 0x1
161 #define SIO_SET_DTR_HIGH ( 1 | ( SIO_SET_DTR_MASK << 8))
162 #define SIO_SET_DTR_LOW ( 0 | ( SIO_SET_DTR_MASK << 8))
163 #define SIO_SET_RTS_MASK 0x2
164 #define SIO_SET_RTS_HIGH ( 2 | ( SIO_SET_RTS_MASK << 8 ))
165 #define SIO_SET_RTS_LOW ( 0 | ( SIO_SET_RTS_MASK << 8 ))
166 
167 #define SIO_RTS_CTS_HS (0x1 << 8)
168 
169 /* marker for unused usb urb structures
170  (taken from libusb) */
171 #define FTDI_URB_USERCONTEXT_COOKIE ((void *)0x1)
172 
173 #ifdef __GNUC__
174  #define DEPRECATED(func) func __attribute__ ((deprecated))
175 #elif defined(_MSC_VER)
176  #define DEPRECATED(func) __declspec(deprecated) func
177 #else
178  #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
179  #define DEPRECATED(func) func
180 #endif
181 
183 {
185  unsigned char *buf;
186  int size;
187  int offset;
189  struct libusb_transfer *transfer;
190 };
191 
198 {
199  /* USB specific */
201  struct libusb_context *usb_ctx;
203  struct libusb_device_handle *usb_dev;
208 
209  /* FTDI specific */
213  int baudrate;
215  unsigned char bitbang_enabled;
217  unsigned char *readbuffer;
219  unsigned int readbuffer_offset;
221  unsigned int readbuffer_remaining;
223  unsigned int readbuffer_chunksize;
225  unsigned int writebuffer_chunksize;
227  unsigned int max_packet_size;
228 
229  /* FTDI FT2232C requirecments */
231  int interface; /* 0 or 1 */
233  int index; /* 1 or 2 */
234  /* Endpoints */
236  int in_ep;
237  int out_ep; /* 1 or 2 */
238 
240  unsigned char bitbang_mode;
241 
244 
246  char *error_str;
247 
250 };
251 
256 {
269  MAX_POWER = 12,
287  INVERT = 30,
300  CHIP_SIZE = 43,
301  CHIP_TYPE = 44,
312 };
313 
318 {
322  struct libusb_device *dev;
323 };
324 #define FT1284_CLK_IDLE_STATE 0x01
325 #define FT1284_DATA_LSB 0x02 /* DS_FT232H 1.3 amd ftd2xx.h 1.0.4 disagree here*/
326 #define FT1284_FLOW_CONTROL 0x04
327 #define POWER_SAVE_DISABLE_H 0x80
328 
329 #define USE_SERIAL_NUM 0x08
330 enum ftdi_cbus_func {/* FIXME: Recheck value, especially the last */
333  CBUS_IOMODE = 0xa, CBUS_BB_WR = 0xb, CBUS_BB_RD = 0xc, CBUS_BB = 0xd};
334 
335 enum ftdi_cbush_func {/* FIXME: Recheck value, especially the last */
338  CBUSH_CLK30 = 0xa, CBUSH_CLK15 = 0xb, CBUSH_CLK7_5 = 0xc};
339 
341 #define INVERT_TXD 0x01
342 
343 #define INVERT_RXD 0x02
344 
345 #define INVERT_RTS 0x04
346 
347 #define INVERT_CTS 0x08
348 
349 #define INVERT_DTR 0x10
350 
351 #define INVERT_DSR 0x20
352 
353 #define INVERT_DCD 0x40
354 
355 #define INVERT_RI 0x80
356 
358 #define CHANNEL_IS_UART 0x0
359 #define CHANNEL_IS_FIFO 0x1
360 #define CHANNEL_IS_OPTO 0x2
361 #define CHANNEL_IS_CPU 0x4
362 #define CHANNEL_IS_FT1284 0x8
363 
364 #define CHANNEL_IS_RS485 0x10
365 
366 #define DRIVE_4MA 0
367 #define DRIVE_8MA 1
368 #define DRIVE_12MA 2
369 #define DRIVE_16MA 3
370 #define SLOW_SLEW 4
371 #define IS_SCHMITT 8
372 
374 #define DRIVER_VCP 0x08
375 #define DRIVER_VCPH 0x10 /* FT232H has moved the VCP bit */
376 
377 #define USE_USB_VERSION_BIT 0x10
378 
379 #define SUSPEND_DBUS7_BIT 0x80
380 
382 #define HIGH_CURRENT_DRIVE 0x10
383 #define HIGH_CURRENT_DRIVE_R 0x04
384 
389 {
390  uint64_t totalBytes;
391  struct timeval time;
392 };
393 
394 typedef struct
395 {
396  struct size_and_time first;
397  struct size_and_time prev;
398  struct size_and_time current;
399  double totalTime;
400  double totalRate;
401  double currentRate;
403 
404 typedef int (FTDIStreamCallback)(uint8_t *buffer, int length,
405  FTDIProgressInfo *progress, void *userdata);
406 
416 {
417  int major;
418  int minor;
419  int micro;
420  const char *version_str;
421  const char *snapshot_str;
422 };
423 
424 
425 #ifdef __cplusplus
426 extern "C"
427 {
428 #endif
429 
430  int ftdi_init(struct ftdi_context *ftdi);
431  struct ftdi_context *ftdi_new(void);
433 
434  void ftdi_deinit(struct ftdi_context *ftdi);
435  void ftdi_free(struct ftdi_context *ftdi);
436  void ftdi_set_usbdev (struct ftdi_context *ftdi, struct libusb_device_handle *usbdev);
437 
438  struct ftdi_version_info ftdi_get_library_version();
439 
440  int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
441  int vendor, int product);
442  void ftdi_list_free(struct ftdi_device_list **devlist);
443  void ftdi_list_free2(struct ftdi_device_list *devlist);
444  int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
445  char * manufacturer, int mnf_len,
446  char * description, int desc_len,
447  char * serial, int serial_len);
448 
449  int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
450  int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
451  const char* description, const char* serial);
452  int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
453  const char* description, const char* serial, unsigned int index);
454  int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev);
455  int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);
456 
457  int ftdi_usb_close(struct ftdi_context *ftdi);
458  int ftdi_usb_reset(struct ftdi_context *ftdi);
459  int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
460  int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
461  int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
462 
463  int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
464  int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
465  enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
466  int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
467  enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
468  enum ftdi_break_type break_type);
469 
470  int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
471  int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
472  int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
473 
474  int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
475  int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
476  int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
477 
478  int ftdi_readstream(struct ftdi_context *ftdi, FTDIStreamCallback *callback,
479  void *userdata, int packetsPerTransfer, int numTransfers);
480  struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
481 
482  struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size);
484 
485  int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
486  int ftdi_disable_bitbang(struct ftdi_context *ftdi);
487  int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
488 
489  int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
490  int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
491 
492  int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
493 
494  /* flow control */
495  int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl);
496  int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts);
497  int ftdi_setdtr(struct ftdi_context *ftdi, int state);
498  int ftdi_setrts(struct ftdi_context *ftdi, int state);
499 
500  int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
501  int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
502 
503  /* init eeprom for the given FTDI type */
504  int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi,
505  char * manufacturer, char *product,
506  char * serial);
507  int ftdi_eeprom_build(struct ftdi_context *ftdi);
508  int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose);
509 
510  int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
511  int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value);
512 
513  int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
514  int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size);
515 
516  int ftdi_read_eeprom(struct ftdi_context *ftdi);
517  int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
518  int ftdi_write_eeprom(struct ftdi_context *ftdi);
519  int ftdi_erase_eeprom(struct ftdi_context *ftdi);
520 
521  int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
522  int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val);
523 
524  char *ftdi_get_error_string(struct ftdi_context *ftdi);
525 
526 #ifdef __cplusplus
527 }
528 #endif
529 
530 #endif /* __libftdi_h__ */