1
|
/* This program is distributed under the GPL, version 2 */
|
2
|
|
3
|
#include <stdio.h>
|
4
|
#include <unistd.h>
|
5
|
#include <string.h>
|
6
|
#include <ftdi.h>
|
7
|
|
8
|
#include "soft_uart.h"
|
9
|
|
10
|
static unsigned char last;
|
11
|
static struct suart_data su;
|
12
|
|
13
|
#define CLK_BIT 0x20
|
14
|
#define RST_BIT 0x08
|
15
|
#define IO_BIT 0x02
|
16
|
|
17
|
static void handle_sample(unsigned char c)
|
18
|
{
|
19
|
uint8_t rst = 0, io = 0, clk = 0;
|
20
|
|
21
|
if (c & RST_BIT)
|
22
|
rst = 1;
|
23
|
|
24
|
if (c & CLK_BIT)
|
25
|
clk = 1;
|
26
|
|
27
|
if (c & IO_BIT)
|
28
|
io = 1;
|
29
|
|
30
|
#if 0
|
31
|
if ((c & 0xf) != last) {
|
32
|
printf("%02x ", c & ~(CLK_BIT|IO_BIT));
|
33
|
}
|
34
|
last = c;
|
35
|
#endif
|
36
|
#if 0
|
37
|
if (c & RST_BIT)
|
38
|
printf("-");
|
39
|
else
|
40
|
printf("_");
|
41
|
#endif
|
42
|
#if 1
|
43
|
if (c & IO_BIT)
|
44
|
printf("-");
|
45
|
else
|
46
|
printf("_");
|
47
|
#endif
|
48
|
//suart_process_sample_bit(&su, clk, rst, io);
|
49
|
}
|
50
|
|
51
|
#define OVERSAMPLING 4
|
52
|
|
53
|
int main(int argc, char **argv)
|
54
|
{
|
55
|
struct ftdi_context ftdic;
|
56
|
int f,i;
|
57
|
unsigned char buf[1<<16];
|
58
|
//unsigned int sample_rate = 12900 * OVERSAMPLING; /* 192kS/s */
|
59
|
unsigned int sample_rate = (1 << 18);
|
60
|
|
61
|
memset(&su, 0, sizeof(su));
|
62
|
|
63
|
if (ftdi_init(&ftdic) < 0)
|
64
|
{
|
65
|
fprintf(stderr, "ftdi_init failed\n");
|
66
|
return EXIT_FAILURE;
|
67
|
}
|
68
|
|
69
|
f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
|
70
|
|
71
|
if (f < 0 && f != -5)
|
72
|
{
|
73
|
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
|
74
|
exit(-1);
|
75
|
}
|
76
|
|
77
|
printf("ftdi open succeeded: %d\n",f);
|
78
|
|
79
|
printf("enabling bitbang mode\n");
|
80
|
//ftdi_disable_bitbang(&ftdic);
|
81
|
//ftdi_set_line_property(&ftdic, 8, 1, EVEN);
|
82
|
ftdi_set_bitmode(&ftdic, 0x0, BITMODE_BITBANG);
|
83
|
ftdi_read_data_set_chunksize(&ftdic, sizeof(buf));
|
84
|
ftdi_set_latency_timer(&ftdic, 255);
|
85
|
//ftdi_set_latency_timer(&ftdic, 1);
|
86
|
//f = ftdi_set_baudrate(&ftdic, 300000);
|
87
|
f = ftdi_set_baudrate(&ftdic, sample_rate/16);
|
88
|
if (f < 0) {
|
89
|
fprintf(stderr, "error setting baudrate\n");
|
90
|
exit(1);
|
91
|
}
|
92
|
|
93
|
su.samplerate = sample_rate;
|
94
|
su.recip_etu = sample_rate / OVERSAMPLING;
|
95
|
su.num_bits = 8;
|
96
|
suart_init(&su);
|
97
|
|
98
|
while (1) {
|
99
|
memset(buf, 0, sizeof(buf));
|
100
|
f = ftdi_read_data(&ftdic, buf, sizeof(buf));
|
101
|
if (f < 0) {
|
102
|
fprintf(stderr,"read failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(&ftdic));
|
103
|
exit(1);
|
104
|
}
|
105
|
//printf("ftdi returned %u (of %u)\n", f, sizeof(buf));
|
106
|
for (i = 0; i < f; i++) {
|
107
|
handle_sample(buf[i]);
|
108
|
}
|
109
|
fflush(stdout);
|
110
|
//num_samples += f;
|
111
|
}
|
112
|
|
113
|
printf("\n");
|
114
|
|
115
|
printf("disabling bitbang mode\n");
|
116
|
ftdi_disable_bitbang(&ftdic);
|
117
|
|
118
|
ftdi_usb_close(&ftdic);
|
119
|
ftdi_deinit(&ftdic);
|
120
|
}
|