-
Notifications
You must be signed in to change notification settings - Fork 0
/
libftdimut.c
76 lines (64 loc) · 1.99 KB
/
libftdimut.c
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
#include "libftdimut.h"
FT_STATUS ftdimut_setup() {
FT_STATUS ftStatus;
unsigned char timer;
ftStatus = FT_SetVIDPID(USB_VID, USB_PID);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_ResetDevice(ftHandle);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_SetBaudRate(ftHandle, 15625);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_SetTimeouts(ftHandle, 1000, 1000);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_GetLatencyTimer(ftHandle, &timer);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_SetLatencyTimer(ftHandle, 1);
if(ftStatus != FT_OK) return ftStatus;
return FT_OK;
}
FT_STATUS ftdimut_init() {
FT_STATUS ftStatus;
unsigned char buf[4];
unsigned int bytesRead = 0;
if(ftdimut_getData(0x17) != 0) {
return FT_OK;
}
printf("Sending 0x00 at 5 baud\n");
printf("Break on......\n");
ftStatus = FT_SetBreakOn(ftHandle);
if(ftStatus != FT_OK) return ftStatus;
usleep(1800 * 1000);
printf("Break off......\n");
ftStatus = FT_SetBreakOff(ftHandle);
if(ftStatus != FT_OK) return ftStatus;
ftStatus = FT_Read(ftHandle, buf, 4, &bytesRead);
if(ftStatus != FT_OK) return ftStatus;
if(bytesRead == 4) {
return FT_OK;
}
return FT_OTHER_ERROR;
}
FT_STATUS ftdimut_close() {
return FT_Close(ftHandle);
}
unsigned char ftdimut_getData(unsigned char request) {
unsigned int bytesRead, bytesWrote;
unsigned char buf[2];
FT_Write(ftHandle, &request, 1, &bytesWrote);
if(bytesWrote != 1) {
return 0;
}
FT_Read(ftHandle, buf, 2, &bytesRead);
if(bytesRead != 2) {
return 0;
}
return buf[1];
}