libftdi: (tomj) documentation updates by Tim Ansell
[libftdi] / src / ftdi.c
index 908d420..52ca36e 100644 (file)
 
 #include <usb.h>
 #include <string.h>
-#include <sys/utsname.h>
 
 #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)
 {
@@ -56,18 +59,21 @@ int ftdi_init(struct ftdi_context *ftdi)
     /* All fine. Now allocate the readbuffer */
     return ftdi_read_data_set_chunksize(ftdi, 4096);
 }
-/* ftdi_select_interface
+
+/* ftdi_set_interface
+   
    Call after ftdi_init
+   
    Open selected channels on a chip, otherwise use first channel
-   0: all fine
-   1: unknown interface
+    0: all fine
+   -1: unknown interface
 */
-int ftdi_select_interface(struct ftdi_context *ftdi, enum ftdi_interface 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*/
+        /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
         break;
     case INTERFACE_B:
         ftdi->interface = 1;
@@ -81,6 +87,10 @@ int ftdi_select_interface(struct ftdi_context *ftdi, enum ftdi_interface interfa
     return 0;
 }
 
+/* ftdi_deinit
+
+   Deinitializes a ftdi_context.
+*/
 void ftdi_deinit(struct ftdi_context *ftdi)
 {
     if (ftdi->readbuffer != NULL) {
@@ -89,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)
 {
@@ -196,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;
@@ -224,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)
@@ -239,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)
 {
@@ -341,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);
@@ -360,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
@@ -392,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)
 {
@@ -473,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,
@@ -651,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
 */