-
Notifications
You must be signed in to change notification settings - Fork 9
/
ft245.cpp
169 lines (138 loc) · 3.13 KB
/
ft245.cpp
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
/************************************************************************//**
*
* \file ft245.cpp
* \brief QT FTDI FT245 synchronous fifo interface
* \author marc at pignat dot org
*
****************************************************************************
*
* Copyright 2015,2016 Marc Pignat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#include "ft245.h"
#include <QDebug>
static int read_callback(uint8_t *buffer, int length, void *userdata)
{
Ft245 *self = (Ft245 *)userdata;
if (length)
{
emit self->rx(buffer, length);
}
if (self->_stop)
{
return 1;
}
return 0;
}
void Ft245::rx_callback(const quint8 *data, quint64 size)
{
emit rx(data, size);
}
void Ft245::poll()
{
int ret = ftdi_duplex_poll(duplex);
if (ret < 0)
{
fatal("ftdi_duplex_poll", __FILE__, __LINE__ );
}
}
int Ft245::open(void)
{
struct ftdi_version_info info = ftdi_get_library_version();
QList<uint16_t> supported_pid = QList<uint16_t>() << 0x6010 << 0x6014;
qDebug() << "ftdi_get_library_version" << info.major << "." << info.minor << "." << info.micro;
ftdi = ftdi_new();
if (!ftdi)
{
fatal("ftdi_new", __FILE__, __LINE__ );
return -1;
}
if (ftdi_set_interface(ftdi, INTERFACE_A))
{
fatal("ftdi_set_interface failed", __FILE__, __LINE__ );
close();
return -1;
}
uint16_t found_pid = 0;
foreach(uint16_t pid, supported_pid)
{
if (ftdi_usb_open(ftdi, 0x0403, pid) == 0)
{
found_pid = pid;
break;
}
}
if (!found_pid)
{
fatal("no devices found", __FILE__, __LINE__ );
printf("Searched for vid:pid : ");
foreach(uint16_t pid, supported_pid)
{
printf("0403:%04x, ", pid);
}
printf("\n");
close();
return -1;
}
_stop = false;
duplex = ftdi_duplex_start(ftdi, read_callback, this, 128, 8);
if (duplex == NULL)
{
fatal("ftdi_duplex_start", __FILE__, __LINE__ );
close();
return -1;
}
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(poll()));
timer->start(0);
return 0;
}
void Ft245::close(void)
{
if (!ftdi)
{
return;
}
if (ftdi_set_bitmode(ftdi, 0xff, BITMODE_RESET) < 0)
{
qDebug() << "ftdi_set_bitmode" << __FILE__ << ":" << __LINE__ ;
}
ftdi_usb_close(ftdi);
ftdi_free(ftdi);
ftdi = NULL;
}
Ft245::Ft245(QObject *parent) : QObject(parent)
{
}
int Ft245::start(void)
{
return open();
}
void Ft245::fatal(const char *msg, const char *file, int n)
{
qDebug() << msg << "in file" << file << ", line : " << n;
emit fatal();
}
void Ft245::tx(const quint8 *data, quint64 size)
{
int ret = ftdi_duplex_write(duplex, (const char *)data, size);
if (ret < 0)
{
qDebug() << "ftdi_duplex_write failed with status" << ret;
return;
}
}
Ft245::~Ft245()
{
}