-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft245-usb-relay.c
247 lines (209 loc) · 5.11 KB
/
ft245-usb-relay.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
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
/**
******************************************************************************
* FT245 chip control fo Quimat 5V USB relay module (and similar products)
******************************************************************************
*
* COPYRIGHT(c) 2017 Davide Fogliano <[email protected]>
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
******************************************************************************
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <ftdi.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
bool debug_enable; // Debug enabled (-v) DEFUALT = false
void debug(const char *fmt, ...)
{
va_list args;
if(debug_enable)
{
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
}
bool read_hex_byte(const char *str, unsigned char *val, char option)
{
bool valid = false;
int tmp = -1;
if(sscanf(str,"%02x",&tmp) == 1)
{
if(tmp>=0 && tmp<256)
{
*val = (unsigned char)tmp;
valid = true;
}
}
if(!valid)
{
fprintf(stderr, "'-%c' value '%s' not valid\n", option, str);
fprintf(stderr, "'-%c' provide valid byte [00-FF]\n", option);
}
return valid;
}
int main(int argc, char **argv)
{
/* Ftdi context */
struct ftdi_context *ftdi;
/* Command line options */
bool print_only; // Print status only (no '-o' or '-p' option)
int port; // Selected ouput port (-p) [0-7]
bool state; // State of the ouput (-e) [0-1] DEFUALT = 1
unsigned char output; // Ouput value (-o)
unsigned char mask; // Ouput mask (-m) DEFUALT = 0xFF
bool single_port; // single(-p) or multiport (-o)
int ret;
unsigned char data = 0;
int i;
int tmp;
int chipid;
/*initialize defualt values */
print_only = false;
debug_enable = false;
port = -1;
state = true;
output = 0x00;
mask = 0xFF;
single_port = true;
if(argc == 1)
{
print_only = true;
}
else
{
/* Get the command line options */
while ((i = getopt(argc, argv, "vp:e:o:m:")) != -1)
{
switch (i)
{
case 'v':
debug_enable = true;
break;
case 'p':
port = strtoul(optarg, NULL, 0);
if (port > 7 || port < 0)
{
fprintf(stderr, "'-p' value '%s' not valid\n",optarg);
fprintf(stderr, "'-p' provide valid port number [0-7]\n");
exit(-1);
}
break;
case 'e':
if(optarg[0] != '0' && optarg[0] != '1')
{
fprintf(stderr, "'-e' value '%s' not valid\n",optarg);
fprintf(stderr, "'-e' provide valid value [0-1]\n");
exit(-1);
}
tmp = strtoul(optarg, NULL, 0);
if(tmp)
{
state = 1;
}
else
{
state = 0;
}
break;
case 'o':
if(!read_hex_byte(optarg, &output, i))
exit(-1);
break;
case 'm':
if(!read_hex_byte(optarg, &mask, i))
exit(-1);
break;
default:
fprintf(stderr, "usage: %s [-p 0-7 [-e 0-1]]|[-o HH [-m HH]] [-v]\n", *argv);
exit(-1);
}
}
}
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ERROR: ftdi_new failed\n");
return -1;
}
if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x6001)) < 0)
{
fprintf(stderr, "ERROR: unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return -1;
}
if(print_only)
{
if(ftdi_read_chipid(ftdi, &chipid) == 0)
{
printf("FTDI chipid: %X\n", chipid);
}
else
{
fprintf(stderr, "ERROR: Unable to read chip ID, errno:%d", errno);
ftdi_free(ftdi);
return -1;
}
if(ftdi_read_data(ftdi, &data, 1) == 1)
{
printf("*** OUPUT ***\n");
for(int b = 0 ; b < 8 ; b++)
{
printf("D%d:%d\n", b, ((data>>b)&0x01));
}
printf("*************\n");
}
else
{
fprintf(stderr, "ERROR: Unable to read output values, errno:%d\n", errno);
ftdi_free(ftdi);
return -1;
}
}
else
{
if(ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG ) != 0)
{
fprintf(stderr, "ERROR: Unable to set bitmode, errno:%d", errno);
ftdi_free(ftdi);
return -1;
}
/* Calculate mask and ouput value based on parameters */
if(port >=0)
{
mask = (0x01)<<port;
output = state<<port;
}
if(ftdi_read_data(ftdi, &data, 1) == 1)
{
data &= ~mask;
data |= output;
if(ftdi_write_data(ftdi, &data, 1) != 1)
{
fprintf(stderr, "ERROR: Unable to write output values, errno:%d", errno);
ftdi_free(ftdi);
return -1;
}
}
else
{
fprintf(stderr, "ERROR: Unable to read output values, errno:%d", errno);
ftdi_free(ftdi);
return -1;
}
}
ftdi_free(ftdi);
return EXIT_SUCCESS;
}