-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds2490.c
1088 lines (930 loc) · 28.1 KB
/
ds2490.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) Bjorn Andersson <[email protected]> */
#include <usb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "ds2490.h"
#define VENDOR_MAXIM 0x04FA
#define PRODUCT_2490 0x2490
#define MAX_USBDEVS 4
#define MAX_OWDEVS 128
#define USB_TIMEOUT 5000
#define DS2490_FIFOSIZE 128
#define USB_ALT_INTERFACE 1
#define EP3 3
#define CONTROL_CMD 0x00
#define COMM_CMD 0x01
#define MODE_CMD 0x02
#define REGULAR_RESET_US 1096 /* 512us low + 584us high */
#define REGULAR_SLOT_US 86
#define OVERDRIVE_RESET_US 138 /* 64us low + 74us high */
#define OVERDRIVE_SLOT_US 10
#define FLEXIBLE_RESET_US 1096 /* 512us low + 584us high */
#define FLEXIBLE_SLOT_US 70
#define REGULAR_BPS 1000000 / 68
#define OVERDRIVE_BPS 1000000 / 10
#define FLEXIBLE_BPS 1000000 / 79
#define USB_DEVICE_TO_HOST 0x40
/*
* Three different vendor-specific command types exist to control and
* communicate with the DS2490: Control, Communication, and Mode.
*
* Control, Communication and Mode commands, like USB core requests,
* are communicated over the default control pipe at EP0.
*/
/*
* Control commands are used to manage various device functions
* including the processing of communication commands, buffer
* clearing, and SW reset.
*/
#define CTL_RESET_DEVICE 0x0000
#define CTL_START_EXE 0x0001
#define CTL_RESUME_EXE 0x0002
#define CTL_HALT_EXE_IDLE 0x0003
#define CTL_HALT_EXE_DONE 0x0004
#define CTL_FLUSH_COMM_CMDS 0x0007
#define CTL_FLUSH_RCV_BUFFER 0x0008
#define CTL_FLUSH_XMT_BUFFER 0x0009
#define CTL_GET_COMM_CMDS 0x000A
/*
* Mode commands are used to establish the 1-Wire operational
* characteristics of the DS2490 such as slew rate, low time, strong
* pullup, etc.
*/
#define MOD_PULSE_EN 0x0000
#define MOD_SPEED_CHANGE_EN 0x0001
#define MOD_1WIRE_SPEED 0x0002
#define MOD_STRONG_PU_DURATION 0x0003
#define MOD_PULLDOWN_SLEWRATE 0x0004
#define MOD_PROG_PULSE_DURATION 0x0005
#define MOD_WRITE1_LOWTIME 0x0006
#define MOD_DSOW0_TREC 0x0007
/*
* Communication commands are used for 1-Wire data and command I/O.
*/
#define COM_SET_DURATION 0x12
#define COM_BIT_IO 0x20
#define COM_PULSE 0x30
#define COM_RESET 0x42
#define COM_BYTE_IO 0x52
#define COM_MATCH_ACCESS 0x64
#define COM_BLOCK_IO 0x74
#define COM_READ_STRAIGHT 0x80
#define COM_DO_AND_RELEASE 0x92
#define COM_SET_PATH 0xA2
#define COM_WRITE_SRAM_PAGE 0xB2
#define COM_WRITE_EPROM 0xC4
#define COM_READ_CRC_PROT_PAGE 0xD4
#define COM_READ_REDIRECT_PAGE 0xE4
#define COM_SEARCH_ACCESS 0xF4
#define PARAM_PRGE 0x01
#define PARAM_SPUE 0x02
owusb_device_t owusb_devs[MAX_USBDEVS];
int owusb_dev_count = 0;
const char *ow_speed[] = {
"Regular",
"Flexible",
"Overdrive"
};
const char *ow_slew_rate[] = {
"15V/us",
"2.20V/us",
"1.65Vus",
"1.37V/us",
"1.10V/us",
"0.83V/us",
"0.70V/us",
"0.55V/us"
};
/**************************************************************
* Control commands
*
* Control commands are used to manage various device functions
* including the processing of communication commands, buffer
* clearing, and SW reset.
**************************************************************/
/* owusb_ctrl_reset
*
* Performs a hardware reset equivalent to the power-on reset. This
* includes clearing all endpoint buffers and loading the Mode control
* registers with their default values.
*/
int
owusb_ctl_reset(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_RESET_DEVICE, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_start_ext
*
* Starts execution of Communication commands. This command is also
* required to start the execution of Communication commands with an
* IM (immediate execution control) bit set to logic 0.
*/
int
owusb_ctl_start_exe(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_START_EXE, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_resume_exe
*
* Resume execution of a Communication command that was halted with
* either of the owusb_ctl_halt_exe_idle() or
* owusb_ctl_halt_exe_done() functions.
*/
int
owusb_ctl_resume_exe(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_RESUME_EXE, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_halt_exe_idle
*
* Halt the execution of the current Communication command after the
* 1-Wire bus has returned to the idle state. Further Communication
* command processing is stopped until owusb_ctl_resume_exe() is
* called. This function, or the owusb_ctl_halt_exe_done() function,
* is also used to terminate a strong pullup or programming pulse of
* semi-infinite or infinite duration.
*/
int
owusb_ctl_halt_exe_idle(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_HALT_EXE_IDLE, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_halt_exe_done
*
* Halt the execution of a Communication command after the current
* command execution is complete. Further Communication command
* processing is stopped until owusb_ctl_resume_exe() is called. This
* function, or the owusb_ctl_halt_exe_idle(), is also used to
* terminate a strong pullup or programming pulse of semi-infinite or
* infinite duration.
*/
int
owusb_ctl_halt_exe_done(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_HALT_EXE_DONE, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_flush_comm_cmds
*
* Clear all unexecuted Communication commands from the command
* FIFO. The DS2490 must be in a halted state before this function can
* be called.
*/
/* FIX: add check that it is in halted state? */
int
owusb_ctl_flush_comm_cmds(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_FLUSH_COMM_CMDS, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_fush_rcv_buffer
*
* Clear EP3 receive data FIFO (data from 1-Wire device). The DS2490
* must be in a halted state before this function can be called.
*/
int
owusb_ctl_flush_rcv_buffer(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_FLUSH_RCV_BUFFER, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_flush_xmt_buffer
*
* Clear EP2 transmit data FIFO (data to 1-Wire device). The DS2490
* must be in a halted state before this function can be called.
*/
int
owusb_ctl_flush_xmt_buffer(owusb_device_t *d)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_FLUSH_XMT_BUFFER, 0x0000, NULL, 0, USB_TIMEOUT);
}
/* owusb_ctl_get_comm_cmds
*
* Retrieve unexecuted Communication commands and parameters from the
* command FIFO. The DS2490 must be in a halted state before this
* function can be called. Unexecuted commands are returned over EP0
* in the control transfer data phase. Host software is responsible
* for determining the number of command/parameter bytes to be
* returned and specifying the value in the wLength field of the
* control transfer setup packet. Commands/parameters are deleted
* from the FIFO as they are transmitted to the host; the command
* pointer used with the FIFO is updated as values are read. Any
* commands/parameters that are not transferred remain in the FIFO and
* will be processed when command execution resumes. If the wLength
* value passed is larger than the number of command/parameter bytes,
* the DS2490 will terminate the control transfer with a short data
* packet.
*/
int
owusb_ctl_get_comm_cmds(owusb_device_t *d, uint8_t *cmds, int len)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, CONTROL_CMD, CTL_RESUME_EXE, 0x0000, (char *)cmds, len, USB_TIMEOUT);
}
/**************************************************************
* Mode commands
*
* Mode commands are used to establish the 1-Wire operational
* characteristics of the DS2490 such as slew rate, low time, strong
* pullup, etc.
**************************************************************/
/* owusb_mod_pulse_en
*
* Enable a 1-Wire strong pullup pulse to 5V and/or +12V EPROM
* programming pulse.
*
* The power-up default state for both strong pullup and
* programming pulse is disabled.
*
* @param params
* - PARAM_SPUE - Strong pullup enabled
* - PARAM_PRGE - Programming pulse enabled
*/
int
owusb_mod_pulse_en(owusb_device_t *d, int params)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_PULSE_EN, params & 0x3, NULL, 0, USB_TIMEOUT);
}
/* owusb_mod_speed_change_en
*
* Enable or disable a 1-Wire communication speed change.
*
* The power-up default state for speed change is disabled.
*
* @param enable
* - 0: disable
* - 1: enable
*/
int
owusb_mod_speed_change_en(owusb_device_t *d, int enable)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_SPEED_CHANGE_EN, enable & 0x1, NULL, 0, USB_TIMEOUT);
}
/* owusb_mod_speed
*
* Set the speed of 1-Wire communication
*
* The power-up default communication speed is regular.
*
* @param speed PARAM_SPEED_REGULAR, PARAM_SPEED_FLEXIBLE or
* PARAM_SPEED_OVERDIRVE
*/
int
owusb_mod_speed(owusb_device_t *d, int speed)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_1WIRE_SPEED, speed & 0x3, NULL, 0, USB_TIMEOUT);
}
/* owusb_mod_strong_pu_duration
*
* Set the time duration of a 1-Wire strong pullup. The time is
* controlled with an unsigned 8-bit binary number between 0x00 and
* 0xfe which specifies the duration in multiples of 16ms. A value of
* 0x01 specifies 16ms, 0x02 equals 32ms, etc. A value of 0x00
* specifies infinite duration. Parameter value 0xff is reserved and
* will cause the device to deliver a pullup duration of <1µs. To
* terminate an infinite duration pullup call either
* owusb_ctl_halt_exe_done() or owusb_halt_exe_idle().
*
* The power-up default strong pullup duration register value is
* 512ms.
*
*/
int
owusb_mod_strong_pu_duration(owusb_device_t *d, int duration)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_STRONG_PU_DURATION, duration & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* Select the pulldown slew rate for 1-Wire bus Flexible Speed
* operation. The pulldown slew rate power-up default value for
* Flexible speed is 0.83V/us.
*
* @param slewrate PARAM_SLEWRATE_15Vus, PARAM_SLEWRATE_2_20Vus,
* PARAM_SLEWRATE_1_65Vus, PARAM_SLEWRATE_1_37Vus,
* PARAM_SLEWRATE_1_10Vus, PARAM_SLEWRATE_0_83Vus,
* PARAM_SLEWRATE_0_70Vus, PARAM_SLEWRATE_0_55Vus
*
*/
int
owusb_mod_pulldown_slewrate(owusb_device_t *d, int slewrate)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_PULLDOWN_SLEWRATE, slewrate & 0xf, NULL, 0, USB_TIMEOUT);
}
/*
* Set the time duration of a 1-Wire Programming Pulse. The time is
* controlled with a an unsigned 8-bit binary number between 0x00 and
* 0xfe specifying the duration in multiples of 8µs. A value of 0x00
* stands for infinite duration. Parameter value 0xff is reserved and
* will cause the device to deliver a pulse duration of <1µs. To
* terminate an infinite duration programming pulse call either
* owusb_ctl_halt_exe_done() or owusb_halt_exe_idle(). The power-up
* default strong pullup duration is 512µs.
*/
int
owusb_mod_prog_pulse_duration(owusb_device_t *d, int duration)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_PROG_PULSE_DURATION, duration & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* Select the Write-1 low time for 1-Wire bus Flexible speed
* operation; The nominal Write-1 Low Time for Regular speed is 8us,
* at Overdrive speed it is 1us. The Write-1 Low Time power-up
* default value for Flexible speed is 12us.
*/
int
owusb_mod_write1_lowtime(owusb_device_t *d, int duration)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_WRITE1_LOWTIME, duration & 0xf, NULL, 0, USB_TIMEOUT);
}
/*
* Select the Data Sample Offset (tDSO) / Write-0 recovery (tW0R) time
* (DSO/W0R) for 1-Wire bus Flexible Speed operation.
*/
int
owusb_mod_dsow0_trec(owusb_device_t *d, int duration)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, MODE_CMD, MOD_DSOW0_TREC, duration & 0xf, NULL, 0, USB_TIMEOUT);
}
/*******************************************************************
* Communication commands
*
* Communication commands are used for 1-Wire data and command I/O.
*******************************************************************/
/*
* Change the State Register pulse duration value for either the +12V
* programming pulse or strong pullup.
*
* @param params NTF, ICP, IM (TYPE)
* @param type 0: strong pullup; 1: programming pulse
*/
int
owusb_com_set_duration(owusb_device_t *d, int params, int type, int duration)
{
if (type) params |= PARAM_TYPE;
params |= COM_SET_DURATION;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, params, duration & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* Temporarily pull the 1-Wire bus to +12V in order to program an
* EPROM device or to generate a strong pullup to 5V in order to
* provide extra power for an attached iButton device, e.g.,
* temperature sensor or crypto iButton.
*
* @param params F, NTF, ICP, TYPE, IM (TYPE)
* @param type 0: strong pullup; 1: programming pulse
*/
int
owusb_com_pulse(owusb_device_t *d, int params, int type)
{
if (type) params |= PARAM_TYPE;
params |= COM_PULSE;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, params, 0, NULL, 0, USB_TIMEOUT);
}
/*
* Generate a reset pulse on the 1-Wire bus and to optionally change
* the 1-Wire speed.
*
* @param params PST, F, NTF, ICP, IM
* @param present Reset until present
* @param speed PARAM_SPEED_REGULAR, PARAM_SPEED_FLEXIBLE,
* PARAM_SPEED_OVERDIRVE
*
*/
int
owusb_com_reset(owusb_device_t *d, int params, int present, int speed)
{
if (present) params |= PARAM_PST;
params |= COM_RESET;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, params, speed & 0x3, NULL, 0, USB_TIMEOUT);
}
/*
* Generate a single time slot on the 1-Wire bus.
*
* This time slot may optionally be followed by a strong pullup using
* embedded command bits SPU and CIB. With CIB = 1, a requested strong
* pullup will only occur if the read-back revealed a 0. Data is
* returned to the host only if the embedded command bit ICP = 0. If
* ICP = 0, the bit read from the 1-Wire device is stored in the EP3
* FIFO and is read by the host using an EP3 bulk transaction.
*
* @param params SPU, NTF, ICP, IM (CIB, D)
* @param bit Bit to write (0 or 1)
*/
int
owusb_com_bit_io(owusb_device_t *d, int params, int bit)
{
if (bit) params |= PARAM_D;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_BIT_IO | params, 0, NULL, 0, USB_TIMEOUT);
}
/*
* Accomplish a direct 1-Wire write and read with optional strong
* pullup after the last bit of the byte.
*
* The optional strong pullup is controlled using embedded command bit
* SPU. For a read sequence, the setup packet data byte value is set
* to 0xFF. Data is returned to the host only if the embedded command
* bit ICP = 0. If ICP = 0, the byte read from the 1-Wire device is
* stored in the EP3 FIFO and is read by the host using an EP3 bulk
* transaction.
*
* @param params SPU, NTF, ICP, IM
* @param byte Byte to write
*/
int
owusb_com_byte_io(owusb_device_t *d, int params, uint8_t byte)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_BYTE_IO | params, byte & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* Accomplish a direct 1-Wire write or read with optional strong
* pullup after the last byte of the block.
*
* The optional strong pullup is controlled using embedded command bit
* SPU. Embedded command bit RST enables a 1-Wire reset before the
* command executes. To accomplish a READ function all input data
* should be 0xFF, otherwise the data read from the 1-Wire bus will be
* masked. For a block write sequence the EP2 FIFO must be pre-filled
* with data before command execution. Additionally, for block sizes
* greater then the FIFO size, the FIFO content status must be
* monitored by host SW so that additional data can be sent to the
* FIFO when necessary. A similar EP3 FIFO content monitoring
* requirement exists for block read sequences. During a block read
* the number of bytes loaded into the EP3 FIFO must be monitored so
* that the data can be read before the FIFO overflows.
*
* @param params SPU, NTF, ICP, RST, IM
* @param len
*/
int
owusb_com_block_io(owusb_device_t *d, int params, int len)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_BLOCK_IO | params, len, NULL, 0, USB_TIMEOUT);
}
/*
* Address a device on the active section of the 1-Wire bus using the
* Match ROM or Overdrive Match command code.
*
* @param params NTF, ICP, RST, SE, IM
* @param speed PARAM_SPEED_REGULAR, PARAM_SPEED_FLEXIBLE,
* PARAM_SPEED_OVERDRIVE
* @param cmd WIRE_CMD_MATCH_ROM, WIRE_CMD_OD_MATCH_ROM
*
*/
int
owusb_com_match_access(owusb_device_t *d, int params, int speed, uint8_t cmd)
{
int index = speed << 8 & cmd;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_MATCH_ACCESS | params, index, NULL, 0, USB_TIMEOUT);
}
/*
* params: NTF, ICP, RST, IM
*/
int
owusb_com_read_straight(owusb_device_t *d, int params, int writelen, int readlen)
{
int p = 0;
if (params & PARAM_NTF) p |= 0x8;
if (params & PARAM_ICP) p |= 0x4;
if (params & PARAM_RST) p |= 0x2;
if (params & PARAM_IM) p |= 0x1;
p |= writelen << 8;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_READ_STRAIGHT | p, readlen, NULL, 0, USB_TIMEOUT);
}
/*
* params: SPU, F, NTF, ICP, R, IM
*/
int
owusb_com_do_and_release(owusb_device_t *d, int params, int len)
{
int cmd = 0x6000 | COM_DO_AND_RELEASE | params;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, cmd, len & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* params: F, NTF, ICP, RST, IM
*/
int
owusb_com_set_path(owusb_device_t *d, int params, int len)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_SET_PATH | params, len & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* params: PS, DT, F, NTF, ICP, IM
*/
int
owusb_com_write_sram_page(owusb_device_t *d, int params, int len)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_WRITE_SRAM_PAGE | params, len & 0xff, NULL, 0, USB_TIMEOUT);
}
/*
* params: DT, F, NTF, ICP, Z, IM
*/
int
owusb_com_write_eprom(owusb_device_t *d, int params, int len)
{
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_WRITE_EPROM | params, len, NULL, 0, USB_TIMEOUT);
}
/*
* params: PS, DT, F, NTF, ICP, IM
*/
int
owusb_com_read_crc_prot_page(owusb_device_t *d, int params, int page_count, int page_size)
{
int index = page_count << 8 | page_size;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_READ_CRC_PROT_PAGE | params, index, NULL, 0, USB_TIMEOUT);
}
/*
* params: F, NTF, ICP, CH, IM
*/
int
owusb_com_read_redirect_page(owusb_device_t *d, int params, int page_number, int page_size)
{
int value = COM_READ_REDIRECT_PAGE | 0x2100 | params;
int index = page_number << 8 | page_size;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, value, index, NULL, 0, USB_TIMEOUT);
}
/*
* params: F, NTF, ICP, RST, IM (RTS, SM)
* cmd: WIRE_CMD_SEARCH_ROM, WIRE_CMD_COND_SEARCH_ROM
*/
int
owusb_com_search_access(owusb_device_t *d, int params, int discrepancy, int noaccess, int device_count, int cmd)
{
int index = device_count << 8 | cmd;
if (discrepancy) params |= PARAM_RTS;
if (noaccess) params |= PARAM_SM;
return usb_control_msg(d->handle, USB_DEVICE_TO_HOST, COMM_CMD, COM_SEARCH_ACCESS | params, index, NULL, 0, USB_TIMEOUT);
}
/*
* Initialize DS2490 device
*
* @param i DS2490 count
* @param dev USB device
*
* Returns: 0 on success, < 0 on failure
*/
static int
owusb_init_dev(int i, struct usb_device *dev)
{
struct usb_dev_handle *h;
h = usb_open(dev);
if (h <= 0) {
return -1;
}
/* Configuration 0: not configured; 1: configured */
if (usb_set_configuration(h, 1) < 0) {
return -2;
}
/* Interface 0 is the only valid interface value for the DS2490 */
if (usb_claim_interface(h, 0) < 0) {
return -3;
}
/* Alternate settings:
* EP poll EP2/3 packet size
* 0: 10ms 16 bytes
* 1: 10ms 64 bytes
* 2: 1ms 16 bytes
* 3: 1ms 64 bytes
*/
if (usb_set_altinterface(h, USB_ALT_INTERFACE) < 0) {
return -4;
}
owusb_devs[i].device = dev;
owusb_devs[i].handle = h;
owusb_devs[i].timeout = USB_TIMEOUT;
owusb_devs[i].interrupt_len = 0;
owusb_devs[i].setting = USB_ALT_INTERFACE;
owusb_ctl_reset(&owusb_devs[i]);
return 0;
}
/*
* High level functions
*/
/*
* Initalize the owusb library. This function must be called before any
* other function.
*
* Returns: 0 on success, < 0 on failure
*/
int
owusb_init(void)
{
int b, d, e;
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
b = usb_find_busses();
d = usb_find_devices();
owusb_dev_count = 0;
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == VENDOR_MAXIM &&
dev->descriptor.idProduct == PRODUCT_2490) {
if ((e = owusb_init_dev(owusb_dev_count++, dev)) != 0) {
return e;
}
}
}
}
return 0;
}
/*
* Finalize the owusb library.
*/
void
owusb_fini(void)
{
}
void
owusb_interrupt_read(owusb_device_t *dev)
{
dev->interrupt_len = usb_interrupt_read(dev->handle,
USB_ENDPOINT_TYPE_ISOCHRONOUS,
(char *)dev->interrupt_data,
INTERRUPT_DATA_LEN,
dev->timeout);
dev->interrupt_count++;
}
int
owusb_write(owusb_device_t *dev, const uint8_t *data, int len)
{
return usb_bulk_write(dev->handle, USB_ENDPOINT_TYPE_BULK,
(char *)data, len, dev->timeout);
}
int
owusb_read(owusb_device_t *dev, uint8_t *data, int len)
{
return usb_bulk_read(dev->handle, USB_ENDPOINT_TYPE_INTERRUPT,
(char *)data, len, dev->timeout);
}
/* Wait for a command to complete */
void
owusb_wait_until_idle(owusb_device_t *dev)
{
owusb_interrupt_read(dev);
while (!owusb_isidle(dev)){
owusb_interrupt_read(dev);
};
}
void
owusb_wait_for_presence(owusb_device_t *dev)
{
owusb_interrupt_read(dev);
while (owusb_result(dev) != 0xa5) {
owusb_interrupt_read(dev);
}
}
int
owusb_datain(owusb_device_t *dev)
{
return dev->interrupt_data[STATE_DATA_IN_BUFFER_STATUS];
}
int
owusb_isidle(owusb_device_t *dev)
{
return dev->interrupt_data[STATE_STATUS_FLAGS] & 0x20;
}
uint16_t
owusb_result(owusb_device_t *dev)
{
int i;
uint16_t result = 0;
for (i = 16; i < dev->interrupt_len; i++) {
if (dev->interrupt_data[STATE_COMBYTE2] == RESULT_DETECT) {
result |= RESULT_XDETECT;
} else {
result |= dev->interrupt_data[i];
}
}
return result;
}
int
owusb_presence_detect(owusb_device_t *dev)
{
uint16_t r;
owusb_interrupt_read(dev);
r = owusb_result(dev);
return r & RESULT_XDETECT;
}
void
owusb_print_state(owusb_device_t *dev)
{
uint8_t *data = dev->interrupt_data;
printf("============================\n");
printf("Enable Flags: %02x\n", data[STATE_ENABLE_FLAGS]);
printf("1-Wire speed: %s\n", ow_speed[data[STATE_1WIRE_SPEED]]);
printf("Strong Pullup Duration: %dms\n", data[STATE_SPU_DURATION] * 16);
printf("Programming Pulse: %dus\n", data[STATE_PROG_PULSE_DURATION] * 8);
printf("Pulldown Slew Rate: %s\n", ow_slew_rate[data[STATE_PULLDOWN_SLEW_RATE_CTRL]]);
printf("Write-1 Low Time: %dus\n", data[STATE_WRITE1_LOW_TIME] + 8);
printf("Data Sample Offset: %dus\n", data[STATE_DSO] + 3);
/* byte 7 reserved */
printf("Status: %02x\n", data[STATE_STATUS_FLAGS]);
printf("Com command: %02x%02x\n", data[STATE_COMBYTE2], data[STATE_COMBYTE1]);
printf("Comstat: %d bytes\n", data[STATE_COMBUFFER_STATUS]);
printf("Dataout: %d bytes\n", data[STATE_DATA_OUT_BUFFER_STATUS]);
printf("Datain: %d bytes\n", data[STATE_DATA_IN_BUFFER_STATUS]);
/* bytes 14 and 15 reserved */
owusb_print_result(dev);
printf("============================\n");
}
void
owusb_print_result(owusb_device_t *dev)
{
int i;
for (i = 0x10; i < dev->interrupt_len; i++) {
printf("Result: %02x\n", dev->interrupt_data[i]);
}
}
int
owusb_search(owusb_device_t *dev, uint8_t type, uint8_t *data, int len)
{
uint8_t zeros[8];
int r;
memset(zeros, 0, 8);
r = owusb_write(dev, zeros, 8);
if (r < 0) {
return r;
}
/* no discrepancy, no access, no device limit */
r = owusb_com_search_access(dev, PARAM_F | PARAM_RST | PARAM_IM, 0, 1, 0, type);
if (r < 0) {
return r;
}
/* Sleep for the reset and eventual first ROM to finish */
usleep(REGULAR_RESET_US);
/* 3 bits for each ROM bit */
usleep(3 * 64 * FLEXIBLE_SLOT_US);
owusb_interrupt_read(dev);
while (!owusb_isidle(dev)) {
/* If are not idle, then there is probably more ROMs to read */
usleep(3 * 64 * FLEXIBLE_SLOT_US);
owusb_interrupt_read(dev);
}
return owusb_read(dev, data, len);
}
int
owusb_search_all(owusb_device_t *dev, uint8_t *data, int len)
{
return owusb_search(dev, 0xf0, data, len);
}
#if 0
static int
highest_bit(uint8_t b)
{
int r = 0;
while (b >>= 1) {
r++;
}
return r;
}
#endif
uint8_t
compare(uint8_t disc, uint8_t addr)
{
uint8_t mask = 0x80;
uint8_t mask2 = 0xff;
while (mask) {
/*printf("%02x %02x\n", mask & disc, mask & addr);*/
if (mask & disc && !(mask & addr)) {
return (disc & addr & mask2) | mask;
}
mask >>= 1;
mask2 >>= 1;
}
return 0;
}
/* Return 1 if more devices, 0 if none */
int
owusb_search_next(owusb_device_t *dev, uint8_t *data)
{
uint8_t disc[16];
int r;
int i;
uint8_t b;
int set = 0;
if (dev->search_stop) {
return 0;
}
r = owusb_write(dev, dev->discrepancy, 8);
if (r < 0) {
return 0;
}
/* discrepancy, access, 1 device */
r = owusb_com_search_access(dev, PARAM_F | PARAM_RST | PARAM_IM, 1, 1, 1, dev->search_cmd);
if (r < 0) {
return 0;
}
usleep(REGULAR_RESET_US + 3 * 64 * FLEXIBLE_SLOT_US + 100);
/*owusb_interrupt_read(dev);*/
r = owusb_read(dev, disc, 16);
if (r < 8) {
dev->search_stop = 1;
return 0;
}
memcpy(data, disc, 8);
if (r == 16) {
for (i = 7; i >= 0; i--) {
/*printf("disc: %02x\n", disc[i]);*/
if (disc[i] && !set) {
b = compare(disc[i + 8], disc[i]);
if (b) {
set = 1;
dev->discrepancy[i] = b;
continue;
}
}
if (set) {
dev->discrepancy[i] = disc[i] & disc[i + 8];
} else {
dev->discrepancy[i] = 0;
}
}
} else {
dev->search_stop = 1;
}
return 1;
}
int
owusb_search_first(owusb_device_t *dev, uint8_t type, uint8_t *data)
{
dev->search_cmd = type;
dev->search_stop = 0;
memset(dev->discrepancy, 0, 8);
dev->last_bit = 0;
dev->last_byte = 0;
return owusb_search_next(dev, data);
}