annotate onewire.c @ 371:7dec59b7eeef

cheap_daemon()
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 Jun 2012 21:36:54 +0800
parents 0a64532c1de1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 Access Dallas 1-Wire Devices with ATMEL AVRs
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 Author of the initial code: Peter Dannegger (danni(at)specs.de)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 modified by Martin Thomas (mthomas(at)rhrk.uni-kl.de)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 9/2004 - use of delay.h, optional bus configuration at runtime
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 10/2009 - additional delay in ow_bit_io for recovery
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 5/2010 - timing modifcations, additonal config-values and comments,
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use of atomic.h macros, internal pull-up support
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 7/2010 - added method to skip recovery time after last bit transfered
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 via ow_command_skip_last_recovery
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 */
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #include <avr/io.h>
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #include <util/delay.h>
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #include <util/atomic.h>
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #include "onewire.h"
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #ifdef OW_ONE_BUS
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #define OW_GET_IN() ( OW_IN & (1<<OW_PIN))
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #define OW_OUT_LOW() ( OW_OUT &= (~(1 << OW_PIN)) )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 #define OW_OUT_HIGH() ( OW_OUT |= (1 << OW_PIN) )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #define OW_DIR_IN() ( OW_DDR &= (~(1 << OW_PIN )) )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #define OW_DIR_OUT() ( OW_DDR |= (1 << OW_PIN) )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #else
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 /* set bus-config with ow_set_bus() */
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 uint8_t OW_PIN_MASK;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 volatile uint8_t* OW_IN;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 volatile uint8_t* OW_OUT;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 volatile uint8_t* OW_DDR;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #define OW_GET_IN() ( *OW_IN & OW_PIN_MASK )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define OW_OUT_LOW() ( *OW_OUT &= (uint8_t) ~OW_PIN_MASK )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define OW_OUT_HIGH() ( *OW_OUT |= (uint8_t) OW_PIN_MASK )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define OW_DIR_IN() ( *OW_DDR &= (uint8_t) ~OW_PIN_MASK )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define OW_DIR_OUT() ( *OW_DDR |= (uint8_t) OW_PIN_MASK )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 void ow_set_bus(volatile uint8_t* in,
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 volatile uint8_t* out,
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 volatile uint8_t* ddr,
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 uint8_t pin)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 OW_DDR=ddr;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 OW_OUT=out;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 OW_IN=in;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 OW_PIN_MASK = (1 << pin);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 ow_reset();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 uint8_t ow_input_pin_state()
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return OW_GET_IN();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 void ow_parasite_enable(void)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 OW_OUT_HIGH();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 OW_DIR_OUT();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 void ow_parasite_disable(void)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 OW_DIR_IN();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #if (!OW_USE_INTERNAL_PULLUP)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 OW_OUT_LOW();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 uint8_t ow_reset(void)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 uint8_t err;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 OW_OUT_LOW();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 OW_DIR_OUT(); // pull OW-Pin low for 480us
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 _delay_us(480);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 // set Pin as input - wait for clients to pull low
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 OW_DIR_IN(); // input
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #if OW_USE_INTERNAL_PULLUP
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 OW_OUT_HIGH();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 _delay_us(64); // was 66
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 err = OW_GET_IN(); // no presence detect
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 // if err!=0: nobody pulled to low, still high
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 // after a delay the clients should release the line
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 // and input-pin gets back to high by pull-up-resistor
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 _delay_us(480 - 64); // was 480-66
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if( OW_GET_IN() == 0 ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 err = 1; // short circuit, expected low but got high
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 return err;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 /* Timing issue when using runtime-bus-selection (!OW_ONE_BUS):
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 The master should sample at the end of the 15-slot after initiating
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 the read-time-slot. The variable bus-settings need more
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 cycles than the constant ones so the delays had to be shortened
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 to achive a 15uS overall delay
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 Setting/clearing a bit in I/O Register needs 1 cyle in OW_ONE_BUS
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 but around 14 cyles in configureable bus (us-Delay is 4 cyles per uS) */
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 static uint8_t ow_bit_io_intern( uint8_t b, uint8_t with_parasite_enable )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #if OW_USE_INTERNAL_PULLUP
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 OW_OUT_LOW();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 OW_DIR_OUT(); // drive bus low
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 _delay_us(2); // T_INT > 1usec accoding to timing-diagramm
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 if ( b ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 OW_DIR_IN(); // to write "1" release bus, resistor pulls high
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #if OW_USE_INTERNAL_PULLUP
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 OW_OUT_HIGH();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 // "Output data from the DS18B20 is valid for 15usec after the falling
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 // edge that initiated the read time slot. Therefore, the master must
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 // release the bus and then sample the bus state within 15ussec from
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 // the start of the slot."
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 _delay_us(15-2-OW_CONF_DELAYOFFSET);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 if( OW_GET_IN() == 0 ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 b = 0; // sample at end of read-timeslot
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 _delay_us(60-15-2+OW_CONF_DELAYOFFSET);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #if OW_USE_INTERNAL_PULLUP
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 OW_OUT_HIGH();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 #endif
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 OW_DIR_IN();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 if ( with_parasite_enable ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 ow_parasite_enable();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 } /* ATOMIC_BLOCK */
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 _delay_us(OW_RECOVERY_TIME); // may be increased for longer wires
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 return b;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 uint8_t ow_bit_io( uint8_t b )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 return ow_bit_io_intern( b & 1, 0 );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 uint8_t ow_byte_wr( uint8_t b )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 uint8_t i = 8, j;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 do {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 j = ow_bit_io( b & 1 );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 b >>= 1;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if( j ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 b |= 0x80;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 } while( --i );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 return b;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 uint8_t ow_byte_wr_with_parasite_enable( uint8_t b )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 uint8_t i = 8, j;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 do {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 if ( i != 1 ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 j = ow_bit_io_intern( b & 1, 0 );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 } else {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 j = ow_bit_io_intern( b & 1, 1 );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 b >>= 1;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 if( j ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 b |= 0x80;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 } while( --i );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 return b;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 uint8_t ow_byte_rd( void )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 // read by sending only "1"s, so bus gets released
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 // after the init low-pulse in every slot
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 return ow_byte_wr( 0xFF );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 uint8_t ow_rom_search( uint8_t diff, uint8_t *id )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 uint8_t i, j, next_diff;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 uint8_t b;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 if( ow_reset() ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 return OW_PRESENCE_ERR; // error, no device found <--- early exit!
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 ow_byte_wr( OW_SEARCH_ROM ); // ROM search command
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 next_diff = OW_LAST_DEVICE; // unchanged on last device
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 i = OW_ROMCODE_SIZE * 8; // 8 bytes
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 do {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 j = 8; // 8 bits
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 do {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 b = ow_bit_io( 1 ); // read bit
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 if( ow_bit_io( 1 ) ) { // read complement bit
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 if( b ) { // 0b11
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 return OW_DATA_ERR; // data error <--- early exit!
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 else {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 if( !b ) { // 0b00 = 2 devices
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 if( diff > i || ((*id & 1) && diff != i) ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 b = 1; // now 1
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 next_diff = i; // next pass 0
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 ow_bit_io( b ); // write bit
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 *id >>= 1;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if( b ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 *id |= 0x80; // store bit
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 i--;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 } while( --j );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 id++; // next byte
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 } while( i );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 return next_diff; // to continue search
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 static void ow_command_intern( uint8_t command, uint8_t *id, uint8_t with_parasite_enable )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 uint8_t i;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 ow_reset();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 if( id ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 ow_byte_wr( OW_MATCH_ROM ); // to a single device
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 i = OW_ROMCODE_SIZE;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 do {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 ow_byte_wr( *id );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 id++;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 } while( --i );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 else {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 ow_byte_wr( OW_SKIP_ROM ); // to all devices
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 if ( with_parasite_enable ) {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 ow_byte_wr_with_parasite_enable( command );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 } else {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 ow_byte_wr( command );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 void ow_command( uint8_t command, uint8_t *id )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 ow_command_intern( command, id, 0);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 void ow_command_with_parasite_enable( uint8_t command, uint8_t *id )
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 ow_command_intern( command, id, 1 );
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287