1 /***************************************************************************
5 copyright : (C) 2003-2008 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/en/developer/libftdi/
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 */
39 #define ftdi_error_return(code, str) do { \
40 ftdi->error_str = str; \
46 Internal function to close usb device pointer.
47 Sets ftdi->usb_dev to NULL.
50 \param ftdi pointer to ftdi_context
54 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
58 libusb_close (ftdi->usb_dev);
64 Initializes a ftdi_context.
66 \param ftdi pointer to ftdi_context
69 \retval -1: couldn't allocate read buffer
71 \remark This should be called before all functions
73 int ftdi_init(struct ftdi_context *ftdi)
76 ftdi->usb_read_timeout = 5000;
77 ftdi->usb_write_timeout = 5000;
79 ftdi->type = TYPE_BM; /* chip type */
81 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
83 ftdi->readbuffer = NULL;
84 ftdi->readbuffer_offset = 0;
85 ftdi->readbuffer_remaining = 0;
86 ftdi->writebuffer_chunksize = 4096;
87 ftdi->max_packet_size = 0;
93 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
95 ftdi->error_str = NULL;
97 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
99 /* All fine. Now allocate the readbuffer */
100 return ftdi_read_data_set_chunksize(ftdi, 4096);
104 Allocate and initialize a new ftdi_context
106 \return a pointer to a new ftdi_context, or NULL on failure
108 struct ftdi_context *ftdi_new(void)
110 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
117 if (ftdi_init(ftdi) != 0)
127 Open selected channels on a chip, otherwise use first channel.
129 \param ftdi pointer to ftdi_context
130 \param interface Interface to use for FT2232C/2232H/4232H chips.
133 \retval -1: unknown interface
135 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
141 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
145 ftdi->index = INTERFACE_B;
151 ftdi->index = INTERFACE_C;
157 ftdi->index = INTERFACE_D;
162 ftdi_error_return(-1, "Unknown interface");
168 Deinitializes a ftdi_context.
170 \param ftdi pointer to ftdi_context
172 void ftdi_deinit(struct ftdi_context *ftdi)
174 ftdi_usb_close_internal (ftdi);
176 if (ftdi->readbuffer != NULL)
178 free(ftdi->readbuffer);
179 ftdi->readbuffer = NULL;
184 Deinitialize and free an ftdi_context.
186 \param ftdi pointer to ftdi_context
188 void ftdi_free(struct ftdi_context *ftdi)
195 Use an already open libusb device.
197 \param ftdi pointer to ftdi_context
198 \param usb libusb libusb_device_handle to use
200 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
207 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
208 needs to be deallocated by ftdi_list_free() after use.
210 \param ftdi pointer to ftdi_context
211 \param devlist Pointer where to store list of found devices
212 \param vendor Vendor ID to search for
213 \param product Product ID to search for
215 \retval >0: number of devices found
216 \retval -3: out of memory
217 \retval -4: libusb_init() failed
218 \retval -5: libusb_get_device_list() failed
219 \retval -6: libusb_get_device_descriptor() failed
221 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
223 struct ftdi_device_list **curdev;
225 libusb_device **devs;
229 if (libusb_init(NULL) < 0)
230 ftdi_error_return(-4, "libusb_init() failed");
232 if (libusb_get_device_list(NULL, &devs) < 0)
233 ftdi_error_return(-5, "libusb_get_device_list() failed");
238 while ((dev = devs[i++]) != NULL)
240 struct libusb_device_descriptor desc;
242 if (libusb_get_device_descriptor(dev, &desc) < 0)
243 ftdi_error_return(-6, "libusb_get_device_descriptor() failed");
245 if (desc.idVendor == vendor && desc.idProduct == product)
247 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
249 ftdi_error_return(-3, "out of memory");
251 (*curdev)->next = NULL;
252 (*curdev)->dev = dev;
254 curdev = &(*curdev)->next;
263 Frees a usb device list.
265 \param devlist USB device list created by ftdi_usb_find_all()
267 void ftdi_list_free(struct ftdi_device_list **devlist)
269 struct ftdi_device_list *curdev, *next;
271 for (curdev = *devlist; curdev != NULL;)
282 Frees a usb device list.
284 \param devlist USB device list created by ftdi_usb_find_all()
286 void ftdi_list_free2(struct ftdi_device_list *devlist)
288 ftdi_list_free(&devlist);
292 Return device ID strings from the usb device.
294 The parameters manufacturer, description and serial may be NULL
295 or pointer to buffers to store the fetched strings.
297 \note Use this function only in combination with ftdi_usb_find_all()
298 as it closes the internal "usb_dev" after use.
300 \param ftdi pointer to ftdi_context
301 \param dev libusb usb_dev to use
302 \param manufacturer Store manufacturer string here if not NULL
303 \param mnf_len Buffer size of manufacturer string
304 \param description Store product description string here if not NULL
305 \param desc_len Buffer size of product description string
306 \param serial Store serial string here if not NULL
307 \param serial_len Buffer size of serial string
310 \retval -1: wrong arguments
311 \retval -4: unable to open device
312 \retval -7: get product manufacturer failed
313 \retval -8: get product description failed
314 \retval -9: get serial number failed
315 \retval -11: libusb_get_device_descriptor() failed
317 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
318 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
320 struct libusb_device_descriptor desc;
322 if ((ftdi==NULL) || (dev==NULL))
325 if (libusb_open(dev, &ftdi->usb_dev) < 0)
326 ftdi_error_return(-4, "libusb_open() failed");
328 if (libusb_get_device_descriptor(dev, &desc) < 0)
329 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
331 if (manufacturer != NULL)
333 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
335 ftdi_usb_close_internal (ftdi);
336 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
340 if (description != NULL)
342 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
344 ftdi_usb_close_internal (ftdi);
345 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
351 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
353 ftdi_usb_close_internal (ftdi);
354 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
358 ftdi_usb_close_internal (ftdi);
364 * Internal function to determine the maximum packet size.
365 * \param ftdi pointer to ftdi_context
366 * \param dev libusb usb_dev to use
367 * \retval Maximum packet size for this device
369 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
371 struct libusb_device_descriptor desc;
372 struct libusb_config_descriptor *config0;
373 unsigned int packet_size;
375 // Determine maximum packet size. Init with default value.
376 // New hi-speed devices from FTDI use a packet size of 512 bytes
377 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
378 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
383 if (libusb_get_device_descriptor(dev, &desc) < 0)
386 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
389 if (desc.bNumConfigurations > 0)
391 if (ftdi->interface < config0->bNumInterfaces)
393 struct libusb_interface interface = config0->interface[ftdi->interface];
394 if (interface.num_altsetting > 0)
396 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
397 if (descriptor.bNumEndpoints > 0)
399 packet_size = descriptor.endpoint[0].wMaxPacketSize;
405 libusb_free_config_descriptor (config0);
410 Opens a ftdi device given by an usb_device.
412 \param ftdi pointer to ftdi_context
413 \param dev libusb usb_dev to use
416 \retval -3: unable to config device
417 \retval -4: unable to open device
418 \retval -5: unable to claim device
419 \retval -6: reset failed
420 \retval -7: set baudrate failed
421 \retval -9: libusb_get_device_descriptor() failed
422 \retval -10: libusb_get_config_descriptor() failed
423 \retval -11: libusb_etach_kernel_driver() failed
424 \retval -12: libusb_get_configuration() failed
426 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
428 struct libusb_device_descriptor desc;
429 struct libusb_config_descriptor *config0;
432 if (libusb_open(dev, &ftdi->usb_dev) < 0)
433 ftdi_error_return(-4, "libusb_open() failed");
435 if (libusb_get_device_descriptor(dev, &desc) < 0)
436 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
438 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
439 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
440 cfg0 = config0->bConfigurationValue;
441 libusb_free_config_descriptor (config0);
443 #ifdef LIBUSB_HAS_GET_DRIVER_NP
444 // Try to detach ftdi_sio kernel module.
445 // Returns ENODATA if driver is not loaded.
447 // The return code is kept in a separate variable and only parsed
448 // if usb_set_configuration() or usb_claim_interface() fails as the
449 // detach operation might be denied and everything still works fine.
450 // Likely scenario is a static ftdi_sio kernel module.
451 ret = libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface);
452 if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND)
453 ftdi_error_return(-11, "libusb_detach_kernel_driver () failed");
456 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
457 ftdi_error_return(-12, "libusb_get_configuration () failed");
459 // set configuration (needed especially for windows)
460 // tolerate EBUSY: one device with one configuration, but two interfaces
461 // and libftdi sessions to both interfaces (e.g. FT2232)
462 if (desc.bNumConfigurations > 0 && cfg != cfg0)
464 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
466 ftdi_usb_close_internal (ftdi);
467 ftdi_error_return(-3, "unable to set usb configuration. Make sure ftdi_sio is unloaded!");
471 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
473 ftdi_usb_close_internal (ftdi);
474 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
477 if (ftdi_usb_reset (ftdi) != 0)
479 ftdi_usb_close_internal (ftdi);
480 ftdi_error_return(-6, "ftdi_usb_reset failed");
483 // Try to guess chip type
484 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
485 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
486 && desc.iSerialNumber == 0))
487 ftdi->type = TYPE_BM;
488 else if (desc.bcdDevice == 0x200)
489 ftdi->type = TYPE_AM;
490 else if (desc.bcdDevice == 0x500)
491 ftdi->type = TYPE_2232C;
492 else if (desc.bcdDevice == 0x600)
494 else if (desc.bcdDevice == 0x700)
495 ftdi->type = TYPE_2232H;
496 else if (desc.bcdDevice == 0x800)
497 ftdi->type = TYPE_4232H;
499 // Set default interface on dual/quad type chips
506 ftdi->index = INTERFACE_A;
512 // Determine maximum packet size
513 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
515 if (ftdi_set_baudrate (ftdi, 9600) != 0)
517 ftdi_usb_close_internal (ftdi);
518 ftdi_error_return(-7, "set baudrate failed");
521 ftdi_error_return(0, "all fine");
525 Opens the first device with a given vendor and product ids.
527 \param ftdi pointer to ftdi_context
528 \param vendor Vendor ID
529 \param product Product ID
531 \retval same as ftdi_usb_open_desc()
533 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
535 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
539 Opens the first device with a given, vendor id, product id,
540 description and serial.
542 \param ftdi pointer to ftdi_context
543 \param vendor Vendor ID
544 \param product Product ID
545 \param description Description to search for. Use NULL if not needed.
546 \param serial Serial to search for. Use NULL if not needed.
549 \retval -3: usb device not found
550 \retval -4: unable to open device
551 \retval -5: unable to claim device
552 \retval -6: reset failed
553 \retval -7: set baudrate failed
554 \retval -8: get product description failed
555 \retval -9: get serial number failed
556 \retval -11: libusb_init() failed
557 \retval -12: libusb_get_device_list() failed
558 \retval -13: libusb_get_device_descriptor() failed
560 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
561 const char* description, const char* serial)
563 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
567 Opens the index-th device with a given, vendor id, product id,
568 description and serial.
570 \param ftdi pointer to ftdi_context
571 \param vendor Vendor ID
572 \param product Product ID
573 \param description Description to search for. Use NULL if not needed.
574 \param serial Serial to search for. Use NULL if not needed.
575 \param index Number of matching device to open if there are more than one, starts with 0.
578 \retval -1: usb_find_busses() failed
579 \retval -2: usb_find_devices() failed
580 \retval -3: usb device not found
581 \retval -4: unable to open device
582 \retval -5: unable to claim device
583 \retval -6: reset failed
584 \retval -7: set baudrate failed
585 \retval -8: get product description failed
586 \retval -9: get serial number failed
587 \retval -10: unable to close device
589 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
590 const char* description, const char* serial, unsigned int index)
593 libusb_device **devs;
597 if (libusb_init(NULL) < 0)
598 ftdi_error_return(-11, "libusb_init() failed");
600 if (libusb_get_device_list(NULL, &devs) < 0)
601 ftdi_error_return(-12, "libusb_get_device_list() failed");
603 while ((dev = devs[i++]) != NULL)
605 struct libusb_device_descriptor desc;
607 if (libusb_get_device_descriptor(dev, &desc) < 0)
608 ftdi_error_return(-13, "libusb_get_device_descriptor() failed");
610 if (desc.idVendor == vendor && desc.idProduct == product)
612 if (libusb_open(dev, &ftdi->usb_dev) < 0)
613 ftdi_error_return(-4, "usb_open() failed");
615 if (description != NULL)
617 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
619 libusb_close (ftdi->usb_dev);
620 ftdi_error_return(-8, "unable to fetch product description");
622 if (strncmp(string, description, sizeof(string)) != 0)
624 libusb_close (ftdi->usb_dev);
630 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
632 ftdi_usb_close_internal (ftdi);
633 ftdi_error_return(-9, "unable to fetch serial number");
635 if (strncmp(string, serial, sizeof(string)) != 0)
637 ftdi_usb_close_internal (ftdi);
642 ftdi_usb_close_internal (ftdi);
650 return ftdi_usb_open_dev(ftdi, dev);
655 ftdi_error_return(-3, "device not found");
659 Opens the ftdi-device described by a description-string.
660 Intended to be used for parsing a device-description given as commandline argument.
662 \param ftdi pointer to ftdi_context
663 \param description NULL-terminated description-string, using this format:
664 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
665 \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
666 \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one
667 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
669 \note The description format may be extended in later versions.
672 \retval -1: libusb_init() failed
673 \retval -2: libusb_get_device_list() failed
674 \retval -3: usb device not found
675 \retval -4: unable to open device
676 \retval -5: unable to claim device
677 \retval -6: reset failed
678 \retval -7: set baudrate failed
679 \retval -8: get product description failed
680 \retval -9: get serial number failed
681 \retval -10: unable to close device
682 \retval -11: illegal description format
684 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
686 if (description[0] == 0 || description[1] != ':')
687 ftdi_error_return(-11, "illegal description format");
689 if (description[0] == 'd')
692 libusb_device **devs;
693 unsigned int bus_number, device_address;
696 if (libusb_init (NULL) < 0)
697 ftdi_error_return(-1, "libusb_init() failed");
699 if (libusb_get_device_list(NULL, &devs) < 0)
700 ftdi_error_return(-2, "libusb_get_device_list() failed");
702 /* XXX: This doesn't handle symlinks/odd paths/etc... */
703 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
704 ftdi_error_return(-11, "illegal description format");
706 while ((dev = devs[i++]) != NULL)
708 if (bus_number == libusb_get_bus_number (dev)
709 && device_address == libusb_get_device_address (dev))
710 return ftdi_usb_open_dev(ftdi, dev);
714 ftdi_error_return(-3, "device not found");
716 else if (description[0] == 'i' || description[0] == 's')
719 unsigned int product;
720 unsigned int index=0;
721 const char *serial=NULL;
722 const char *startp, *endp;
725 startp=description+2;
726 vendor=strtoul((char*)startp,(char**)&endp,0);
727 if (*endp != ':' || endp == startp || errno != 0)
728 ftdi_error_return(-11, "illegal description format");
731 product=strtoul((char*)startp,(char**)&endp,0);
732 if (endp == startp || errno != 0)
733 ftdi_error_return(-11, "illegal description format");
735 if (description[0] == 'i' && *endp != 0)
737 /* optional index field in i-mode */
739 ftdi_error_return(-11, "illegal description format");
742 index=strtoul((char*)startp,(char**)&endp,0);
743 if (*endp != 0 || endp == startp || errno != 0)
744 ftdi_error_return(-11, "illegal description format");
746 if (description[0] == 's')
749 ftdi_error_return(-11, "illegal description format");
751 /* rest of the description is the serial */
755 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
759 ftdi_error_return(-11, "illegal description format");
764 Resets the ftdi device.
766 \param ftdi pointer to ftdi_context
769 \retval -1: FTDI reset failed
771 int ftdi_usb_reset(struct ftdi_context *ftdi)
773 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
774 SIO_RESET_REQUEST, SIO_RESET_SIO,
775 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
776 ftdi_error_return(-1,"FTDI reset failed");
778 // Invalidate data in the readbuffer
779 ftdi->readbuffer_offset = 0;
780 ftdi->readbuffer_remaining = 0;
786 Clears the read buffer on the chip and the internal read buffer.
788 \param ftdi pointer to ftdi_context
791 \retval -1: read buffer purge failed
793 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
795 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
796 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
797 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
798 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
800 // Invalidate data in the readbuffer
801 ftdi->readbuffer_offset = 0;
802 ftdi->readbuffer_remaining = 0;
808 Clears the write buffer on the chip.
810 \param ftdi pointer to ftdi_context
813 \retval -1: write buffer purge failed
815 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
817 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
818 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
819 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
820 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
826 Clears the buffers on the chip and the internal read buffer.
828 \param ftdi pointer to ftdi_context
831 \retval -1: read buffer purge failed
832 \retval -2: write buffer purge failed
834 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
838 result = ftdi_usb_purge_rx_buffer(ftdi);
842 result = ftdi_usb_purge_tx_buffer(ftdi);
852 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
854 \param ftdi pointer to ftdi_context
857 \retval -1: usb_release failed
859 int ftdi_usb_close(struct ftdi_context *ftdi)
863 if (ftdi->usb_dev != NULL)
864 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
867 ftdi_usb_close_internal (ftdi);
873 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
874 Function is only used internally
877 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
878 unsigned short *value, unsigned short *index)
880 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
881 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
882 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
883 int divisor, best_divisor, best_baud, best_baud_diff;
884 unsigned long encoded_divisor;
893 divisor = 24000000 / baudrate;
895 if (ftdi->type == TYPE_AM)
897 // Round down to supported fraction (AM only)
898 divisor -= am_adjust_dn[divisor & 7];
901 // Try this divisor and the one above it (because division rounds down)
905 for (i = 0; i < 2; i++)
907 int try_divisor = divisor + i;
911 // Round up to supported divisor value
912 if (try_divisor <= 8)
914 // Round up to minimum supported divisor
917 else if (ftdi->type != TYPE_AM && try_divisor < 12)
919 // BM doesn't support divisors 9 through 11 inclusive
922 else if (divisor < 16)
924 // AM doesn't support divisors 9 through 15 inclusive
929 if (ftdi->type == TYPE_AM)
931 // Round up to supported fraction (AM only)
932 try_divisor += am_adjust_up[try_divisor & 7];
933 if (try_divisor > 0x1FFF8)
935 // Round down to maximum supported divisor value (for AM)
936 try_divisor = 0x1FFF8;
941 if (try_divisor > 0x1FFFF)
943 // Round down to maximum supported divisor value (for BM)
944 try_divisor = 0x1FFFF;
948 // Get estimated baud rate (to nearest integer)
949 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
950 // Get absolute difference from requested baud rate
951 if (baud_estimate < baudrate)
953 baud_diff = baudrate - baud_estimate;
957 baud_diff = baud_estimate - baudrate;
959 if (i == 0 || baud_diff < best_baud_diff)
961 // Closest to requested baud rate so far
962 best_divisor = try_divisor;
963 best_baud = baud_estimate;
964 best_baud_diff = baud_diff;
967 // Spot on! No point trying
972 // Encode the best divisor value
973 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
974 // Deal with special cases for encoded value
975 if (encoded_divisor == 1)
977 encoded_divisor = 0; // 3000000 baud
979 else if (encoded_divisor == 0x4001)
981 encoded_divisor = 1; // 2000000 baud (BM only)
983 // Split into "value" and "index" values
984 *value = (unsigned short)(encoded_divisor & 0xFFFF);
985 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
987 *index = (unsigned short)(encoded_divisor >> 8);
989 *index |= ftdi->index;
992 *index = (unsigned short)(encoded_divisor >> 16);
994 // Return the nearest baud rate
999 Sets the chip baud rate
1001 \param ftdi pointer to ftdi_context
1002 \param baudrate baud rate to set
1005 \retval -1: invalid baudrate
1006 \retval -2: setting baudrate failed
1008 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1010 unsigned short value, index;
1011 int actual_baudrate;
1013 if (ftdi->bitbang_enabled)
1015 baudrate = baudrate*4;
1018 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1019 if (actual_baudrate <= 0)
1020 ftdi_error_return (-1, "Silly baudrate <= 0.");
1022 // Check within tolerance (about 5%)
1023 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1024 || ((actual_baudrate < baudrate)
1025 ? (actual_baudrate * 21 < baudrate * 20)
1026 : (baudrate * 21 < actual_baudrate * 20)))
1027 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1029 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1030 SIO_SET_BAUDRATE_REQUEST, value,
1031 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1032 ftdi_error_return (-2, "Setting new baudrate failed");
1034 ftdi->baudrate = baudrate;
1039 Set (RS232) line characteristics.
1040 The break type can only be set via ftdi_set_line_property2()
1041 and defaults to "off".
1043 \param ftdi pointer to ftdi_context
1044 \param bits Number of bits
1045 \param sbit Number of stop bits
1046 \param parity Parity mode
1049 \retval -1: Setting line property failed
1051 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1052 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1054 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1058 Set (RS232) line characteristics
1060 \param ftdi pointer to ftdi_context
1061 \param bits Number of bits
1062 \param sbit Number of stop bits
1063 \param parity Parity mode
1064 \param break_type Break type
1067 \retval -1: Setting line property failed
1069 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1070 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1071 enum ftdi_break_type break_type)
1073 unsigned short value = bits;
1078 value |= (0x00 << 8);
1081 value |= (0x01 << 8);
1084 value |= (0x02 << 8);
1087 value |= (0x03 << 8);
1090 value |= (0x04 << 8);
1097 value |= (0x00 << 11);
1100 value |= (0x01 << 11);
1103 value |= (0x02 << 11);
1110 value |= (0x00 << 14);
1113 value |= (0x01 << 14);
1117 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1118 SIO_SET_DATA_REQUEST, value,
1119 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1120 ftdi_error_return (-1, "Setting new line property failed");
1126 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1128 \param ftdi pointer to ftdi_context
1129 \param buf Buffer with the data
1130 \param size Size of the buffer
1132 \retval <0: error code from usb_bulk_write()
1133 \retval >0: number of bytes written
1135 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1140 while (offset < size)
1142 int write_size = ftdi->writebuffer_chunksize;
1144 if (offset+write_size > size)
1145 write_size = size-offset;
1147 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1148 ftdi_error_return(-1, "usb bulk write failed");
1150 offset += actual_length;
1156 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1157 #ifdef USB_CLASS_PTP
1158 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1160 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1162 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1163 struct ftdi_context *ftdi = tc->ftdi;
1164 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1166 // New hi-speed devices from FTDI use a packet size of 512 bytes
1167 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1172 actual_length = transfer->actual_length;
1174 if (actual_length > 2)
1176 // skip FTDI status bytes.
1177 // Maybe stored in the future to enable modem use
1178 num_of_chunks = actual_length / packet_size;
1179 chunk_remains = actual_length % packet_size;
1180 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1182 ftdi->readbuffer_offset += 2;
1185 if (actual_length > packet_size - 2)
1187 for (i = 1; i < num_of_chunks; i++)
1188 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1189 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1191 if (chunk_remains > 2)
1193 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1194 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1196 actual_length -= 2*num_of_chunks;
1199 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1202 if (actual_length > 0)
1204 // data still fits in buf?
1205 if (tc->offset + actual_length <= tc->size)
1207 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1208 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1209 tc->offset += actual_length;
1211 ftdi->readbuffer_offset = 0;
1212 ftdi->readbuffer_remaining = 0;
1214 /* Did we read exactly the right amount of bytes? */
1215 if (tc->offset == tc->size)
1217 //printf("read_data exact rem %d offset %d\n",
1218 //ftdi->readbuffer_remaining, offset);
1225 // only copy part of the data or size <= readbuffer_chunksize
1226 int part_size = tc->size - tc->offset;
1227 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1228 tc->offset += part_size;
1230 ftdi->readbuffer_offset += part_size;
1231 ftdi->readbuffer_remaining = actual_length - part_size;
1233 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1234 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1240 ret = libusb_submit_transfer (transfer);
1246 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1248 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1249 struct ftdi_context *ftdi = tc->ftdi;
1251 tc->offset = transfer->actual_length;
1253 if (tc->offset == tc->size)
1259 int write_size = ftdi->writebuffer_chunksize;
1262 if (tc->offset + write_size > tc->size)
1263 write_size = tc->size - tc->offset;
1265 transfer->length = write_size;
1266 transfer->buffer = tc->buf + tc->offset;
1267 ret = libusb_submit_transfer (transfer);
1275 Writes data to the chip. Does not wait for completion of the transfer
1276 nor does it make sure that the transfer was successful.
1278 Use libusb 1.0 Asynchronous API.
1279 Only available if compiled with --with-async-mode.
1281 \param ftdi pointer to ftdi_context
1282 \param buf Buffer with the data
1283 \param size Size of the buffer
1285 \retval NULL: Some error happens when submit transfer
1286 \retval !NULL: Pointer to a ftdi_transfer_control
1289 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1291 struct ftdi_transfer_control *tc;
1292 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1293 int write_size, ret;
1295 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1297 if (!tc || !transfer)
1306 if (size < ftdi->writebuffer_chunksize)
1309 write_size = ftdi->writebuffer_chunksize;
1311 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf, write_size, ftdi_write_data_cb, tc, ftdi->usb_write_timeout);
1312 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1314 ret = libusb_submit_transfer(transfer);
1317 libusb_free_transfer(transfer);
1319 tc->transfer = NULL;
1322 tc->transfer = transfer;
1328 Reads data from the chip. Does not wait for completion of the transfer
1329 nor does it make sure that the transfer was successful.
1331 Use libusb 1.0 Asynchronous API.
1332 Only available if compiled with --with-async-mode.
1334 \param ftdi pointer to ftdi_context
1335 \param buf Buffer with the data
1336 \param size Size of the buffer
1338 \retval NULL: Some error happens when submit transfer
1339 \retval !NULL: Pointer to a ftdi_transfer_control
1342 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1344 struct ftdi_transfer_control *tc;
1345 struct libusb_transfer *transfer;
1348 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1356 if (size <= ftdi->readbuffer_remaining)
1358 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1361 ftdi->readbuffer_remaining -= size;
1362 ftdi->readbuffer_offset += size;
1364 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1368 tc->transfer = NULL;
1373 if (ftdi->readbuffer_remaining != 0)
1375 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1377 tc->offset = ftdi->readbuffer_remaining;
1382 transfer = libusb_alloc_transfer(0);
1389 ftdi->readbuffer_remaining = 0;
1390 ftdi->readbuffer_offset = 0;
1392 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout);
1393 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1395 ret = libusb_submit_transfer(transfer);
1398 libusb_free_transfer(transfer);
1402 tc->transfer = transfer;
1408 Wait for completion of the transfer.
1410 Use libusb 1.0 Asynchronous API.
1411 Only available if compiled with --with-async-mode.
1413 \param tc pointer to ftdi_transfer_control
1415 \retval < 0: Some error happens
1416 \retval >= 0: Data size transferred
1419 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1423 while (!tc->completed)
1425 ret = libusb_handle_events(NULL);
1428 if (ret == LIBUSB_ERROR_INTERRUPTED)
1430 libusb_cancel_transfer(tc->transfer);
1431 while (!tc->completed)
1432 if (libusb_handle_events(NULL) < 0)
1434 libusb_free_transfer(tc->transfer);
1441 if (tc->transfer->status == LIBUSB_TRANSFER_COMPLETED)
1446 libusb_free_transfer(tc->transfer);
1451 #endif // LIBFTDI_LINUX_ASYNC_MODE
1454 Configure write buffer chunk size.
1457 \param ftdi pointer to ftdi_context
1458 \param chunksize Chunk size
1462 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1464 ftdi->writebuffer_chunksize = chunksize;
1469 Get write buffer chunk size.
1471 \param ftdi pointer to ftdi_context
1472 \param chunksize Pointer to store chunk size in
1476 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1478 *chunksize = ftdi->writebuffer_chunksize;
1483 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1485 Automatically strips the two modem status bytes transfered during every read.
1487 \param ftdi pointer to ftdi_context
1488 \param buf Buffer to store data in
1489 \param size Size of the buffer
1491 \retval <0: error code from libusb_bulk_transfer()
1492 \retval 0: no data was available
1493 \retval >0: number of bytes read
1496 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1498 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1499 int packet_size = ftdi->max_packet_size;
1500 int actual_length = 1;
1502 // Packet size sanity check (avoid division by zero)
1503 if (packet_size == 0)
1504 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1506 // everything we want is still in the readbuffer?
1507 if (size <= ftdi->readbuffer_remaining)
1509 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1512 ftdi->readbuffer_remaining -= size;
1513 ftdi->readbuffer_offset += size;
1515 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1519 // something still in the readbuffer, but not enough to satisfy 'size'?
1520 if (ftdi->readbuffer_remaining != 0)
1522 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1525 offset += ftdi->readbuffer_remaining;
1527 // do the actual USB read
1528 while (offset < size && actual_length > 0)
1530 ftdi->readbuffer_remaining = 0;
1531 ftdi->readbuffer_offset = 0;
1532 /* returns how much received */
1533 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1535 ftdi_error_return(ret, "usb bulk read failed");
1537 if (actual_length > 2)
1539 // skip FTDI status bytes.
1540 // Maybe stored in the future to enable modem use
1541 num_of_chunks = actual_length / packet_size;
1542 chunk_remains = actual_length % packet_size;
1543 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1545 ftdi->readbuffer_offset += 2;
1548 if (actual_length > packet_size - 2)
1550 for (i = 1; i < num_of_chunks; i++)
1551 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1552 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1554 if (chunk_remains > 2)
1556 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1557 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1559 actual_length -= 2*num_of_chunks;
1562 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1565 else if (actual_length <= 2)
1567 // no more data to read?
1570 if (actual_length > 0)
1572 // data still fits in buf?
1573 if (offset+actual_length <= size)
1575 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1576 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1577 offset += actual_length;
1579 /* Did we read exactly the right amount of bytes? */
1581 //printf("read_data exact rem %d offset %d\n",
1582 //ftdi->readbuffer_remaining, offset);
1587 // only copy part of the data or size <= readbuffer_chunksize
1588 int part_size = size-offset;
1589 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1591 ftdi->readbuffer_offset += part_size;
1592 ftdi->readbuffer_remaining = actual_length-part_size;
1593 offset += part_size;
1595 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1596 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1607 Configure read buffer chunk size.
1610 Automatically reallocates the buffer.
1612 \param ftdi pointer to ftdi_context
1613 \param chunksize Chunk size
1617 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1619 unsigned char *new_buf;
1621 // Invalidate all remaining data
1622 ftdi->readbuffer_offset = 0;
1623 ftdi->readbuffer_remaining = 0;
1625 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1626 ftdi_error_return(-1, "out of memory for readbuffer");
1628 ftdi->readbuffer = new_buf;
1629 ftdi->readbuffer_chunksize = chunksize;
1635 Get read buffer chunk size.
1637 \param ftdi pointer to ftdi_context
1638 \param chunksize Pointer to store chunk size in
1642 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1644 *chunksize = ftdi->readbuffer_chunksize;
1650 Enable bitbang mode.
1652 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1654 \param ftdi pointer to ftdi_context
1655 \param bitmask Bitmask to configure lines.
1656 HIGH/ON value configures a line as output.
1659 \retval -1: can't enable bitbang mode
1661 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1663 unsigned short usb_val;
1665 usb_val = bitmask; // low byte: bitmask
1666 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1667 usb_val |= (ftdi->bitbang_mode << 8);
1669 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1670 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1671 NULL, 0, ftdi->usb_write_timeout) < 0)
1672 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1674 ftdi->bitbang_enabled = 1;
1679 Disable bitbang mode.
1681 \param ftdi pointer to ftdi_context
1684 \retval -1: can't disable bitbang mode
1686 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1688 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1689 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1691 ftdi->bitbang_enabled = 0;
1696 Enable/disable bitbang modes.
1698 \param ftdi pointer to ftdi_context
1699 \param bitmask Bitmask to configure lines.
1700 HIGH/ON value configures a line as output.
1701 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1704 \retval -1: can't enable bitbang mode
1706 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1708 unsigned short usb_val;
1710 usb_val = bitmask; // low byte: bitmask
1711 usb_val |= (mode << 8);
1712 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1713 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1715 ftdi->bitbang_mode = mode;
1716 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1721 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1723 \param ftdi pointer to ftdi_context
1724 \param pins Pointer to store pins into
1727 \retval -1: read pins failed
1729 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1731 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1)
1732 ftdi_error_return(-1, "read pins failed");
1740 The FTDI chip keeps data in the internal buffer for a specific
1741 amount of time if the buffer is not full yet to decrease
1742 load on the usb bus.
1744 \param ftdi pointer to ftdi_context
1745 \param latency Value between 1 and 255
1748 \retval -1: latency out of range
1749 \retval -2: unable to set latency timer
1751 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1753 unsigned short usb_val;
1756 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1759 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1760 ftdi_error_return(-2, "unable to set latency timer");
1768 \param ftdi pointer to ftdi_context
1769 \param latency Pointer to store latency value in
1772 \retval -1: unable to get latency timer
1774 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1776 unsigned short usb_val;
1777 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1778 ftdi_error_return(-1, "reading latency timer failed");
1780 *latency = (unsigned char)usb_val;
1785 Poll modem status information
1787 This function allows the retrieve the two status bytes of the device.
1788 The device sends these bytes also as a header for each read access
1789 where they are discarded by ftdi_read_data(). The chip generates
1790 the two stripped status bytes in the absence of data every 40 ms.
1792 Layout of the first byte:
1793 - B0..B3 - must be 0
1794 - B4 Clear to send (CTS)
1797 - B5 Data set ready (DTS)
1800 - B6 Ring indicator (RI)
1803 - B7 Receive line signal detect (RLSD)
1807 Layout of the second byte:
1808 - B0 Data ready (DR)
1809 - B1 Overrun error (OE)
1810 - B2 Parity error (PE)
1811 - B3 Framing error (FE)
1812 - B4 Break interrupt (BI)
1813 - B5 Transmitter holding register (THRE)
1814 - B6 Transmitter empty (TEMT)
1815 - B7 Error in RCVR FIFO
1817 \param ftdi pointer to ftdi_context
1818 \param status Pointer to store status information in. Must be two bytes.
1821 \retval -1: unable to retrieve status information
1823 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1827 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
1828 ftdi_error_return(-1, "getting modem status failed");
1830 *status = (usb_val[1] << 8) | usb_val[0];
1836 Set flowcontrol for ftdi chip
1838 \param ftdi pointer to ftdi_context
1839 \param flowctrl flow control to use. should be
1840 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1843 \retval -1: set flow control failed
1845 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1847 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1848 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1849 NULL, 0, ftdi->usb_write_timeout) < 0)
1850 ftdi_error_return(-1, "set flow control failed");
1858 \param ftdi pointer to ftdi_context
1859 \param state state to set line to (1 or 0)
1862 \retval -1: set dtr failed
1864 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1866 unsigned short usb_val;
1869 usb_val = SIO_SET_DTR_HIGH;
1871 usb_val = SIO_SET_DTR_LOW;
1873 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1874 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1875 NULL, 0, ftdi->usb_write_timeout) < 0)
1876 ftdi_error_return(-1, "set dtr failed");
1884 \param ftdi pointer to ftdi_context
1885 \param state state to set line to (1 or 0)
1888 \retval -1 set rts failed
1890 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1892 unsigned short usb_val;
1895 usb_val = SIO_SET_RTS_HIGH;
1897 usb_val = SIO_SET_RTS_LOW;
1899 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1900 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1901 NULL, 0, ftdi->usb_write_timeout) < 0)
1902 ftdi_error_return(-1, "set of rts failed");
1908 Set dtr and rts line in one pass
1910 \param ftdi pointer to ftdi_context
1911 \param dtr DTR state to set line to (1 or 0)
1912 \param rts RTS state to set line to (1 or 0)
1915 \retval -1 set dtr/rts failed
1917 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
1919 unsigned short usb_val;
1922 usb_val = SIO_SET_DTR_HIGH;
1924 usb_val = SIO_SET_DTR_LOW;
1927 usb_val |= SIO_SET_RTS_HIGH;
1929 usb_val |= SIO_SET_RTS_LOW;
1931 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1932 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1933 NULL, 0, ftdi->usb_write_timeout) < 0)
1934 ftdi_error_return(-1, "set of rts/dtr failed");
1940 Set the special event character
1942 \param ftdi pointer to ftdi_context
1943 \param eventch Event character
1944 \param enable 0 to disable the event character, non-zero otherwise
1947 \retval -1: unable to set event character
1949 int ftdi_set_event_char(struct ftdi_context *ftdi,
1950 unsigned char eventch, unsigned char enable)
1952 unsigned short usb_val;
1958 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1959 ftdi_error_return(-1, "setting event character failed");
1967 \param ftdi pointer to ftdi_context
1968 \param errorch Error character
1969 \param enable 0 to disable the error character, non-zero otherwise
1972 \retval -1: unable to set error character
1974 int ftdi_set_error_char(struct ftdi_context *ftdi,
1975 unsigned char errorch, unsigned char enable)
1977 unsigned short usb_val;
1983 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1984 ftdi_error_return(-1, "setting error character failed");
1992 \param ftdi pointer to ftdi_context
1993 \param eeprom Pointer to ftdi_eeprom
1997 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
1999 ftdi->eeprom_size=size;
2004 Init eeprom with default values.
2006 \param eeprom Pointer to ftdi_eeprom
2008 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2010 eeprom->vendor_id = 0x0403;
2011 eeprom->product_id = 0x6001;
2013 eeprom->self_powered = 1;
2014 eeprom->remote_wakeup = 1;
2015 eeprom->BM_type_chip = 1;
2017 eeprom->in_is_isochronous = 0;
2018 eeprom->out_is_isochronous = 0;
2019 eeprom->suspend_pull_downs = 0;
2021 eeprom->use_serial = 0;
2022 eeprom->change_usb_version = 0;
2023 eeprom->usb_version = 0x0200;
2024 eeprom->max_power = 0;
2026 eeprom->manufacturer = NULL;
2027 eeprom->product = NULL;
2028 eeprom->serial = NULL;
2030 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2034 Build binary output from ftdi_eeprom structure.
2035 Output is suitable for ftdi_write_eeprom().
2037 \param eeprom Pointer to ftdi_eeprom
2038 \param output Buffer of 128 bytes to store eeprom image to
2040 \retval >0: used eeprom size
2041 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2043 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2046 unsigned short checksum, value;
2047 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2050 if (eeprom->manufacturer != NULL)
2051 manufacturer_size = strlen(eeprom->manufacturer);
2052 if (eeprom->product != NULL)
2053 product_size = strlen(eeprom->product);
2054 if (eeprom->serial != NULL)
2055 serial_size = strlen(eeprom->serial);
2057 size_check = eeprom->size;
2058 size_check -= 28; // 28 are always in use (fixed)
2060 // Top half of a 256byte eeprom is used just for strings and checksum
2061 // it seems that the FTDI chip will not read these strings from the lower half
2062 // Each string starts with two bytes; offset and type (0x03 for string)
2063 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2064 if (eeprom->size>=256)size_check = 120;
2065 size_check -= manufacturer_size*2;
2066 size_check -= product_size*2;
2067 size_check -= serial_size*2;
2069 // eeprom size exceeded?
2074 memset (output, 0, eeprom->size);
2076 // Addr 00: Stay 00 00
2077 // Addr 02: Vendor ID
2078 output[0x02] = eeprom->vendor_id;
2079 output[0x03] = eeprom->vendor_id >> 8;
2081 // Addr 04: Product ID
2082 output[0x04] = eeprom->product_id;
2083 output[0x05] = eeprom->product_id >> 8;
2085 // Addr 06: Device release number (0400h for BM features)
2086 output[0x06] = 0x00;
2088 if (eeprom->BM_type_chip == 1)
2089 output[0x07] = 0x04;
2091 output[0x07] = 0x02;
2093 // Addr 08: Config descriptor
2095 // Bit 6: 1 if this device is self powered, 0 if bus powered
2096 // Bit 5: 1 if this device uses remote wakeup
2097 // Bit 4: 1 if this device is battery powered
2099 if (eeprom->self_powered == 1)
2101 if (eeprom->remote_wakeup == 1)
2105 // Addr 09: Max power consumption: max power = value * 2 mA
2106 output[0x09] = eeprom->max_power;
2108 // Addr 0A: Chip configuration
2109 // Bit 7: 0 - reserved
2110 // Bit 6: 0 - reserved
2111 // Bit 5: 0 - reserved
2112 // Bit 4: 1 - Change USB version
2113 // Bit 3: 1 - Use the serial number string
2114 // Bit 2: 1 - Enable suspend pull downs for lower power
2115 // Bit 1: 1 - Out EndPoint is Isochronous
2116 // Bit 0: 1 - In EndPoint is Isochronous
2119 if (eeprom->in_is_isochronous == 1)
2121 if (eeprom->out_is_isochronous == 1)
2123 if (eeprom->suspend_pull_downs == 1)
2125 if (eeprom->use_serial == 1)
2127 if (eeprom->change_usb_version == 1)
2131 // Addr 0B: reserved
2132 output[0x0B] = 0x00;
2134 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2135 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2136 if (eeprom->change_usb_version == 1)
2138 output[0x0C] = eeprom->usb_version;
2139 output[0x0D] = eeprom->usb_version >> 8;
2143 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2144 // Addr 0F: Length of manufacturer string
2145 output[0x0F] = manufacturer_size*2 + 2;
2147 // Addr 10: Offset of the product string + 0x80, calculated later
2148 // Addr 11: Length of product string
2149 output[0x11] = product_size*2 + 2;
2151 // Addr 12: Offset of the serial string + 0x80, calculated later
2152 // Addr 13: Length of serial string
2153 output[0x13] = serial_size*2 + 2;
2157 if (eeprom->size>=256) i = 0x80;
2160 // Output manufacturer
2161 output[0x0E] = i | 0x80; // calculate offset
2162 output[i++] = manufacturer_size*2 + 2;
2163 output[i++] = 0x03; // type: string
2164 for (j = 0; j < manufacturer_size; j++)
2166 output[i] = eeprom->manufacturer[j], i++;
2167 output[i] = 0x00, i++;
2170 // Output product name
2171 output[0x10] = i | 0x80; // calculate offset
2172 output[i] = product_size*2 + 2, i++;
2173 output[i] = 0x03, i++;
2174 for (j = 0; j < product_size; j++)
2176 output[i] = eeprom->product[j], i++;
2177 output[i] = 0x00, i++;
2181 output[0x12] = i | 0x80; // calculate offset
2182 output[i] = serial_size*2 + 2, i++;
2183 output[i] = 0x03, i++;
2184 for (j = 0; j < serial_size; j++)
2186 output[i] = eeprom->serial[j], i++;
2187 output[i] = 0x00, i++;
2190 // calculate checksum
2193 for (i = 0; i < eeprom->size/2-1; i++)
2195 value = output[i*2];
2196 value += output[(i*2)+1] << 8;
2198 checksum = value^checksum;
2199 checksum = (checksum << 1) | (checksum >> 15);
2202 output[eeprom->size-2] = checksum;
2203 output[eeprom->size-1] = checksum >> 8;
2209 Decode binary EEPROM image into an ftdi_eeprom structure.
2211 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2212 \param buf Buffer of \a size bytes of raw eeprom data
2213 \param size size size of eeprom data in bytes
2216 \retval -1: something went wrong
2218 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2219 FIXME: Strings are malloc'ed here and should be freed somewhere
2221 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2224 unsigned short checksum, eeprom_checksum, value;
2225 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2226 int eeprom_size = 128;
2228 size_check = eeprom->size;
2229 size_check -= 28; // 28 are always in use (fixed)
2231 // Top half of a 256byte eeprom is used just for strings and checksum
2232 // it seems that the FTDI chip will not read these strings from the lower half
2233 // Each string starts with two bytes; offset and type (0x03 for string)
2234 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2235 if (eeprom->size>=256)size_check = 120;
2236 size_check -= manufacturer_size*2;
2237 size_check -= product_size*2;
2238 size_check -= serial_size*2;
2240 // eeprom size exceeded?
2245 // empty eeprom struct
2246 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2248 // Addr 00: Stay 00 00
2250 // Addr 02: Vendor ID
2251 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2253 // Addr 04: Product ID
2254 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2256 value = buf[0x06] + (buf[0x07]<<8);
2260 eeprom->BM_type_chip = 1;
2263 eeprom->BM_type_chip = 0;
2265 default: // Unknown device
2266 eeprom->BM_type_chip = 0;
2270 // Addr 08: Config descriptor
2272 // Bit 6: 1 if this device is self powered, 0 if bus powered
2273 // Bit 5: 1 if this device uses remote wakeup
2274 // Bit 4: 1 if this device is battery powered
2276 if (j&0x40) eeprom->self_powered = 1;
2277 if (j&0x20) eeprom->remote_wakeup = 1;
2279 // Addr 09: Max power consumption: max power = value * 2 mA
2280 eeprom->max_power = buf[0x09];
2282 // Addr 0A: Chip configuration
2283 // Bit 7: 0 - reserved
2284 // Bit 6: 0 - reserved
2285 // Bit 5: 0 - reserved
2286 // Bit 4: 1 - Change USB version
2287 // Bit 3: 1 - Use the serial number string
2288 // Bit 2: 1 - Enable suspend pull downs for lower power
2289 // Bit 1: 1 - Out EndPoint is Isochronous
2290 // Bit 0: 1 - In EndPoint is Isochronous
2293 if (j&0x01) eeprom->in_is_isochronous = 1;
2294 if (j&0x02) eeprom->out_is_isochronous = 1;
2295 if (j&0x04) eeprom->suspend_pull_downs = 1;
2296 if (j&0x08) eeprom->use_serial = 1;
2297 if (j&0x10) eeprom->change_usb_version = 1;
2299 // Addr 0B: reserved
2301 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2302 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2303 if (eeprom->change_usb_version == 1)
2305 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2308 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2309 // Addr 0F: Length of manufacturer string
2310 manufacturer_size = buf[0x0F]/2;
2311 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2312 else eeprom->manufacturer = NULL;
2314 // Addr 10: Offset of the product string + 0x80, calculated later
2315 // Addr 11: Length of product string
2316 product_size = buf[0x11]/2;
2317 if (product_size > 0) eeprom->product = malloc(product_size);
2318 else eeprom->product = NULL;
2320 // Addr 12: Offset of the serial string + 0x80, calculated later
2321 // Addr 13: Length of serial string
2322 serial_size = buf[0x13]/2;
2323 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2324 else eeprom->serial = NULL;
2326 // Decode manufacturer
2327 i = buf[0x0E] & 0x7f; // offset
2328 for (j=0;j<manufacturer_size-1;j++)
2330 eeprom->manufacturer[j] = buf[2*j+i+2];
2332 eeprom->manufacturer[j] = '\0';
2334 // Decode product name
2335 i = buf[0x10] & 0x7f; // offset
2336 for (j=0;j<product_size-1;j++)
2338 eeprom->product[j] = buf[2*j+i+2];
2340 eeprom->product[j] = '\0';
2343 i = buf[0x12] & 0x7f; // offset
2344 for (j=0;j<serial_size-1;j++)
2346 eeprom->serial[j] = buf[2*j+i+2];
2348 eeprom->serial[j] = '\0';
2353 for (i = 0; i < eeprom_size/2-1; i++)
2356 value += buf[(i*2)+1] << 8;
2358 checksum = value^checksum;
2359 checksum = (checksum << 1) | (checksum >> 15);
2362 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2364 if (eeprom_checksum != checksum)
2366 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2374 Read eeprom location
2376 \param ftdi pointer to ftdi_context
2377 \param eeprom_addr Address of eeprom location to be read
2378 \param eeprom_val Pointer to store read eeprom location
2381 \retval -1: read failed
2383 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2385 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
2386 ftdi_error_return(-1, "reading eeprom failed");
2394 \param ftdi pointer to ftdi_context
2395 \param eeprom Pointer to store eeprom into
2398 \retval -1: read failed
2400 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2404 for (i = 0; i < ftdi->eeprom_size/2; i++)
2406 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2407 ftdi_error_return(-1, "reading eeprom failed");
2414 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2415 Function is only used internally
2418 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2420 return ((value & 1) << 1) |
2421 ((value & 2) << 5) |
2422 ((value & 4) >> 2) |
2423 ((value & 8) << 4) |
2424 ((value & 16) >> 1) |
2425 ((value & 32) >> 1) |
2426 ((value & 64) >> 4) |
2427 ((value & 128) >> 2);
2431 Read the FTDIChip-ID from R-type devices
2433 \param ftdi pointer to ftdi_context
2434 \param chipid Pointer to store FTDIChip-ID
2437 \retval -1: read failed
2439 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2441 unsigned int a = 0, b = 0;
2443 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2)
2445 a = a << 8 | a >> 8;
2446 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2)
2448 b = b << 8 | b >> 8;
2449 a = (a << 16) | (b & 0xFFFF);
2450 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2451 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2452 *chipid = a ^ 0xa5f0f7d1;
2457 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2461 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2462 Call this function then do a write then call again to see if size changes, if so write again.
2464 \param ftdi pointer to ftdi_context
2465 \param eeprom Pointer to store eeprom into
2466 \param maxsize the size of the buffer to read into
2468 \retval size of eeprom
2470 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2472 int i=0,j,minsize=32;
2477 for (j = 0; i < maxsize/2 && j<size; j++)
2479 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2480 SIO_READ_EEPROM_REQUEST, 0, i,
2481 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2482 ftdi_error_return(-1, "reading eeprom failed");
2487 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2493 Write eeprom location
2495 \param ftdi pointer to ftdi_context
2496 \param eeprom_addr Address of eeprom location to be written
2497 \param eeprom_val Value to be written
2500 \retval -1: read failed
2502 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2504 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2505 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2506 NULL, 0, ftdi->usb_write_timeout) != 0)
2507 ftdi_error_return(-1, "unable to write eeprom");
2515 \param ftdi pointer to ftdi_context
2516 \param eeprom Pointer to read eeprom from
2519 \retval -1: read failed
2521 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2523 unsigned short usb_val, status;
2526 /* These commands were traced while running MProg */
2527 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2529 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2531 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2534 for (i = 0; i < ftdi->eeprom_size/2; i++)
2536 usb_val = eeprom[i*2];
2537 usb_val += eeprom[(i*2)+1] << 8;
2538 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2539 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2540 NULL, 0, ftdi->usb_write_timeout) < 0)
2541 ftdi_error_return(-1, "unable to write eeprom");
2550 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2552 \param ftdi pointer to ftdi_context
2555 \retval -1: erase failed
2557 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2559 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
2560 ftdi_error_return(-1, "unable to erase eeprom");
2566 Get string representation for last error code
2568 \param ftdi pointer to ftdi_context
2570 \retval Pointer to error string
2572 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2574 return ftdi->error_str;
2577 /* @} end of doxygen libftdi group */