forked from swift-nav/piksi_sample_grabber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_fifo_mode.c
222 lines (198 loc) · 6.64 KB
/
set_fifo_mode.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
/*
* Copyright (C) 2012-2014 Swift Navigation Inc.
*
* Contacts: Fergus Noble <[email protected]>
* Colin Beighley <[email protected]>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*
* "set_fifo_mode.c"
*
* Purpose : Writes settings to EEPROM attached to FT232H for synchronous
* FIFO mode in order to stream raw samples from the RF frontend
* through the FPGA. Must be used before running sample_grabber.
*
* Usage : Plug in device and run set_fifo_mode. You may have to use
* sudo rmmod ftdi_sio first.
*
* Options : ./set_fifo_mode [-v] [-i] [-h]
* [--verbose -v] Print more verbose output.
* [--id -i] Product ID to assign to Piksi.
* Default is 0x8398.
* Valid range 0x0001 to 0xFFFF.
* [--help -h] Print this information.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "ftd2xx.h"
/* FTDI VID / Piksi custom PID. */
#define USB_CUSTOM_VID 0x0403
#define USB_CUSTOM_PID 0x8398
#define USB_DEFAULT_VID 0x0403
#define USB_DEFAULT_PID 0x6014
FT_HANDLE ft_handle;
FT_STATUS ft_status;
int verbose = 0;
int pid = USB_CUSTOM_PID;
FT_PROGRAM_DATA eeprom_data;
void print_usage()
{
printf("Usage: set_fifo_mode [-v] [-i pid] [-h]\n"
"Options:\n"
" [--verbose -v] Print more verbose output.\n"
" [--id -i] Product ID to assign to Piksi.\n"
" Default is 0x8398.\n"
" Valid range 0x0001 to 0xFFFF.\n"
" [--help -h] Print this information.\n"
);
}
/* Parse command line arg --id, USB Product ID.
* Input: PID in hex or decimal format, i.e. '0xFFFF' or '65535'.
*
* Returns 0x0001 to 0xFFFF as valid ID, or 0 for error.
*/
int parse_pid(char *arg) {
int pid = 0;
/* Find out if it is hex or base 10. Hex preceeded by '0x'. */
if ((arg[0] == '0') && (arg[1] == 'x')) {
/* Hexadecimal. */
/* Check if length is <= 6, i.e. '0xFFFF'. */
if (strlen(arg) > 6)
return 0;
pid = (int)strtol(arg+2, NULL, 16);
} else {
/* Decimal. */
/* Check if length is <= 5, i.e. '65535'. */
if (strlen(arg) > 5)
return 0;
pid = atoi(arg);
}
return pid;
}
int main(int argc, char *argv[])
{
static const struct option long_opts[] = {
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"id", required_argument, NULL, 'i'},
{NULL, no_argument, NULL, 0}
};
opterr = 0;
int c;
int option_index = 0;
while ((c = getopt_long(argc, argv, "vhi:", long_opts, &option_index)) != -1)
switch (c) {
case 'v':
verbose++;
break;
case 'h':
print_usage();
return EXIT_SUCCESS;
case 'i': {
pid = parse_pid(optarg);
if (!pid) {
fprintf(stderr, "Invalid ID argument.\n");
return EXIT_FAILURE;
}
break;
}
case '?':
if (optopt == 'i')
fprintf(stderr, "ID argument requires an argument.\n");
else
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
return EXIT_FAILURE;
default:
abort();
}
DWORD num_devs;
int iport = 0;
/* See how many devices are plugged in, fail if greater than 1. */
if (verbose)
printf("Creating device info list\n");
ft_status = FT_CreateDeviceInfoList(&num_devs);
if (ft_status != FT_OK){
fprintf(stderr,"ERROR : Failed to create device info list, ft_status = %d\n",ft_status);
return EXIT_FAILURE;
}
/* Check that there aren't more than 1 device plugged in. */
if (verbose)
printf("Making sure only one FTDI device is plugged in\n");
if (num_devs > 1){
fprintf(stderr,"ERROR : More than one FTDI device plugged in\n");
return EXIT_FAILURE;
}
/* Try to open device using default FTDI VID/PID. */
if (verbose)
printf("Trying to open with VID=0x%04x, PID=0x%04x...", USB_DEFAULT_VID,
USB_DEFAULT_PID);
ft_status = FT_SetVIDPID(USB_DEFAULT_VID, USB_DEFAULT_PID);
if (ft_status != FT_OK){
fprintf(stderr,"ERROR : Failed to set VID and PID, ft_status = %d\n",ft_status);
return EXIT_FAILURE;
}
ft_status = FT_Open(iport, &ft_handle);
/* Exit program if we haven't opened the device. */
if (ft_status != FT_OK){
if (verbose)
printf("FAILED\n");
fprintf(stderr,"ERROR : Failed to open device : ft_status = %d\n", ft_status);
if (ft_status == FT_DEVICE_NOT_OPENED)
fprintf(stderr,"Have you tried (sudo rmmod ftdi_sio)?\n");
return EXIT_FAILURE;
}
if (verbose)
printf("SUCCESS\n");
/* Erase the EEPROM. */
ft_status = FT_EraseEE(ft_handle);
if(ft_status != FT_OK) {
fprintf(stderr, "ERROR: Device EEPROM could not be erased : ft_status = %d\n", ft_status);
return EXIT_FAILURE;
}
if (verbose)
printf("Erased device's EEPROM\n");
/* Assign appropriate values to eeprom data. */
eeprom_data.Signature1 = 0x00000000;
eeprom_data.Signature2 = 0xffffffff;
eeprom_data.Version = 5; /* 5=FT232H. */
eeprom_data.VendorId = USB_CUSTOM_VID;
eeprom_data.ProductId = pid;
eeprom_data.Manufacturer = "FTDI";
eeprom_data.ManufacturerId = "FT";
eeprom_data.Description = "Piksi Passthrough";
eeprom_data.IsFifoH = 1; /* Needed for FIFO samples passthrough. */
/* Program device EEPROM. */
ft_status = FT_EE_Program(ft_handle, &eeprom_data);
if (ft_status != FT_OK) {
fprintf(stderr,"ERROR : Failed to program device EEPROM : ft_status = %d\n",ft_status);
return EXIT_FAILURE;
}
if (verbose)
printf("Programmed device's EEPROM, set PID to: 0x%04x\n", pid);
/* Reset the device. */
ft_status = FT_ResetDevice(ft_handle);
if (ft_status != FT_OK) {
fprintf(stderr, "ERROR: Device could not be reset : ft_status = %d\n", ft_status);
return EXIT_FAILURE;
}
if (verbose)
printf("Reset device\n");
/* Close the device. */
if (verbose)
printf("Closing device\n");
FT_Close(ft_handle);
if(ft_status != FT_OK) {
fprintf(stderr,"ERROR : Failed to close device : ft_status = %d\n",ft_status);
return EXIT_FAILURE;
}
if (verbose)
printf("Re-configuring for FIFO mode successful, please unplug and replug your device now.\n");
return EXIT_SUCCESS;
}