aboutsummaryrefslogtreecommitdiff
path: root/drivers/ftdi_d2xx/sm_d2xx.c
blob: 5b6a578b8e43f1404c960ea8fda1949047ed4bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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)
            {
                //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_SetTimeouts(h,readTimeoutMs,readTimeoutMs)!=FT_OK)
                {
                    smDebug( -1, Low, "FTDI port error: failed to set timeout\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;
}