-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonewire0.c
777 lines (621 loc) · 17.3 KB
/
onewire0.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
/* vim:sw=4:ts=4:
** 1-wire protocol library for ATTiny85
** (C) 2011, Nick Andrew <nick@nick-andrew.net>
** All Rights Reserved.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include "maxim-crc8.h"
/*
** ---------------------------------------------------------------------------
** Pin and speed definitions
** ---------------------------------------------------------------------------
*/
// Specify the single I/O pin
#ifndef PIN
#define PIN ( 1 << PORTB4 )
#endif
// Strong pullup is on PORTB1 (active low)
#ifndef ONEWIRE_STRONG_PIN
#define ONEWIRE_STRONG_PIN (PORTB1)
#endif
#ifndef CPU_FREQ
#define CPU_FREQ 8000000
#endif
#if CPU_FREQ == 8000000
// Prescaler CLKio/8 = 1 us resolution
#define PRESCALER ( 1<<CS01 )
// When resetting the devices, use CLKio/64 (8 us resolution)
#define RESET_PRESCALER ( 0<<CS02 | 1<<CS01 | 1<<CS00 )
// For long delays, use CLKio/1024 (32 us resolution)
#define DELAY_PRESCALER ( 1<<CS02 | 0<<CS01 | 1<<CS00 )
// When device is idle, interrupt every IDLE_DELAY us
#define IDLE_DELAY 20
#else
#error "Only CPU_FREQ of 8 MHz is presently supported"
#endif
#include "onewire0.h"
struct onewire onewire0;
struct onewire_search search0;
static void _starttimer(void)
{
// Setup timer0
GTCCR |= (1<<TSM | 1<<PSR0); // Disable the timer for programming
// Enable CTC mode (mode 2); TCNT0 counts from 0 to OCR0A inclusive
TCCR0A |= ( 1<<WGM01 );
// Setup Clock Select = 2 (1<<CS01) for clkIO/8
TCCR0B = PRESCALER;
// Start counting from 0
TCNT0 = 0;
// Initially, interrupt once every 20us
OCR0A = IDLE_DELAY - 1;
// OCR0B is not used
OCR0B = 0xff;
// Enable interrupt on Compare Match A
TIMSK |= ( 1<<OCIE0A );
// Clear any pending timer interrupt
TIFR |= ( 1<<OCF0A );
// Start the timer
GTCCR &= ~( 1<<TSM );
}
inline void _medtimer(void)
{
// Halt the counter for a moment to reconfigure
GTCCR |= ( 1<<TSM | 1<<PSR0 );
TCCR0B = (TCCR0B & 0xf8) | RESET_PRESCALER;
// Reset counter, so start counting from the moment the timer is re-enabled
TCNT0 = 0;
OCR0A = onewire0.ocr0a;
// Resume counting
GTCCR &= ~( 1<<TSM );
}
inline void _delaytimer(void)
{
// Halt the counter for a moment to reconfigure
GTCCR |= ( 1<<TSM | 1<<PSR0 );
TCCR0B = (TCCR0B & 0xf8) | DELAY_PRESCALER;
// Reset counter, so start counting from the moment the timer is re-enabled
TCNT0 = 0;
// 256 counts per interrupt
OCR0A = 255;
// Resume counting
GTCCR &= ~( 1<<TSM );
}
/*
** If the timer is not already in fast mode (found by checking the
** prescaler) then halt the timer, reconfigure it in fast mode,
** reset the counter then resume counting.
**
** The conditional reset logic is used to ensure that an already-fast
** timer need not be stopped and restarted, which would introduce a
** small delay.
*/
inline void _fasttimer(void)
{
if ((TCCR0B & 0x07) != PRESCALER) {
// Halt the counter for a moment to reconfigure
GTCCR |= 1<<TSM | 1<<PSR0;
TCCR0B = (TCCR0B & 0xf8) | PRESCALER;
// Reset counter, so start counting from the moment the timer is re-enabled
TCNT0 = 0;
// Resume counting
GTCCR &= ~( 1<<TSM );
}
}
// Get the value of a bit in a multi-byte array.
// Bit numbers start from 1, as used here:
// http://www.maxim-ic.com/app-notes/index.mvp/id/187
static uint8_t _getbit(volatile uint8_t *cp, uint8_t bit_id)
{
uint8_t bit_mask;
bit_id --;
// Point to the containing byte
cp += (bit_id >> 3);
bit_mask = 1 << (bit_id & 0x07);
if (*cp & bit_mask) {
return 1;
}
return 0;
}
// Set/Clear the value of a bit in a multi-byte array.
// Bit numbers start from 1.
static void _setbit(volatile uint8_t *cp, uint8_t bit_id, uint8_t value)
{
uint8_t bit_mask;
bit_id --;
// Point to the containing byte
cp += (bit_id >> 3);
bit_mask = 1 << (bit_id & 0x07);
if (value) {
*cp |= bit_mask;
} else {
*cp &= ~bit_mask;
}
}
// Set a strong pullup on the 1-wire bus (active low)
inline void _enable_strong(void)
{
PORTB &= ~(1 << ONEWIRE_STRONG_PIN);
}
// Disable a strong pullup (active low)
inline void _disable_strong(void) {
PORTB |= (1 << ONEWIRE_STRONG_PIN);
}
// Reset search
static inline void _resetsearch(void)
{
search0.last_discrepancy = 0;
search0.last_family_discrepancy = 0;
search0.last_device_flag = 0;
for (uint8_t i = 0; i < 8; ++i) {
search0.device_id[i] = 0;
}
}
void onewire0_init(void)
{
onewire0.state = OW0_IDLE;
onewire0.process = OW0_PIDLE;
_resetsearch();
// Setup pullup pin, mode output, initially disabled
DDRB |= (1 << ONEWIRE_STRONG_PIN);
_disable_strong();
// Setup I/O pin, initial tri-state, when enabled output low
DDRB &= ~( PIN ); // Set pin mode to input
PORTB &= ~( PIN ); // Disable weak pullup
_starttimer();
}
inline void _release(void)
{
_disable_strong();
PORTB &= ~( PIN ); // Disable weak pullup
DDRB &= ~( PIN ); // Set pin mode to input
}
inline void _pulllow(void)
{
_disable_strong();
// PORTB is expected to be low at this point
DDRB |= PIN;
}
static void _wait(void)
{
while (onewire0.state != OW0_IDLE) { }
}
static void _writebit(uint8_t value)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.current_byte = value ? 1 : 0;
onewire0.bit_id = 1;
onewire0.state = OW0_START;
}
static uint8_t _readbit(void)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.current_byte = 1; // Write a 1 bit to sample input
onewire0.bit_id = 1;
onewire0.state = OW0_START;
while (onewire0.state != OW0_IDLE) { }
return (onewire0.current_byte & 0x80) ? 1 : 0;
}
// Start to write 8 bits.
// Wait for device to be idle before starting.
// Do not wait for the bits to be sent.
static void _write8(uint8_t byte)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.current_byte = byte;
onewire0.bit_id = 8;
onewire0.state = OW0_START;
}
// Start to read 8 bits.
// Wait for device to be idle before starting.
// Do not wait for the bits to arrive.
static void _read8(void)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.current_byte = 0xff; // Write all 1-bits to sample input 8 times
onewire0.bit_id = 8;
onewire0.state = OW0_START;
}
// Start to read 2 bits.
// Wait for device to be idle before starting.
// Do not wait for the bits to arrive.
static void _read2(void)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.current_byte = 0xff; // Write all 1-bits to sample input 2 times
onewire0.bit_id = 2;
onewire0.state = OW0_START;
}
/* void onewire0_writebyte(uint8_t byte)
**
** Write 8 bits to the bus.
** This function takes from 505us to 560us depending on idle state.
** It returns from 10us to 55us before the next time slot starts.
*/
void onewire0_writebyte(uint8_t byte)
{
_write8(byte);
}
/* uint8_t onewire0_readbyte()
**
** Read 8 bits from the bus and return the byte value.
** This function takes from 505us to 560us depending on idle state.
** It returns soon after sampling the last data bit, before the
** end of the last time slot.
*/
uint8_t onewire0_readbyte(void)
{
_read8();
while (onewire0.state != OW0_IDLE) { }
return onewire0.current_byte;
}
/* void onewire0_reset(void)
**
** Reset devices on the bus. Wait for the reset process to complete.
** Return 1 if there are devices on the bus, else 0.
*/
uint8_t onewire0_reset(void)
{
while (onewire0.state != OW0_IDLE) { }
onewire0.state = OW0_RESET;
while (onewire0.state != OW0_IDLE) { }
return (onewire0.current_byte & 0x80) ? 0 : 1;
}
/* void onewire0_delay1(uint8_t ocr0a, uint16_t usec1)
**
** Setup a delay for some number of microseconds.
** The delay = (ocr0a + 1) x usec1 x 1 us.
** The state will become OW0_IDLE after the programmed delay, but there
** will be another 20 us until the next interrupt. So take that into
** consideration.
**
** ocr0a = 0..255 as the number of counts per interrupt (minus 1)
** Values of ocr0a less than 10 or so won't work, as the interrupt handler
** takes around 10 us to process each interrupt.
** usec1 = 0..65535 as the number of interrupts (0 means 65536).
**
** Maximum sleep time = 65536 x 256 x 1 us = 16.777216 seconds.
*/
void onewire0_delay1(uint8_t ocr0a, uint16_t usec1) {
while (onewire0.state != OW0_IDLE) { }
onewire0.ocr0a = ocr0a;
onewire0.delay_count = usec1;
onewire0.state = OW0_DELAY1US;
}
/* void onewire0_delay8(uint8_t ocr0a, uint16_t usec8)
**
** Setup a delay for some number of microseconds.
** The delay = (ocr0a + 1) x usec8 x 8 us.
** The state will become OW0_IDLE after the programmed delay, but there
** will be another 20 us until the next interrupt. So take that into
** consideration.
**
** ocr0a = 0..255 as the number of counts per interrupt (minus 1)
** Values of ocr0a less than 2 won't work, as the interrupt handler
** takes around 10 us to process each interrupt.
** usec8 = 0..65535 as the number of interrupts (0 means 65536).
**
** Maximum sleep time = 65536 x 256 x 8 us = ~134 seconds.
*/
void onewire0_delay8(uint8_t ocr0a, uint16_t usec8) {
while (onewire0.state != OW0_IDLE) { }
onewire0.ocr0a = ocr0a;
onewire0.delay_count = usec8;
onewire0.state = OW0_DELAY8US;
}
/* void onewire0_delay128(uint8_t ocr0a, uint16_t usec128)
**
** Setup a delay for some number of microseconds.
** The delay = (ocr0a + 1) x usec128 x 128 us.
** The state will become OW0_IDLE after the programmed delay, but there
** will be another 20 us until the next interrupt. So take that into
** consideration.
**
** ocr0a = 0..255 as the number of counts per interrupt (minus 1)
** usec128 = 0..65535 as the number of interrupts (0 means 65536).
**
** Maximum sleep time = 65536 x 256 x 128 us = ~2147 seconds.
*/
void onewire0_delay128(uint8_t ocr0a, uint16_t usec128) {
while (onewire0.state != OW0_IDLE) { }
onewire0.ocr0a = ocr0a;
onewire0.delay_count = usec128;
onewire0.state = OW0_DELAY128US;
}
/* uint8_t onewire0_search(void)
**
** Initiate a 1wire device number search algorithm,
** and find the first 64-bit address.
**
** Return 1 if a device was found, 0 if no device.
*/
uint8_t onewire0_search(void)
{
uint8_t i;
uint8_t id_bit_number = 1;
if (!onewire0_reset() || search0.last_device_flag) {
_resetsearch();
return 0;
}
search0.last_zero = 0;
onewire0_writebyte(0xf0);
while (id_bit_number <= 64) {
uint8_t search_direction;
_read2();
_wait();
// Pick top 2 bits of this byte only
// bit 7 = cmp_id_bit
// bit 6 = id_bit
i = onewire0.current_byte & 0xc0;
// if id_bit == cmp_id_bit == 1
if (i == 0xc0) {
// No device found
_resetsearch();
return 0;
}
if (i == 0x00) {
if (id_bit_number == search0.last_discrepancy) {
search_direction = 1;
}
else if (id_bit_number > search0.last_discrepancy) {
search_direction = 0;
}
else {
// Set search_direction bit to id_bit_number bit in ROM_NO
search_direction = _getbit(search0.device_id, id_bit_number);
}
if (search_direction == 0) {
search0.last_zero = id_bit_number;
if (search0.last_zero < 9) {
search0.last_family_discrepancy = search0.last_zero;
}
}
} else {
search_direction = (i & 0x40) ? 1 : 0;
}
_setbit(search0.device_id, id_bit_number, search_direction);
_writebit(search_direction);
id_bit_number ++;
}
search0.last_discrepancy = search0.last_zero;
if (search0.last_discrepancy == 0) {
search0.last_device_flag = 1;
}
if (onewire0_check_crc((uint8_t *) search0.device_id, sizeof(search0.device_id))) {
// Device ID fails CRC check; start over
_resetsearch();
return 0;
}
return 1;
}
/* void onewire0_poll(void)
**
** Fast poll function.
** If the bus is busy, just return.
** If the bus is idle, then run more protocol if possible.
*/
void onewire0_poll(void)
{
if (onewire0.state != OW0_IDLE) {
// Still going
return;
}
switch(onewire0.process) {
case OW0_PIDLE:
// Do nothing
break;
}
}
// Prepare for the next bit of I/O.
// If we're processing a byte, then go to state OW0_START for the next bit.
// Otherwise, enter idle state upon next interrupt.
static inline void _nextbit(void) {
// Perform the next action in the meta-process
if (--onewire0.bit_id) {
// Continue reading/writing a byte with the next bit
onewire0.state = OW0_START;
} else {
// The next state will be idle unless mainline code changes it
// before the next interrupt (e.g. more bytes to send).
onewire0.state = OW0_IDLE;
}
}
// Interrupt routine for timer0, OCR0A
ISR(TIMER0_COMPA_vect)
{
switch(onewire0.state) {
case OW0_IDLE:
// Wait 20us until the next interrupt
OCR0A = IDLE_DELAY - 1;
break;
case OW0_START:
_pulllow();
if (onewire0.current_byte & 1) {
uint8_t delay;
// Write a 1-bit or read a bit:
// 6us low, 9us wait, sample, 55us high
OCR0A = 70 - 1;
// Delay 15 us within the interupt function:
// 6 us signal low (48 instruction times)
// This loop takes 5 x N instruction times
for (delay = 0; delay < 8; ++delay) {
asm("nop");
}
// 9 us tri-state
_release();
for (delay = 0; delay < 14; ++delay) {
asm("nop");
}
// shift byte then sample the signal
onewire0.current_byte = (onewire0.current_byte >> 1) | ((PINB & (PIN)) ? 0x80 : 0);
_nextbit();
} else {
// Write a 0-bit
// 60us low, 10us high
OCR0A = GAP_C - 1;
onewire0.state = OW0_RELEASE;
onewire0.current_byte >>= 1;
}
break;
case OW0_READWAIT:
// Let the signal go high, wait 9us then sample.
_release();
OCR0A = GAP_E - 1;
onewire0.state = OW0_SAMPLE;
break;
case OW0_SAMPLE:
// Bits are read from 0 to 7, which means we
// have to shift current_byte down and store in bit 7
// Shifting is done in state OW0_START so no need to do it again here.
onewire0.current_byte |= ((PINB & (PIN)) ? 0x80 : 0);
OCR0A = GAP_F - 1;
_nextbit();
break;
case OW0_RELEASE:
// Let the signal go high for 10us.
_release();
OCR0A = GAP_D + 5 - 1;
_nextbit();
break;
case OW0_RESET:
// Pull the bus down and wait 480us (slow down the prescaler)
_pulllow();
onewire0.ocr0a = GAP_H - 1;
_medtimer();
onewire0.state = OW0_RESET1;
break;
case OW0_RESET1:
// Release the bus, speed up the prescaler and wait for 9us
_release();
OCR0A = GAP_I - 1;
onewire0.state = OW0_RESET2;
break;
case OW0_RESET2:
// Sample the bus, slow the prescaler down again and wait 408us
onewire0.current_byte = ((PINB & (PIN)) ? 0x80 : 0);
onewire0.ocr0a = GAP_J - 1;
_medtimer();
onewire0.state = OW0_RESET3;
break;
case OW0_RESET3:
// Speed up the prescaler again, go to idle state with 20us between interrupts
OCR0A = IDLE_DELAY - 1;
_fasttimer();
onewire0.state = OW0_IDLE;
break;
case OW0_DELAY1US:
OCR0A = onewire0.ocr0a;
// The timer is assumed to already be in fast mode
onewire0.state = OW0_DELAY;
break;
case OW0_DELAY8US:
// Setup timer to interrupt every 8 us x (ocr0a + 1), then enter delay loop
_medtimer();
onewire0.state = OW0_DELAY;
break;
case OW0_DELAY128US:
// Setup timer to interrupt every 128 us x (ocr0a + 1), then enter delay loop
_delaytimer();
onewire0.state = OW0_DELAY;
break;
case OW0_DELAY:
if (! --onewire0.delay_count) {
// Delay is finished; setup the next interrupt in 20 us
OCR0A = IDLE_DELAY - 1;
_fasttimer();
onewire0.state = OW0_IDLE;
}
break;
case OW0_DELAY_END:
onewire0.state = OW0_IDLE;
break;
case OW0_CONVERT:
// Program a 750 ms delay
// 750ms = 1 us * 240 * 3125
// 1000ms = 1 us * 250 * 4000
OCR0A = 249;
onewire0.delay_count = 4000;
_enable_strong();
onewire0.state = OW0_CONVERT_DELAY;
break;
case OW0_CONVERT_DELAY:
if (! --onewire0.delay_count) {
// Delay is finished; setup the next interrupt in 20 us
OCR0A = IDLE_DELAY - 1;
_release();
onewire0.state = OW0_IDLE;
}
break;
}
// Return from interrupt
}
uint8_t onewire0_isidle(void) {
return (onewire0.state == OW0_IDLE);
}
/* Return current device state. This is used for debugging.
*/
uint8_t onewire0_state(void) {
return onewire0.state;
}
/*
** High Level Functions
*/
void onewire0_readrom(struct onewire_id *buf) {
uint8_t *cp = buf->device_id;
uint8_t byte_id;
onewire0_writebyte(0x33);
for (byte_id = 0; byte_id < 8; ++byte_id) {
*cp++ = onewire0_readbyte();
}
}
void onewire0_matchrom(struct onewire_id *dev) {
uint8_t *buf = dev->device_id;
uint8_t byte_id;
onewire0_writebyte(0x55);
for (byte_id = 0; byte_id < 8; ++byte_id) {
onewire0_writebyte(*buf++);
}
}
void onewire0_skiprom(void) {
onewire0_writebyte(0xcc);
}
void onewire0_readscratchpad(void) {
onewire0_writebyte(0xbe);
}
// Issue 0x44, "Convert T".
void onewire0_convert(void) {
onewire0_writebyte(0x44);
}
void onewire0_convertdelay(void) {
while (onewire0.state != OW0_IDLE) { }
// Start the strong pullup (will be reset on next call to _pulllow)
_enable_strong();
// Start a 750 ms delay, with a strong pullup to power the chips
onewire0.state = OW0_CONVERT;
}
void onewire0_writescratch(char *scratch) {
uint8_t byte_id;
onewire0_writebyte(0x4e);
for (byte_id = 0; byte_id < 3; ++byte_id) {
onewire0_writebyte(*scratch++);
}
}
uint8_t onewire0_readpower(void) {
onewire0_writebyte(0xb4);
return _readbit();
}
uint8_t onewire0_get_family_code(struct onewire_id *dev) {
return dev->device_id[0];
}
/* Check an array of bytes using the Maxim 8-bit CRC algorithm.
** The last byte must be the expected CRC to make the final
** result zero.
*/
uint8_t onewire0_check_crc(uint8_t *cp, uint8_t length) {
uint8_t crc = 0x00;
while (length--) {
crc = crc8_update(crc, *cp++);
}
return crc;
}