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");
162 for (bus = usb_busses; bus; bus = bus->next) {
163 for (dev = bus->devices; dev; dev = dev->next) {
164 if (dev->descriptor.idVendor == vendor
165 && dev->descriptor.idProduct == product)
167 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
169 ftdi_error_return(-3, "out of memory");
171 (*curdev)->next = NULL;
172 (*curdev)->dev = dev;
174 curdev = &(*curdev)->next;
184 Frees a usb device list.
186 \param devlist USB device list created by ftdi_usb_find_all()
188 void ftdi_list_free(struct ftdi_device_list **devlist)
190 struct ftdi_device_list *curdev, *next;
192 for (curdev = *devlist; curdev != NULL;) {
202 Return device ID strings from the usb device.
204 The parameters manufacturer, description and serial may be NULL
205 or pointer to buffers to store the fetched strings.
207 \note Use this function only in combination with ftdi_usb_find_all()
208 as it closes the internal "usb_dev" after use.
210 \param ftdi pointer to ftdi_context
211 \param dev libusb usb_dev to use
212 \param manufacturer Store manufacturer string here if not NULL
213 \param mnf_len Buffer size of manufacturer string
214 \param description Store product description string here if not NULL
215 \param desc_len Buffer size of product description string
216 \param serial Store serial string here if not NULL
217 \param serial_len Buffer size of serial string
220 \retval -1: wrong arguments
221 \retval -4: unable to open device
222 \retval -7: get product manufacturer failed
223 \retval -8: get product description failed
224 \retval -9: get serial number failed
225 \retval -10: unable to close device
227 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
228 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
230 if ((ftdi==NULL) || (dev==NULL))
233 if (!(ftdi->usb_dev = usb_open(dev)))
234 ftdi_error_return(-4, usb_strerror());
236 if (manufacturer != NULL) {
237 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) {
238 usb_close (ftdi->usb_dev);
239 ftdi_error_return(-7, usb_strerror());
243 if (description != NULL) {
244 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) {
245 usb_close (ftdi->usb_dev);
246 ftdi_error_return(-8, usb_strerror());
250 if (serial != NULL) {
251 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) {
252 usb_close (ftdi->usb_dev);
253 ftdi_error_return(-9, usb_strerror());
257 if (usb_close (ftdi->usb_dev) != 0)
258 ftdi_error_return(-10, usb_strerror());
264 Opens a ftdi device given by a usb_device.
266 \param ftdi pointer to ftdi_context
267 \param dev libusb usb_dev to use
270 \retval -4: unable to open device
271 \retval -5: unable to claim device
272 \retval -6: reset failed
273 \retval -7: set baudrate failed
275 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
277 int detach_errno = 0;
278 if (!(ftdi->usb_dev = usb_open(dev)))
279 ftdi_error_return(-4, "usb_open() failed");
281 #ifdef LIBUSB_HAS_GET_DRIVER_NP
282 // Try to detach ftdi_sio kernel module
283 // Returns ENODATA if driver is not loaded
284 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
285 detach_errno = errno;
288 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
289 usb_close (ftdi->usb_dev);
290 if (detach_errno == EPERM) {
291 ftdi_error_return(-8, "inappropriate permissions on device!");
293 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
297 if (ftdi_usb_reset (ftdi) != 0) {
298 usb_close (ftdi->usb_dev);
299 ftdi_error_return(-6, "ftdi_usb_reset failed");
302 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
303 usb_close (ftdi->usb_dev);
304 ftdi_error_return(-7, "set baudrate failed");
307 // Try to guess chip type
308 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
309 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
310 && dev->descriptor.iSerialNumber == 0))
311 ftdi->type = TYPE_BM;
312 else if (dev->descriptor.bcdDevice == 0x200)
313 ftdi->type = TYPE_AM;
314 else if (dev->descriptor.bcdDevice == 0x500) {
315 ftdi->type = TYPE_2232C;
317 ftdi->index = INTERFACE_A;
320 ftdi_error_return(0, "all fine");
324 Opens the first device with a given vendor and product ids.
326 \param ftdi pointer to ftdi_context
327 \param vendor Vendor ID
328 \param product Product ID
330 \retval same as ftdi_usb_open_desc()
332 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
334 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
338 Opens the first device with a given, vendor id, product id,
339 description and serial.
341 \param ftdi pointer to ftdi_context
342 \param vendor Vendor ID
343 \param product Product ID
344 \param description Description to search for. Use NULL if not needed.
345 \param serial Serial to search for. Use NULL if not needed.
348 \retval -1: usb_find_busses() failed
349 \retval -2: usb_find_devices() failed
350 \retval -3: usb device not found
351 \retval -4: unable to open device
352 \retval -5: unable to claim device
353 \retval -6: reset failed
354 \retval -7: set baudrate failed
355 \retval -8: get product description failed
356 \retval -9: get serial number failed
357 \retval -10: unable to close device
359 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
360 const char* description, const char* serial)
363 struct usb_device *dev;
368 if (usb_find_busses() < 0)
369 ftdi_error_return(-1, "usb_find_busses() failed");
370 if (usb_find_devices() < 0)
371 ftdi_error_return(-2, "usb_find_devices() failed");
373 for (bus = usb_busses; bus; bus = bus->next) {
374 for (dev = bus->devices; dev; dev = dev->next) {
375 if (dev->descriptor.idVendor == vendor
376 && dev->descriptor.idProduct == product) {
377 if (!(ftdi->usb_dev = usb_open(dev)))
378 ftdi_error_return(-4, "usb_open() failed");
380 if (description != NULL) {
381 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
382 usb_close (ftdi->usb_dev);
383 ftdi_error_return(-8, "unable to fetch product description");
385 if (strncmp(string, description, sizeof(string)) != 0) {
386 if (usb_close (ftdi->usb_dev) != 0)
387 ftdi_error_return(-10, "unable to close device");
391 if (serial != NULL) {
392 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
393 usb_close (ftdi->usb_dev);
394 ftdi_error_return(-9, "unable to fetch serial number");
396 if (strncmp(string, serial, sizeof(string)) != 0) {
397 if (usb_close (ftdi->usb_dev) != 0)
398 ftdi_error_return(-10, "unable to close device");
403 if (usb_close (ftdi->usb_dev) != 0)
404 ftdi_error_return(-10, "unable to close device");
406 return ftdi_usb_open_dev(ftdi, dev);
412 ftdi_error_return(-3, "device not found");
416 Resets the ftdi device.
418 \param ftdi pointer to ftdi_context
421 \retval -1: FTDI reset failed
423 int ftdi_usb_reset(struct ftdi_context *ftdi)
425 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
426 ftdi_error_return(-1,"FTDI reset failed");
428 // Invalidate data in the readbuffer
429 ftdi->readbuffer_offset = 0;
430 ftdi->readbuffer_remaining = 0;
436 Clears the buffers on the chip.
438 \param ftdi pointer to ftdi_context
441 \retval -1: write buffer purge failed
442 \retval -2: read buffer purge failed
444 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
446 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
447 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
449 // Invalidate data in the readbuffer
450 ftdi->readbuffer_offset = 0;
451 ftdi->readbuffer_remaining = 0;
453 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
454 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
460 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
462 \param ftdi pointer to ftdi_context
465 \retval -1: usb_release failed
466 \retval -2: usb_close failed
468 int ftdi_usb_close(struct ftdi_context *ftdi)
472 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
475 if (usb_close (ftdi->usb_dev) != 0)
482 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
483 Function is only used internally
486 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
487 unsigned short *value, unsigned short *index)
489 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
490 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
491 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
492 int divisor, best_divisor, best_baud, best_baud_diff;
493 unsigned long encoded_divisor;
501 divisor = 24000000 / baudrate;
503 if (ftdi->type == TYPE_AM) {
504 // Round down to supported fraction (AM only)
505 divisor -= am_adjust_dn[divisor & 7];
508 // Try this divisor and the one above it (because division rounds down)
512 for (i = 0; i < 2; i++) {
513 int try_divisor = divisor + i;
517 // Round up to supported divisor value
518 if (try_divisor <= 8) {
519 // Round up to minimum supported divisor
521 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
522 // BM doesn't support divisors 9 through 11 inclusive
524 } else if (divisor < 16) {
525 // AM doesn't support divisors 9 through 15 inclusive
528 if (ftdi->type == TYPE_AM) {
529 // Round up to supported fraction (AM only)
530 try_divisor += am_adjust_up[try_divisor & 7];
531 if (try_divisor > 0x1FFF8) {
532 // Round down to maximum supported divisor value (for AM)
533 try_divisor = 0x1FFF8;
536 if (try_divisor > 0x1FFFF) {
537 // Round down to maximum supported divisor value (for BM)
538 try_divisor = 0x1FFFF;
542 // Get estimated baud rate (to nearest integer)
543 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
544 // Get absolute difference from requested baud rate
545 if (baud_estimate < baudrate) {
546 baud_diff = baudrate - baud_estimate;
548 baud_diff = baud_estimate - baudrate;
550 if (i == 0 || baud_diff < best_baud_diff) {
551 // Closest to requested baud rate so far
552 best_divisor = try_divisor;
553 best_baud = baud_estimate;
554 best_baud_diff = baud_diff;
555 if (baud_diff == 0) {
556 // Spot on! No point trying
561 // Encode the best divisor value
562 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
563 // Deal with special cases for encoded value
564 if (encoded_divisor == 1) {
565 encoded_divisor = 0; // 3000000 baud
566 } else if (encoded_divisor == 0x4001) {
567 encoded_divisor = 1; // 2000000 baud (BM only)
569 // Split into "value" and "index" values
570 *value = (unsigned short)(encoded_divisor & 0xFFFF);
571 if(ftdi->type == TYPE_2232C) {
572 *index = (unsigned short)(encoded_divisor >> 8);
574 *index |= ftdi->index;
577 *index = (unsigned short)(encoded_divisor >> 16);
579 // Return the nearest baud rate
584 Sets the chip baud rate
586 \param ftdi pointer to ftdi_context
587 \param baudrate baud rate to set
590 \retval -1: invalid baudrate
591 \retval -2: setting baudrate failed
593 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
595 unsigned short value, index;
598 if (ftdi->bitbang_enabled) {
599 baudrate = baudrate*4;
602 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
603 if (actual_baudrate <= 0)
604 ftdi_error_return (-1, "Silly baudrate <= 0.");
606 // Check within tolerance (about 5%)
607 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
608 || ((actual_baudrate < baudrate)
609 ? (actual_baudrate * 21 < baudrate * 20)
610 : (baudrate * 21 < actual_baudrate * 20)))
611 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
613 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
614 ftdi_error_return (-2, "Setting new baudrate failed");
616 ftdi->baudrate = baudrate;
621 Set (RS232) line characteristics by Alain Abbas
623 \param ftdi pointer to ftdi_context
624 \param bits Number of bits
625 \param sbit Number of stop bits
626 \param parity Parity mode
629 \retval -1: Setting line property failed
631 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
632 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
634 unsigned short value = bits;
638 value |= (0x00 << 8);
641 value |= (0x01 << 8);
644 value |= (0x02 << 8);
647 value |= (0x03 << 8);
650 value |= (0x04 << 8);
656 value |= (0x00 << 11);
659 value |= (0x01 << 11);
662 value |= (0x02 << 11);
666 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
667 ftdi_error_return (-1, "Setting new line property failed");
673 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
675 \param ftdi pointer to ftdi_context
676 \param buf Buffer with the data
677 \param size Size of the buffer
679 \retval <0: error code from usb_bulk_write()
680 \retval >0: number of bytes written
682 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
686 int total_written = 0;
688 while (offset < size) {
689 int write_size = ftdi->writebuffer_chunksize;
691 if (offset+write_size > size)
692 write_size = size-offset;
694 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
696 ftdi_error_return(ret, "usb bulk write failed");
698 total_written += ret;
699 offset += write_size;
702 return total_written;
706 Configure write buffer chunk size.
709 \param ftdi pointer to ftdi_context
710 \param chunksize Chunk size
714 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
716 ftdi->writebuffer_chunksize = chunksize;
721 Get write buffer chunk size.
723 \param ftdi pointer to ftdi_context
724 \param chunksize Pointer to store chunk size in
728 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
730 *chunksize = ftdi->writebuffer_chunksize;
735 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
737 Automatically strips the two modem status bytes transfered during every read.
739 \param ftdi pointer to ftdi_context
740 \param buf Buffer to store data in
741 \param size Size of the buffer
743 \retval <0: error code from usb_bulk_read()
744 \retval >0: number of bytes read
746 \remark This function is not useful in bitbang mode.
747 Use ftdi_read_pins() to get the current state of the pins.
749 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
751 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
753 // everything we want is still in the readbuffer?
754 if (size <= ftdi->readbuffer_remaining) {
755 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
758 ftdi->readbuffer_remaining -= size;
759 ftdi->readbuffer_offset += size;
761 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
765 // something still in the readbuffer, but not enough to satisfy 'size'?
766 if (ftdi->readbuffer_remaining != 0) {
767 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
770 offset += ftdi->readbuffer_remaining;
772 // do the actual USB read
773 while (offset < size && ret > 0) {
774 ftdi->readbuffer_remaining = 0;
775 ftdi->readbuffer_offset = 0;
776 /* returns how much received */
777 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
779 ftdi_error_return(ret, "usb bulk read failed");
782 // skip FTDI status bytes.
783 // Maybe stored in the future to enable modem use
784 num_of_chunks = ret / 64;
785 chunk_remains = ret % 64;
786 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
788 ftdi->readbuffer_offset += 2;
792 for (i = 1; i < num_of_chunks; i++)
793 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
794 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
796 if (chunk_remains > 2) {
797 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
798 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
800 ret -= 2*num_of_chunks;
802 ret -= 2*(num_of_chunks-1)+chunk_remains;
804 } else if (ret <= 2) {
805 // no more data to read?
809 // data still fits in buf?
810 if (offset+ret <= size) {
811 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
812 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
815 /* Did we read exactly the right amount of bytes? */
817 //printf("read_data exact rem %d offset %d\n",
818 //ftdi->readbuffer_remaining, offset);
821 // only copy part of the data or size <= readbuffer_chunksize
822 int part_size = size-offset;
823 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
825 ftdi->readbuffer_offset += part_size;
826 ftdi->readbuffer_remaining = ret-part_size;
829 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
830 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
841 Configure read buffer chunk size.
844 Automatically reallocates the buffer.
846 \param ftdi pointer to ftdi_context
847 \param chunksize Chunk size
851 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
853 unsigned char *new_buf;
855 // Invalidate all remaining data
856 ftdi->readbuffer_offset = 0;
857 ftdi->readbuffer_remaining = 0;
859 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
860 ftdi_error_return(-1, "out of memory for readbuffer");
862 ftdi->readbuffer = new_buf;
863 ftdi->readbuffer_chunksize = chunksize;
869 Get read buffer chunk size.
871 \param ftdi pointer to ftdi_context
872 \param chunksize Pointer to store chunk size in
876 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
878 *chunksize = ftdi->readbuffer_chunksize;
886 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
888 \param ftdi pointer to ftdi_context
889 \param bitmask Bitmask to configure lines.
890 HIGH/ON value configures a line as output.
893 \retval -1: can't enable bitbang mode
895 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
897 unsigned short usb_val;
899 usb_val = bitmask; // low byte: bitmask
900 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
901 usb_val |= (ftdi->bitbang_mode << 8);
903 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
904 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
906 ftdi->bitbang_enabled = 1;
911 Disable bitbang mode.
913 \param ftdi pointer to ftdi_context
916 \retval -1: can't disable bitbang mode
918 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
920 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
921 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
923 ftdi->bitbang_enabled = 0;
928 Enable advanced bitbang mode for FT2232C chips.
930 \param ftdi pointer to ftdi_context
931 \param bitmask Bitmask to configure lines.
932 HIGH/ON value configures a line as output.
933 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
936 \retval -1: can't enable bitbang mode
938 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
940 unsigned short usb_val;
942 usb_val = bitmask; // low byte: bitmask
943 usb_val |= (mode << 8);
944 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
945 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
947 ftdi->bitbang_mode = mode;
948 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
953 Directly read pin state. Useful for bitbang mode.
955 \param ftdi pointer to ftdi_context
956 \param pins Pointer to store pins into
959 \retval -1: read pins failed
961 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
963 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
964 ftdi_error_return(-1, "read pins failed");
972 The FTDI chip keeps data in the internal buffer for a specific
973 amount of time if the buffer is not full yet to decrease
976 \param ftdi pointer to ftdi_context
977 \param latency Value between 1 and 255
980 \retval -1: latency out of range
981 \retval -2: unable to set latency timer
983 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
985 unsigned short usb_val;
988 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
991 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
992 ftdi_error_return(-2, "unable to set latency timer");
1000 \param ftdi pointer to ftdi_context
1001 \param latency Pointer to store latency value in
1004 \retval -1: unable to get latency timer
1006 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1008 unsigned short usb_val;
1009 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1010 ftdi_error_return(-1, "reading latency timer failed");
1012 *latency = (unsigned char)usb_val;
1017 Init eeprom with default values.
1019 \param eeprom Pointer to ftdi_eeprom
1021 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
1023 eeprom->vendor_id = 0x0403;
1024 eeprom->product_id = 0x6001;
1026 eeprom->self_powered = 1;
1027 eeprom->remote_wakeup = 1;
1028 eeprom->BM_type_chip = 1;
1030 eeprom->in_is_isochronous = 0;
1031 eeprom->out_is_isochronous = 0;
1032 eeprom->suspend_pull_downs = 0;
1034 eeprom->use_serial = 0;
1035 eeprom->change_usb_version = 0;
1036 eeprom->usb_version = 0x0200;
1037 eeprom->max_power = 0;
1039 eeprom->manufacturer = NULL;
1040 eeprom->product = NULL;
1041 eeprom->serial = NULL;
1045 Build binary output from ftdi_eeprom structure.
1046 Output is suitable for ftdi_write_eeprom().
1048 \param eeprom Pointer to ftdi_eeprom
1049 \param output Buffer of 128 bytes to store eeprom image to
1051 \retval >0: used eeprom size
1052 \retval -1: eeprom size (128 bytes) exceeded by custom strings
1054 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
1057 unsigned short checksum, value;
1058 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1061 if (eeprom->manufacturer != NULL)
1062 manufacturer_size = strlen(eeprom->manufacturer);
1063 if (eeprom->product != NULL)
1064 product_size = strlen(eeprom->product);
1065 if (eeprom->serial != NULL)
1066 serial_size = strlen(eeprom->serial);
1068 size_check = 128; // eeprom is 128 bytes
1069 size_check -= 28; // 28 are always in use (fixed)
1070 size_check -= manufacturer_size*2;
1071 size_check -= product_size*2;
1072 size_check -= serial_size*2;
1074 // eeprom size exceeded?
1079 memset (output, 0, 128);
1081 // Addr 00: Stay 00 00
1082 // Addr 02: Vendor ID
1083 output[0x02] = eeprom->vendor_id;
1084 output[0x03] = eeprom->vendor_id >> 8;
1086 // Addr 04: Product ID
1087 output[0x04] = eeprom->product_id;
1088 output[0x05] = eeprom->product_id >> 8;
1090 // Addr 06: Device release number (0400h for BM features)
1091 output[0x06] = 0x00;
1093 if (eeprom->BM_type_chip == 1)
1094 output[0x07] = 0x04;
1096 output[0x07] = 0x02;
1098 // Addr 08: Config descriptor
1099 // Bit 1: remote wakeup if 1
1100 // Bit 0: self powered if 1
1103 if (eeprom->self_powered == 1)
1105 if (eeprom->remote_wakeup == 1)
1109 // Addr 09: Max power consumption: max power = value * 2 mA
1110 output[0x09] = eeprom->max_power;
1113 // Addr 0A: Chip configuration
1114 // Bit 7: 0 - reserved
1115 // Bit 6: 0 - reserved
1116 // Bit 5: 0 - reserved
1117 // Bit 4: 1 - Change USB version
1118 // Bit 3: 1 - Use the serial number string
1119 // Bit 2: 1 - Enable suspend pull downs for lower power
1120 // Bit 1: 1 - Out EndPoint is Isochronous
1121 // Bit 0: 1 - In EndPoint is Isochronous
1124 if (eeprom->in_is_isochronous == 1)
1126 if (eeprom->out_is_isochronous == 1)
1128 if (eeprom->suspend_pull_downs == 1)
1130 if (eeprom->use_serial == 1)
1132 if (eeprom->change_usb_version == 1)
1136 // Addr 0B: reserved
1137 output[0x0B] = 0x00;
1139 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1140 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1141 if (eeprom->change_usb_version == 1) {
1142 output[0x0C] = eeprom->usb_version;
1143 output[0x0D] = eeprom->usb_version >> 8;
1147 // Addr 0E: Offset of the manufacturer string + 0x80
1148 output[0x0E] = 0x14 + 0x80;
1150 // Addr 0F: Length of manufacturer string
1151 output[0x0F] = manufacturer_size*2 + 2;
1153 // Addr 10: Offset of the product string + 0x80, calculated later
1154 // Addr 11: Length of product string
1155 output[0x11] = product_size*2 + 2;
1157 // Addr 12: Offset of the serial string + 0x80, calculated later
1158 // Addr 13: Length of serial string
1159 output[0x13] = serial_size*2 + 2;
1162 output[0x14] = manufacturer_size*2 + 2;
1163 output[0x15] = 0x03; // type: string
1167 // Output manufacturer
1168 for (j = 0; j < manufacturer_size; j++) {
1169 output[i] = eeprom->manufacturer[j], i++;
1170 output[i] = 0x00, i++;
1173 // Output product name
1174 output[0x10] = i + 0x80; // calculate offset
1175 output[i] = product_size*2 + 2, i++;
1176 output[i] = 0x03, i++;
1177 for (j = 0; j < product_size; j++) {
1178 output[i] = eeprom->product[j], i++;
1179 output[i] = 0x00, i++;
1183 output[0x12] = i + 0x80; // calculate offset
1184 output[i] = serial_size*2 + 2, i++;
1185 output[i] = 0x03, i++;
1186 for (j = 0; j < serial_size; j++) {
1187 output[i] = eeprom->serial[j], i++;
1188 output[i] = 0x00, i++;
1191 // calculate checksum
1194 for (i = 0; i < 63; i++) {
1195 value = output[i*2];
1196 value += output[(i*2)+1] << 8;
1198 checksum = value^checksum;
1199 checksum = (checksum << 1) | (checksum >> 15);
1202 output[0x7E] = checksum;
1203 output[0x7F] = checksum >> 8;
1211 \param ftdi pointer to ftdi_context
1212 \param eeprom Pointer to store eeprom into
1215 \retval -1: read failed
1217 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1221 for (i = 0; i < 64; i++) {
1222 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1223 ftdi_error_return(-1, "reading eeprom failed");
1232 \param ftdi pointer to ftdi_context
1233 \param eeprom Pointer to read eeprom from
1236 \retval -1: read failed
1238 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1240 unsigned short usb_val;
1243 for (i = 0; i < 64; i++) {
1244 usb_val = eeprom[i*2];
1245 usb_val += eeprom[(i*2)+1] << 8;
1246 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1247 ftdi_error_return(-1, "unable to write eeprom");
1256 \param ftdi pointer to ftdi_context
1259 \retval -1: erase failed
1261 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1263 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1264 ftdi_error_return(-1, "unable to erase eeprom");
1270 Get string representation for last error code
1272 \param ftdi pointer to ftdi_context
1274 \retval Pointer to error string
1276 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1278 return ftdi->error_str;
1282 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1283 and Matthias Kranz (matthias@hcilab.org)
1286 Set flowcontrol for ftdi chip
1288 \param ftdi pointer to ftdi_context
1289 \param flowctrl flow control to use. should be
1290 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1293 \retval -1: set flow control failed
1295 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1297 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1298 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1299 NULL, 0, ftdi->usb_write_timeout) != 0)
1300 ftdi_error_return(-1, "set flow control failed");
1308 \param ftdi pointer to ftdi_context
1309 \param state state to set line to (1 or 0)
1312 \retval -1: set dtr failed
1314 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1316 unsigned short usb_val;
1319 usb_val = SIO_SET_DTR_HIGH;
1321 usb_val = SIO_SET_DTR_LOW;
1323 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1324 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1325 NULL, 0, ftdi->usb_write_timeout) != 0)
1326 ftdi_error_return(-1, "set dtr failed");
1334 \param ftdi pointer to ftdi_context
1335 \param state state to set line to (1 or 0)
1338 \retval -1 set rts failed
1340 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1342 unsigned short usb_val;
1345 usb_val = SIO_SET_RTS_HIGH;
1347 usb_val = SIO_SET_RTS_LOW;
1349 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1350 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1351 NULL, 0, ftdi->usb_write_timeout) != 0)
1352 ftdi_error_return(-1, "set of rts failed");
1357 /* @} end of doxygen libftdi group */