diff options
| author | Tero Kontkanen <tero.k@granitedevices.com> | 2017-08-23 14:44:10 +0300 |
|---|---|---|
| committer | Tero Kontkanen <tero.k@granitedevices.com> | 2017-08-23 14:44:10 +0300 |
| commit | 98a89371108bbcdedb201caf7be8b82a323e60cc (patch) | |
| tree | 915a5596c632256ab77d5dc42236644307ea3b66 | |
| parent | 5d21340928cd46b3cb97cae0b9c707a71ef9ced0 (diff) | |
| parent | 4176d9358a526ff207bb5f4d127f4143a632d748 (diff) | |
| download | SimpleMotionV2-98a89371108bbcdedb201caf7be8b82a323e60cc.tar.gz SimpleMotionV2-98a89371108bbcdedb201caf7be8b82a323e60cc.zip | |
Merge branch 'feature/port_driver_ftdi_d2xx' into develop
| -rw-r--r-- | SimpleMotionV2.pri | 20 | ||||
| -rw-r--r-- | busdevice.c | 208 | ||||
| -rw-r--r-- | busdevice.h | 8 | ||||
| -rw-r--r-- | devicedeployment.c | 2 | ||||
| -rw-r--r-- | devicedeployment.h | 4 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/d2xx/Readme and license.txt | 168 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/sm_d2xx.c | 264 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/sm_d2xx.h | 40 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/third_party/Readme and license.txt | 20 | ||||
| -rwxr-xr-x | drivers/ftdi_d2xx/third_party/WinTypes.h | 154 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/third_party/ftd2xx.h | 1446 | ||||
| -rw-r--r-- | drivers/ftdi_d2xx/third_party/osx/libftd2xx.a | bin | 0 -> 350680 bytes | |||
| -rw-r--r-- | drivers/ftdi_d2xx/third_party/win_32bit/ftd2xx.lib | bin | 0 -> 22190 bytes | |||
| -rw-r--r-- | drivers/serial/pcserialport.c (renamed from pcserialport.c) | 3 | ||||
| -rw-r--r-- | drivers/serial/pcserialport.h (renamed from pcserialport.h) | 0 | ||||
| -rw-r--r-- | drivers/tcpip/tcpclient.c (renamed from tcpclient.c) | 115 | ||||
| -rw-r--r-- | drivers/tcpip/tcpclient.h (renamed from tcpclient.h) | 6 | ||||
| -rw-r--r-- | simplemotion.c | 46 | ||||
| -rw-r--r-- | simplemotion.h | 35 | ||||
| -rw-r--r-- | simplemotion_private.h | 16 |
20 files changed, 2403 insertions, 152 deletions
diff --git a/SimpleMotionV2.pri b/SimpleMotionV2.pri index 8222018..304d404 100644 --- a/SimpleMotionV2.pri +++ b/SimpleMotionV2.pri @@ -1,3 +1,5 @@ +#User options
+SUPPORT_FTDI_D2XX_DRIVER = 1 #requires FTDI D2XX driver & library. benefit of this support is automatic detection of correct device and automatic low latency setting for FTDI USB serial converters
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
@@ -5,10 +7,20 @@ DEPENDPATH += $$PWD DEFINES += SIMPLEMOTIONV2_LIBRARY
-SOURCES += $$PWD/sm_consts.c $$PWD/simplemotion.c $$PWD/busdevice.c $$PWD/pcserialport.c \
- $$PWD/bufferedmotion.c $$PWD/tcpclient.c $$PWD/devicedeployment.c
+SOURCES += $$PWD/sm_consts.c $$PWD/simplemotion.c $$PWD/busdevice.c $$PWD/drivers/serial/pcserialport.c \
+ $$PWD/bufferedmotion.c $$PWD/drivers/tcpip/tcpclient.c $$PWD/devicedeployment.c
HEADERS += $$PWD/simplemotion_private.h\
- $$PWD/pcserialport.h $$PWD/busdevice.h $$PWD/simplemotion.h $$PWD/sm485.h $$PWD/simplemotion_defs.h \
- $$PWD/bufferedmotion.h $$PWD/tcpclient.h $$PWD/devicedeployment.h
+ $$PWD/drivers/serial/pcserialport.h $$PWD/busdevice.h $$PWD/simplemotion.h $$PWD/sm485.h $$PWD/simplemotion_defs.h \
+ $$PWD/bufferedmotion.h $$PWD/drivers/tcpip/tcpclient.h $$PWD/devicedeployment.h
+
+#If FTDI D2XX support is enabled
+greaterThan(SUPPORT_FTDI_D2XX_DRIVER, 0+) {
+ SOURCES += $$PWD/drivers/ftdi_d2xx/sm_d2xx.c
+ HEADERS += $$PWD/drivers/ftdi_d2xx/sm_d2xx.c
+ macx:LIBS += $$PWD/drivers/ftdi_d2xx/third_party/osx/libftd2xx.a -framework CoreFoundation #mac will needs insetalling some FTDI helper tool & reboot to make port open to work. see d2xx downloads page from ftdi.
+ win32:LIBS += $$PWD/drivers/ftdi_d2xx/third_party/win_32bit/ftd2xx.lib
+ linux:LIBS += #tbd
+ DEFINES += FTDI_D2XX_SUPPORT
+}
diff --git a/busdevice.c b/busdevice.c index 4bc0ee0..7c42202 100644 --- a/busdevice.c +++ b/busdevice.c @@ -1,12 +1,12 @@ #include "busdevice.h" -#include "pcserialport.h" -#include "tcpclient.h" +#include "drivers/serial/pcserialport.h" +#include "drivers/tcpip/tcpclient.h" +#include "drivers/ftdi_d2xx/sm_d2xx.h" #include <string.h> #include <errno.h> #include <stdlib.h> -#include <ctype.h> #define BD_NONE 0 #define BD_RS 1 @@ -52,117 +52,6 @@ void smBDinit() bdInitialized=smtrue; } -//accepted TCP/IP address format is nnn.nnn.nnn.nnn:pppp where n is IP address numbers and p is port number -static int validateIpAddress(const char *s, const char **pip_end, - const char **pport_start) -{ - int octets = 0; - int ch = 0, prev = 0; - int len = 0; - const char *ip_end = NULL; - const char *port_start = NULL; - - while (*s) - { - ch = *s; - - if (isdigit(ch)) - { - ++len; - // Octet len must be 1-3 digits - if (len > 3) - { - return -1; - } - } - else if (ch == '.' && isdigit(prev)) - { - ++octets; - len = 0; - // No more than 4 octets please - if (octets > 4) - { - return -1; - } - } - else if (ch == ':' && isdigit(prev)) - { - ++octets; - // We want exactly 4 octets at this point - if (octets != 4) - { - return -1; - } - ip_end = s; - ++s; - port_start = s; - while (isdigit((ch = *s))) - ++s; - // After port we want the end of the string - if (ch != '\0') - return -1; - // This will skip over the ++s below - continue; - } - else - { - return -1; - } - - prev = ch; - ++s; - } - - // We reached the end of the string and did not encounter the port - if (*s == '\0' && ip_end == NULL) - { - ++octets; - ip_end = s; - } - - // Check that there are exactly 4 octets - if (octets != 4) - return -1; - - if (pip_end) - *pip_end = ip_end; - - if (pport_start) - *pport_start = port_start; - - return 0; -} - -static int parseIpAddress(const char *s, char *ip, size_t ipsize, short *port) -{ - const char *ip_end, *port_start; - - //ip_end and port_start are pointers to memory area of s, not offsets or indexes to s - if (validateIpAddress(s, &ip_end, &port_start) == -1) - return -1; - - // If ip=NULL, we just report that the parsing was ok - if (!ip) - return 0; - - if (ipsize < (size_t)(ip_end - s + 1)) - return -1; - - memcpy(ip, s, ip_end - s); - ip[ip_end - s] = '\0'; - - if (port_start) - { - *port = 0; - while (*port_start) - { - *port = *port * 10 + (*port_start - '0'); - ++port_start; - } - } - - return 0; -} //ie "COM1" "VSD2USB" //return -1 if fails, otherwise handle number @@ -207,8 +96,21 @@ smbusdevicehandle smBDOpen( const char *devicename ) BusDevice[handle].bdType=BD_TCP; BusDevice[handle].txBufferUsed=0; } +#ifdef FTDI_D2XX_SUPPORT + else if (strncmp(devicename,"FTDI",4) == 0)//starts with FTDI. Full name is FTDIn where n=index starting from 0. + { + BusDevice[handle].comPort=d2xxPortOpen( devicename, SMBusBaudrate ); + if( BusDevice[handle].comPort == -1 ) + { + return -1; //failed to open + } + BusDevice[handle].bdType=BD_FTDI; + BusDevice[handle].txBufferUsed=0; + } +#endif else//no other bus types supproted yet { + smDebug( -1, Low, "smBDOpen device name argument syntax didn't match any supported driver port name"); return -1; } @@ -243,8 +145,16 @@ smbool smBDClose( const smbusdevicehandle handle ) BusDevice[handle].opened=smfalse; return smtrue; } +#ifdef FTDI_D2XX_SUPPORT + else if( BusDevice[handle].bdType==BD_FTDI ) + { + d2xxPortClose( BusDevice[handle].comPort ); + BusDevice[handle].opened=smfalse; + return smtrue; + } +#endif - return smfalse; + return smfalse; } @@ -257,20 +167,15 @@ smbool smBDWrite(const smbusdevicehandle handle, const smuint8 byte ) //check if handle valid & open if( smIsBDHandleOpen(handle)==smfalse ) return smfalse; - if( BusDevice[handle].bdType==BD_RS || BusDevice[handle].bdType==BD_TCP ) - { - if(BusDevice[handle].txBufferUsed<TANSMIT_BUFFER_LENGTH) - { - //append to buffer - BusDevice[handle].txBuffer[BusDevice[handle].txBufferUsed]=byte; - BusDevice[handle].txBufferUsed++; - return smtrue; - } - else - return smfalse; - } + if(BusDevice[handle].txBufferUsed<TANSMIT_BUFFER_LENGTH) + { + //append to buffer + BusDevice[handle].txBuffer[BusDevice[handle].txBufferUsed]=byte; + BusDevice[handle].txBufferUsed++; + return smtrue; + } - return smfalse; + return smfalse; } smbool smBDTransmit(const smbusdevicehandle handle) @@ -304,6 +209,21 @@ smbool smBDTransmit(const smbusdevicehandle handle) return smfalse; } } +#ifdef FTDI_D2XX_SUPPORT + else if( BusDevice[handle].bdType==BD_FTDI ) + { + if(d2xxPortWriteBuffer(BusDevice[handle].comPort,BusDevice[handle].txBuffer, BusDevice[handle].txBufferUsed)==BusDevice[handle].txBufferUsed) + { + BusDevice[handle].txBufferUsed=0; + return smtrue; + } + else + { + BusDevice[handle].txBufferUsed=0; + return smfalse; + } + } +#endif return smfalse; } @@ -329,7 +249,37 @@ smbool smBDRead( const smbusdevicehandle handle, smuint8 *byte ) if( n!=1 ) return smfalse; else return smtrue; } - +#ifdef FTDI_D2XX_SUPPORT + else if( BusDevice[handle].bdType==BD_FTDI ) + { + int n; + n=d2xxPortRead(BusDevice[handle].comPort, byte, 1); + if( n!=1 ) return smfalse; + else return smtrue; + } +#endif return smfalse; } + +//BUS DEVICE INFO FETCH FUNCTIONS: + +//Return number of bus devices found. details of each device may be consequently fetched by smGetBusDeviceDetails() +smint smBDGetNumberOfDetectedBuses() +{ + //only supports FTDI D2XX at the moment +#ifdef FTDI_D2XX_SUPPORT + return d2xxGetNumberOfDetectedBuses(); +#endif + return 0; +} + +smbool smBDGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info ) +{ + //only supports FTDI D2XX at the moment +#ifdef FTDI_D2XX_SUPPORT + return d2xxGetBusDeviceDetails(index,info); +#endif + return smfalse; + +} diff --git a/busdevice.h b/busdevice.h index 6a02a26..3cb4e24 100644 --- a/busdevice.h +++ b/busdevice.h @@ -31,5 +31,13 @@ smbool smBDTransmit(const smbusdevicehandle handle); smbool smBDRead( const smbusdevicehandle handle , smuint8 *byte ); +//BUS DEVICE INFO FETCH FUNCTIONS: + +// Return number of bus devices found. details of each device may be consequently fetched by smBDGetBusDeviceDetails() +smint smBDGetNumberOfDetectedBuses(); + +//return smtrue if success +smbool smBDGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info ); + #endif diff --git a/devicedeployment.c b/devicedeployment.c index 4b5a50e..c832575 100644 --- a/devicedeployment.c +++ b/devicedeployment.c @@ -667,7 +667,7 @@ FirmwareUploadStatus smFirmwareUploadFromBuffer( const smbus smhandle, const int } if(i==256)//DFU device not found - return abortFWUpload(CFGConnectingDFUModeFailed,&state,400);//setting DFU mode failed + return abortFWUpload(FWConnectingDFUModeFailed,&state,400);//setting DFU mode failed progress=3; } diff --git a/devicedeployment.h b/devicedeployment.h index 60f1a69..f9cfaa1 100644 --- a/devicedeployment.h +++ b/devicedeployment.h @@ -41,7 +41,8 @@ typedef enum FWIncompatibleFW=-3, FWConnectionLoss=-4, FWUnsupportedTargetDevice=-5, - FWFileNotReadable=-6 + FWFileNotReadable=-6, + FWConnectingDFUModeFailed=-7 } FirmwareUploadStatus; /** @@ -68,7 +69,6 @@ typedef enum CFGComplete=100, CFGInvalidFile=-1, CFGCommunicationError=-2, - CFGConnectingDFUModeFailed=-3, CFGIncompatibleFW=-4, CFGUnsupportedTargetDevice=-5, CFGUnableToOpenFile=-6 diff --git a/drivers/ftdi_d2xx/d2xx/Readme and license.txt b/drivers/ftdi_d2xx/d2xx/Readme and license.txt new file mode 100644 index 0000000..e4082f2 --- /dev/null +++ b/drivers/ftdi_d2xx/d2xx/Readme and license.txt @@ -0,0 +1,168 @@ +Files in this folder are downloaded from FTDI: +http://www.ftdichip.com/Drivers/D2XX.htm + + +Driver license +-------------- + +IMPORTANT NOTICE: PLEASE READ CAREFULLY BEFORE INSTALLING THE RELEVANT SOFTWARE: This licence agreement ("Licence") is a legal agreement between you ("Licencee" or "you") and Future Technology Devices International Limited of 2 Seaward Place, Centurion Business Park, Glasgow, Scotland, G41 1HH (UK Company Number SC136640) ("Licensor" or "we) for use of driver software provided by the Licensor ("Software"). + +BY INSTALLING OR USING THIS SOFTWARE YOU AGREE TO THE TERMS OF THIS LICENCE WHICH WILL BIND YOU. IF YOU DO NOT AGREE TO THE TERMS OF THIS LICENCE, WE ARE UNWILLING TO LICENSE THE SOFTWARE TO YOU AND YOU MUST DISCONTINUE INSTALLATION OF THE SOFTWARE NOW. + +1. Grant and scope of licence + +1.1 In consideration of you agreeing to abide by the terms of this Licence, the Licensor hereby grants to you a non-exclusive, non-transferable, royalty free licence to use the Software on the terms of this Licence. + +1.2 In this Licence a "Genuine FTDI Component" means an item of hardware that was manufactured for, and sold by, the Licensor or a member of the Licensor's group of companies. It does not include any counterfeit or fake products. + +1.3 If you are a manufacturer of a device that includes a Genuine FTDI Component (each a "Device") then you may install the Software onto that device. If you are a seller or distributor of a Device then You may distribute the Software with the Device. If you are a user of a Device then you may install the Software on the Device, or onto a computer system in order to use the Device. + +1.4 In each of those cases you may: + +1.4.1 install and use the Software for your purposes only; and + +1.4.2 only use the Software in conjunction with products based on and/or incorporating a Genuine FTDI Component. + +1.5 The Software will not function properly on or with a component that is not a Genuine FTDI Component. Use of the Software as a driver for, or installation of the Software onto, a component that is not a Genuine FTDI Component, including without limitation counterfeit components, MAY IRRETRIEVABLY DAMAGE THAT COMPONENT. It is the Licensee's responsibility to make sure that all chips it installs the Software on, or uses the Software as a driver for, are Genuine FTDI Components. If in doubt then contact the Licensor. + +2. If a custom vendor ID and/or product ID or description string are used, it is the responsibility of the product manufacturer to maintain any changes and subsequent WHQL re-certification as a result of making these changes. + +3. Licensee's undertakings + +3.1 Except as expressly set out in this Licence or as permitted by any local law, you undertake: + +3.1.1 not to copy the Software, except where such copying is incidental to normal use of the Software or where it is necessary for the purpose of back-up or operational security; + +3.1.2 not to rent, lease, sub-license, loan, translate, merge, adapt, vary or modify the Software or any part of it; + +3.1.3 not to make alterations to, or modifications of, the whole or any part of the Software nor permit the Software or any part of it to be combined with, or become incorporated in, any other programs; + +3.1.4 not to disassemble, de-compile, reverse engineer or create derivative works based on the whole or any part of the Software; + +3.1.5 to keep all copies of the Software secure; + +3.1.6 to include the copyright notice of the Licensor on all entire and partial copies of the Software in any form; and + +3.1.7 not to provide, or otherwise make available, the Software in any form, in whole or in part (including, but not limited to, program listings, object and source program listings, object code and source code) to any person. + +4. Intellectual property rights + +4.1 You acknowledge that all intellectual property rights in the Software throughout the world belong to the Licensor, that rights in the Software are licensed (not sold) to you, and that you have no rights in, or to, the Software other than the right to use them in accordance with the terms of this Licence. + +5. Warranty + +5.1 To the maximum extent permitted by applicable law, the software is provided "as is". + +5.2 All implied warranties, implied conditions and/or implied licences are excluded from this Licence, including but not limited to implied warranties of quality and/or fitness for purpose (in all cases) to the fullest extent permitted by law. + + + +5.3 You acknowledge that the Software has not been developed to meet your individual requirements and that the Software may not be uninterrupted or free of bugs or errors. + +6. Licensor's liability + +6.1 To the maximum extent permitted by applicable law, in no event shall the Licensor be liable for any: + +6.1.1 special loss or damage; + +6.1.2 incidental loss or damage; + +6.1.3 indirect or consequential loss or damage: + +6.1.4 loss of income; + +6.1.5 loss of business; + +6.1.6 loss of profits; + +6.1.7 loss of revenue; + +6.1.8 loss of contracts; + +6.1.9 business interruption; + +6.1.10 loss of the use of money or anticipated savings; + +6.1.11 loss of information; + +6.1.12 loss of opportunity; + +6.1.13 loss of goodwill or reputation; and/or + +6.1.14 loss of, damage to or corruption of data; + +(in each case) of any kind howsoever arising and whether caused by delict (including negligence), breach of contract or otherwise. + +6.2 FTDI's total liability to you in relation to the Software shall not exceed 500 US Dollars. + +6.3 Nothing in this Licence limits or excludes liability for death or personal injury or for fraud. + +7. Termination + +7.1 The Licensor may terminate this Licence immediately if: + +7.1.1 you fail to comply with any of the terms and conditions of the Licence; or + +7.1.2 you commence or participate in any legal proceedings against the Licensor. + +7.2 Upon termination: + +7.2.1 all rights granted to you under this Licence shall cease; + +7.2.2 you must cease all activities authorised by this Licence; and + +7.2.3 you must immediately delete or remove the Software from all computer equipment in your possession and immediately destroy all copies of the Software then in your possession, custody or control. + +8. Transfer of rights and obligations + +8.1 You may not transfer, assign, charge or otherwise dispose of this Licence, or any of your rights or obligations arising under it. + +8.2 The Licensor may transfer, assign, charge, sub-contract or otherwise dispose of this Licence, or any of his rights or obligations arising under it, at any time during the term of the Licence. + +9. Waiver + +9.1 If the Licensor fails, at any time during the term of this Licence, to insist on strict performance of any of your obligations under this Licence, or if the Licensor fails to exercise any of the rights or remedies to which he is entitled under this Licence, this shall not constitute a waiver of such rights or remedies and shall not relieve you from compliance with such obligations. + +9.2 A waiver by the Licensor of any default shall not constitute a waiver of any subsequent default. + +9.3 No waiver by the Licensor of any of these terms and conditions shall be effective unless it is expressly stated to be a waiver and is communicated to you in writing. + +10. Severability + +If any of the terms of this Licence are determined by any competent authority to be invalid, unlawful or unenforceable to any extent, such term, condition or provision will to that extent be severed from the remaining terms, conditions and provisions which will continue to be valid to the fullest extent permitted by law. + +11. Entire agreement + +11.1 This Licence constitutes the whole agreement between us and supersedes any previous arrangement, understanding or agreement between us, relating to the licensing of the Software. + +11.2 Each party acknowledges that in entering into this Licence it does not rely on any statement, representation, warranty or understanding other than those expressly set out in this Licence. Each party agrees that it will have no remedy in respect of any statement, representation, warranty or understanding that is not expressly set out in this Licence. Each party agrees that its only remedy in respect of those representations, statements, assurances and warranties that are set out in this Licence will be for breach of contract in accordance with the terms of this Licence. + +11.3 The parties agree that nothing in this Licence will limit or exclude any liability they may have for fraud. + +12. Miscellaneous + +12.1 This Licence does not create a partnership or joint venture between the parties to it, nor authorise a party to act as agent for the other. + +12.2 This Licence does not create any legal rights enforceable by any third party. + +12.3 This Licence may only be varied by express written legal agreement between the parties. + +13. Law and jurisdiction + +This Licence, its subject matter or its formation (including non-contractual disputes or claims) shall be governed by and construed in accordance with Scots law and submitted to the non-exclusive jurisdiction of the Scottish courts. + + + +OS X library +------------ + +OS X D2XX Library Version 1.4.4 +© Future Technology Devices International Ltd. 2016 + +DISCLAIMER + +This software is supplied on an as-is basis and no warranty as to their suitability for any particular purpose is either made or implied. Future Technology Devices International Ltd. will not accept any claim for damages howsoever arising as a result of use or failure of this software. Your statutory rights are not affected. This software or any variant of it is not intended for use in any medical appliance, device or system in which the failure of the product might reasonably be expected to result in personal injury. This document provides preliminary information that may be subject to change without notice. + +Libusb + +This package uses an unmodified version of libusb (libusb.info) which is distributed under the terms of the GNU Lesser General Public License (see libusb/COPYING or www.gnu.org/licenses). Source code for libusb is included in this distribution. diff --git a/drivers/ftdi_d2xx/sm_d2xx.c b/drivers/ftdi_d2xx/sm_d2xx.c new file mode 100644 index 0000000..aa5eef7 --- /dev/null +++ b/drivers/ftdi_d2xx/sm_d2xx.c @@ -0,0 +1,264 @@ +/* + * ftdi_d2xx.c + * + * Header for FTDI D2XX serial port access library + * + * Created on: 19.8.2017 + * Author: Tero Kontkanen + */ + +#include "drivers/ftdi_d2xx/sm_d2xx.h" +#include "simplemotion_private.h" //needed for timeout variable +#include "drivers/ftdi_d2xx/third_party/ftd2xx.h" +#include <string.h> +#include <simplemotion.h> + +smbool handles_initialized=smfalse; +FT_HANDLE handles[MAX_OPEN_PORTS];//FT_HANDLE type is just a pointer + + +static int stringToNumber( const char *str, smbool *ok ) +{ + int len=strlen(str); + int i, number=0, decade=1; + + if(len<1) + { + *ok=smfalse; + return 0; + } + + for(i=len-1;i>=0;i--) + { + if(str[i]<'0' || str[i]>'9')//non-numeric char->fail + { + *ok=smfalse; + return 0; + } + number+=decade*(str[i]-'0'); + decade*=10; + } + *ok=smtrue; + return number; +} + +smint32 d2xxPortOpen(const char *port_device_name, smint32 baudrate_bps) +{ + //init array of handles if not done yet + if(handles_initialized==smfalse) + { + int i; + for(i=0;i<MAX_OPEN_PORTS;i++) + handles[i]=NULL; + handles_initialized=smtrue; + } + + //parse name string + int ftdiIndex; + smbool ok; + + if (strncmp(port_device_name,"FTDI",4) != 0)//must start with FTDI. Full name is FTDIn where n=index starting from 0. + { + smDebug( -1, Low, "FTDI port error: malformed port name '%s'. Must be 'FTDIx' where x is index starting from 0.\n",port_device_name); + return -1; + } + ftdiIndex=stringToNumber(port_device_name+4,&ok); + if (ok==smfalse) + { + smDebug( -1, Low, "FTDI port error: malformed port number '%s'. Must be 'FTDIx' where x is index starting from 0.\n",port_device_name); + return -1; + } + + //open port + FT_HANDLE h; + FT_STATUS s=FT_Open(ftdiIndex,&h); + + if(s==FT_OK) + { + //all good, find free handle + int i; + for(i=0;i<MAX_OPEN_PORTS;i++) + { + if(handles[i]==NULL) + { + if(FT_ResetDevice(h)!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to reset USB chip\n"); + goto error; + } + + //init port settings + s=FT_SetBaudRate(h,baudrate_bps); + if(s!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to set baud rate\n"); + goto error; + } + + if(FT_SetLatencyTimer(h,1)!=FT_OK)//API doc says 2ms is minimum but 1 seem to work too + { + smDebug( -1, Low, "FTDI port error: failed to set latency\n"); + goto error; + } + + if(FT_SetFlowControl(h,FT_FLOW_NONE,0,0)!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to set flow control\n"); + goto error; + } + + if(FT_SetDataCharacteristics(h,FT_BITS_8,FT_STOP_BITS_1,FT_PARITY_NONE)!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to set data characteristics\n"); + goto error; + } + + if(FT_SetTimeouts(h,readTimeoutMs,readTimeoutMs)!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to set timeout\n"); + goto error; + } + + if(FT_Purge(h,FT_PURGE_RX|FT_PURGE_TX)!=FT_OK) + { + smDebug( -1, Low, "FTDI port error: failed to set purge\n"); + goto error; + } + + smDebug( -1, Mid, "FTDI port opened\n"); + handles[i]=h; + return i; + } + } + smDebug( -1, Low, "FTDI port error: all handles taken, too many ports open\n"); + goto error; + } + else + smDebug( -1, Low, "FTDI port error: FT_Open failed\n"); + + error: + FT_Close(h); + return -1; +} + + +smint32 d2xxPortRead(smint32 serialport_handle, unsigned char *buf, smint32 size) +{ + FT_STATUS s; + DWORD BytesReceived; + + s=FT_Read(handles[serialport_handle],buf,size,&BytesReceived); + if(s!=FT_OK) + { + //failed + smDebug( -1, Low, "FTDI port error: failed to receive data from port\n"); + } + + return BytesReceived; +} + + +smint32 d2xxPortWriteByte(smint32 serialport_handle, unsigned char byte) +{ + unsigned char buf[1]; + DWORD BytesWritten; + buf[0]=byte; + FT_STATUS s=FT_Write(handles[serialport_handle], buf, 1, &BytesWritten); + + if(s!=FT_OK) + { + //failed + smDebug( -1, Low, "FTDI port error: failed to write data to port\n"); + } + + return BytesWritten; +} + + +smint32 d2xxPortWriteBuffer(smint32 serialport_handle, unsigned char *buf, smint32 size) +{ + DWORD BytesWritten; + FT_STATUS s=FT_Write(handles[serialport_handle], buf, size, &BytesWritten); + + if(s!=FT_OK) + { + //failed + smDebug( -1, Low, "FTDI port error: failed to write data to port\n"); + } + + return BytesWritten; +} + + +void d2xxPortClose(smint32 serialport_handle) +{ + if(FT_Close(handles[serialport_handle])!=FT_OK) + { + //failed + smDebug( -1, Low, "FTDI port error: failed to close port\n"); + } + else + handles[serialport_handle]=NULL; +} + + + +//BUS DEVICE INFO FETCH FUNCTIONS: + +//Return number of bus devices found. details of each device may be consequently fetched by smGetBusDeviceDetails() +smint d2xxGetNumberOfDetectedBuses() +{ + FT_STATUS ftStatus; + DWORD numDevs; + // Get the number of devices currently connected + ftStatus = FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY); + if (ftStatus == FT_OK) + { + return numDevs; + } + else + { + return 0; + } + + return 0; +} + +smbool d2xxGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info ) +{ + DWORD devIndex = index; + char description[64]; // more than enough room + FT_STATUS ftStatus = FT_ListDevices((PVOID)devIndex, description, FT_LIST_BY_INDEX|FT_OPEN_BY_DESCRIPTION); + if (ftStatus == FT_OK) + { + smbool compatible=smfalse; + if(strncmp(description,"TTL232R",7)==0 + || strncmp(description,"SMV2USB",7)==0 + || strncmp(description,"USB-SMV2",8)==0 + || strncmp(description,"FT230X",6)==0 + || strncmp(description,"IONICUBE",8)==0 + || strncmp(description,"ATOMI",5)==0) + compatible=smtrue; + + info->is_simplemotion_device=compatible; + + if(compatible==smtrue) + { + sprintf(info->description,"SimpleMotion USB (%s)",description); + sprintf(info->device_name,"FTDI%d",index); + } + else//some unknown device with FTDI chip + { + sprintf(info->description,"Unknown FTDI device (%s)",description); + sprintf(info->device_name,"FTDI%d",index); + } + return smtrue; + } + else + { + return smfalse; + } + + return smfalse; +} + diff --git a/drivers/ftdi_d2xx/sm_d2xx.h b/drivers/ftdi_d2xx/sm_d2xx.h new file mode 100644 index 0000000..289ae8a --- /dev/null +++ b/drivers/ftdi_d2xx/sm_d2xx.h @@ -0,0 +1,40 @@ +/* + * ftdi_d2xx.h + * + * Header for FTDI D2XX serial port access library + * + * Created on: 19.8.2017 + * Author: Tero Kontkanen + */ + + +#ifndef SM_D2XX_H +#define SM_D2XX_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "simplemotion.h" + +//max number of simultaneously connected FTDI devices +#define MAX_OPEN_PORTS 32 + +//return port handle or -1 if fails +smint32 d2xxPortOpen(const char *port_device_name, smint32 baudrate_bps); +smint32 d2xxPortRead(smint32 serialport_handle, unsigned char *buf, smint32 size); +smint32 d2xxPortWriteByte(smint32 serialport_handle, unsigned char byte); +smint32 d2xxPortWriteBuffer(smint32 serialport_handle, unsigned char *buf, smint32 size); +void d2xxPortClose(smint32 serialport_handle); + +//Return number of bus devices found. details of each device may be consequently fetched by smGetBusDeviceDetails() +smint d2xxGetNumberOfDetectedBuses(); +smbool d2xxGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info ); + +#ifdef __cplusplus +} +#endif + +#endif + + diff --git a/drivers/ftdi_d2xx/third_party/Readme and license.txt b/drivers/ftdi_d2xx/third_party/Readme and license.txt new file mode 100644 index 0000000..4cbd58e --- /dev/null +++ b/drivers/ftdi_d2xx/third_party/Readme and license.txt @@ -0,0 +1,20 @@ +Files in this folder are downloaded from FTDI: +http://www.ftdichip.com/Drivers/D2XX.htm + +Copyright notes +--------------- + + +OS X library +------------ + +OS X D2XX Library Version 1.4.4 +© Future Technology Devices International Ltd. 2016 + +DISCLAIMER + +This software is supplied on an as-is basis and no warranty as to their suitability for any particular purpose is either made or implied. Future Technology Devices International Ltd. will not accept any claim for damages howsoever arising as a result of use or failure of this software. Your statutory rights are not affected. This software or any variant of it is not intended for use in any medical appliance, device or system in which the failure of the product might reasonably be expected to result in personal injury. This document provides preliminary information that may be subject to change without notice. + +Libusb + +This package uses an unmodified version of libusb (libusb.info) which is distributed under the terms of the GNU Lesser General Public License (see libusb/COPYING or www.gnu.org/licenses). Source code for libusb is included in this distribution. diff --git a/drivers/ftdi_d2xx/third_party/WinTypes.h b/drivers/ftdi_d2xx/third_party/WinTypes.h new file mode 100755 index 0000000..88e500e --- /dev/null +++ b/drivers/ftdi_d2xx/third_party/WinTypes.h @@ -0,0 +1,154 @@ +#ifndef __WINDOWS_TYPES__ +#define __WINDOWS_TYPES__ + +#define WINAPI + +typedef unsigned int DWORD; +typedef unsigned int ULONG; +typedef unsigned short USHORT; +typedef unsigned short SHORT; +typedef unsigned char UCHAR; +typedef unsigned short WORD; +typedef unsigned short WCHAR; +typedef unsigned char BYTE; +typedef BYTE *LPBYTE; +typedef unsigned int BOOL; +typedef unsigned char BOOLEAN; +typedef unsigned char CHAR; +typedef BOOL *LPBOOL; +typedef UCHAR *PUCHAR; +typedef const char *LPCSTR; +typedef char *PCHAR; +typedef void *PVOID; +typedef void *HANDLE; +typedef unsigned int LONG; +typedef int INT; +typedef unsigned int UINT; +typedef char *LPSTR; +typedef char *LPTSTR; +typedef const char *LPCTSTR; +typedef DWORD *LPDWORD; +typedef WORD *LPWORD; +typedef ULONG *PULONG; +typedef LONG *LPLONG; +typedef PVOID LPVOID; +typedef void VOID; +typedef USHORT *PUSHORT; +typedef unsigned long long int ULONGLONG; + +typedef struct _OVERLAPPED { + DWORD Internal; + DWORD InternalHigh; + union { + struct{ + DWORD Offset; + DWORD OffsetHigh; + }; + PVOID Pointer; + }; + HANDLE hEvent; +} OVERLAPPED, *LPOVERLAPPED; + +typedef struct _SECURITY_ATTRIBUTES { + DWORD nLength; + LPVOID lpSecurityDescriptor; + BOOL bInheritHandle; +} SECURITY_ATTRIBUTES , *LPSECURITY_ATTRIBUTES; + +#include <pthread.h> +// Substitute for HANDLE returned by Windows CreateEvent API. +// FT_SetEventNotification expects parameter 3 to be the address +// of one of these structures. +typedef struct _EVENT_HANDLE +{ + pthread_cond_t eCondVar; + pthread_mutex_t eMutex; + int iVar; +} EVENT_HANDLE; + +typedef struct timeval SYSTEMTIME; +typedef struct timeval FILETIME; + +// WaitForSingleObject return values. +#define WAIT_ABANDONED 0x00000080L +#define WAIT_OBJECT_0 0x00000000L +#define WAIT_TIMEOUT 0x00000102L +#define WAIT_FAILED 0xFFFFFFFF +// Special value for WaitForSingleObject dwMilliseconds parameter +#define INFINITE 0xFFFFFFFF // Infinite timeout + +#ifndef TRUE +#define TRUE 1 +#endif +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef CONST +#define CONST const +#endif +// +// Modem Status Flags +// +#define MS_CTS_ON ((DWORD)0x0010) +#define MS_DSR_ON ((DWORD)0x0020) +#define MS_RING_ON ((DWORD)0x0040) +#define MS_RLSD_ON ((DWORD)0x0080) + +// +// Error Flags +// +#define CE_RXOVER 0x0001 // Receive Queue overflow +#define CE_OVERRUN 0x0002 // Receive Overrun Error +#define CE_RXPARITY 0x0004 // Receive Parity Error +#define CE_FRAME 0x0008 // Receive Framing error +#define CE_BREAK 0x0010 // Break Detected +#define CE_TXFULL 0x0100 // TX Queue is full +#define CE_PTO 0x0200 // LPTx Timeout +#define CE_IOE 0x0400 // LPTx I/O Error +#define CE_DNS 0x0800 // LPTx Device not selected +#define CE_OOP 0x1000 // LPTx Out-Of-Paper +#define CE_MODE 0x8000 // Requested mode unsupported + +// +// Events +// +#define EV_RXCHAR 0x0001 // Any Character received +#define EV_RXFLAG 0x0002 // Received certain character +#define EV_TXEMPTY 0x0004 // Transmit Queue Empty +#define EV_CTS 0x0008 // CTS changed state +#define EV_DSR 0x0010 // DSR changed state +#define EV_RLSD 0x0020 // RLSD changed state +#define EV_BREAK 0x0040 // BREAK received +#define EV_ERR 0x0080 // Line status error occurred +#define EV_RING 0x0100 // Ring signal detected +#define EV_PERR 0x0200 // Printer error occured +#define EV_RX80FULL 0x0400 // Receive buffer is 80 percent full +#define EV_EVENT1 0x0800 // Provider specific event 1 +#define EV_EVENT2 0x1000 // Provider specific event 2 + +// +// Escape Functions +// +#define SETXOFF 1 // Simulate XOFF received +#define SETXON 2 // Simulate XON received +#define SETRTS 3 // Set RTS high +#define CLRRTS 4 // Set RTS low +#define SETDTR 5 // Set DTR high +#define CLRDTR 6 // Set DTR low +#define RESETDEV 7 // Reset device if possible +#define SETBREAK 8 // Set the device break line. +#define CLRBREAK 9 // Clear the device break line. + +// +// PURGE function flags. +// +#define PURGE_TXABORT 0x0001 // Kill the pending/current writes to the comm port. +#define PURGE_RXABORT 0x0002 // Kill the pending/current reads to the comm port. +#define PURGE_TXCLEAR 0x0004 // Kill the transmit queue if there. +#define PURGE_RXCLEAR 0x0008 // Kill the typeahead buffer if there. + +#ifndef INVALID_HANDLE_VALUE +#define INVALID_HANDLE_VALUE 0xFFFFFFFF +#endif + +#endif /* __WINDOWS_TYPES__ */ diff --git a/drivers/ftdi_d2xx/third_party/ftd2xx.h b/drivers/ftdi_d2xx/third_party/ftd2xx.h new file mode 100644 index 0000000..895f226 --- /dev/null +++ b/drivers/ftdi_d2xx/third_party/ftd2xx.h @@ -0,0 +1,1446 @@ +/*++ + +Copyright © 2001-2011 Future Technology Devices International Limited + +THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +FTDI DRIVERS MAY BE USED ONLY IN CONJUNCTION WITH PRODUCTS BASED ON FTDI PARTS. + +FTDI DRIVERS MAY BE DISTRIBUTED IN ANY FORM AS LONG AS LICENSE INFORMATION IS NOT MODIFIED. + +IF A CUSTOM VENDOR ID AND/OR PRODUCT ID OR DESCRIPTION STRING ARE USED, IT IS THE +RESPONSIBILITY OF THE PRODUCT MANUFACTURER TO MAINTAIN ANY CHANGES AND SUBSEQUENT WHQL +RE-CERTIFICATION AS A RESULT OF MAKING THESE CHANGES. + + +Module Name: + +ftd2xx.h + +Abstract: + +Native USB device driver for FTDI FT232x, FT245x, FT2232x and FT4232x devices +FTD2XX library definitions + +Environment: + +kernel & user mode + + +--*/ + + +#ifndef FTD2XX_H +#define FTD2XX_H + +#ifdef _WIN32 +// Compiling on Windows +#include <windows.h> + +// The following ifdef block is the standard way of creating macros +// which make exporting from a DLL simpler. All files within this DLL +// are compiled with the FTD2XX_EXPORTS symbol defined on the command line. +// This symbol should not be defined on any project that uses this DLL. +// This way any other project whose source files include this file see +// FTD2XX_API functions as being imported from a DLL, whereas this DLL +// sees symbols defined with this macro as being exported. + +#ifdef FTD2XX_EXPORTS +#define FTD2XX_API __declspec(dllexport) +#elif defined(FTD2XX_STATIC) +// Avoid decorations when linking statically to D2XX. +#define FTD2XX_API +// Static D2XX depends on these Windows libs: +#pragma comment(lib, "setupapi.lib") +#pragma comment(lib, "advapi32.lib") +#pragma comment(lib, "user32.lib") +#else +#define FTD2XX_API __declspec(dllimport) +#endif + +#else // _WIN32 +// Compiling on non-Windows platform. +#include "WinTypes.h" +// No decorations needed. +#define FTD2XX_API + +#endif // _WIN32 + +typedef PVOID FT_HANDLE; +typedef ULONG FT_STATUS; + +// +// Device status +// +enum { + FT_OK, + FT_INVALID_HANDLE, + FT_DEVICE_NOT_FOUND, + FT_DEVICE_NOT_OPENED, + FT_IO_ERROR, + FT_INSUFFICIENT_RESOURCES, + FT_INVALID_PARAMETER, + FT_INVALID_BAUD_RATE, + + FT_DEVICE_NOT_OPENED_FOR_ERASE, + FT_DEVICE_NOT_OPENED_FOR_WRITE, + FT_FAILED_TO_WRITE_DEVICE, + FT_EEPROM_READ_FAILED, + FT_EEPROM_WRITE_FAILED, + FT_EEPROM_ERASE_FAILED, + FT_EEPROM_NOT_PRESENT, + FT_EEPROM_NOT_PROGRAMMED, + FT_INVALID_ARGS, + FT_NOT_SUPPORTED, + FT_OTHER_ERROR, + FT_DEVICE_LIST_NOT_READY, +}; + + +#define FT_SUCCESS(status) ((status) == FT_OK) + +// +// FT_OpenEx Flags +// + +#define FT_OPEN_BY_SERIAL_NUMBER 1 +#define FT_OPEN_BY_DESCRIPTION 2 +#define FT_OPEN_BY_LOCATION 4 + +#define FT_OPEN_MASK (FT_OPEN_BY_SERIAL_NUMBER | \ + FT_OPEN_BY_DESCRIPTION | \ + FT_OPEN_BY_LOCATION) + +// +// FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags +// + +#define FT_LIST_NUMBER_ONLY 0x80000000 +#define FT_LIST_BY_INDEX 0x40000000 +#define FT_LIST_ALL 0x20000000 + +#define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) + +// +// Baud Rates +// + +#define FT_BAUD_300 300 +#define FT_BAUD_600 600 +#define FT_BAUD_1200 1200 +#define FT_BAUD_2400 2400 +#define FT_BAUD_4800 4800 +#define FT_BAUD_9600 9600 +#define FT_BAUD_14400 14400 +#define FT_BAUD_19200 19200 +#define FT_BAUD_38400 38400 +#define FT_BAUD_57600 57600 +#define FT_BAUD_115200 115200 +#define FT_BAUD_230400 230400 +#define FT_BAUD_460800 460800 +#define FT_BAUD_921600 921600 + +// +// Word Lengths +// + +#define FT_BITS_8 (UCHAR) 8 +#define FT_BITS_7 (UCHAR) 7 + +// +// Stop Bits +// + +#define FT_STOP_BITS_1 (UCHAR) 0 +#define FT_STOP_BITS_2 (UCHAR) 2 + +// +// Parity +// + +#define FT_PARITY_NONE (UCHAR) 0 +#define FT_PARITY_ODD (UCHAR) 1 +#define FT_PARITY_EVEN (UCHAR) 2 +#define FT_PARITY_MARK (UCHAR) 3 +#define FT_PARITY_SPACE (UCHAR) 4 + +// +// Flow Control +// + +#define FT_FLOW_NONE 0x0000 +#define FT_FLOW_RTS_CTS 0x0100 +#define FT_FLOW_DTR_DSR 0x0200 +#define FT_FLOW_XON_XOFF 0x0400 + +// +// Purge rx and tx buffers +// +#define FT_PURGE_RX 1 +#define FT_PURGE_TX 2 + +// +// Events +// + +typedef void (*PFT_EVENT_HANDLER)(DWORD,DWORD); + +#define FT_EVENT_RXCHAR 1 +#define FT_EVENT_MODEM_STATUS 2 +#define FT_EVENT_LINE_STATUS 4 + +// +// Timeouts +// + +#define FT_DEFAULT_RX_TIMEOUT 300 +#define FT_DEFAULT_TX_TIMEOUT 300 + +// +// Device types +// + +typedef ULONG FT_DEVICE; + +enum { + FT_DEVICE_BM, + FT_DEVICE_AM, + FT_DEVICE_100AX, + FT_DEVICE_UNKNOWN, + FT_DEVICE_2232C, + FT_DEVICE_232R, + FT_DEVICE_2232H, + FT_DEVICE_4232H, + FT_DEVICE_232H, + FT_DEVICE_X_SERIES, + FT_DEVICE_4222H_0, + FT_DEVICE_4222H_1_2, + FT_DEVICE_4222H_3, + FT_DEVICE_4222_PROG, + FT_DEVICE_900, + FT_DEVICE_930, + FT_DEVICE_UMFTPD3A, +}; + +// +// Bit Modes +// + +#define FT_BITMODE_RESET 0x00 +#define FT_BITMODE_ASYNC_BITBANG 0x01 +#define FT_BITMODE_MPSSE 0x02 +#define FT_BITMODE_SYNC_BITBANG 0x04 +#define FT_BITMODE_MCU_HOST 0x08 +#define FT_BITMODE_FAST_SERIAL 0x10 +#define FT_BITMODE_CBUS_BITBANG 0x20 +#define FT_BITMODE_SYNC_FIFO 0x40 + +// +// FT232R CBUS Options EEPROM values +// + +#define FT_232R_CBUS_TXDEN 0x00 // Tx Data Enable +#define FT_232R_CBUS_PWRON 0x01 // Power On +#define FT_232R_CBUS_RXLED 0x02 // Rx LED +#define FT_232R_CBUS_TXLED 0x03 // Tx LED +#define FT_232R_CBUS_TXRXLED 0x04 // Tx and Rx LED +#define FT_232R_CBUS_SLEEP 0x05 // Sleep +#define FT_232R_CBUS_CLK48 0x06 // 48MHz clock +#define FT_232R_CBUS_CLK24 0x07 // 24MHz clock +#define FT_232R_CBUS_CLK12 0x08 // 12MHz clock +#define FT_232R_CBUS_CLK6 0x09 // 6MHz clock +#define FT_232R_CBUS_IOMODE 0x0A // IO Mode for CBUS bit-bang +#define FT_232R_CBUS_BITBANG_WR 0x0B // Bit-bang write strobe +#define FT_232R_CBUS_BITBANG_RD 0x0C // Bit-bang read strobe + +// +// FT232H CBUS Options EEPROM values +// + +#define FT_232H_CBUS_TRISTATE 0x00 // Tristate +#define FT_232H_CBUS_TXLED 0x01 // Tx LED +#define FT_232H_CBUS_RXLED 0x02 // Rx LED +#define FT_232H_CBUS_TXRXLED 0x03 // Tx and Rx LED +#define FT_232H_CBUS_PWREN 0x04 // Power Enable +#define FT_232H_CBUS_SLEEP 0x05 // Sleep +#define FT_232H_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 +#define FT_232H_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 +#define FT_232H_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang +#define FT_232H_CBUS_TXDEN 0x09 // Tx Data Enable +#define FT_232H_CBUS_CLK30 0x0A // 30MHz clock +#define FT_232H_CBUS_CLK15 0x0B // 15MHz clock +#define FT_232H_CBUS_CLK7_5 0x0C // 7.5MHz clock + +// +// FT X Series CBUS Options EEPROM values +// + +#define FT_X_SERIES_CBUS_TRISTATE 0x00 // Tristate +#define FT_X_SERIES_CBUS_TXLED 0x01 // Tx LED +#define FT_X_SERIES_CBUS_RXLED 0x02 // Rx LED +#define FT_X_SERIES_CBUS_TXRXLED 0x03 // Tx and Rx LED +#define FT_X_SERIES_CBUS_PWREN 0x04 // Power Enable +#define FT_X_SERIES_CBUS_SLEEP 0x05 // Sleep +#define FT_X_SERIES_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 +#define FT_X_SERIES_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 +#define FT_X_SERIES_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang +#define FT_X_SERIES_CBUS_TXDEN 0x09 // Tx Data Enable +#define FT_X_SERIES_CBUS_CLK24 0x0A // 24MHz clock +#define FT_X_SERIES_CBUS_CLK12 0x0B // 12MHz clock +#define FT_X_SERIES_CBUS_CLK6 0x0C // 6MHz clock +#define FT_X_SERIES_CBUS_BCD_CHARGER 0x0D // Battery charger detected +#define FT_X_SERIES_CBUS_BCD_CHARGER_N 0x0E // Battery charger detected inverted +#define FT_X_SERIES_CBUS_I2C_TXE 0x0F // I2C Tx empty +#define FT_X_SERIES_CBUS_I2C_RXF 0x10 // I2C Rx full +#define FT_X_SERIES_CBUS_VBUS_SENSE 0x11 // Detect VBUS +#define FT_X_SERIES_CBUS_BITBANG_WR 0x12 // Bit-bang write strobe +#define FT_X_SERIES_CBUS_BITBANG_RD 0x13 // Bit-bang read strobe +#define FT_X_SERIES_CBUS_TIMESTAMP 0x14 // Toggle output when a USB SOF token is received +#define FT_X_SERIES_CBUS_KEEP_AWAKE 0x15 // + + +// Driver types +#define FT_DRIVER_TYPE_D2XX 0 +#define FT_DRIVER_TYPE_VCP 1 + + + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef FTD2XX_STATIC + FTD2XX_API + FT_STATUS WINAPI FT_Initialise( + void + ); + + FTD2XX_API + void WINAPI FT_Finalise( + void + ); +#endif // FTD2XX_STATIC + + FTD2XX_API + FT_STATUS WINAPI FT_Open( + int deviceNumber, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_OpenEx( + PVOID pArg1, + DWORD Flags, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ListDevices( + PVOID pArg1, + PVOID pArg2, + DWORD Flags + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Close( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Read( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToRead, + LPDWORD lpBytesReturned + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Write( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToWrite, + LPDWORD lpBytesWritten + ); + + FTD2XX_API + FT_STATUS WINAPI FT_IoCtl( + FT_HANDLE ftHandle, + DWORD dwIoControlCode, + LPVOID lpInBuf, + DWORD nInBufSize, + LPVOID lpOutBuf, + DWORD nOutBufSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBaudRate( + FT_HANDLE ftHandle, + ULONG BaudRate + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDivisor( + FT_HANDLE ftHandle, + USHORT Divisor + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDataCharacteristics( + FT_HANDLE ftHandle, + UCHAR WordLength, + UCHAR StopBits, + UCHAR Parity + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetFlowControl( + FT_HANDLE ftHandle, + USHORT FlowControl, + UCHAR XonChar, + UCHAR XoffChar + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetDevice( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetModemStatus( + FT_HANDLE ftHandle, + ULONG *pModemStatus + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetChars( + FT_HANDLE ftHandle, + UCHAR EventChar, + UCHAR EventCharEnabled, + UCHAR ErrorChar, + UCHAR ErrorCharEnabled + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Purge( + FT_HANDLE ftHandle, + ULONG Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetTimeouts( + FT_HANDLE ftHandle, + ULONG ReadTimeout, + ULONG WriteTimeout + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetEventNotification( + FT_HANDLE ftHandle, + DWORD Mask, + PVOID Param + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes, + DWORD *dwTxBytes, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOn( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOff( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetWaitMask( + FT_HANDLE ftHandle, + DWORD Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WaitOnMask( + FT_HANDLE ftHandle, + DWORD *Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetEventStatus( + FT_HANDLE ftHandle, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ReadEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WriteEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + WORD wValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EraseEE( + FT_HANDLE ftHandle + ); + + // + // structure to hold program data for FT_EE_Program, FT_EE_ProgramEx, FT_EE_Read + // and FT_EE_ReadEx functions + // + typedef struct ft_program_data { + + DWORD Signature1; // Header - must be 0x00000000 + DWORD Signature2; // Header - must be 0xffffffff + DWORD Version; // Header - FT_PROGRAM_DATA version + // 0 = original + // 1 = FT2232 extensions + // 2 = FT232R extensions + // 3 = FT2232H extensions + // 4 = FT4232H extensions + // 5 = FT232H extensions + + WORD VendorId; // 0x0403 + WORD ProductId; // 0x6001 + char *Manufacturer; // "FTDI" + char *ManufacturerId; // "FT" + char *Description; // "USB HS Serial Converter" + char *SerialNumber; // "FT000001" if fixed, or NULL + WORD MaxPower; // 0 < MaxPower <= 500 + WORD PnP; // 0 = disabled, 1 = enabled + WORD SelfPowered; // 0 = bus powered, 1 = self powered + WORD RemoteWakeup; // 0 = not capable, 1 = capable + // + // Rev4 (FT232B) extensions + // + UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise + UCHAR IsoIn; // non-zero if in endpoint is isochronous + UCHAR IsoOut; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable; // non-zero if pull down enabled + UCHAR SerNumEnable; // non-zero if serial number to be used + UCHAR USBVersionEnable; // non-zero if chip uses USBVersion + WORD USBVersion; // BCD (0x0200 => USB2) + // + // Rev 5 (FT2232) extensions + // + UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise + UCHAR IsoInA; // non-zero if in endpoint is isochronous + UCHAR IsoInB; // non-zero if in endpoint is isochronous + UCHAR IsoOutA; // non-zero if out endpoint is isochronous + UCHAR IsoOutB; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable5; // non-zero if pull down enabled + UCHAR SerNumEnable5; // non-zero if serial number to be used + UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion + WORD USBVersion5; // BCD (0x0200 => USB2) + UCHAR AIsHighCurrent; // non-zero if interface is high current + UCHAR BIsHighCurrent; // non-zero if interface is high current + UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer; // non-zero if interface is Fast serial + UCHAR AIsVCP; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsVCP; // non-zero if interface is to use VCP drivers + // + // Rev 6 (FT232R) extensions + // + UCHAR UseExtOsc; // Use External Oscillator + UCHAR HighDriveIOs; // High Drive I/Os + UCHAR EndpointSize; // Endpoint size + UCHAR PullDownEnableR; // non-zero if pull down enabled + UCHAR SerNumEnableR; // non-zero if serial number to be used + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR RIsD2XX; // non-zero if using D2XX driver + // + // Rev 7 (FT2232H) Extensions + // + UCHAR PullDownEnable7; // non-zero if pull down enabled + UCHAR SerNumEnable7; // non-zero if serial number to be used + UCHAR ALSlowSlew; // non-zero if AL pins have slow slew + UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR AHSlowSlew; // non-zero if AH pins have slow slew + UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BLSlowSlew; // non-zero if BL pins have slow slew + UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BHSlowSlew; // non-zero if BH pins have slow slew + UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial + UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial + UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // + // Rev 8 (FT4232H) Extensions + // + UCHAR PullDownEnable8; // non-zero if pull down enabled + UCHAR SerNumEnable8; // non-zero if serial number to be used + UCHAR ASlowSlew; // non-zero if A pins have slow slew + UCHAR ASchmittInput; // non-zero if A pins are Schmitt input + UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BSlowSlew; // non-zero if B pins have slow slew + UCHAR BSchmittInput; // non-zero if B pins are Schmitt input + UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR CSlowSlew; // non-zero if C pins have slow slew + UCHAR CSchmittInput; // non-zero if C pins are Schmitt input + UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR DSlowSlew; // non-zero if D pins have slow slew + UCHAR DSchmittInput; // non-zero if D pins are Schmitt input + UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN + UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN + UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN + UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN + UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers + // + // Rev 9 (FT232H) Extensions + // + UCHAR PullDownEnableH; // non-zero if pull down enabled + UCHAR SerNumEnableH; // non-zero if serial number to be used + UCHAR ACSlowSlewH; // non-zero if AC pins have slow slew + UCHAR ACSchmittInputH; // non-zero if AC pins are Schmitt input + UCHAR ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlewH; // non-zero if AD pins have slow slew + UCHAR ADSchmittInputH; // non-zero if AD pins are Schmitt input + UCHAR ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR Cbus0H; // Cbus Mux control + UCHAR Cbus1H; // Cbus Mux control + UCHAR Cbus2H; // Cbus Mux control + UCHAR Cbus3H; // Cbus Mux control + UCHAR Cbus4H; // Cbus Mux control + UCHAR Cbus5H; // Cbus Mux control + UCHAR Cbus6H; // Cbus Mux control + UCHAR Cbus7H; // Cbus Mux control + UCHAR Cbus8H; // Cbus Mux control + UCHAR Cbus9H; // Cbus Mux control + UCHAR IsFifoH; // non-zero if interface is 245 FIFO + UCHAR IsFifoTarH; // non-zero if interface is 245 FIFO CPU target + UCHAR IsFastSerH; // non-zero if interface is Fast serial + UCHAR IsFT1248H; // non-zero if interface is FT1248 + UCHAR FT1248CpolH; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248LsbH; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControlH; // FT1248 flow control enable + UCHAR IsVCPH; // non-zero if interface is to use VCP drivers + UCHAR PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs + + } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Program( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ProgramEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Read( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UASize( + FT_HANDLE ftHandle, + LPDWORD lpdwSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UAWrite( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UARead( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen, + LPDWORD lpdwBytesRead + ); + + + typedef struct ft_eeprom_header { + FT_DEVICE deviceType; // FTxxxx device type to be programmed + // Device descriptor options + WORD VendorId; // 0x0403 + WORD ProductId; // 0x6001 + UCHAR SerNumEnable; // non-zero if serial number to be used + // Config descriptor options + WORD MaxPower; // 0 < MaxPower <= 500 + UCHAR SelfPowered; // 0 = bus powered, 1 = self powered + UCHAR RemoteWakeup; // 0 = not capable, 1 = capable + // Hardware options + UCHAR PullDownEnable; // non-zero if pull down in suspend enabled + } FT_EEPROM_HEADER, *PFT_EEPROM_HEADER; + + + // FT232B EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232b { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + } FT_EEPROM_232B, *PFT_EEPROM_232B; + + + // FT2232 EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_2232 { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR AIsHighCurrent; // non-zero if interface is high current + UCHAR BIsHighCurrent; // non-zero if interface is high current + // Hardware options + UCHAR AIsFifo; // non-zero if interface is 245 FIFO + UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR AIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsFifo; // non-zero if interface is 245 FIFO + UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR BIsFastSer; // non-zero if interface is Fast serial + // Driver option + UCHAR ADriverType; // + UCHAR BDriverType; // + } FT_EEPROM_2232, *PFT_EEPROM_2232; + + + // FT232R EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232r { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR IsHighCurrent; // non-zero if interface is high current + // Hardware options + UCHAR UseExtOsc; // Use External Oscillator + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + // Driver option + UCHAR DriverType; // + } FT_EEPROM_232R, *PFT_EEPROM_232R; + + + // FT2232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_2232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ALSlowSlew; // non-zero if AL pins have slow slew + UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR AHSlowSlew; // non-zero if AH pins have slow slew + UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BLSlowSlew; // non-zero if BL pins have slow slew + UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BHSlowSlew; // non-zero if BH pins have slow slew + UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // Hardware options + UCHAR AIsFifo; // non-zero if interface is 245 FIFO + UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR AIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsFifo; // non-zero if interface is 245 FIFO + UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR BIsFastSer; // non-zero if interface is Fast serial + UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // Driver option + UCHAR ADriverType; // + UCHAR BDriverType; // + } FT_EEPROM_2232H, *PFT_EEPROM_2232H; + + + // FT4232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_4232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ASlowSlew; // non-zero if A pins have slow slew + UCHAR ASchmittInput; // non-zero if A pins are Schmitt input + UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BSlowSlew; // non-zero if B pins have slow slew + UCHAR BSchmittInput; // non-zero if B pins are Schmitt input + UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR CSlowSlew; // non-zero if C pins have slow slew + UCHAR CSchmittInput; // non-zero if C pins are Schmitt input + UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR DSlowSlew; // non-zero if D pins have slow slew + UCHAR DSchmittInput; // non-zero if D pins are Schmitt input + UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // Hardware options + UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN + UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN + UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN + UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN + // Driver option + UCHAR ADriverType; // + UCHAR BDriverType; // + UCHAR CDriverType; // + UCHAR DDriverType; // + } FT_EEPROM_4232H, *PFT_EEPROM_4232H; + + + // FT232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew + UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input + UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew + UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input + UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // CBUS options + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR Cbus5; // Cbus Mux control + UCHAR Cbus6; // Cbus Mux control + UCHAR Cbus7; // Cbus Mux control + UCHAR Cbus8; // Cbus Mux control + UCHAR Cbus9; // Cbus Mux control + // FT1248 options + UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControl; // FT1248 flow control enable + // Hardware options + UCHAR IsFifo; // non-zero if interface is 245 FIFO + UCHAR IsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IsFastSer; // non-zero if interface is Fast serial + UCHAR IsFT1248 ; // non-zero if interface is FT1248 + UCHAR PowerSaveEnable; // + // Driver option + UCHAR DriverType; // + } FT_EEPROM_232H, *PFT_EEPROM_232H; + + + // FT X Series EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_x_series { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew + UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input + UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew + UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input + UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // CBUS options + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR Cbus5; // Cbus Mux control + UCHAR Cbus6; // Cbus Mux control + // UART signal options + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + // Battery Charge Detect options + UCHAR BCDEnable; // Enable Battery Charger Detection + UCHAR BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected + UCHAR BCDDisableSleep; // forces the device never to go into sleep mode + // I2C options + WORD I2CSlaveAddress; // I2C slave device address + DWORD I2CDeviceId; // I2C device ID + UCHAR I2CDisableSchmitt; // Disable I2C Schmitt trigger + // FT1248 options + UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControl; // FT1248 flow control enable + // Hardware options + UCHAR RS485EchoSuppress; // + UCHAR PowerSaveEnable; // + // Driver option + UCHAR DriverType; // + } FT_EEPROM_X_SERIES, *PFT_EEPROM_X_SERIES; + + + FTD2XX_API + FT_STATUS WINAPI FT_EEPROM_Read( + FT_HANDLE ftHandle, + void *eepromData, + DWORD eepromDataSize, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_EEPROM_Program( + FT_HANDLE ftHandle, + void *eepromData, + DWORD eepromDataSize, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_SetLatencyTimer( + FT_HANDLE ftHandle, + UCHAR ucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLatencyTimer( + FT_HANDLE ftHandle, + PUCHAR pucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBitMode( + FT_HANDLE ftHandle, + UCHAR ucMask, + UCHAR ucEnable + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetBitMode( + FT_HANDLE ftHandle, + PUCHAR pucMode + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetUSBParameters( + FT_HANDLE ftHandle, + ULONG ulInTransferSize, + ULONG ulOutTransferSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDeadmanTimeout( + FT_HANDLE ftHandle, + ULONG ulDeadmanTimeout + ); + +#ifndef _WIN32 + // Extra functions for non-Windows platforms to compensate + // for lack of .INF file to specify Vendor and Product IDs. + + FTD2XX_API + FT_STATUS FT_SetVIDPID( + DWORD dwVID, + DWORD dwPID + ); + + FTD2XX_API + FT_STATUS FT_GetVIDPID( + DWORD * pdwVID, + DWORD * pdwPID + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceLocId( + FT_HANDLE ftHandle, + LPDWORD lpdwLocId + ); +#endif // _WIN32 + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfo( + FT_HANDLE ftHandle, + FT_DEVICE *lpftDevice, + LPDWORD lpdwID, + PCHAR SerialNumber, + PCHAR Description, + LPVOID Dummy + ); + + FTD2XX_API + FT_STATUS WINAPI FT_StopInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_RestartInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetResetPipeRetryCount( + FT_HANDLE ftHandle, + DWORD dwCount + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetPort( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_CyclePort( + FT_HANDLE ftHandle + ); + + + // + // Win32-type functions + // + + FTD2XX_API + FT_HANDLE WINAPI FT_W32_CreateFile( + LPCTSTR lpszName, + DWORD dwAccess, + DWORD dwShareMode, + LPSECURITY_ATTRIBUTES lpSecurityAttributes, + DWORD dwCreate, + DWORD dwAttrsAndFlags, + HANDLE hTemplate + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CloseHandle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ReadFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WriteFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesWritten, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + DWORD WINAPI FT_W32_GetLastError( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetOverlappedResult( + FT_HANDLE ftHandle, + LPOVERLAPPED lpOverlapped, + LPDWORD lpdwBytesTransferred, + BOOL bWait + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CancelIo( + FT_HANDLE ftHandle + ); + + + // + // Win32 COMM API type functions + // + typedef struct _FTCOMSTAT { + DWORD fCtsHold : 1; + DWORD fDsrHold : 1; + DWORD fRlsdHold : 1; + DWORD fXoffHold : 1; + DWORD fXoffSent : 1; + DWORD fEof : 1; + DWORD fTxim : 1; + DWORD fReserved : 25; + DWORD cbInQue; + DWORD cbOutQue; + } FTCOMSTAT, *LPFTCOMSTAT; + + typedef struct _FTDCB { + DWORD DCBlength; /* sizeof(FTDCB) */ + DWORD BaudRate; /* Baudrate at which running */ + DWORD fBinary: 1; /* Binary Mode (skip EOF check) */ + DWORD fParity: 1; /* Enable parity checking */ + DWORD fOutxCtsFlow:1; /* CTS handshaking on output */ + DWORD fOutxDsrFlow:1; /* DSR handshaking on output */ + DWORD fDtrControl:2; /* DTR Flow control */ + DWORD fDsrSensitivity:1; /* DSR Sensitivity */ + DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */ + DWORD fOutX: 1; /* Enable output X-ON/X-OFF */ + DWORD fInX: 1; /* Enable input X-ON/X-OFF */ + DWORD fErrorChar: 1; /* Enable Err Replacement */ + DWORD fNull: 1; /* Enable Null stripping */ + DWORD fRtsControl:2; /* Rts Flow control */ + DWORD fAbortOnError:1; /* Abort all reads and writes on Error */ + DWORD fDummy2:17; /* Reserved */ + WORD wReserved; /* Not currently used */ + WORD XonLim; /* Transmit X-ON threshold */ + WORD XoffLim; /* Transmit X-OFF threshold */ + BYTE ByteSize; /* Number of bits/byte, 4-8 */ + BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ + BYTE StopBits; /* FT_STOP_BITS_1 or FT_STOP_BITS_2 */ + char XonChar; /* Tx and Rx X-ON character */ + char XoffChar; /* Tx and Rx X-OFF character */ + char ErrorChar; /* Error replacement char */ + char EofChar; /* End of Input character */ + char EvtChar; /* Received Event character */ + WORD wReserved1; /* Fill for now. */ + } FTDCB, *LPFTDCB; + + typedef struct _FTTIMEOUTS { + DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ + DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ + DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ + } FTTIMEOUTS,*LPFTTIMEOUTS; + + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommError( + FT_HANDLE ftHandle, + LPDWORD lpdwErrors, + LPFTCOMSTAT lpftComstat + ); + + FTD2XX_API + BOOL WINAPI FT_W32_EscapeCommFunction( + FT_HANDLE ftHandle, + DWORD dwFunc + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommModemStatus( + FT_HANDLE ftHandle, + LPDWORD lpdwModemStatus + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_PurgeComm( + FT_HANDLE ftHandle, + DWORD dwMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommMask( + FT_HANDLE ftHandle, + ULONG ulEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommMask( + FT_HANDLE ftHandle, + LPDWORD lpdwEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetupComm( + FT_HANDLE ftHandle, + DWORD dwReadBufferSize, + DWORD dwWriteBufferSize + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WaitCommEvent( + FT_HANDLE ftHandle, + PULONG pulEvent, + LPOVERLAPPED lpOverlapped + ); + + + // + // Device information + // + + typedef struct _ft_device_list_info_node { + ULONG Flags; + ULONG Type; + ULONG ID; + DWORD LocId; + char SerialNumber[16]; + char Description[64]; + FT_HANDLE ftHandle; + } FT_DEVICE_LIST_INFO_NODE; + + // Device information flags + enum { + FT_FLAGS_OPENED = 1, + FT_FLAGS_HISPEED = 2 + }; + + + FTD2XX_API + FT_STATUS WINAPI FT_CreateDeviceInfoList( + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoList( + FT_DEVICE_LIST_INFO_NODE *pDest, + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoDetail( + DWORD dwIndex, + LPDWORD lpdwFlags, + LPDWORD lpdwType, + LPDWORD lpdwID, + LPDWORD lpdwLocId, + LPVOID lpSerialNumber, + LPVOID lpDescription, + FT_HANDLE *pftHandle + ); + + + // + // Version information + // + + FTD2XX_API + FT_STATUS WINAPI FT_GetDriverVersion( + FT_HANDLE ftHandle, + LPDWORD lpdwVersion + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLibraryVersion( + LPDWORD lpdwVersion + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_Rescan( + void + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Reload( + WORD wVid, + WORD wPid + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetComPortNumber( + FT_HANDLE ftHandle, + LPLONG lpdwComPortNumber + ); + + + // + // FT232H additional EEPROM functions + // + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + PUCHAR pucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_WriteConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + UCHAR ucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadECC( + FT_HANDLE ftHandle, + UCHAR ucOption, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatusEx( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ComPortIdle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ComPortCancelIdle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdGet( + FT_HANDLE ftHandle, + UCHAR Request, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdSet( + FT_HANDLE ftHandle, + UCHAR Request, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdGetEx( + FT_HANDLE ftHandle, + USHORT wValue, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdSetEx( + FT_HANDLE ftHandle, + USHORT wValue, + UCHAR *Buf, + USHORT Len + ); + +#ifdef __cplusplus +} +#endif + + +#endif /* FTD2XX_H */ + diff --git a/drivers/ftdi_d2xx/third_party/osx/libftd2xx.a b/drivers/ftdi_d2xx/third_party/osx/libftd2xx.a Binary files differnew file mode 100644 index 0000000..bcee161 --- /dev/null +++ b/drivers/ftdi_d2xx/third_party/osx/libftd2xx.a diff --git a/drivers/ftdi_d2xx/third_party/win_32bit/ftd2xx.lib b/drivers/ftdi_d2xx/third_party/win_32bit/ftd2xx.lib Binary files differnew file mode 100644 index 0000000..afc8f19 --- /dev/null +++ b/drivers/ftdi_d2xx/third_party/win_32bit/ftd2xx.lib diff --git a/pcserialport.c b/drivers/serial/pcserialport.c index 8cbe281..2a16b82 100644 --- a/pcserialport.c +++ b/drivers/serial/pcserialport.c @@ -140,7 +140,8 @@ smint32 serialPortOpen(const char * port_device_name, smint32 baudrate_bps) new_port_settings.c_lflag = 0; new_port_settings.c_cc[VMIN] = 0; /* non blocking mode */ new_port_settings.c_cc[VTIME] = readTimeoutMs/100; /* timeout 100 ms steps */ - + if(new_port_settings.c_cc[VTIME]<1)//don't allow value 0ms + new_port_settings.c_cc[VTIME]=1; #if defined(_BSD_SOURCE) cfsetspeed(&new_port_settings, baudrateEnumValue); #else diff --git a/pcserialport.h b/drivers/serial/pcserialport.h index 832786d..832786d 100644 --- a/pcserialport.h +++ b/drivers/serial/pcserialport.h diff --git a/tcpclient.c b/drivers/tcpip/tcpclient.c index 09d2426..2b0620c 100644 --- a/tcpclient.c +++ b/drivers/tcpip/tcpclient.c @@ -1,6 +1,8 @@ #include "simplemotion_private.h" #include "tcpclient.h" #include <stdio.h> +#include <ctype.h> +#include <string.h> #if defined(_WIN32) #if defined(CM_NONE) @@ -187,3 +189,116 @@ void CloseTCPport(int sockfd) WSACleanup(); #endif } + + +//accepted TCP/IP address format is nnn.nnn.nnn.nnn:pppp where n is IP address numbers and p is port number +int validateIpAddress(const char *s, const char **pip_end, + const char **pport_start) +{ + int octets = 0; + int ch = 0, prev = 0; + int len = 0; + const char *ip_end = NULL; + const char *port_start = NULL; + + while (*s) + { + ch = *s; + + if (isdigit(ch)) + { + ++len; + // Octet len must be 1-3 digits + if (len > 3) + { + return -1; + } + } + else if (ch == '.' && isdigit(prev)) + { + ++octets; + len = 0; + // No more than 4 octets please + if (octets > 4) + { + return -1; + } + } + else if (ch == ':' && isdigit(prev)) + { + ++octets; + // We want exactly 4 octets at this point + if (octets != 4) + { + return -1; + } + ip_end = s; + ++s; + port_start = s; + while (isdigit((ch = *s))) + ++s; + // After port we want the end of the string + if (ch != '\0') + return -1; + // This will skip over the ++s below + continue; + } + else + { + return -1; + } + + prev = ch; + ++s; + } + + // We reached the end of the string and did not encounter the port + if (*s == '\0' && ip_end == NULL) + { + ++octets; + ip_end = s; + } + + // Check that there are exactly 4 octets + if (octets != 4) + return -1; + + if (pip_end) + *pip_end = ip_end; + + if (pport_start) + *pport_start = port_start; + + return 0; +} + +int parseIpAddress(const char *s, char *ip, size_t ipsize, short *port) +{ + const char *ip_end, *port_start; + + //ip_end and port_start are pointers to memory area of s, not offsets or indexes to s + if (validateIpAddress(s, &ip_end, &port_start) == -1) + return -1; + + // If ip=NULL, we just report that the parsing was ok + if (!ip) + return 0; + + if (ipsize < (size_t)(ip_end - s + 1)) + return -1; + + memcpy(ip, s, ip_end - s); + ip[ip_end - s] = '\0'; + + if (port_start) + { + *port = 0; + while (*port_start) + { + *port = *port * 10 + (*port_start - '0'); + ++port_start; + } + } + + return 0; +} diff --git a/tcpclient.h b/drivers/tcpip/tcpclient.h index ea02e42..5ed075a 100644 --- a/tcpclient.h +++ b/drivers/tcpip/tcpclient.h @@ -13,6 +13,12 @@ int SendTCPBuf(int, unsigned char *, int); void CloseTCPport(int); +//accepted TCP/IP address format is nnn.nnn.nnn.nnn:pppp where n is IP address numbers and p is port number +int validateIpAddress(const char *s, const char **pip_end, + const char **pport_start); +int parseIpAddress(const char *s, char *ip, size_t ipsize, short *port); + + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/simplemotion.c b/simplemotion.c index 58627cb..1211d63 100644 --- a/simplemotion.c +++ b/simplemotion.c @@ -73,17 +73,18 @@ void smDebug( smbus handle, smVerbosityLevel verbositylevel, char *format, ...) va_list fmtargs;
char buffer[1024];
-
- //check if bus handle is valid & opened
- if(smIsHandleOpen(handle)==smfalse) return;
-
if(smDebugOut!=NULL && verbositylevel <= smDebugThreshold )
{
va_start(fmtargs,format);
vsnprintf(buffer,sizeof(buffer)-1,format,fmtargs);
va_end(fmtargs);
if(handle>=0)
- fprintf(smDebugOut,"%s: %s",smBus[handle].busDeviceName, buffer);
+ {
+ if(smIsHandleOpen(handle)==smtrue)
+ {
+ fprintf(smDebugOut,"%s: %s",smBus[handle].busDeviceName, buffer);
+ }
+ }
else
fprintf(smDebugOut,"SMLib: %s",buffer);//no handle given
}
@@ -150,7 +151,7 @@ smuint8 calcCRC8Buf( smuint8 *buf, int len, int crcinit ) SM_STATUS smSetTimeout( smuint16 millsecs )
{
- if(millsecs<=5000)
+ if(millsecs<=5000 && millsecs>=1)
{
readTimeoutMs=millsecs;
return SM_OK;
@@ -953,3 +954,36 @@ SM_STATUS resetCumulativeStatus( const smbus handle ) return SM_OK;
}
+
+/** Return number of bus devices found. details of each device may be consequently fetched by smGetBusDeviceDetails() */
+smint smGetNumberOfDetectedBuses()
+{
+ return smBDGetNumberOfDetectedBuses();
+}
+
+/** Fetch information of detected bus nodes at certain index. Example:
+
+ smint num=smGetNumberOfDetectedBuses();
+ for(int i=0;i<num;i++)
+ {
+ SM_BUS_DEVICE_INFO info;
+ SM_STATUS s=smGetBusDeviceDetails(i,&info);
+ if(s==SM_OK)
+ {
+ ...do something with info...
+ }
+ else
+ {
+ ...report error...
+ }
+ }
+*/
+LIB SM_STATUS smGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info )
+{
+ smbool ok=smBDGetBusDeviceDetails(index,info);
+
+ if(ok==smtrue)
+ return SM_OK;
+ else
+ return SM_ERR_NODEVICE;
+}
diff --git a/simplemotion.h b/simplemotion.h index c189af8..38d97e8 100644 --- a/simplemotion.h +++ b/simplemotion.h @@ -46,10 +46,18 @@ typedef int32_t smint32; typedef int16_t smint16;
typedef int8_t smint8;
typedef int8_t smbool;
+typedef smint32 smint;
#define smtrue 1
#define smfalse 0
typedef int SM_STATUS;
typedef smuint8 smaddr;
+typedef struct
+{
+ smbool is_simplemotion_device;//smtrue if usable in SM lib
+ char device_name[64];//name that should be fed to smOpenBus
+ char description[128];//such as "SimpleMotion USB"
+} SM_BUS_DEVICE_INFO;
+
//comment out to disable, gives smaller & faster code
#define ENABLE_DEBUG_PRINTS
@@ -91,7 +99,10 @@ LIB smbus smOpenBus( const char * devicename ); LIB void smSetBaudrate( unsigned long pbs );
/** Set timeout of how long to wait reply packet from bus. Must be set before smOpenBus and cannot be changed afterwards
- * max value 5000ms. In unix this is rounded to 100ms (rounding downwards), so 99 or less gives 0ms timeout.
+ * max value 5000ms. Range may depend on underyling OS / drivers. If supplied argument is lower than minimum supported by drivers,
+ * then driver minimum is used without notice (return SM_OK).
+ *
+ * In unix PC serial port minimum is 100ms, on Windows serial port recommended minimum is 30ms and with FTDI driver 10ms. On TCP/IP: TBD.
*
*This is the only function that returns SM_STATUS which doesn't accumulate status bits to be read with getCumulativeStatus because it has no bus handle
*/
@@ -147,6 +158,28 @@ LIB SM_STATUS smGetBufferClock( const smbus handle, const smaddr targetaddr, smu *content are application specific and defined . */
LIB SM_STATUS smFastUpdateCycle( smbus handle, smuint8 nodeAddress, smuint16 write1, smuint16 write2, smuint16 *read1, smuint16 *read2);
+/** Return number of bus devices found. details of each device may be consequently fetched by smGetBusDeviceDetails() */
+LIB smint smGetNumberOfDetectedBuses();
+
+/** Fetch information of detected bus nodes at certain index. Example:
+
+ smint num=smGetNumberOfDetectedBuses();
+ for(int i=0;i<num;i++)
+ {
+ SM_BUS_DEVICE_INFO info;
+ SM_STATUS s=smGetBusDeviceDetails(i,&info);
+ if(s==SM_OK)
+ {
+ ...do something with info...
+ }
+ else
+ {
+ ...report error...
+ }
+ }
+*/
+LIB SM_STATUS smGetBusDeviceDetails( smint index, SM_BUS_DEVICE_INFO *info );
+
#ifdef __cplusplus
}
#endif
diff --git a/simplemotion_private.h b/simplemotion_private.h index 67128b7..676fe9f 100644 --- a/simplemotion_private.h +++ b/simplemotion_private.h @@ -9,7 +9,7 @@ #include "busdevice.h"
#include <stdio.h>
-#define SM_VERSION 0x020100
+#define SM_VERSION 0x020500
//max number of simultaneously opened buses. change this and recompiple SMlib if
//necessary (to increase channels or reduce to save memory)
#define SM_MAX_BUSES 10
@@ -58,7 +58,7 @@ typedef struct { * ID=3 reserved
*/
long param :30; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand32;
typedef struct {
@@ -68,7 +68,7 @@ typedef struct { * ID=3 reserved
*/
long param :14; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand16;
typedef struct {
@@ -78,7 +78,7 @@ typedef struct { * ID=3 reserved
*/
long param :22; //MSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand24;
//SM payload command return data structure
@@ -89,7 +89,7 @@ typedef struct { * ID=3 reserved
*/
long retData: 30; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet32;
//SM payload command return data structure
@@ -100,7 +100,7 @@ typedef struct { * ID=3 reserved
*/
long retData: 22; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet24;
//SM payload command return data structure
@@ -111,7 +111,7 @@ typedef struct { * ID=3 reserved
*/
long retData: 14; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet16;
//SM payload command return data structure
@@ -122,7 +122,7 @@ typedef struct { * ID=3 reserved
*/
long retData: 6; //LSB 30 bits
- long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
+ unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet8;
/*Workaround to have packed structs that compile on GCC and MSVC*/
|
