X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=src%2Fftdi.c;h=52ca36ed8e6fc2d86e0e306c2a1ed1a6a053fdd8;hp=1ee86ebb9c23fcd5b3c79aeb0cf779f943c516a3;hb=4837f98ad4e0b6fae27c2e2741a2d527cd6b8289;hpb=21abaf2ee8b96fe16aeffc8677faa8439869b0e3 diff --git a/src/ftdi.c b/src/ftdi.c index 1ee86eb..52ca36e 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -16,19 +16,22 @@ #include #include -#include #include "ftdi.h" #define ftdi_error_return(code, str) do { \ - ftdi->error_str = str; \ + ftdi->error_str = str; \ return code; \ } while(0); -/* ftdi_init return codes: - 0: all fine - -1: couldn't allocate read buffer +/* ftdi_init + + Initalises a ftdi_context. + + Return codes: + 0: All fine + -1: Couldn't allocate read buffer */ int ftdi_init(struct ftdi_context *ftdi) { @@ -57,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) { @@ -66,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) { @@ -159,9 +198,11 @@ 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; - + if (!ftdi->index) + ftdi->index = INTERFACE_A; + } ftdi_error_return(0, "all fine"); } } @@ -171,27 +212,19 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, 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_return(-1,"FTDI reset failed"); -#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_return(-2, "USB reset failed"); - } -#endif - // Invalidate data in the readbuffer ftdi->readbuffer_offset = 0; ftdi->readbuffer_remaining = 0; @@ -199,6 +232,15 @@ 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) @@ -214,10 +256,14 @@ int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) 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) { @@ -269,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) { @@ -316,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); @@ -335,7 +381,11 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, } /* - 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,6 +417,55 @@ int ftdi_set_baudrate(struct ftdi_context *ftdi, int 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) { @@ -448,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, @@ -474,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 @@ -548,6 +649,20 @@ int ftdi_disable_bitbang(struct ftdi_context *ftdi) } +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; @@ -610,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 */