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 ***************************************************************************/
23 #define ftdi_error_return(code, str) do { \
24 ftdi->error_str = str; \
30 Initializes a ftdi_context.
32 \param ftdi pointer to ftdi_context
35 \retval -1: couldn't allocate read buffer
37 \remark This should be called before all functions
39 int ftdi_init(struct ftdi_context *ftdi)
42 ftdi->usb_read_timeout = 5000;
43 ftdi->usb_write_timeout = 5000;
45 ftdi->type = TYPE_BM; /* chip type */
47 ftdi->bitbang_enabled = 0;
49 ftdi->readbuffer = NULL;
50 ftdi->readbuffer_offset = 0;
51 ftdi->readbuffer_remaining = 0;
52 ftdi->writebuffer_chunksize = 4096;
58 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
60 ftdi->error_str = NULL;
62 /* All fine. Now allocate the readbuffer */
63 return ftdi_read_data_set_chunksize(ftdi, 4096);
67 Open selected channels on a chip, otherwise use first channel.
69 \param ftdi pointer to ftdi_context
70 \param interface Interface to use for FT2232C chips.
73 \retval -1: unknown interface
75 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
80 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
84 ftdi->index = INTERFACE_B;
89 ftdi_error_return(-1, "Unknown interface");
95 Deinitializes a ftdi_context.
97 \param ftdi pointer to ftdi_context
99 void ftdi_deinit(struct ftdi_context *ftdi)
101 if (ftdi->readbuffer != NULL) {
102 free(ftdi->readbuffer);
103 ftdi->readbuffer = NULL;
108 Use an already open libusb device.
110 \param ftdi pointer to ftdi_context
111 \param usb libusb usb_dev_handle to use
113 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
120 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
121 needs to be deallocated by ftdi_list_free() after use.
123 \param ftdi pointer to ftdi_context
124 \param devlist Pointer where to store list of found devices
125 \param vendor Vendor ID to search for
126 \param product Product ID to search for
128 \retval >0: number of devices found
129 \retval -1: usb_find_busses() failed
130 \retval -2: usb_find_devices() failed
131 \retval -3: out of memory
133 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
135 struct ftdi_device_list **curdev;
137 struct usb_device *dev;
141 if (usb_find_busses() < 0)
142 ftdi_error_return(-1, "usb_find_busses() failed");
143 if (usb_find_devices() < 0)
144 ftdi_error_return(-2, "usb_find_devices() failed");
147 for (bus = usb_busses; bus; bus = bus->next) {
148 for (dev = bus->devices; dev; dev = dev->next) {
149 if (dev->descriptor.idVendor == vendor
150 && dev->descriptor.idProduct == product)
152 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
154 ftdi_error_return(-3, "out of memory");
156 (*curdev)->next = NULL;
157 (*curdev)->dev = dev;
159 curdev = &(*curdev)->next;
169 Frees a usb device list.
171 \param devlist USB device list created by ftdi_usb_find_all()
173 void ftdi_list_free(struct ftdi_device_list **devlist)
175 struct ftdi_device_list **curdev;
176 for (; *devlist == NULL; devlist = curdev) {
177 curdev = &(*devlist)->next;
185 Opens a ftdi device given by a usb_device.
187 \param ftdi pointer to ftdi_context
188 \param dev libusb usb_dev to use
191 \retval -4: unable to open device
192 \retval -5: unable to claim device
193 \retval -6: reset failed
194 \retval -7: set baudrate failed
196 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
198 int detach_errno = 0;
199 if (!(ftdi->usb_dev = usb_open(dev)))
200 ftdi_error_return(-4, "usb_open() failed");
202 #ifdef LIBUSB_HAS_GET_DRIVER_NP
203 // Try to detach ftdi_sio kernel module
204 // Returns ENODATA if driver is not loaded
205 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
206 detach_errno = errno;
209 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
210 usb_close (ftdi->usb_dev);
211 if (detach_errno == EPERM) {
212 ftdi_error_return(-8, "inappropriate permissions on device!");
214 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
218 if (ftdi_usb_reset (ftdi) != 0) {
219 usb_close (ftdi->usb_dev);
220 ftdi_error_return(-6, "ftdi_usb_reset failed");
223 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
224 usb_close (ftdi->usb_dev);
225 ftdi_error_return(-7, "set baudrate failed");
228 // Try to guess chip type
229 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
230 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
231 && dev->descriptor.iSerialNumber == 0))
232 ftdi->type = TYPE_BM;
233 else if (dev->descriptor.bcdDevice == 0x200)
234 ftdi->type = TYPE_AM;
235 else if (dev->descriptor.bcdDevice == 0x500) {
236 ftdi->type = TYPE_2232C;
238 ftdi->index = INTERFACE_A;
241 ftdi_error_return(0, "all fine");
245 Opens the first device with a given vendor and product ids.
247 \param ftdi pointer to ftdi_context
248 \param vendor Vendor ID
249 \param product Product ID
251 \retval \see ftdi_usb_open_desc()
253 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
255 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
259 Opens the first device with a given, vendor id, product id,
260 description and serial.
262 \param ftdi pointer to ftdi_context
263 \param vendor Vendor ID
264 \param product Product ID
265 \param description Description to search for. Use NULL if not needed.
266 \param serial Serial to search for. Use NULL if not needed.
269 \retval -1: usb_find_busses() failed
270 \retval -2: usb_find_devices() failed
271 \retval -3: usb device not found
272 \retval -4: unable to open device
273 \retval -5: unable to claim device
274 \retval -6: reset failed
275 \retval -7: set baudrate failed
276 \retval -8: get product description failed
277 \retval -9: get serial number failed
278 \retval -10: unable to close device
280 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
281 const char* description, const char* serial)
284 struct usb_device *dev;
289 if (usb_find_busses() < 0)
290 ftdi_error_return(-1, "usb_find_busses() failed");
291 if (usb_find_devices() < 0)
292 ftdi_error_return(-2, "usb_find_devices() failed");
294 for (bus = usb_busses; bus; bus = bus->next) {
295 for (dev = bus->devices; dev; dev = dev->next) {
296 if (dev->descriptor.idVendor == vendor
297 && dev->descriptor.idProduct == product) {
298 if (!(ftdi->usb_dev = usb_open(dev)))
299 ftdi_error_return(-4, "usb_open() failed");
301 if (description != NULL) {
302 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
303 usb_close (ftdi->usb_dev);
304 ftdi_error_return(-8, "unable to fetch product description");
306 if (strncmp(string, description, sizeof(string)) != 0) {
307 if (usb_close (ftdi->usb_dev) != 0)
308 ftdi_error_return(-10, "unable to close device");
312 if (serial != NULL) {
313 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
314 usb_close (ftdi->usb_dev);
315 ftdi_error_return(-9, "unable to fetch serial number");
317 if (strncmp(string, serial, sizeof(string)) != 0) {
318 if (usb_close (ftdi->usb_dev) != 0)
319 ftdi_error_return(-10, "unable to close device");
324 if (usb_close (ftdi->usb_dev) != 0)
325 ftdi_error_return(-10, "unable to close device");
327 return ftdi_usb_open_dev(ftdi, dev);
333 ftdi_error_return(-3, "device not found");
337 Resets the ftdi device.
339 \param ftdi pointer to ftdi_context
342 \retval -1: FTDI reset failed
344 int ftdi_usb_reset(struct ftdi_context *ftdi)
346 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
347 ftdi_error_return(-1,"FTDI reset failed");
349 // Invalidate data in the readbuffer
350 ftdi->readbuffer_offset = 0;
351 ftdi->readbuffer_remaining = 0;
357 Clears the buffers on the chip.
359 \param ftdi pointer to ftdi_context
362 \retval -1: write buffer purge failed
363 \retval -2: read buffer purge failed
365 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
367 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
368 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
370 // Invalidate data in the readbuffer
371 ftdi->readbuffer_offset = 0;
372 ftdi->readbuffer_remaining = 0;
374 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
375 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
381 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
383 \param ftdi pointer to ftdi_context
386 \retval -1: usb_release failed
387 \retval -2: usb_close failed
389 int ftdi_usb_close(struct ftdi_context *ftdi)
393 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
396 if (usb_close (ftdi->usb_dev) != 0)
403 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
404 Function is only used internally
406 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
407 unsigned short *value, unsigned short *index)
409 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
410 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
411 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
412 int divisor, best_divisor, best_baud, best_baud_diff;
413 unsigned long encoded_divisor;
421 divisor = 24000000 / baudrate;
423 if (ftdi->type == TYPE_AM) {
424 // Round down to supported fraction (AM only)
425 divisor -= am_adjust_dn[divisor & 7];
428 // Try this divisor and the one above it (because division rounds down)
432 for (i = 0; i < 2; i++) {
433 int try_divisor = divisor + i;
437 // Round up to supported divisor value
438 if (try_divisor <= 8) {
439 // Round up to minimum supported divisor
441 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
442 // BM doesn't support divisors 9 through 11 inclusive
444 } else if (divisor < 16) {
445 // AM doesn't support divisors 9 through 15 inclusive
448 if (ftdi->type == TYPE_AM) {
449 // Round up to supported fraction (AM only)
450 try_divisor += am_adjust_up[try_divisor & 7];
451 if (try_divisor > 0x1FFF8) {
452 // Round down to maximum supported divisor value (for AM)
453 try_divisor = 0x1FFF8;
456 if (try_divisor > 0x1FFFF) {
457 // Round down to maximum supported divisor value (for BM)
458 try_divisor = 0x1FFFF;
462 // Get estimated baud rate (to nearest integer)
463 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
464 // Get absolute difference from requested baud rate
465 if (baud_estimate < baudrate) {
466 baud_diff = baudrate - baud_estimate;
468 baud_diff = baud_estimate - baudrate;
470 if (i == 0 || baud_diff < best_baud_diff) {
471 // Closest to requested baud rate so far
472 best_divisor = try_divisor;
473 best_baud = baud_estimate;
474 best_baud_diff = baud_diff;
475 if (baud_diff == 0) {
476 // Spot on! No point trying
481 // Encode the best divisor value
482 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
483 // Deal with special cases for encoded value
484 if (encoded_divisor == 1) {
485 encoded_divisor = 0; // 3000000 baud
486 } else if (encoded_divisor == 0x4001) {
487 encoded_divisor = 1; // 2000000 baud (BM only)
489 // Split into "value" and "index" values
490 *value = (unsigned short)(encoded_divisor & 0xFFFF);
491 if(ftdi->type == TYPE_2232C) {
492 *index = (unsigned short)(encoded_divisor >> 8);
494 *index |= ftdi->index;
497 *index = (unsigned short)(encoded_divisor >> 16);
499 // Return the nearest baud rate
504 Sets the chip baudrate
506 \param ftdi pointer to ftdi_context
509 \retval -1: invalid baudrate
510 \retval -2: setting baudrate failed
512 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
514 unsigned short value, index;
517 if (ftdi->bitbang_enabled) {
518 baudrate = baudrate*4;
521 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
522 if (actual_baudrate <= 0)
523 ftdi_error_return (-1, "Silly baudrate <= 0.");
525 // Check within tolerance (about 5%)
526 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
527 || ((actual_baudrate < baudrate)
528 ? (actual_baudrate * 21 < baudrate * 20)
529 : (baudrate * 21 < actual_baudrate * 20)))
530 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
532 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
533 ftdi_error_return (-2, "Setting new baudrate failed");
535 ftdi->baudrate = baudrate;
540 Set (RS232) line characteristics by Alain Abbas
542 \param ftdi pointer to ftdi_context
543 \param bits Number of bits
544 \param sbit Number of stop bits
545 \param parity Parity mode
548 \retval -1: Setting line property failed
550 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
551 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
553 unsigned short value = bits;
557 value |= (0x00 << 8);
560 value |= (0x01 << 8);
563 value |= (0x02 << 8);
566 value |= (0x03 << 8);
569 value |= (0x04 << 8);
575 value |= (0x00 << 11);
578 value |= (0x01 << 11);
581 value |= (0x02 << 11);
585 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
586 ftdi_error_return (-1, "Setting new line property failed");
592 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
594 \param ftdi pointer to ftdi_context
595 \param buf Buffer with the data
596 \param size Size of the buffer
598 \retval <0: error code from usb_bulk_write()
599 \retval >0: number of bytes written
601 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
605 int total_written = 0;
607 while (offset < size) {
608 int write_size = ftdi->writebuffer_chunksize;
610 if (offset+write_size > size)
611 write_size = size-offset;
613 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
615 ftdi_error_return(ret, "usb bulk write failed");
617 total_written += ret;
618 offset += write_size;
621 return total_written;
625 Configure write buffer chunk size.
628 \param ftdi pointer to ftdi_context
629 \param chunksize Chunk size
633 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
635 ftdi->writebuffer_chunksize = chunksize;
640 Get write buffer chunk size.
642 \param ftdi pointer to ftdi_context
643 \param chunksize Pointer to store chunk size in
647 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
649 *chunksize = ftdi->writebuffer_chunksize;
654 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
656 Automatically strips the two modem status bytes transfered during every read.
658 \param ftdi pointer to ftdi_context
659 \param buf Buffer to store data in
660 \param size Size of the buffer
662 \retval <0: error code from usb_bulk_read()
663 \retval >0: number of bytes read
665 \remark This function is not useful in bitbang mode.
666 Use ftdi_read_pins() to get the current state of the pins.
668 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
670 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
672 // everything we want is still in the readbuffer?
673 if (size <= ftdi->readbuffer_remaining) {
674 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
677 ftdi->readbuffer_remaining -= size;
678 ftdi->readbuffer_offset += size;
680 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
684 // something still in the readbuffer, but not enough to satisfy 'size'?
685 if (ftdi->readbuffer_remaining != 0) {
686 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
689 offset += ftdi->readbuffer_remaining;
691 // do the actual USB read
692 while (offset < size && ret > 0) {
693 ftdi->readbuffer_remaining = 0;
694 ftdi->readbuffer_offset = 0;
695 /* returns how much received */
696 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
698 ftdi_error_return(ret, "usb bulk read failed");
701 // skip FTDI status bytes.
702 // Maybe stored in the future to enable modem use
703 num_of_chunks = ret / 64;
704 chunk_remains = ret % 64;
705 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
707 ftdi->readbuffer_offset += 2;
711 for (i = 1; i < num_of_chunks; i++)
712 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
713 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
715 if (chunk_remains > 2) {
716 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
717 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
719 ret -= 2*num_of_chunks;
721 ret -= 2*(num_of_chunks-1)+chunk_remains;
723 } else if (ret <= 2) {
724 // no more data to read?
728 // data still fits in buf?
729 if (offset+ret <= size) {
730 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
731 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
734 /* Did we read exactly the right amount of bytes? */
736 //printf("read_data exact rem %d offset %d\n",
737 //ftdi->readbuffer_remaining, offset);
740 // only copy part of the data or size <= readbuffer_chunksize
741 int part_size = size-offset;
742 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
744 ftdi->readbuffer_offset += part_size;
745 ftdi->readbuffer_remaining = ret-part_size;
748 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
749 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
760 Configure read buffer chunk size.
763 Automatically reallocates the buffer.
765 \param ftdi pointer to ftdi_context
766 \param chunksize Chunk size
770 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
772 unsigned char *new_buf;
774 // Invalidate all remaining data
775 ftdi->readbuffer_offset = 0;
776 ftdi->readbuffer_remaining = 0;
778 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
779 ftdi_error_return(-1, "out of memory for readbuffer");
781 ftdi->readbuffer = new_buf;
782 ftdi->readbuffer_chunksize = chunksize;
788 Get read buffer chunk size.
790 \param ftdi pointer to ftdi_context
791 \param chunksize Pointer to store chunk size in
795 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
797 *chunksize = ftdi->readbuffer_chunksize;
805 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
807 \param ftdi pointer to ftdi_context
808 \param bitmask Bitmask to configure lines.
809 HIGH/ON value configures a line as output.
812 \retval -1: can't enable bitbang mode
814 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
816 unsigned short usb_val;
818 usb_val = bitmask; // low byte: bitmask
819 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
820 usb_val |= (ftdi->bitbang_mode << 8);
822 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
823 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
825 ftdi->bitbang_enabled = 1;
830 Disable bitbang mode.
832 \param ftdi pointer to ftdi_context
835 \retval -1: can't disable bitbang mode
837 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
839 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
840 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
842 ftdi->bitbang_enabled = 0;
847 Enable advanced bitbang mode for FT2232C chips.
849 \param ftdi pointer to ftdi_context
850 \param bitmask Bitmask to configure lines.
851 HIGH/ON value configures a line as output.
852 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
855 \retval -1: can't enable bitbang mode
857 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
859 unsigned short usb_val;
861 usb_val = bitmask; // low byte: bitmask
862 usb_val |= (mode << 8);
863 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
864 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
866 ftdi->bitbang_mode = mode;
867 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
872 Directly read pin state. Useful for bitbang mode.
874 \param ftdi pointer to ftdi_context
875 \param pins Pointer to store pins into
878 \retval -1: read pins failed
880 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
882 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
883 ftdi_error_return(-1, "read pins failed");
891 The FTDI chip keeps data in the internal buffer for a specific
892 amount of time if the buffer is not full yet to decrease
895 \param ftdi pointer to ftdi_context
896 \param latency Value between 1 and 255
899 \retval -1: latency out of range
900 \retval -2: unable to set latency timer
902 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
904 unsigned short usb_val;
907 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
910 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
911 ftdi_error_return(-2, "unable to set latency timer");
919 \param ftdi pointer to ftdi_context
920 \param latency Pointer to store latency value in
923 \retval -1: unable to get latency timer
925 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
927 unsigned short usb_val;
928 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
929 ftdi_error_return(-1, "reading latency timer failed");
931 *latency = (unsigned char)usb_val;
936 Init eeprom with default values.
938 \param eeprom Pointer to ftdi_eeprom
940 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
942 eeprom->vendor_id = 0x0403;
943 eeprom->product_id = 0x6001;
945 eeprom->self_powered = 1;
946 eeprom->remote_wakeup = 1;
947 eeprom->BM_type_chip = 1;
949 eeprom->in_is_isochronous = 0;
950 eeprom->out_is_isochronous = 0;
951 eeprom->suspend_pull_downs = 0;
953 eeprom->use_serial = 0;
954 eeprom->change_usb_version = 0;
955 eeprom->usb_version = 0x0200;
956 eeprom->max_power = 0;
958 eeprom->manufacturer = NULL;
959 eeprom->product = NULL;
960 eeprom->serial = NULL;
964 Build binary output from ftdi_eeprom structure.
965 Output is suitable for ftdi_write_eeprom().
967 \param eeprom Pointer to ftdi_eeprom
968 \param output Buffer of 128 bytes to store eeprom image to
970 \retval >0: used eeprom size
971 \retval -1: eeprom size (128 bytes) exceeded by custom strings
973 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
976 unsigned short checksum, value;
977 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
980 if (eeprom->manufacturer != NULL)
981 manufacturer_size = strlen(eeprom->manufacturer);
982 if (eeprom->product != NULL)
983 product_size = strlen(eeprom->product);
984 if (eeprom->serial != NULL)
985 serial_size = strlen(eeprom->serial);
987 size_check = 128; // eeprom is 128 bytes
988 size_check -= 28; // 28 are always in use (fixed)
989 size_check -= manufacturer_size*2;
990 size_check -= product_size*2;
991 size_check -= serial_size*2;
993 // eeprom size exceeded?
998 memset (output, 0, 128);
1000 // Addr 00: Stay 00 00
1001 // Addr 02: Vendor ID
1002 output[0x02] = eeprom->vendor_id;
1003 output[0x03] = eeprom->vendor_id >> 8;
1005 // Addr 04: Product ID
1006 output[0x04] = eeprom->product_id;
1007 output[0x05] = eeprom->product_id >> 8;
1009 // Addr 06: Device release number (0400h for BM features)
1010 output[0x06] = 0x00;
1012 if (eeprom->BM_type_chip == 1)
1013 output[0x07] = 0x04;
1015 output[0x07] = 0x02;
1017 // Addr 08: Config descriptor
1018 // Bit 1: remote wakeup if 1
1019 // Bit 0: self powered if 1
1022 if (eeprom->self_powered == 1)
1024 if (eeprom->remote_wakeup == 1)
1028 // Addr 09: Max power consumption: max power = value * 2 mA
1029 output[0x09] = eeprom->max_power;
1032 // Addr 0A: Chip configuration
1033 // Bit 7: 0 - reserved
1034 // Bit 6: 0 - reserved
1035 // Bit 5: 0 - reserved
1036 // Bit 4: 1 - Change USB version
1037 // Bit 3: 1 - Use the serial number string
1038 // Bit 2: 1 - Enable suspend pull downs for lower power
1039 // Bit 1: 1 - Out EndPoint is Isochronous
1040 // Bit 0: 1 - In EndPoint is Isochronous
1043 if (eeprom->in_is_isochronous == 1)
1045 if (eeprom->out_is_isochronous == 1)
1047 if (eeprom->suspend_pull_downs == 1)
1049 if (eeprom->use_serial == 1)
1051 if (eeprom->change_usb_version == 1)
1055 // Addr 0B: reserved
1056 output[0x0B] = 0x00;
1058 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1059 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1060 if (eeprom->change_usb_version == 1) {
1061 output[0x0C] = eeprom->usb_version;
1062 output[0x0D] = eeprom->usb_version >> 8;
1066 // Addr 0E: Offset of the manufacturer string + 0x80
1067 output[0x0E] = 0x14 + 0x80;
1069 // Addr 0F: Length of manufacturer string
1070 output[0x0F] = manufacturer_size*2 + 2;
1072 // Addr 10: Offset of the product string + 0x80, calculated later
1073 // Addr 11: Length of product string
1074 output[0x11] = product_size*2 + 2;
1076 // Addr 12: Offset of the serial string + 0x80, calculated later
1077 // Addr 13: Length of serial string
1078 output[0x13] = serial_size*2 + 2;
1081 output[0x14] = manufacturer_size*2 + 2;
1082 output[0x15] = 0x03; // type: string
1086 // Output manufacturer
1087 for (j = 0; j < manufacturer_size; j++) {
1088 output[i] = eeprom->manufacturer[j], i++;
1089 output[i] = 0x00, i++;
1092 // Output product name
1093 output[0x10] = i + 0x80; // calculate offset
1094 output[i] = product_size*2 + 2, i++;
1095 output[i] = 0x03, i++;
1096 for (j = 0; j < product_size; j++) {
1097 output[i] = eeprom->product[j], i++;
1098 output[i] = 0x00, i++;
1102 output[0x12] = i + 0x80; // calculate offset
1103 output[i] = serial_size*2 + 2, i++;
1104 output[i] = 0x03, i++;
1105 for (j = 0; j < serial_size; j++) {
1106 output[i] = eeprom->serial[j], i++;
1107 output[i] = 0x00, i++;
1110 // calculate checksum
1113 for (i = 0; i < 63; i++) {
1114 value = output[i*2];
1115 value += output[(i*2)+1] << 8;
1117 checksum = value^checksum;
1118 checksum = (checksum << 1) | (checksum >> 15);
1121 output[0x7E] = checksum;
1122 output[0x7F] = checksum >> 8;
1130 \param ftdi pointer to ftdi_context
1131 \param eeprom Pointer to store eeprom into
1134 \retval -1: read failed
1136 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1140 for (i = 0; i < 64; i++) {
1141 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1142 ftdi_error_return(-1, "reading eeprom failed");
1151 \param ftdi pointer to ftdi_context
1152 \param eeprom Pointer to read eeprom from
1155 \retval -1: read failed
1157 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1159 unsigned short usb_val;
1162 for (i = 0; i < 64; i++) {
1163 usb_val = eeprom[i*2];
1164 usb_val += eeprom[(i*2)+1] << 8;
1165 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1166 ftdi_error_return(-1, "unable to write eeprom");
1175 \param ftdi pointer to ftdi_context
1178 \retval -1: erase failed
1180 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1182 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1183 ftdi_error_return(-1, "unable to erase eeprom");
1189 Get string representation for last error code
1191 \param ftdi pointer to ftdi_context
1193 \retval Pointer to error string
1195 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1197 return ftdi->error_str;
1201 Set flowcontrol for ftdi chip
1203 \param ftdi pointer to ftdi_context
1204 \param flowctrl flow control to use. should be
1205 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1208 \retval -1: set flow control failed
1210 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1212 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1213 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1214 NULL, 0, ftdi->usb_write_timeout) != 0)
1215 ftdi_error_return(-1, "set flow control failed");
1223 \param ftdi pointer to ftdi_context
1224 \param state state to set line to (1 or 0)
1227 \retval -1: set dtr failed
1229 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1231 unsigned short usb_val;
1234 usb_val = SIO_SET_DTR_HIGH;
1236 usb_val = SIO_SET_DTR_LOW;
1238 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1239 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1240 NULL, 0, ftdi->usb_write_timeout) != 0)
1241 ftdi_error_return(-1, "set dtr failed");
1249 \param ftdi pointer to ftdi_context
1250 \param state state to set line to (1 or 0)
1253 \retval -1 set rts failed
1255 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1257 unsigned short usb_val;
1260 usb_val = SIO_SET_RTS_HIGH;
1262 usb_val = SIO_SET_RTS_LOW;
1264 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1265 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1266 NULL, 0, ftdi->usb_write_timeout) != 0)
1267 ftdi_error_return(-1, "set of rts failed");