-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathlibftdi_serial.cpp
More file actions
356 lines (314 loc) · 9.32 KB
/
Copy pathlibftdi_serial.cpp
File metadata and controls
356 lines (314 loc) · 9.32 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "libftdi_serial.h"
#include "serial.h"
#ifdef USE_LIBFTDI
#include <chrono>
#include <ftdi.h>
#include "board.h"
#if defined(__ANDROID__)
static void android_ensure_libusb_init ();
#endif
LibFTDISerial::LibFTDISerial (const char *description, Board *board)
: description (description), port_open (false), lib_init (false), board (board)
{
#if defined(__ANDROID__)
android_ensure_libusb_init ();
#endif
// setup libftdi
last_result = ftdi_init (&ftdi);
if (last_result == 0)
{
lib_init = true;
}
else
{
log_error ("LibFTDISerial");
}
}
LibFTDISerial::~LibFTDISerial ()
{
if (port_open)
{
last_result = ftdi_usb_close (&ftdi);
}
if (lib_init)
{
ftdi_deinit (&ftdi);
}
}
void LibFTDISerial::log_error (const char *action, const char *message)
{
if (board != nullptr)
{
if (message == nullptr)
{
message = ftdi_get_error_string (&ftdi);
}
board->safe_logger (
spdlog::level::err, "libftdi {}: {} -> {}", description, action, message);
}
}
int LibFTDISerial::open_serial_port ()
{
if (!lib_init)
{
// failed to init libftdi; at least check the port name
if (description.size () < 4)
{
return SerialExitCodes::PORT_NAME_ERROR;
}
if (description[0] == '/' || description[1] != ':')
{
return SerialExitCodes::PORT_NAME_ERROR;
}
return SerialExitCodes::OPEN_PORT_ERROR;
}
// https://www.intra2net.com/en/developer/libftdi/documentation/ftdi_8c.html#aae805b82251a61dae46b98345cd84d5c
last_result = ftdi_usb_open_string (&ftdi, description.c_str ());
if (last_result == 0)
{
port_open = true;
return (int)SerialExitCodes::OK;
}
log_error ("open_serial_port ()");
switch (last_result)
{
case -10:
return (int)SerialExitCodes::CLOSE_ERROR;
case -11:
return (int)SerialExitCodes::PORT_NAME_ERROR;
default:
return (int)SerialExitCodes::OPEN_PORT_ERROR;
}
}
bool LibFTDISerial::is_port_open ()
{
return port_open;
}
int LibFTDISerial::set_serial_port_settings (int ms_timeout, bool timeout_only)
{
if (!timeout_only)
{
last_result = ftdi_set_line_property (&ftdi, BITS_8, STOP_BIT_1, NONE);
if (last_result != 0)
{
log_error ("set_serial_port_settings");
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}
last_result = set_custom_baudrate (115200);
if (last_result != (int)SerialExitCodes::OK)
{
return last_result;
}
last_result = ftdi_setdtr_rts (&ftdi, 1, 1);
last_result |= ftdi_setflowctrl (&ftdi, SIO_DISABLE_FLOW_CTRL);
if (last_result != 0)
{
// -1 setting failed, -2 usb device unavailable
log_error ("set_serial_port_settings");
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}
}
ftdi.usb_read_timeout = ms_timeout;
return (int)SerialExitCodes::OK;
}
int LibFTDISerial::set_custom_baudrate (int baudrate)
{
last_result = ftdi_set_baudrate (&ftdi, baudrate);
switch (last_result)
{
case 0:
return (int)SerialExitCodes::OK;
case -3: // usb device unavailable
log_error ("set_custom_baudrate");
return (int)SerialExitCodes::OPEN_PORT_ERROR;
default: // -1 invalid baudrate, -2 setting baudrate failed
log_error ("set_custom_baudrate");
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}
}
int LibFTDISerial::set_custom_latency (int latency)
{
log_error ("libftdi", "set_custom_latency is not supported for libftdi serial");
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}
int LibFTDISerial::flush_buffer ()
{
#if FTDI_MAJOR_VERSION >= 2 || (FTDI_MAJOR_VERSION == 1 && FTDI_MINOR_VERSIOM >= 5)
// correct tcflush was added in libftdi 1.5
last_result = ftdi_tcioflush (&ftdi);
switch (last_result)
{
case 0:
return (int)SerialExitCodes::OK;
case -3: // usb device unavailable
log_error ("flush_buffer ()");
return (int)SerialExitCodes::OPEN_PORT_ERROR;
default: // -1,-2 chip failed to purge a buffer
log_error ("flush_buffer ()");
}
#else
log_error ("flush_buffer ()", "libftdi version <=1.4, tcflush unimplemented");
#endif
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}
int LibFTDISerial::read_from_serial_port (void *bytes_to_read, int size)
{
// the ftdi will send us data after its latency (max 255ms, default
// 16ms) times out, even if its buffer is empty, despite the usb
// timeout. libftdi does not enforce waiting for the usb timeout if
// the chip responds, even if the chip responds with an empty buffer.
// so, the read is repeated until the timeout is reached.
// this latency behavior is documented in
// http://www.ftdichip.com/Support/Documents/AppNotes/AN232B-04_DataLatencyFlow.pdf
auto deadline =
std::chrono::steady_clock::now () + std::chrono::milliseconds (ftdi.usb_read_timeout);
last_result = 0;
while (last_result == 0 && size > 0 && std::chrono::steady_clock::now () < deadline)
{
last_result = ftdi_read_data (&ftdi, static_cast<unsigned char *> (bytes_to_read), size);
// TODO: negative values are libusb error codes, -666 means usb device unavailable
if (last_result < 0)
{
log_error ("read_from_serial_port");
}
}
return last_result;
}
int LibFTDISerial::send_to_serial_port (const void *message, int length)
{
last_result = ftdi_write_data (&ftdi, static_cast<unsigned const char *> (message), length);
// TODO: negative values are libusb error codes, -666 means usb device unavailable
if (last_result < 0)
{
log_error ("send_to_serial_port");
}
return last_result;
}
int LibFTDISerial::close_serial_port ()
{
last_result = ftdi_usb_close (&ftdi);
if (last_result == 0)
{
port_open = false;
return (int)SerialExitCodes::OK;
}
else
{
log_error ("close_serial_port ()");
return (int)SerialExitCodes::CLOSE_ERROR;
}
}
const char *LibFTDISerial::get_port_name ()
{
return description.c_str ();
}
#if defined(__ANDROID__)
//////
// on android we can pass the jnienv pointer to libusb
//////
extern "C"
{
// forward declarations from libusb.h
struct libusb_version
{
const uint16_t major, minor, micro, nano;
};
enum libusb_option
{
LIBUSB_OPTION_WEAK_AUTHORITY = 2,
LIBUSB_OPTION_ANDROID_JNIENV = 3
};
const struct libusb_version *libusb_get_version ();
int libusb_set_option (struct libusb_context *ctx, enum libusb_option option, ...);
}
void android_ensure_libusb_init ()
{
// constructed on first call
class Init
{
public:
Init ()
{
libusb_version const &ver_obj = *libusb_get_version ();
uint64_t usb_version = 0;
for (uint64_t ver_part : {ver_obj.major, ver_obj.minor, ver_obj.micro, ver_obj.nano})
{
usb_version = (usb_version << 16) | ver_part;
}
// libusb_set_option was officially introduced in 1.0.22
if (usb_version >= 0x0001000000160000)
{
jnienv_set = false;
// on android, this disables device scan during usb_init, which lets it succeed
libusb_set_option (nullptr, LIBUSB_OPTION_WEAK_AUTHORITY, nullptr);
}
else
{
// libusb is too old to pass options in
jnienv_set = true;
}
}
bool jnienv_set;
} static init;
if (!init.jnienv_set && Board::java_jnienv != nullptr)
{
init.jnienv_set = true;
// pass jnienv pointer to libusb
int r =
libusb_set_option (nullptr, LIBUSB_OPTION_ANDROID_JNIENV, Board::java_jnienv, nullptr);
if (r == 0)
{
// disable weak authority
libusb_set_option (nullptr, LIBUSB_OPTION_WEAK_AUTHORITY, -1, nullptr);
}
}
}
#endif // defined(__ANDROID__)
#else
LibFTDISerial::LibFTDISerial (const char *description, Board *board)
{
}
LibFTDISerial::~LibFTDISerial ()
{
}
int LibFTDISerial::open_serial_port ()
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
bool LibFTDISerial::is_port_open ()
{
return false;
}
int LibFTDISerial::set_serial_port_settings (int ms_timeout, bool timeout_only)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::set_custom_baudrate (int baudrate)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::set_custom_latency (int latency)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::flush_buffer ()
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::read_from_serial_port (void *bytes_to_read, int size)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::send_to_serial_port (const void *message, int length)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
int LibFTDISerial::close_serial_port ()
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}
const char *LibFTDISerial::get_port_name ()
{
return "";
}
#endif