aboutsummaryrefslogtreecommitdiff
path: root/busdevice.c
diff options
context:
space:
mode:
authorTero Kontkanen <tero.k@granitedevices.com>2017-08-23 14:44:10 +0300
committerTero Kontkanen <tero.k@granitedevices.com>2017-08-23 14:44:10 +0300
commit98a89371108bbcdedb201caf7be8b82a323e60cc (patch)
tree915a5596c632256ab77d5dc42236644307ea3b66 /busdevice.c
parent5d21340928cd46b3cb97cae0b9c707a71ef9ced0 (diff)
parent4176d9358a526ff207bb5f4d127f4143a632d748 (diff)
downloadSimpleMotionV2-98a89371108bbcdedb201caf7be8b82a323e60cc.tar.gz
SimpleMotionV2-98a89371108bbcdedb201caf7be8b82a323e60cc.zip
Merge branch 'feature/port_driver_ftdi_d2xx' into develop
Diffstat (limited to 'busdevice.c')
-rw-r--r--busdevice.c208
1 files changed, 79 insertions, 129 deletions
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;
+
+}