-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
207 lines (152 loc) · 4.97 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <signal.h>
#include <err.h>
#include <libusb.h>
/*
ft232h-async-fifo
A small tool to use a FT232H as an *asynchronous* FIFO for transmitting data to the PC. Uses only libusb.
(c) 2024 by kittennbfive - https://github.com/kittennbfive/
AGPLv3+ and NO WARRANTY! Experimental stuff!
Please read the fine manual.
*/
#define SIZE_BUF 512
#define LATENCY_TIMER_VALUE 255 //1-255, set to 0 to leave unchanged
#define NB_PAR_TRANSFERS 8
#define RUN_FOR_SECONDS 0 //set to 0 to run forever/until Ctrl+C
//--- do not change anything below this line ---
#define TIME_DELTA_SEC(tv_a, tv_b) (((tv_b.tv_sec*1E6+tv_b.tv_usec)-(tv_a.tv_sec*1E6+tv_a.tv_usec))/1E6)
#if RUN_FOR_SECONDS
static struct timeval tv_started;
#endif
static bool running=true;
//a struct is not needed for a single variable, but it makes the code more future proof for further additions...
typedef struct
{
uint_fast64_t nb_transfer;
} user_data_t;
#if !RUN_FOR_SECONDS
static void sighandler(int sig)
{
(void)sig;
fprintf(stderr, "\nreceived Ctrl+C, stopping\n");
running=false;
}
#endif
static void cb(struct libusb_transfer *transfer)
{
static uint_fast64_t next_nb_transfer=0;
struct timeval tv;
gettimeofday(&tv, NULL);
uint_fast64_t * ptr_nb_transfer=&(((user_data_t*)(transfer->user_data))->nb_transfer);
#if RUN_FOR_SECONDS
if(TIME_DELTA_SEC(tv_started, tv)>RUN_FOR_SECONDS)
{
fprintf(stderr, "time limit reached, stopping\n");
running=false;
}
#endif
if(transfer->status==LIBUSB_TRANSFER_COMPLETED)
{
if(transfer->actual_length>2) //more than modem status stuff?
{
if(next_nb_transfer==(*ptr_nb_transfer))
fwrite(&transfer->buffer[2], (transfer->actual_length-2), 1, stdout);
else
errx(1, "inside callback: data arrival out of order! expected %lu but got %lu", next_nb_transfer, *ptr_nb_transfer);
}
if(running)
{
(*ptr_nb_transfer)+=NB_PAR_TRANSFERS;
next_nb_transfer++;
if(libusb_submit_transfer(transfer))
errx(1, "inside callback: libusb_submit_transfer for transfer %lu failed", *ptr_nb_transfer);
}
else
libusb_free_transfer(transfer);
}
else
errx(1, "inside callback: status %u (%s) != LIBUSB_TRANSFER_COMPLETED for transfer %lu\n", (uint)transfer->status, libusb_error_name(transfer->status), *ptr_nb_transfer);
}
int main(void)
{
fprintf(stderr, "This is ft232h-async-fifo by kittennbfive - AGPLv3+ - NO WARRANTY\n");
#if !RUN_FOR_SECONDS
struct sigaction act;
act.sa_handler=&sighandler;
act.sa_flags=0;
if(sigaction(SIGINT, &act, NULL)<0)
err(1, "registering handler for SIGINT failed");
#endif
int ret;
ret=libusb_init(NULL);
if(ret!=0)
errx(1, "libusb_init: %s", libusb_strerror(ret));
fprintf(stderr, "libusb init ok\n");
libusb_device ** list;
libusb_device * found=NULL;
libusb_device_handle * dev_handle;
struct libusb_device_descriptor descriptor;
ssize_t nb_dev=libusb_get_device_list(NULL, &list);
fprintf(stderr, "found %ld USB devices\n", nb_dev);
ssize_t i;
for(i=0; i<nb_dev; i++)
{
ret=libusb_get_device_descriptor(list[i], &descriptor);
if(ret!=0)
errx(1, "libusb_get_device_descriptor: %s", libusb_strerror(ret));
if(descriptor.idVendor==0x0403 && descriptor.idProduct==0x6014) //FT232H
{
found=list[i];
break;
}
}
if(found==NULL)
errx(1, "device not found");
libusb_free_device_list(list, 1);
ret=libusb_open(found, &dev_handle);
if(ret!=0)
errx(1, "libusb_open: %s", libusb_strerror(ret));
fprintf(stderr, "device opened\n");
//RESET
ret=libusb_control_transfer(dev_handle, 0x40, 0x00, 0x0000, 0x0000, NULL, 0, 100);
if(ret!=0)
errx(1, "libusb_control_transfer for RESET: %s", libusb_strerror(ret));
fprintf(stderr, "RESET sent\n");
#if LATENCY_TIMER_VALUE
//LATENCY TIMER (ms)
ret=libusb_control_transfer(dev_handle, 0x40, 0x09, LATENCY_TIMER_VALUE, 0x0000, NULL, 0, 100);
if(ret!=0)
errx(1, "libusb_control_transfer for latency timer: %s", libusb_strerror(ret));
fprintf(stderr, "latency timer set to %ums\n", LATENCY_TIMER_VALUE);
#endif
fprintf(stderr, "using %u parallel transfers\n", NB_PAR_TRANSFERS);
uint8_t * buf[NB_PAR_TRANSFERS];
struct libusb_transfer * transfers[NB_PAR_TRANSFERS];
user_data_t user_data[NB_PAR_TRANSFERS];
for(i=0; i<NB_PAR_TRANSFERS; i++)
{
buf[i]=malloc(SIZE_BUF*sizeof(uint8_t));
transfers[i]=libusb_alloc_transfer(0);
user_data[i].nb_transfer=i;
libusb_fill_bulk_transfer(transfers[i], dev_handle, 0x81, buf[i], SIZE_BUF, &cb, &user_data[i], 0);
ret=libusb_submit_transfer(transfers[i]);
if(ret!=0)
errx(1, "libusb_submit_transfer for %ld failed: %s", i, libusb_strerror(ret));
}
fprintf(stderr, "%u transfers submitted\n", NB_PAR_TRANSFERS);
#if RUN_FOR_SECONDS
gettimeofday(&tv_started, NULL);
#endif
while(running)
libusb_handle_events(NULL);
fprintf(stderr, "cleanup\n");
libusb_close(dev_handle);
libusb_exit(NULL);
for(i=0; i<NB_PAR_TRANSFERS; i++)
free(buf[i]);
fprintf(stderr, "all done\n\n");
return 0;
}