Check in
[owTools.git] / src / libusbds2490.h
1 //---------------------------------------------------------------------------\r
2 // Copyright (C) 2004 Dallas Semiconductor Corporation, All Rights Reserved.\r
3 //\r
4 // Permission is hereby granted, free of charge, to any person obtaining a\r
5 // copy of this software and associated documentation files (the "Software"),\r
6 // to deal in the Software without restriction, including without limitation\r
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
8 // and/or sell copies of the Software, and to permit persons to whom the\r
9 // Software is furnished to do so, subject to the following conditions:\r
10 //\r
11 // The above copyright notice and this permission notice shall be included\r
12 // in all copies or substantial portions of the Software.\r
13 //\r
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
16 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES\r
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
20 // OTHER DEALINGS IN THE SOFTWARE.\r
21 //\r
22 // Except as contained in this notice, the name of Dallas Semiconductor\r
23 // shall not be used except as stated in the Dallas Semiconductor\r
24 // Branding Policy.\r
25 //---------------------------------------------------------------------------\r
26 //\r
27 \r
28 #ifndef _ds2490comm_h_\r
29 #define _ds2490comm_h_\r
30 \r
31 #define SMALLINT char\r
32 #define uchar char\r
33 \r
34 /*#if defined(_WINDOWS) || defined(__WINDOWS__) || defined(_WIN32) || defined(WIN32)\r
35    #define USE_WINDOWS_TIME 1\r
36    #include <windows.h>\r
37    #include "win32/usb.h" // libusb header\r
38 #else*/\r
39    #define USE_WINDOWS_TIME 0\r
40    #include <time.h>\r
41    #include <sys/time.h>\r
42 \r
43    #include <usb.h>\r
44 //#endif\r
45 \r
46 #ifndef ushort\r
47 #define ushort uint16_t\r
48 #endif\r
49 \r
50 #ifndef ulong\r
51 #define ulong uint32_t\r
52 #endif\r
53 \r
54 \r
55 // single byte alignment on structures\r
56 #pragma pack(push, 1)\r
57 \r
58 /** EP1 -- control read */\r
59 #define DS2490_EP1              0x81\r
60 /** EP2 -- bulk write */\r
61 #define DS2490_EP2              0x02\r
62 /** EP3 -- bulk read */\r
63 #define DS2490_EP3              0x83\r
64 \r
65 // Error codes\r
66 #define DS2490COMM_ERR_NOERROR               0   // an error has not yet been encountered\r
67 #define DS2490COMM_ERR_GETLASTERROR          1   // use GetLastError() for more information\r
68 #define DS2490COMM_ERR_RESULTREGISTERS       2   // use DS2490COMM_GetLastResultRegister() for more info\r
69 #define DS2490COMM_ERR_USBDEVICE             3   // error from USB device driver\r
70 #define DS2490COMM_ERR_READWRITE             4   // an I/O error occurred while communicating w/ device\r
71 #define DS2490COMM_ERR_TIMEOUT               5   // an operation timed out before completion\r
72 #define DS2490COMM_ERR_INCOMPLETEWRITE       6   // not all data could be sent for output\r
73 #define DS2490COMM_ERR_INCOMPLETEREAD        7   // not all data could be received for an input\r
74 #define DS2490COMM_ERR_INITTOUCHBYTE         8   // the touch byte thread could not be started\r
75 #define g_DS2490COMM_LAST_SHORTEDBUS         9   // 1Wire bus shorted on\r
76 #define g_DS2490COMM_LAST_EMPTYBUS           10  // 1Wire bus empty\r
77 \r
78 #define IOCTL_INBUF_SIZE                512\r
79 #define IOCTL_OUTBUF_SIZE               512\r
80 #define TIMEOUT_PER_BYTE            15    //1000 modified 4/27/00 BJV\r
81 #define TIMEOUT_LIBUSB              5000\r
82 \r
83 // Definition is taken from DDK\r
84 typedef struct _USB_DEVICE_DESCRIPTOR {\r
85     uchar bLength;\r
86     uchar bDescriptorType;\r
87     ushort bcdUSB;\r
88     ushort bDeviceClass;\r
89     uchar bDeviceSubClass;\r
90     uchar bDeviceProtocol;\r
91     uchar bMaxPacketSize0;\r
92     ushort idVendor;\r
93     ushort idProduct;\r
94     ushort bcdDevice;\r
95     uchar iManufacturer;\r
96     uchar iProduct;\r
97     uchar iSerialNumber;\r
98     uchar bNumConfigurations;\r
99 } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;       \r
100 \r
101 // IOCTL codes\r
102 #define DS2490_IOCTL_VENDOR CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)\r
103 #define DS2490_IOCTL_STATUS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)\r
104 #define DS2490_IOCTL_DEVICE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)\r
105 \r
106 \r
107 // Device Information is put here\r
108 typedef struct _USB_DEVICE_INFO\r
109 {\r
110         ulong   DriverVersion;\r
111         USB_DEVICE_DESCRIPTOR   Descriptor;\r
112         uchar   Unit;\r
113 } USB_DEVICE_INFO, *PUSB_DEVICE_INFO;   \r
114 \r
115 // Setup packet\r
116 typedef struct _SETUP_PACKET\r
117 {\r
118    // Only for vendor specifc bits for COMM commands.\r
119         uchar   RequestTypeReservedBits;\r
120     // The Request byte\r
121         uchar   Request;\r
122     // The Value byte\r
123         ushort  Value;\r
124     // The Index byte\r
125         ushort  Index;\r
126     // The length of extra Data In or Out\r
127         ushort  Length;\r
128    // Does the extra data go In, or Out?\r
129    SMALLINT DataOut;\r
130    // If there is extra data In, it goes here.\r
131    uchar   DataInBuffer[0];\r
132 } SETUP_PACKET, *PSETUP_PACKET;\r
133 \r
134 // Request byte, Command Type Code Constants \r
135 #define CONTROL_CMD                 0x00\r
136 #define COMM_CMD                               0x01\r
137 #define MODE_CMD                               0x02\r
138 #define TEST_CMD                               0x03\r
139 \r
140 //\r
141 // Value field, Control commands\r
142 //\r
143 // Control Command Code Constants \r
144 #define CTL_RESET_DEVICE                      0x0000\r
145 #define CTL_START_EXE                         0x0001\r
146 #define CTL_RESUME_EXE                        0x0002\r
147 #define CTL_HALT_EXE_IDLE                     0x0003\r
148 #define CTL_HALT_EXE_DONE                     0x0004\r
149 #define CTL_CANCEL_CMD                        0x0005\r
150 #define CTL_CANCEL_MACRO                      0x0006\r
151 #define CTL_FLUSH_COMM_CMDS           0x0007\r
152 #define CTL_FLUSH_RCV_BUFFER          0x0008\r
153 #define CTL_FLUSH_XMT_BUFFER          0x0009\r
154 #define CTL_GET_COMM_CMDS                     0x000A\r
155 \r
156 //\r
157 // Value field COMM Command options\r
158 //\r
159 // COMM Bits (bitwise or into COMM commands to build full value byte pairs)\r
160 // Byte 1\r
161 #define COMM_TYPE                              0x0008\r
162 #define COMM_SE                     0x0008\r
163 #define COMM_D                      0x0008\r
164 #define COMM_Z                      0x0008\r
165 #define COMM_CH                     0x0008\r
166 #define COMM_SM                     0x0008\r
167 #define COMM_R                      0x0008\r
168 #define COMM_IM                     0x0001\r
169 \r
170 // Byte 2\r
171 #define COMM_PS                     0x4000\r
172 #define COMM_PST                               0x4000\r
173 #define COMM_CIB                    0x4000\r
174 #define COMM_RTS                    0x4000\r
175 #define COMM_DT                     0x2000\r
176 #define COMM_SPU                               0x1000\r
177 #define COMM_F                                 0x0800\r
178 #define COMM_ICP                               0x0200\r
179 #define COMM_RST                               0x0100\r
180 \r
181 // Read Straight command, special bits \r
182 #define COMM_READ_STRAIGHT_NTF          0x0008\r
183 #define COMM_READ_STRAIGHT_ICP          0x0004\r
184 #define COMM_READ_STRAIGHT_RST          0x0002\r
185 #define COMM_READ_STRAIGHT_IM           0x0001\r
186 \r
187 //\r
188 // Value field COMM Command options (0-F plus assorted bits)\r
189 //\r
190 #define COMM_ERROR_ESCAPE               0x0601\r
191 #define COMM_SET_DURATION               0x0012\r
192 #define COMM_BIT_IO                     0x0020\r
193 #define COMM_PULSE                      0x0030\r
194 #define COMM_1_WIRE_RESET               0x0042\r
195 #define COMM_BYTE_IO                    0x0052\r
196 #define COMM_MATCH_ACCESS               0x0064\r
197 #define COMM_BLOCK_IO                   0x0074\r
198 #define COMM_READ_STRAIGHT              0x0080\r
199 #define COMM_DO_RELEASE                 0x6092\r
200 #define COMM_SET_PATH                   0x00A2\r
201 #define COMM_WRITE_SRAM_PAGE            0x00B2\r
202 #define COMM_WRITE_EPROM                0x00C4\r
203 #define COMM_READ_CRC_PROT_PAGE         0x00D4\r
204 #define COMM_READ_REDIRECT_PAGE_CRC     0x21E4\r
205 #define COMM_SEARCH_ACCESS              0x00F4\r
206 \r
207 // Mode Command Code Constants \r
208 // Enable Pulse Constants\r
209 #define ENABLEPULSE_PRGE                         0x01  // strong pull-up\r
210 #define ENABLEPULSE_SPUE                         0x02  // programming pulse\r
211 \r
212 // 1Wire Bus Speed Setting Constants\r
213 #define ONEWIREBUSSPEED_REGULAR        0x00\r
214 #define ONEWIREBUSSPEED_FLEXIBLE       0x01\r
215 #define ONEWIREBUSSPEED_OVERDRIVE      0x02\r
216 \r
217 //\r
218 // Value field Mode Commands options\r
219 //\r
220 #define MOD_PULSE_EN                   0x0000\r
221 #define MOD_SPEED_CHANGE_EN            0x0001\r
222 #define MOD_1WIRE_SPEED                0x0002\r
223 #define MOD_STRONG_PU_DURATION         0x0003\r
224 #define MOD_PULLDOWN_SLEWRATE          0x0004\r
225 #define MOD_PROG_PULSE_DURATION        0x0005\r
226 #define MOD_WRITE1_LOWTIME             0x0006\r
227 #define MOD_DSOW0_TREC                 0x0007\r
228 \r
229 //\r
230 // This is the status structure as returned by DS2490_IOCTL_STATUS.\r
231 //\r
232 typedef struct _STATUS_PACKET\r
233 {\r
234         uchar   EnableFlags;\r
235         uchar   OneWireSpeed;\r
236         uchar   StrongPullUpDuration;\r
237         uchar   ProgPulseDuration;\r
238         uchar   PullDownSlewRate;\r
239         uchar   Write1LowTime;\r
240         uchar   DSOW0RecoveryTime;\r
241         uchar   Reserved1;\r
242         uchar   StatusFlags;\r
243         uchar   CurrentCommCmd1;\r
244         uchar   CurrentCommCmd2;\r
245         uchar   CommBufferStatus;  // Buffer for COMM commands\r
246         uchar   WriteBufferStatus; // Buffer we write to\r
247         uchar   ReadBufferStatus;  // Buffer we read from\r
248         uchar   Reserved2;\r
249         uchar   Reserved3;\r
250    // There may be up to 16 bytes here, or there may not.\r
251    uchar   CommResultCodes[16];\r
252 } STATUS_PACKET, *PSTATUS_PACKET;\r
253 \r
254 \r
255 //\r
256 // STATUS FLAGS\r
257 //\r
258 // Enable Flags\r
259 #define ENABLEFLAGS_SPUE                            0x01  // if set Strong Pull-up to 5V enabled\r
260 #define ENABLEFLAGS_PRGE                            0x02  // if set 12V programming pulse enabled\r
261 #define ENABLEFLAGS_SPCE                            0x04  // if set a dynamic 1-Wire bus speed change through Comm. Cmd. enabled\r
262 \r
263 // Device Status Flags\r
264 #define STATUSFLAGS_SPUA                       0x01  // if set Strong Pull-up is active\r
265 #define STATUSFLAGS_PRGA                       0x02  // if set a 12V programming pulse is being generated\r
266 #define STATUSFLAGS_12VP                       0x04  // if set the external 12V programming voltage is present\r
267 #define STATUSFLAGS_PMOD                       0x08  // if set the DS2490 powered from USB and external sources\r
268 #define STATUSFLAGS_HALT                       0x10  // if set the DS2490 is currently halted\r
269 #define STATUSFLAGS_IDLE                       0x20  // if set the DS2490 is currently idle\r
270 \r
271 // Result Registers\r
272 #define ONEWIREDEVICEDETECT               0xA5  // 1-Wire device detected on bus\r
273 #define COMMCMDERRORRESULT_NRS            0x01  // if set 1-WIRE RESET did not reveal a Presence Pulse or SET PATH did not get a Presence Pulse from the branch to be connected\r
274 #define COMMCMDERRORRESULT_SH             0x02  // if set 1-WIRE RESET revealed a short on the 1-Wire bus or the SET PATH couln not connect a branch due to short\r
275 #define COMMCMDERRORRESULT_APP            0x04  // if set a 1-WIRE RESET revealed an Alarming Presence Pulse\r
276 #define COMMCMDERRORRESULT_VPP            0x08  // if set during a PULSE with TYPE=1 or WRITE EPROM command the 12V programming pulse not seen on 1-Wire bus\r
277 #define COMMCMDERRORRESULT_CMP            0x10  // if set there was an error reading confirmation byte of SET PATH or WRITE EPROM was unsuccessful\r
278 #define COMMCMDERRORRESULT_CRC            0x20  // if set a CRC occurred for one of the commands: WRITE SRAM PAGE, WRITE EPROM, READ EPROM, READ CRC PROT PAGE, or READ REDIRECT PAGE W/CRC\r
279 #define COMMCMDERRORRESULT_RDP            0x40  // if set READ REDIRECT PAGE WITH CRC encountered a redirected page\r
280 #define COMMCMDERRORRESULT_EOS            0x80  // if set SEARCH ACCESS with SM=1 ended sooner than expected with too few ROM IDs\r
281 \r
282 // Strong Pullup \r
283 #define SPU_MULTIPLE_MS                   128\r
284 #define SPU_DEFAULT_CODE                  512 / SPU_MULTIPLE_MS   // default Strong pullup value\r
285 \r
286 // Programming Pulse \r
287 #define PRG_MULTIPLE_US                   8               // Programming Pulse Time Multiple (Time = PRG_MULTIPLE_US * DurationCode)\r
288 #define PRG_DEFAULT_CODE                  512 / PRG_MULTIPLE_US   // default Programming pulse value\r
289 \r
290 // Support functions\r
291 SMALLINT DS2490Detect(usb_dev_handle *hDevice);\r
292 SMALLINT DS2490GetStatus(usb_dev_handle *hDevice, STATUS_PACKET *status, uchar *pResultSize);\r
293 SMALLINT DS2490ShortCheck(usb_dev_handle *hDevice, SMALLINT *present, SMALLINT *vpp);\r
294 SMALLINT DS2490Reset(usb_dev_handle *hDevice);\r
295 SMALLINT DS2490Read(usb_dev_handle *hDevice, uchar *buffer, ushort *pnBytes);\r
296 SMALLINT DS2490Write(usb_dev_handle *hDevice, uchar *buffer, ushort *pnBytes);\r
297 SMALLINT DS2490HaltPulse(usb_dev_handle *hDevice);\r
298 SMALLINT AdapterRecover(int portnum);\r
299 \r
300 #endif // _ds2490comm_h_\r