-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathds9490.cpp
More file actions
312 lines (279 loc) · 7.77 KB
/
ds9490.cpp
File metadata and controls
312 lines (279 loc) · 7.77 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
QIButton: read DS1922 IButton via DS9490B USB 1-Wire
Copyright (C) 2014 Karsten Koop <karsten.koop@gmx.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ds9490.h"
#include <iostream> // Für std::cerr und std::endl
DS9490::DS9490()
{
m_usbDevHandle = NULL;
}
DS9490::~DS9490()
{
if (m_usbDevHandle)
{
Release();
}
}
/**
* @brief Searches for a DS9490 USB device and opens a handle
*
* Currently, only one USB device is supported, so the first one found is used.
* @return bool: true on success, false on failure. On failure, an error message is available from GetLastError().
*/
bool DS9490::OpenUsbDevice()
{
struct usb_bus *bus;
struct usb_device *dev;
// initialize USB subsystem
usb_init();
usb_set_debug(0);
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == 0x04FA &&
dev->descriptor.idProduct == 0x2490) {
// take the first one, support only one DS9490B
return AquireUsb(dev);
}
}
}
m_lastError = "No DS2490 found";
return false;
}
bool DS9490::AquireUsb(struct usb_device* dev)
{
if (m_usbDevHandle!=NULL)
{
Release();
}
m_usbDevHandle = usb_open(dev);
if (m_usbDevHandle==NULL)
{
m_lastError = "Failed to open USB device";
return false;
}
if (usb_set_configuration(m_usbDevHandle, 1) != 0) {
m_lastError = "Failed to set configuration";
Release();
return false;
}
if (usb_claim_interface(m_usbDevHandle, 0)!=0)
{
m_lastError = "Failed to claim interface";
Release();
return false;
}
if (usb_set_altinterface(m_usbDevHandle, 3)!=0)
{
m_lastError = "Failed to set altinterface\n";
Release();
return false;
}
return true;
}
bool DS9490::Release()
{
usb_release_interface(m_usbDevHandle, 0);
usb_close(m_usbDevHandle);
m_usbDevHandle = NULL;
return true;
}
/**
* @brief Scan the 1-Wire bus for devices
*
* The found devices are returned in @p serials.
*/
bool DS9490::Scan1WBus(std::list<uint64_t>& serials)
{
if (!DeviceOpen()) {
m_lastError = "Device not open";
return false;
}
uint64_t lastSerial = 0;
int lastDiscrepancy = 0;
if (!DeviceOpen()) {
m_lastError = "Device not open";
return false;
}
do {
uint64_t currSerial = 0;
if (!Reset1W())
return false;
if (!WriteByte(0xF0)) // search ROM
return false;
/* three timeslots for each bit:
* 1. all participating devices return address bit
* 2. all participating devices return inverted address bit
* 3. master sends chosen address bit
*/
uint8_t bit1, bit2;
uint8_t lastZero = 0;
for (int bitNumber=0; bitNumber<64; bitNumber++)
{
if (!TouchBit(1, bit1) || !TouchBit(1, bit2))
return false;
if (bit1 && bit2)
return true; // no device on bus
uint8_t searchDirection;
if (bit1!=bit2) {
// only one bit is present
searchDirection = bit1;
} else {
// both bits present, use 0 first
if (bitNumber<lastDiscrepancy)
searchDirection = (lastSerial>>bitNumber)&1;
else if (bitNumber==lastDiscrepancy)
searchDirection = 1;
else
searchDirection = 0;
if (searchDirection==0)
lastZero = bitNumber;
}
TouchBit(searchDirection, bit1);
currSerial |= searchDirection<<bitNumber;
}
serials.push_back(currSerial);
lastSerial = currSerial;
lastDiscrepancy = lastZero;
} while (lastDiscrepancy!=0);
return true;
}
bool DS9490::Read1W(uint8_t* buffer, uint length)
{
if (!DeviceOpen()) {
m_lastError = "Device not open";
return false;
}
for (int i=0; i<length; i++) {
// TODO: use block transfer
if (!ReadByte(buffer[i]))
return false;
}
return true;
}
bool DS9490::Write1W(uint8_t* buffer, uint length)
{
if (!DeviceOpen()) {
m_lastError = "Device not open";
return false;
}
if (!Reset1W())
return false;
if (!WriteByte(0xCC)) // skip ROM
return false;
for (int i=0; i<length; i++)
{
if (!WriteByte(buffer[i]))
return false;
}
return true;
}
bool DS9490::Reset1W()
{
if (!DeviceOpen()) {
m_lastError = "Device not open";
return false;
}
int result = usb_control_msg(m_usbDevHandle, 0x40, COMM_CMD,
0x0043, 0, NULL, 0, m_timeout);
if (result!=0) {
m_lastError = "Error writing USB command";
return false;
}
char buffer[32];
// read status until unit is idle
do {
result = usb_bulk_read(m_usbDevHandle, 0x81, // EP1: control
buffer, 0x20, m_timeout);
} while(!(buffer[0x08] & 0x20) && (result>=0));
if (result<0) {
m_lastError = "Error reading device status";
return false;
}
return true;
}
bool DS9490::ReadByte(uint8_t& read)
{
return TouchByte(0xFF, read);
}
bool DS9490::WriteByte(uint8_t data)
{
uint8_t read;
if (TouchByte(data, read))
{
if (read!=data) {
m_lastError = "WriteByte: read data != written data";
return false;
}
return true;
}
return false;
}
bool DS9490::TouchByte(uint8_t write, uint8_t& read)
{
int result = usb_control_msg(m_usbDevHandle, 0x40, COMM_CMD,
0x0053, write, NULL, 0, m_timeout);
if (result!=0) {
m_lastError = "Error writing USB command";
return false;
}
char buffer[32];
// read status until unit is idle
do {
result = usb_bulk_read(m_usbDevHandle, 0x81, // EP1: control
buffer, 0x20, m_timeout);
} while(!(buffer[0x08] & 0x20) && (result>=0));
if (result<0) {
m_lastError = "Error reading device status";
return false;
}
// read data
result = usb_bulk_read(m_usbDevHandle, 0x83, // EP3: bulk read
buffer, 1, m_timeout);
if (result<0) {
m_lastError = "Error reading data";
return false;
}
read = buffer[0];
return true;
}
bool DS9490::TouchBit(uint8_t write, uint8_t& read)
{
int result = usb_control_msg(m_usbDevHandle, 0x40, COMM_CMD,
0x0021|((write&1)<<3), 0, NULL, 0, m_timeout);
if (result!=0) {
m_lastError = "Error writing USB command";
return false;
}
char buffer[32];
// read status until unit is idle
do {
result = usb_bulk_read(m_usbDevHandle, 0x81, // EP1: control
buffer, 0x20, m_timeout);
} while(!(buffer[0x08] & 0x20) && (result>=0));
if (result<0) {
m_lastError = "Error reading device status";
return false;
}
// read data
result = usb_bulk_read(m_usbDevHandle, 0x83, // EP3: bulk read
buffer, 1, m_timeout);
if (result<0) {
m_lastError = "Error reading data";
return false;
}
read = buffer[0];
return true;
}