annotate onewire.c @ 166:5d5424acfed0

- use the fridge temperature for control too, keep it in a 6deg band - ignore params beginning with _
author Matt Johnston <matt@ucc.asn.au>
date Mon, 18 Feb 2013 23:31:32 +0800
parents 54d369e3d689
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 Access Dallas 1-Wire Devices with ATMEL AVRs
54d369e3d689 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)
54d369e3d689 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)
54d369e3d689 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
54d369e3d689 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
54d369e3d689 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,
54d369e3d689 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
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 via ow_command_skip_last_recovery
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 */
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #include <avr/io.h>
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #include <util/delay.h>
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #include <util/atomic.h>
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #include "onewire.h"
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #ifdef OW_ONE_BUS
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
54d369e3d689 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))
54d369e3d689 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)) )
54d369e3d689 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) )
54d369e3d689 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 )) )
54d369e3d689 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) )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #else
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 /* set bus-config with ow_set_bus() */
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 uint8_t OW_PIN_MASK;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 volatile uint8_t* OW_IN;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 volatile uint8_t* OW_OUT;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 volatile uint8_t* OW_DDR;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
54d369e3d689 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 )
54d369e3d689 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 )
54d369e3d689 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 )
54d369e3d689 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 )
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 void ow_set_bus(volatile uint8_t* in,
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 volatile uint8_t* out,
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 volatile uint8_t* ddr,
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 uint8_t pin)
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 OW_DDR=ddr;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 OW_OUT=out;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 OW_IN=in;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 OW_PIN_MASK = (1 << pin);
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 ow_reset();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 uint8_t ow_input_pin_state()
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return OW_GET_IN();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 void ow_parasite_enable(void)
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 OW_OUT_HIGH();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 OW_DIR_OUT();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 void ow_parasite_disable(void)
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 OW_DIR_IN();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #if (!OW_USE_INTERNAL_PULLUP)
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 OW_OUT_LOW();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 uint8_t ow_reset(void)
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 uint8_t err;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 OW_OUT_LOW();
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 _delay_us(480);
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 OW_DIR_IN(); // input
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #if OW_USE_INTERNAL_PULLUP
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 OW_OUT_HIGH();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 _delay_us(64); // was 66
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 err = OW_GET_IN(); // no presence detect
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
54d369e3d689 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
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 _delay_us(480 - 64); // was 480-66
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if( OW_GET_IN() == 0 ) {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 return err;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
54d369e3d689 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):
54d369e3d689 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
54d369e3d689 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
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 to achive a 15uS overall delay
54d369e3d689 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
54d369e3d689 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) */
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #if OW_USE_INTERNAL_PULLUP
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 OW_OUT_LOW();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 OW_DIR_OUT(); // drive bus low
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 if ( b ) {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #if OW_USE_INTERNAL_PULLUP
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 OW_OUT_HIGH();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
54d369e3d689 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
54d369e3d689 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
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 // the start of the slot."
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 _delay_us(15-2-OW_CONF_DELAYOFFSET);
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 if( OW_GET_IN() == 0 ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 b = 0; // sample at end of read-timeslot
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 _delay_us(60-15-2+OW_CONF_DELAYOFFSET);
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #if OW_USE_INTERNAL_PULLUP
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 OW_OUT_HIGH();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 #endif
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 OW_DIR_IN();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 if ( with_parasite_enable ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 ow_parasite_enable();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 } /* ATOMIC_BLOCK */
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 return b;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 uint8_t ow_bit_io( uint8_t b )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 return ow_bit_io_intern( b & 1, 0 );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 uint8_t ow_byte_wr( uint8_t b )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 uint8_t i = 8, j;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 do {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 j = ow_bit_io( b & 1 );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 b >>= 1;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if( j ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 b |= 0x80;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 } while( --i );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 return b;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 uint8_t i = 8, j;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 do {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 if ( i != 1 ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 j = ow_bit_io_intern( b & 1, 0 );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 } else {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 j = ow_bit_io_intern( b & 1, 1 );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 b >>= 1;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 if( j ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 b |= 0x80;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 } while( --i );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 return b;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 uint8_t ow_byte_rd( void )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 // after the init low-pulse in every slot
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 return ow_byte_wr( 0xFF );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 uint8_t i, j, next_diff;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 uint8_t b;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 if( ow_reset() ) {
54d369e3d689 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!
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
54d369e3d689 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
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 i = OW_ROMCODE_SIZE * 8; // 8 bytes
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 do {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 j = 8; // 8 bits
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 do {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 b = ow_bit_io( 1 ); // read bit
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 if( ow_bit_io( 1 ) ) { // read complement bit
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 if( b ) { // 0b11
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 return OW_DATA_ERR; // data error <--- early exit!
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 else {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 if( !b ) { // 0b00 = 2 devices
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 if( diff > i || ((*id & 1) && diff != i) ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 b = 1; // now 1
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 next_diff = i; // next pass 0
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 ow_bit_io( b ); // write bit
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 *id >>= 1;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if( b ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 *id |= 0x80; // store bit
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 i--;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 } while( --j );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 id++; // next byte
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 } while( i );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 return next_diff; // to continue search
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 uint8_t i;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 ow_reset();
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 if( id ) {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 i = OW_ROMCODE_SIZE;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 do {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 ow_byte_wr( *id );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 id++;
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 } while( --i );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 else {
54d369e3d689 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
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 if ( with_parasite_enable ) {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 ow_byte_wr_with_parasite_enable( command );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 } else {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 ow_byte_wr( command );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 ow_command_intern( command, id, 0);
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282
54d369e3d689 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 )
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 {
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 ow_command_intern( command, id, 1 );
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 }
54d369e3d689 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287