1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "owUSBInterface.h"
46 #include "libusbds2490.h"
57 int usb_num_devices = -1;
58 usb_dev_handle *usb_dev_handle_list[20];
59 //static SMALLINT usbhnd_init = 0;
60 struct usb_device *usb_dev_list[20];
61 static void DS2490Discover(void)
64 struct usb_device *dev;
66 // initialize USB subsystem
67 #ifdef LIBUSB_DEBUG_DISABLE // Use this define at compile time to turn off LIBUSB debug info
74 usb_num_devices = 0; // avoid port zero to make it look like other builds
76 for (bus = usb_get_busses(); bus; bus = bus->next)
79 for (dev = bus->devices; dev; dev = dev->next)
81 //printf("%04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
82 if (dev->descriptor.idVendor == 0x04FA &&
83 dev->descriptor.idProduct == 0x2490)
85 //printf("DS2490Discover-find\n");
86 usb_dev_list[++usb_num_devices] = dev;
94 uint8_t AdapterRecover(usb_dev_handle *hDevice)
96 // detect DS2490 and set it up
97 if (DS2490Detect(hDevice))
99 //USBSpeed[portnum] = MODE_NORMAL; // current DS2490 1-Wire Net communication speed
100 //USBLevel[portnum] = MODE_NORMAL; // current DS2490 1-Wire Net level
105 //OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
112 using namespace usb_1w;
116 int owUSBInterface::InitAdapter(uint8_t pin){
117 if(!usbhnd_init) // check to see the USB bus has been setup properly
119 // discover all DS2490s on USB bus...
123 if (usb_num_devices==0) return -1;
125 // check to see if the portnumber is valid
126 if ((usb_num_devices <pin) || (pin == 0))
128 // error - Attempted to select invalid port number
129 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_NO_ADAPTER_FOUND");
133 // check to see if opening the device is valid
136 // error - USB Device already open
137 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_DEVICE_ALREADY_OPENED");
142 hDevice = usb_open(usb_dev_list[pin]);
145 // Failed to open usb device
146 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_OPEN_FAILED");
150 // set the configuration
151 if (usb_set_configuration(hDevice, 1))
153 // Failed to set configuration
154 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_SET_CONFIGURATION_ERROR");
155 usb_close(hDevice); // close handle
159 // claim the interface
160 if (usb_claim_interface(hDevice, 0))
162 // Failed to claim interface
163 //printf("%s\n", usb_strerror());
164 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_CLAIM_INTERFACE_ERROR");
165 usb_close(hDevice); // close handle
169 // set the alt interface
170 if (usb_set_altinterface(hDevice, 3))
172 // Failed to set altinterface
173 //printf("%s\n", usb_strerror());
174 log->set(OWLOG_ERROR,"OWERROR_LIBUSB_SET_ALTINTERFACE_ERROR");
175 usb_release_interface(hDevice, 0); // release interface
176 usb_close(hDevice); // close handle
181 // clear USB endpoints before doing anything with them.
182 usb_clear_halt(hDevice, DS2490_EP3);
183 usb_clear_halt(hDevice, DS2490_EP2);
184 usb_clear_halt(hDevice, DS2490_EP1);
186 // verify adapter is working
187 if (!AdapterRecover(hDevice))
189 log->set(OWLOG_ERROR,"Adapter don't work");
190 usb_release_interface(hDevice, 0); // release interface
191 usb_close(hDevice); // close handle
200 int owUSBInterface::Reset() {
202 SMALLINT present,vpp;
206 setup.RequestTypeReservedBits = 0x40;
207 setup.Request = COMM_CMD;
208 setup.Value = COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE;
209 setup.Index = ONEWIREBUSSPEED_REGULAR;// ONEWIREBUSSPEED_FLEXIBLE;
211 setup.DataOut = FALSE;
212 // call the libusb driver
213 ret = usb_control_msg(hDevice,
214 setup.RequestTypeReservedBits,
224 log->set(OWLOG_ERROR,"OWERROR_RESET_FAILED");
225 //AdapterRecover(portnum);
230 // extra delay for alarming DS1994/DS2404 complience
231 // if ((FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) && (USBSpeed[portnum] != MODE_OVERDRIVE))
234 // success, check for shorts
235 if (DS2490ShortCheck(hDevice, &present,&vpp))
237 //USBVpp[portnum] = vpp;
242 log->set(OWLOG_ERROR,"OWERROR_OW_SHORTED");
245 //AdapterRecover(portnum);
252 uint8_t owUSBInterface::sendrecivBit(uint8_t bit) {
258 // set to do touchbit
259 setup.RequestTypeReservedBits = 0x40;
260 setup.Request = COMM_CMD;
261 setup.Value = COMM_BIT_IO | COMM_IM | ((bit) ? COMM_D : 0);
264 setup.DataOut = FALSE;
266 // call the libusb driver
267 ret = usb_control_msg(hDevice,
268 setup.RequestTypeReservedBits,
278 log->set(OWLOG_ERROR,"OWERROR_ADAPTER_ERROR");
279 AdapterRecover(hDevice);
284 // success, read the result
286 if (DS2490Read(hDevice, buf, &nBytes)) {
292 log->set(OWLOG_ERROR,"OWERROR_ADAPTER_ERROR");
293 AdapterRecover(hDevice);
299 uint8_t owUSBInterface::sendrecivByte(uint8_t byte) {
305 // set to do touchbyte
306 setup.RequestTypeReservedBits = 0x40;
307 setup.Request = COMM_CMD;
308 setup.Value = COMM_BYTE_IO | COMM_IM;
309 setup.Index = byte & 0xFF;
311 setup.DataOut = FALSE;
313 // call the libusb driver
314 ret = usb_control_msg(hDevice,
315 setup.RequestTypeReservedBits,
325 log->set(OWLOG_ERROR,"OWERROR_ADAPTER_ERROR");
326 AdapterRecover(hDevice);
331 // success, read the result
334 if (DS2490Read(hDevice, buf, &nBytes)) {
340 log->set(OWLOG_ERROR,"OWERROR_ADAPTER_ERROR");
341 AdapterRecover(hDevice);
347 void owUSBInterface::ReleaseAdapter() {
348 usb_release_interface(hDevice, 0);