1 /***************************************************************************
5 copyright : (C) 2003 by Intra2net AG
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
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; *
15 ***************************************************************************/
18 \mainpage libftdi API documentation
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/de/produkte/opensource/ftdi/
23 The library is easy to use. Have a look at this short example:
26 More examples can be found in the "examples" directory.
28 /** \addtogroup libftdi */
37 #define ftdi_error_return(code, str) do { \
38 ftdi->error_str = str; \
44 Initializes a ftdi_context.
46 \param ftdi pointer to ftdi_context
49 \retval -1: couldn't allocate read buffer
51 \remark This should be called before all functions
53 int ftdi_init(struct ftdi_context *ftdi)
56 ftdi->usb_read_timeout = 5000;
57 ftdi->usb_write_timeout = 5000;
59 ftdi->type = TYPE_BM; /* chip type */
61 ftdi->bitbang_enabled = 0;
63 ftdi->readbuffer = NULL;
64 ftdi->readbuffer_offset = 0;
65 ftdi->readbuffer_remaining = 0;
66 ftdi->writebuffer_chunksize = 4096;
72 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
74 ftdi->error_str = NULL;
76 /* All fine. Now allocate the readbuffer */
77 return ftdi_read_data_set_chunksize(ftdi, 4096);
81 Open selected channels on a chip, otherwise use first channel.
83 \param ftdi pointer to ftdi_context
84 \param interface Interface to use for FT2232C chips.
87 \retval -1: unknown interface
89 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
94 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
98 ftdi->index = INTERFACE_B;
103 ftdi_error_return(-1, "Unknown interface");
109 Deinitializes a ftdi_context.
111 \param ftdi pointer to ftdi_context
113 void ftdi_deinit(struct ftdi_context *ftdi)
115 if (ftdi->readbuffer != NULL) {
116 free(ftdi->readbuffer);
117 ftdi->readbuffer = NULL;
122 Use an already open libusb device.
124 \param ftdi pointer to ftdi_context
125 \param usb libusb usb_dev_handle to use
127 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
134 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
135 needs to be deallocated by ftdi_list_free() after use.
137 \param ftdi pointer to ftdi_context
138 \param devlist Pointer where to store list of found devices
139 \param vendor Vendor ID to search for
140 \param product Product ID to search for
142 \retval >0: number of devices found
143 \retval -1: usb_find_busses() failed
144 \retval -2: usb_find_devices() failed
145 \retval -3: out of memory
147 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
149 struct ftdi_device_list **curdev;
151 struct usb_device *dev;
155 if (usb_find_busses() < 0)
156 ftdi_error_return(-1, "usb_find_busses() failed");
157 if (usb_find_devices() < 0)
158 ftdi_error_return(-2, "usb_find_devices() failed");
161 for (bus = usb_busses; bus; bus = bus->next) {
162 for (dev = bus->devices; dev; dev = dev->next) {
163 if (dev->descriptor.idVendor == vendor
164 && dev->descriptor.idProduct == product)
166 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
168 ftdi_error_return(-3, "out of memory");
170 (*curdev)->next = NULL;
171 (*curdev)->dev = dev;
173 curdev = &(*curdev)->next;
183 Frees a usb device list.
185 \param devlist USB device list created by ftdi_usb_find_all()
187 void ftdi_list_free(struct ftdi_device_list **devlist)
189 struct ftdi_device_list **curdev;
190 for (; *devlist == NULL; devlist = curdev) {
191 curdev = &(*devlist)->next;
199 Return device ID strings from the usb device.
201 The parameters manufacturer, description and serial may be NULL
202 or pointer to buffers to store the fetched strings.
204 \param ftdi pointer to ftdi_context
205 \param dev libusb usb_dev to use
206 \param manufacturer Store manufacturer string here if not NULL
207 \param mnf_len Buffer size of manufacturer string
208 \param description Store product description string here if not NULL
209 \param desc_len Buffer size of product description string
210 \param serial Store serial string here if not NULL
211 \param serial_len Buffer size of serial string
214 \retval -1: wrong arguments
215 \retval -4: unable to open device
216 \retval -7: get product manufacturer failed
217 \retval -8: get product description failed
218 \retval -9: get serial number failed
219 \retval -10: unable to close device
221 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
222 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
224 if ((ftdi==NULL) || (dev==NULL))
227 if (!(ftdi->usb_dev = usb_open(dev)))
228 ftdi_error_return(-4, usb_strerror());
230 if (manufacturer != NULL) {
231 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) {
232 usb_close (ftdi->usb_dev);
233 ftdi_error_return(-7, usb_strerror());
237 if (description != NULL) {
238 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) {
239 usb_close (ftdi->usb_dev);
240 ftdi_error_return(-8, usb_strerror());
244 if (serial != NULL) {
245 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) {
246 usb_close (ftdi->usb_dev);
247 ftdi_error_return(-9, usb_strerror());
251 if (usb_close (ftdi->usb_dev) != 0)
252 ftdi_error_return(-10, usb_strerror());
258 Opens a ftdi device given by a usb_device.
260 \param ftdi pointer to ftdi_context
261 \param dev libusb usb_dev to use
264 \retval -4: unable to open device
265 \retval -5: unable to claim device
266 \retval -6: reset failed
267 \retval -7: set baudrate failed
269 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
271 int detach_errno = 0;
272 if (!(ftdi->usb_dev = usb_open(dev)))
273 ftdi_error_return(-4, "usb_open() failed");
275 #ifdef LIBUSB_HAS_GET_DRIVER_NP
276 // Try to detach ftdi_sio kernel module
277 // Returns ENODATA if driver is not loaded
278 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
279 detach_errno = errno;
282 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
283 usb_close (ftdi->usb_dev);
284 if (detach_errno == EPERM) {
285 ftdi_error_return(-8, "inappropriate permissions on device!");
287 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
291 if (ftdi_usb_reset (ftdi) != 0) {
292 usb_close (ftdi->usb_dev);
293 ftdi_error_return(-6, "ftdi_usb_reset failed");
296 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
297 usb_close (ftdi->usb_dev);
298 ftdi_error_return(-7, "set baudrate failed");
301 // Try to guess chip type
302 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
303 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
304 && dev->descriptor.iSerialNumber == 0))
305 ftdi->type = TYPE_BM;
306 else if (dev->descriptor.bcdDevice == 0x200)
307 ftdi->type = TYPE_AM;
308 else if (dev->descriptor.bcdDevice == 0x500) {
309 ftdi->type = TYPE_2232C;
311 ftdi->index = INTERFACE_A;
314 ftdi_error_return(0, "all fine");
318 Opens the first device with a given vendor and product ids.
320 \param ftdi pointer to ftdi_context
321 \param vendor Vendor ID
322 \param product Product ID
324 \retval same as ftdi_usb_open_desc()
326 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
328 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
332 Opens the first device with a given, vendor id, product id,
333 description and serial.
335 \param ftdi pointer to ftdi_context
336 \param vendor Vendor ID
337 \param product Product ID
338 \param description Description to search for. Use NULL if not needed.
339 \param serial Serial to search for. Use NULL if not needed.
342 \retval -1: usb_find_busses() failed
343 \retval -2: usb_find_devices() failed
344 \retval -3: usb device not found
345 \retval -4: unable to open device
346 \retval -5: unable to claim device
347 \retval -6: reset failed
348 \retval -7: set baudrate failed
349 \retval -8: get product description failed
350 \retval -9: get serial number failed
351 \retval -10: unable to close device
353 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
354 const char* description, const char* serial)
357 struct usb_device *dev;
362 if (usb_find_busses() < 0)
363 ftdi_error_return(-1, "usb_find_busses() failed");
364 if (usb_find_devices() < 0)
365 ftdi_error_return(-2, "usb_find_devices() failed");
367 for (bus = usb_busses; bus; bus = bus->next) {
368 for (dev = bus->devices; dev; dev = dev->next) {
369 if (dev->descriptor.idVendor == vendor
370 && dev->descriptor.idProduct == product) {
371 if (!(ftdi->usb_dev = usb_open(dev)))
372 ftdi_error_return(-4, "usb_open() failed");
374 if (description != NULL) {
375 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
376 usb_close (ftdi->usb_dev);
377 ftdi_error_return(-8, "unable to fetch product description");
379 if (strncmp(string, description, sizeof(string)) != 0) {
380 if (usb_close (ftdi->usb_dev) != 0)
381 ftdi_error_return(-10, "unable to close device");
385 if (serial != NULL) {
386 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
387 usb_close (ftdi->usb_dev);
388 ftdi_error_return(-9, "unable to fetch serial number");
390 if (strncmp(string, serial, sizeof(string)) != 0) {
391 if (usb_close (ftdi->usb_dev) != 0)
392 ftdi_error_return(-10, "unable to close device");
397 if (usb_close (ftdi->usb_dev) != 0)
398 ftdi_error_return(-10, "unable to close device");
400 return ftdi_usb_open_dev(ftdi, dev);
406 ftdi_error_return(-3, "device not found");
410 Resets the ftdi device.
412 \param ftdi pointer to ftdi_context
415 \retval -1: FTDI reset failed
417 int ftdi_usb_reset(struct ftdi_context *ftdi)
419 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
420 ftdi_error_return(-1,"FTDI reset failed");
422 // Invalidate data in the readbuffer
423 ftdi->readbuffer_offset = 0;
424 ftdi->readbuffer_remaining = 0;
430 Clears the buffers on the chip.
432 \param ftdi pointer to ftdi_context
435 \retval -1: write buffer purge failed
436 \retval -2: read buffer purge failed
438 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
440 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
441 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
443 // Invalidate data in the readbuffer
444 ftdi->readbuffer_offset = 0;
445 ftdi->readbuffer_remaining = 0;
447 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
448 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
454 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
456 \param ftdi pointer to ftdi_context
459 \retval -1: usb_release failed
460 \retval -2: usb_close failed
462 int ftdi_usb_close(struct ftdi_context *ftdi)
466 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
469 if (usb_close (ftdi->usb_dev) != 0)
476 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
477 Function is only used internally
480 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
481 unsigned short *value, unsigned short *index)
483 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
484 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
485 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
486 int divisor, best_divisor, best_baud, best_baud_diff;
487 unsigned long encoded_divisor;
495 divisor = 24000000 / baudrate;
497 if (ftdi->type == TYPE_AM) {
498 // Round down to supported fraction (AM only)
499 divisor -= am_adjust_dn[divisor & 7];
502 // Try this divisor and the one above it (because division rounds down)
506 for (i = 0; i < 2; i++) {
507 int try_divisor = divisor + i;
511 // Round up to supported divisor value
512 if (try_divisor <= 8) {
513 // Round up to minimum supported divisor
515 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
516 // BM doesn't support divisors 9 through 11 inclusive
518 } else if (divisor < 16) {
519 // AM doesn't support divisors 9 through 15 inclusive
522 if (ftdi->type == TYPE_AM) {
523 // Round up to supported fraction (AM only)
524 try_divisor += am_adjust_up[try_divisor & 7];
525 if (try_divisor > 0x1FFF8) {
526 // Round down to maximum supported divisor value (for AM)
527 try_divisor = 0x1FFF8;
530 if (try_divisor > 0x1FFFF) {
531 // Round down to maximum supported divisor value (for BM)
532 try_divisor = 0x1FFFF;
536 // Get estimated baud rate (to nearest integer)
537 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
538 // Get absolute difference from requested baud rate
539 if (baud_estimate < baudrate) {
540 baud_diff = baudrate - baud_estimate;
542 baud_diff = baud_estimate - baudrate;
544 if (i == 0 || baud_diff < best_baud_diff) {
545 // Closest to requested baud rate so far
546 best_divisor = try_divisor;
547 best_baud = baud_estimate;
548 best_baud_diff = baud_diff;
549 if (baud_diff == 0) {
550 // Spot on! No point trying
555 // Encode the best divisor value
556 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
557 // Deal with special cases for encoded value
558 if (encoded_divisor == 1) {
559 encoded_divisor = 0; // 3000000 baud
560 } else if (encoded_divisor == 0x4001) {
561 encoded_divisor = 1; // 2000000 baud (BM only)
563 // Split into "value" and "index" values
564 *value = (unsigned short)(encoded_divisor & 0xFFFF);
565 if(ftdi->type == TYPE_2232C) {
566 *index = (unsigned short)(encoded_divisor >> 8);
568 *index |= ftdi->index;
571 *index = (unsigned short)(encoded_divisor >> 16);
573 // Return the nearest baud rate
578 Sets the chip baud rate
580 \param ftdi pointer to ftdi_context
581 \param baudrate baud rate to set
584 \retval -1: invalid baudrate
585 \retval -2: setting baudrate failed
587 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
589 unsigned short value, index;
592 if (ftdi->bitbang_enabled) {
593 baudrate = baudrate*4;
596 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
597 if (actual_baudrate <= 0)
598 ftdi_error_return (-1, "Silly baudrate <= 0.");
600 // Check within tolerance (about 5%)
601 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
602 || ((actual_baudrate < baudrate)
603 ? (actual_baudrate * 21 < baudrate * 20)
604 : (baudrate * 21 < actual_baudrate * 20)))
605 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
607 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
608 ftdi_error_return (-2, "Setting new baudrate failed");
610 ftdi->baudrate = baudrate;
615 Set (RS232) line characteristics by Alain Abbas
617 \param ftdi pointer to ftdi_context
618 \param bits Number of bits
619 \param sbit Number of stop bits
620 \param parity Parity mode
623 \retval -1: Setting line property failed
625 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
626 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
628 unsigned short value = bits;
632 value |= (0x00 << 8);
635 value |= (0x01 << 8);
638 value |= (0x02 << 8);
641 value |= (0x03 << 8);
644 value |= (0x04 << 8);
650 value |= (0x00 << 11);
653 value |= (0x01 << 11);
656 value |= (0x02 << 11);
660 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
661 ftdi_error_return (-1, "Setting new line property failed");
667 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
669 \param ftdi pointer to ftdi_context
670 \param buf Buffer with the data
671 \param size Size of the buffer
673 \retval <0: error code from usb_bulk_write()
674 \retval >0: number of bytes written
676 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
680 int total_written = 0;
682 while (offset < size) {
683 int write_size = ftdi->writebuffer_chunksize;
685 if (offset+write_size > size)
686 write_size = size-offset;
688 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
690 ftdi_error_return(ret, "usb bulk write failed");
692 total_written += ret;
693 offset += write_size;
696 return total_written;
700 Configure write buffer chunk size.
703 \param ftdi pointer to ftdi_context
704 \param chunksize Chunk size
708 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
710 ftdi->writebuffer_chunksize = chunksize;
715 Get write buffer chunk size.
717 \param ftdi pointer to ftdi_context
718 \param chunksize Pointer to store chunk size in
722 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
724 *chunksize = ftdi->writebuffer_chunksize;
729 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
731 Automatically strips the two modem status bytes transfered during every read.
733 \param ftdi pointer to ftdi_context
734 \param buf Buffer to store data in
735 \param size Size of the buffer
737 \retval <0: error code from usb_bulk_read()
738 \retval >0: number of bytes read
740 \remark This function is not useful in bitbang mode.
741 Use ftdi_read_pins() to get the current state of the pins.
743 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
745 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
747 // everything we want is still in the readbuffer?
748 if (size <= ftdi->readbuffer_remaining) {
749 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
752 ftdi->readbuffer_remaining -= size;
753 ftdi->readbuffer_offset += size;
755 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
759 // something still in the readbuffer, but not enough to satisfy 'size'?
760 if (ftdi->readbuffer_remaining != 0) {
761 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
764 offset += ftdi->readbuffer_remaining;
766 // do the actual USB read
767 while (offset < size && ret > 0) {
768 ftdi->readbuffer_remaining = 0;
769 ftdi->readbuffer_offset = 0;
770 /* returns how much received */
771 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
773 ftdi_error_return(ret, "usb bulk read failed");
776 // skip FTDI status bytes.
777 // Maybe stored in the future to enable modem use
778 num_of_chunks = ret / 64;
779 chunk_remains = ret % 64;
780 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
782 ftdi->readbuffer_offset += 2;
786 for (i = 1; i < num_of_chunks; i++)
787 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
788 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
790 if (chunk_remains > 2) {
791 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
792 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
794 ret -= 2*num_of_chunks;
796 ret -= 2*(num_of_chunks-1)+chunk_remains;
798 } else if (ret <= 2) {
799 // no more data to read?
803 // data still fits in buf?
804 if (offset+ret <= size) {
805 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
806 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
809 /* Did we read exactly the right amount of bytes? */
811 //printf("read_data exact rem %d offset %d\n",
812 //ftdi->readbuffer_remaining, offset);
815 // only copy part of the data or size <= readbuffer_chunksize
816 int part_size = size-offset;
817 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
819 ftdi->readbuffer_offset += part_size;
820 ftdi->readbuffer_remaining = ret-part_size;
823 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
824 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
835 Configure read buffer chunk size.
838 Automatically reallocates the buffer.
840 \param ftdi pointer to ftdi_context
841 \param chunksize Chunk size
845 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
847 unsigned char *new_buf;
849 // Invalidate all remaining data
850 ftdi->readbuffer_offset = 0;
851 ftdi->readbuffer_remaining = 0;
853 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
854 ftdi_error_return(-1, "out of memory for readbuffer");
856 ftdi->readbuffer = new_buf;
857 ftdi->readbuffer_chunksize = chunksize;
863 Get read buffer chunk size.
865 \param ftdi pointer to ftdi_context
866 \param chunksize Pointer to store chunk size in
870 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
872 *chunksize = ftdi->readbuffer_chunksize;
880 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
882 \param ftdi pointer to ftdi_context
883 \param bitmask Bitmask to configure lines.
884 HIGH/ON value configures a line as output.
887 \retval -1: can't enable bitbang mode
889 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
891 unsigned short usb_val;
893 usb_val = bitmask; // low byte: bitmask
894 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
895 usb_val |= (ftdi->bitbang_mode << 8);
897 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
898 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
900 ftdi->bitbang_enabled = 1;
905 Disable bitbang mode.
907 \param ftdi pointer to ftdi_context
910 \retval -1: can't disable bitbang mode
912 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
914 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
915 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
917 ftdi->bitbang_enabled = 0;
922 Enable advanced bitbang mode for FT2232C chips.
924 \param ftdi pointer to ftdi_context
925 \param bitmask Bitmask to configure lines.
926 HIGH/ON value configures a line as output.
927 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
930 \retval -1: can't enable bitbang mode
932 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
934 unsigned short usb_val;
936 usb_val = bitmask; // low byte: bitmask
937 usb_val |= (mode << 8);
938 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
939 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
941 ftdi->bitbang_mode = mode;
942 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
947 Directly read pin state. Useful for bitbang mode.
949 \param ftdi pointer to ftdi_context
950 \param pins Pointer to store pins into
953 \retval -1: read pins failed
955 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
957 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
958 ftdi_error_return(-1, "read pins failed");
966 The FTDI chip keeps data in the internal buffer for a specific
967 amount of time if the buffer is not full yet to decrease
970 \param ftdi pointer to ftdi_context
971 \param latency Value between 1 and 255
974 \retval -1: latency out of range
975 \retval -2: unable to set latency timer
977 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
979 unsigned short usb_val;
982 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
985 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
986 ftdi_error_return(-2, "unable to set latency timer");
994 \param ftdi pointer to ftdi_context
995 \param latency Pointer to store latency value in
998 \retval -1: unable to get latency timer
1000 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1002 unsigned short usb_val;
1003 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1004 ftdi_error_return(-1, "reading latency timer failed");
1006 *latency = (unsigned char)usb_val;
1011 Init eeprom with default values.
1013 \param eeprom Pointer to ftdi_eeprom
1015 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
1017 eeprom->vendor_id = 0x0403;
1018 eeprom->product_id = 0x6001;
1020 eeprom->self_powered = 1;
1021 eeprom->remote_wakeup = 1;
1022 eeprom->BM_type_chip = 1;
1024 eeprom->in_is_isochronous = 0;
1025 eeprom->out_is_isochronous = 0;
1026 eeprom->suspend_pull_downs = 0;
1028 eeprom->use_serial = 0;
1029 eeprom->change_usb_version = 0;
1030 eeprom->usb_version = 0x0200;
1031 eeprom->max_power = 0;
1033 eeprom->manufacturer = NULL;
1034 eeprom->product = NULL;
1035 eeprom->serial = NULL;
1039 Build binary output from ftdi_eeprom structure.
1040 Output is suitable for ftdi_write_eeprom().
1042 \param eeprom Pointer to ftdi_eeprom
1043 \param output Buffer of 128 bytes to store eeprom image to
1045 \retval >0: used eeprom size
1046 \retval -1: eeprom size (128 bytes) exceeded by custom strings
1048 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
1051 unsigned short checksum, value;
1052 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1055 if (eeprom->manufacturer != NULL)
1056 manufacturer_size = strlen(eeprom->manufacturer);
1057 if (eeprom->product != NULL)
1058 product_size = strlen(eeprom->product);
1059 if (eeprom->serial != NULL)
1060 serial_size = strlen(eeprom->serial);
1062 size_check = 128; // eeprom is 128 bytes
1063 size_check -= 28; // 28 are always in use (fixed)
1064 size_check -= manufacturer_size*2;
1065 size_check -= product_size*2;
1066 size_check -= serial_size*2;
1068 // eeprom size exceeded?
1073 memset (output, 0, 128);
1075 // Addr 00: Stay 00 00
1076 // Addr 02: Vendor ID
1077 output[0x02] = eeprom->vendor_id;
1078 output[0x03] = eeprom->vendor_id >> 8;
1080 // Addr 04: Product ID
1081 output[0x04] = eeprom->product_id;
1082 output[0x05] = eeprom->product_id >> 8;
1084 // Addr 06: Device release number (0400h for BM features)
1085 output[0x06] = 0x00;
1087 if (eeprom->BM_type_chip == 1)
1088 output[0x07] = 0x04;
1090 output[0x07] = 0x02;
1092 // Addr 08: Config descriptor
1093 // Bit 1: remote wakeup if 1
1094 // Bit 0: self powered if 1
1097 if (eeprom->self_powered == 1)
1099 if (eeprom->remote_wakeup == 1)
1103 // Addr 09: Max power consumption: max power = value * 2 mA
1104 output[0x09] = eeprom->max_power;
1107 // Addr 0A: Chip configuration
1108 // Bit 7: 0 - reserved
1109 // Bit 6: 0 - reserved
1110 // Bit 5: 0 - reserved
1111 // Bit 4: 1 - Change USB version
1112 // Bit 3: 1 - Use the serial number string
1113 // Bit 2: 1 - Enable suspend pull downs for lower power
1114 // Bit 1: 1 - Out EndPoint is Isochronous
1115 // Bit 0: 1 - In EndPoint is Isochronous
1118 if (eeprom->in_is_isochronous == 1)
1120 if (eeprom->out_is_isochronous == 1)
1122 if (eeprom->suspend_pull_downs == 1)
1124 if (eeprom->use_serial == 1)
1126 if (eeprom->change_usb_version == 1)
1130 // Addr 0B: reserved
1131 output[0x0B] = 0x00;
1133 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1134 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1135 if (eeprom->change_usb_version == 1) {
1136 output[0x0C] = eeprom->usb_version;
1137 output[0x0D] = eeprom->usb_version >> 8;
1141 // Addr 0E: Offset of the manufacturer string + 0x80
1142 output[0x0E] = 0x14 + 0x80;
1144 // Addr 0F: Length of manufacturer string
1145 output[0x0F] = manufacturer_size*2 + 2;
1147 // Addr 10: Offset of the product string + 0x80, calculated later
1148 // Addr 11: Length of product string
1149 output[0x11] = product_size*2 + 2;
1151 // Addr 12: Offset of the serial string + 0x80, calculated later
1152 // Addr 13: Length of serial string
1153 output[0x13] = serial_size*2 + 2;
1156 output[0x14] = manufacturer_size*2 + 2;
1157 output[0x15] = 0x03; // type: string
1161 // Output manufacturer
1162 for (j = 0; j < manufacturer_size; j++) {
1163 output[i] = eeprom->manufacturer[j], i++;
1164 output[i] = 0x00, i++;
1167 // Output product name
1168 output[0x10] = i + 0x80; // calculate offset
1169 output[i] = product_size*2 + 2, i++;
1170 output[i] = 0x03, i++;
1171 for (j = 0; j < product_size; j++) {
1172 output[i] = eeprom->product[j], i++;
1173 output[i] = 0x00, i++;
1177 output[0x12] = i + 0x80; // calculate offset
1178 output[i] = serial_size*2 + 2, i++;
1179 output[i] = 0x03, i++;
1180 for (j = 0; j < serial_size; j++) {
1181 output[i] = eeprom->serial[j], i++;
1182 output[i] = 0x00, i++;
1185 // calculate checksum
1188 for (i = 0; i < 63; i++) {
1189 value = output[i*2];
1190 value += output[(i*2)+1] << 8;
1192 checksum = value^checksum;
1193 checksum = (checksum << 1) | (checksum >> 15);
1196 output[0x7E] = checksum;
1197 output[0x7F] = checksum >> 8;
1205 \param ftdi pointer to ftdi_context
1206 \param eeprom Pointer to store eeprom into
1209 \retval -1: read failed
1211 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1215 for (i = 0; i < 64; i++) {
1216 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1217 ftdi_error_return(-1, "reading eeprom failed");
1226 \param ftdi pointer to ftdi_context
1227 \param eeprom Pointer to read eeprom from
1230 \retval -1: read failed
1232 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1234 unsigned short usb_val;
1237 for (i = 0; i < 64; i++) {
1238 usb_val = eeprom[i*2];
1239 usb_val += eeprom[(i*2)+1] << 8;
1240 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1241 ftdi_error_return(-1, "unable to write eeprom");
1250 \param ftdi pointer to ftdi_context
1253 \retval -1: erase failed
1255 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1257 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1258 ftdi_error_return(-1, "unable to erase eeprom");
1264 Get string representation for last error code
1266 \param ftdi pointer to ftdi_context
1268 \retval Pointer to error string
1270 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1272 return ftdi->error_str;
1276 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1277 and Matthias Kranz (matthias@hcilab.org)
1280 Set flowcontrol for ftdi chip
1282 \param ftdi pointer to ftdi_context
1283 \param flowctrl flow control to use. should be
1284 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1287 \retval -1: set flow control failed
1289 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1291 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1292 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1293 NULL, 0, ftdi->usb_write_timeout) != 0)
1294 ftdi_error_return(-1, "set flow control failed");
1302 \param ftdi pointer to ftdi_context
1303 \param state state to set line to (1 or 0)
1306 \retval -1: set dtr failed
1308 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1310 unsigned short usb_val;
1313 usb_val = SIO_SET_DTR_HIGH;
1315 usb_val = SIO_SET_DTR_LOW;
1317 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1318 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1319 NULL, 0, ftdi->usb_write_timeout) != 0)
1320 ftdi_error_return(-1, "set dtr failed");
1328 \param ftdi pointer to ftdi_context
1329 \param state state to set line to (1 or 0)
1332 \retval -1 set rts failed
1334 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1336 unsigned short usb_val;
1339 usb_val = SIO_SET_RTS_HIGH;
1341 usb_val = SIO_SET_RTS_LOW;
1343 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1344 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1345 NULL, 0, ftdi->usb_write_timeout) != 0)
1346 ftdi_error_return(-1, "set of rts failed");
1351 /* @} end of doxygen libftdi group */