From 6ef9e8c44e29d270c64c22b73ae00508966b4892 Mon Sep 17 00:00:00 2001 From: Tero Kontkanen Date: Sat, 19 Aug 2017 17:05:51 +0300 Subject: Add functions to list compatible bus device (ftdi d2xx only) --- busdevice.c | 22 +++++++++++++++ busdevice.h | 8 ++++++ drivers/ftdi_d2xx/sm_d2xx.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- drivers/ftdi_d2xx/sm_d2xx.h | 3 +++ simplemotion.c | 33 +++++++++++++++++++++++ simplemotion.h | 30 +++++++++++++++++++++ 6 files changed, 157 insertions(+), 4 deletions(-) diff --git a/busdevice.c b/busdevice.c index eff5ccc..85a5b0e 100644 --- a/busdevice.c +++ b/busdevice.c @@ -373,3 +373,25 @@ smbool smBDRead( const smbusdevicehandle handle, smuint8 *byte ) 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/drivers/ftdi_d2xx/sm_d2xx.c b/drivers/ftdi_d2xx/sm_d2xx.c index 20c8405..db180de 100644 --- a/drivers/ftdi_d2xx/sm_d2xx.c +++ b/drivers/ftdi_d2xx/sm_d2xx.c @@ -11,13 +11,11 @@ #include "simplemotion_private.h" //needed for timeout variable #include "drivers/ftdi_d2xx/ftd2xx.h" #include +#include smbool handles_initialized=smfalse; FT_HANDLE handles[MAX_OPEN_PORTS];//FT_HANDLE type is just a pointer -#if defined(__unix__) || defined(__APPLE__) - -#else static int stringToNumber( const char *str, smbool *ok ) { @@ -180,4 +178,63 @@ void d2xxPortClose(smint32 serialport_handle) } -#endif//windows + +//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 index f8e8a1b..289ae8a 100644 --- a/drivers/ftdi_d2xx/sm_d2xx.h +++ b/drivers/ftdi_d2xx/sm_d2xx.h @@ -27,6 +27,9 @@ 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 } diff --git a/simplemotion.c b/simplemotion.c index 30220cf..45c7697 100644 --- a/simplemotion.c +++ b/simplemotion.c @@ -954,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