X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=src%2Fftdi.c;h=52ca36ed8e6fc2d86e0e306c2a1ed1a6a053fdd8;hp=0e48b2837bc25802ff58c7e3a4fc6624baf3c70d;hb=4837f98ad4e0b6fae27c2e2741a2d527cd6b8289;hpb=792418f119810bc9ac88c270d42c2de1b41d6424 diff --git a/src/ftdi.c b/src/ftdi.c index 0e48b28..52ca36e 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -16,13 +16,22 @@ #include #include -#include #include "ftdi.h" -/* ftdi_init return codes: - 0: all fine - -1: couldn't allocate read buffer +#define ftdi_error_return(code, str) do { \ + ftdi->error_str = str; \ + return code; \ + } while(0); + + +/* ftdi_init + + Initalises a ftdi_context. + + Return codes: + 0: All fine + -1: Couldn't allocate read buffer */ int ftdi_init(struct ftdi_context *ftdi) { @@ -51,7 +60,37 @@ int ftdi_init(struct ftdi_context *ftdi) return ftdi_read_data_set_chunksize(ftdi, 4096); } +/* ftdi_set_interface + + Call after ftdi_init + + Open selected channels on a chip, otherwise use first channel + 0: all fine + -1: unknown interface +*/ +int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) +{ + switch (interface) { + case INTERFACE_ANY: + case INTERFACE_A: + /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */ + break; + case INTERFACE_B: + ftdi->interface = 1; + ftdi->index = INTERFACE_B; + ftdi->in_ep = 0x04; + ftdi->out_ep = 0x83; + break; + default: + ftdi_error_return(-1, "Unknown interface"); + } + return 0; +} + +/* ftdi_deinit + Deinitializes a ftdi_context. +*/ void ftdi_deinit(struct ftdi_context *ftdi) { if (ftdi->readbuffer != NULL) { @@ -60,25 +99,31 @@ void ftdi_deinit(struct ftdi_context *ftdi) } } - +/* ftdi_set_usbdev + + Use an already open device. +*/ void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) { ftdi->usb_dev = usb; } +/* ftdi_usb_open -/* ftdi_usb_open return codes: - 0: all fine - -1: usb_find_busses() failed - -2: usb_find_devices() failed - -3: usb device not found - -4: unable to open device - -5: unable to claim device - -6: reset failed - -7: set baudrate failed - -8: get product description failed - -9: get serial number failed - -10: unable to close device + Opens the first device with a given vendor and product ids. + + Return codes: + 0: all fine + -1: usb_find_busses() failed + -2: usb_find_devices() failed + -3: usb device not found + -4: unable to open device + -5: unable to claim device + -6: reset failed + -7: set baudrate failed + -8: get product description failed + -9: get serial number failed + -10: unable to close device */ int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) { @@ -90,75 +135,60 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, { struct usb_bus *bus; struct usb_device *dev; + char string[256]; usb_init(); - if (usb_find_busses() < 0) { - ftdi->error_str = "usb_find_busses() failed"; - return -1; - } + if (usb_find_busses() < 0) + ftdi_error_return(-1, "usb_find_busses() failed"); - if (usb_find_devices() < 0) { - ftdi->error_str = "usb_find_devices() failed"; - return -2; - } + if (usb_find_devices() < 0) + ftdi_error_return(-2,"usb_find_devices() failed"); for (bus = usb_busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vendor - && dev->descriptor.idProduct == product) { - if (!(ftdi->usb_dev = usb_open(dev))) { - ftdi->error_str = "usb_open() failed"; - return -4; - } - - char string[256]; + && dev->descriptor.idProduct == product) { + if (!(ftdi->usb_dev = usb_open(dev))) + ftdi_error_return(-4, "usb_open() failed"); + if (description != NULL) { if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) { - ftdi->error_str = "unable to fetch product description\n"; - if (usb_close (ftdi->usb_dev) != 0) - return -10; - return -8; + usb_close (ftdi->usb_dev); + ftdi_error_return(-8, "unable to fetch product description"); } if (strncmp(string, description, sizeof(string)) != 0) { - ftdi->error_str = "product description not matching\n"; - if (usb_close (ftdi->usb_dev) != 0) - return -10; + if (usb_close (ftdi->usb_dev) < 0) + ftdi_error_return(-10, "product description not matching"); continue; } } if (serial != NULL) { if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) { - ftdi->error_str = "unable to fetch serial number\n"; - if (usb_close (ftdi->usb_dev) != 0) - return -10; - return -9; + usb_close (ftdi->usb_dev); + ftdi_error_return(-9, "unable to fetch serial number"); } if (strncmp(string, serial, sizeof(string)) != 0) { ftdi->error_str = "serial number not matching\n"; if (usb_close (ftdi->usb_dev) != 0) - return -10; + ftdi_error_return(-10, "unable to fetch serial number"); continue; } } if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { - ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!"; - if (usb_close (ftdi->usb_dev) != 0) - return -10; - return -5; + usb_close (ftdi->usb_dev); + ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!"); } if (ftdi_usb_reset (ftdi) != 0) { - if (usb_close (ftdi->usb_dev) != 0) - return -10; - return -6; + usb_close (ftdi->usb_dev); + ftdi_error_return(-6, "ftdi_usb_reset failed"); } - + if (ftdi_set_baudrate (ftdi, 9600) != 0) { - if (usb_close (ftdi->usb_dev) != 0) - return -10; - return -7; + usb_close (ftdi->usb_dev); + ftdi_error_return(-7, "set baudrate failed"); } // Try to guess chip type @@ -168,43 +198,33 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, ftdi->type = TYPE_BM; else if (dev->descriptor.bcdDevice == 0x200) ftdi->type = TYPE_AM; - else if (dev->descriptor.bcdDevice == 0x500) + else if (dev->descriptor.bcdDevice == 0x500) { ftdi->type = TYPE_2232C; - - return 0; + if (!ftdi->index) + ftdi->index = INTERFACE_A; + } + ftdi_error_return(0, "all fine"); } } } // device not found - return -3; + ftdi_error_return(-3, "device not found"); } +/* ftdi_usb_reset -int ftdi_usb_reset(struct ftdi_context *ftdi) + Resets the ftdi device. + + Return codes: + 0: all fine + -1: FTDI reset failed +*/ +nt ftdi_usb_reset(struct ftdi_context *ftdi) { -#if defined(__linux__) - struct utsname kernelver; - int k_major, k_minor, k_myver; -#endif - - if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "FTDI reset failed"; - return -1; - } - -#if defined(__linux__) - /* Kernel 2.6 (maybe higher versions, too) need an additional usb_reset */ - if (uname(&kernelver) == 0 && sscanf(kernelver.release, "%d.%d", &k_major, &k_minor) == 2) { - k_myver = k_major*10 + k_minor; - - if (k_myver >= 26 && usb_reset(ftdi->usb_dev) != 0) { - ftdi->error_str = "USB reset failed"; - return -2; - } - } -#endif - + if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1,"FTDI reset failed"); + // Invalidate data in the readbuffer ftdi->readbuffer_offset = 0; ftdi->readbuffer_remaining = 0; @@ -212,29 +232,38 @@ int ftdi_usb_reset(struct ftdi_context *ftdi) return 0; } +/* ftdi_usb_purge_buffers + + Cleans the buffers of the ftdi device. + + Return codes: + 0: all fine + -1: write buffer purge failed + -2: read buffer purge failed +*/ int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) { - if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "FTDI purge of RX buffer failed"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "FTDI purge of RX buffer failed"); + // Invalidate data in the readbuffer ftdi->readbuffer_offset = 0; ftdi->readbuffer_remaining = 0; - if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "FTDI purge of TX buffer failed"; - return -1; - } - + if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-2, "FTDI purge of TX buffer failed"); return 0; } -/* ftdi_usb_close return codes - 0: all fine - -1: usb_release failed - -2: usb_close failed +/* ftdi_usb_close + + Closes the ftdi device. + + Return codes: + 0: all fine + -1: usb_release failed + -2: usb_close failed */ int ftdi_usb_close(struct ftdi_context *ftdi) { @@ -286,7 +315,7 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, int baud_diff; // Round up to supported divisor value - if (try_divisor < 8) { + if (try_divisor <= 8) { // Round up to minimum supported divisor try_divisor = 8; } else if (ftdi->type != TYPE_AM && try_divisor < 12) { @@ -333,9 +362,9 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); // Deal with special cases for encoded value if (encoded_divisor == 1) { - encoded_divisor = 0; // 3000000 baud + encoded_divisor = 0; // 3000000 baud } else if (encoded_divisor == 0x4001) { - encoded_divisor = 1; // 2000000 baud (BM only) + encoded_divisor = 1; // 2000000 baud (BM only) } // Split into "value" and "index" values *value = (unsigned short)(encoded_divisor & 0xFFFF); @@ -346,13 +375,17 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, } else *index = (unsigned short)(encoded_divisor >> 16); - + // Return the nearest baud rate return best_baud; } /* - ftdi_set_baudrate return codes: + ftdi_set_baudrate + + Sets the chip baudrate + + Return codes: 0: all fine -1: invalid baudrate -2: setting baudrate failed @@ -367,35 +400,79 @@ int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) } actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); - if (actual_baudrate <= 0) { - ftdi->error_str = "Silly baudrate <= 0."; - return -1; - } + if (actual_baudrate <= 0) + ftdi_error_return (-1, "Silly baudrate <= 0."); // Check within tolerance (about 5%) if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) || ((actual_baudrate < baudrate) ? (actual_baudrate * 21 < baudrate * 20) - : (baudrate * 21 < actual_baudrate * 20))) { - ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"; - return -1; - } + : (baudrate * 21 < actual_baudrate * 20))) + ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); - if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Setting new baudrate failed"; - return -2; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return (-2, "Setting new baudrate failed"); ftdi->baudrate = baudrate; return 0; } +/* + ftdi_set_line_property + + set (RS232) line characteristics by Alain Abbas + + Return codes: + 0: all fine + -1: Setting line property failed +*/ +int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, + enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) +{ + unsigned short value = bits; + + switch(parity) { + case NONE: + value |= (0x00 << 8); + break; + case ODD: + value |= (0x01 << 8); + break; + case EVEN: + value |= (0x02 << 8); + break; + case MARK: + value |= (0x03 << 8); + break; + case SPACE: + value |= (0x04 << 8); + break; + } + + switch(sbit) { + case STOP_BIT_1: + value |= (0x00 << 11); + break; + case STOP_BIT_15: + value |= (0x01 << 11); + break; + case STOP_BIT_2: + value |= (0x02 << 11); + break; + } + + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return (-1, "Setting new line property failed"); + + return 0; +} int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) { int ret; int offset = 0; int total_written = 0; + while (offset < size) { int write_size = ftdi->writebuffer_chunksize; @@ -403,15 +480,10 @@ int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) write_size = size-offset; ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); - if (ret < 0) { - if (ret == -1) - ftdi->error_str = "bulk write failed"; - else - ftdi->error_str = "usb failed"; - return ret; - } - total_written += ret; + if (ret < 0) + ftdi_error_return(ret, "usb bulk write failed"); + total_written += ret; offset += write_size; } @@ -462,14 +534,8 @@ int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) ftdi->readbuffer_offset = 0; /* returns how much received */ ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); - - if (ret < 0) { - if (ret == -1) - ftdi->error_str = "bulk read failed"; - else - ftdi->error_str = "usb failed"; - return ret; - } + if (ret < 0) + ftdi_error_return(ret, "usb bulk read failed"); if (ret > 2) { // skip FTDI status bytes. @@ -481,7 +547,7 @@ int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) ftdi->readbuffer_offset += 2; ret -= 2; - if (ret > 64) { + if (ret > 62) { for (i = 1; i < num_of_chunks; i++) memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, ftdi->readbuffer+ftdi->readbuffer_offset+64*i, @@ -507,6 +573,8 @@ int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) /* Did we read exactly the right amount of bytes? */ if (offset == size) + //printf("read_data exact rem %d offset %d\n", + //ftdi->readbuffer_remaining, offset); return offset; } else { // only copy part of the data or size <= readbuffer_chunksize @@ -537,10 +605,8 @@ int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksi ftdi->readbuffer_offset = 0; ftdi->readbuffer_remaining = 0; - if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) { - ftdi->error_str = "out of memory for readbuffer"; - return -1; - } + if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) + ftdi_error_return(-1, "out of memory for readbuffer"); ftdi->readbuffer = new_buf; ftdi->readbuffer_chunksize = chunksize; @@ -565,10 +631,9 @@ int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) /* FT2232C: Set bitbang_mode to 2 to enable SPI */ usb_val |= (ftdi->bitbang_mode << 8); - if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?"); + ftdi->bitbang_enabled = 1; return 0; } @@ -576,23 +641,33 @@ int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) int ftdi_disable_bitbang(struct ftdi_context *ftdi) { - if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); ftdi->bitbang_enabled = 0; return 0; } +int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) +{ + unsigned short usb_val; + + usb_val = bitmask; // low byte: bitmask + usb_val |= (mode << 8); + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?"); + + ftdi->bitbang_mode = mode; + ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0; + return 0; +} + int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) { unsigned short usb_val; - if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { - ftdi->error_str = "Read pins failed"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) + ftdi_error_return(-1, "read pins failed"); *pins = (unsigned char)usb_val; return 0; @@ -603,16 +678,13 @@ int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) { unsigned short usb_val; - if (latency < 1) { - ftdi->error_str = "Latency out of range. Only valid for 1-255"; - return -1; - } + if (latency < 1) + ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); usb_val = latency; - if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Unable to set latency timer"; - return -2; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-2, "unable to set latency timer"); + return 0; } @@ -620,10 +692,8 @@ int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) { unsigned short usb_val; - if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { - ftdi->error_str = "Reading latency timer failed"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) + ftdi_error_return(-1, "reading latency timer failed"); *latency = (unsigned char)usb_val; return 0; @@ -655,7 +725,12 @@ void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) /* - ftdi_eeprom_build return codes: + ftdi_eeprom_build + + Build binary output from ftdi_eeprom structure. + Output is suitable for ftdi_write_eeprom. + + Return codes: positive value: used eeprom size -1: eeprom size (128 bytes) exceeded by custom strings */ @@ -819,10 +894,8 @@ int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) int i; for (i = 0; i < 64; i++) { - if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) { - ftdi->error_str = "Reading eeprom failed"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) + ftdi_error_return(-1, "reading eeprom failed"); } return 0; @@ -837,10 +910,8 @@ int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) for (i = 0; i < 64; i++) { usb_val = eeprom[i*2]; usb_val += eeprom[(i*2)+1] << 8; - if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Unable to write eeprom"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "unable to write eeprom"); } return 0; @@ -849,10 +920,14 @@ int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) int ftdi_erase_eeprom(struct ftdi_context *ftdi) { - if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) { - ftdi->error_str = "Unable to erase eeprom"; - return -1; - } + if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) + ftdi_error_return(-1, "unable to erase eeprom"); return 0; } + + +char *ftdi_get_error_string (struct ftdi_context *ftdi) +{ + return ftdi->error_str; +}