-
Notifications
You must be signed in to change notification settings - Fork 0
/
onewire.c
208 lines (171 loc) · 4.71 KB
/
onewire.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
#include "onewire.h"
/*
The MIT License
Copyright (c) 2014 Kim Ebert ([email protected])
Copyright (c) 2010-2012 David Siroky ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
typedef struct {
volatile uint8_t *port_out;
const volatile uint8_t *port_in;
volatile uint8_t *port_ren;
volatile uint8_t *port_dir;
int pin;
} onewire_t;
inline void onewire_line_low(onewire_t *ow)
{
*(ow->port_dir) |= ow->pin;
*(ow->port_out) &= ~ow->pin;
*(ow->port_ren) &= ~ow->pin;
}
inline void onewire_line_high(onewire_t *ow)
{
*(ow->port_dir) |= ow->pin;
*(ow->port_out) |= ow->pin;
*(ow->port_ren) &= ~ow->pin;
}
inline void onewire_line_release(onewire_t *ow)
{
*(ow->port_dir) &= ~ow->pin; // input
*(ow->port_ren) |= ow->pin;
*(ow->port_out) |= ow->pin; // internal resistor pullup
}
/// @return: 0 if ok
int onewire_reset(onewire_t *ow)
{
onewire_line_low(ow);
sleepMicro(550); // 480us minimum
onewire_line_release(ow);
sleepMicro(70); // slave waits 15-60us
if (*(ow->port_in) & ow->pin) return 1; // line should be pulled down by slave
sleepMicro(300); // slave TX presence pulse 60-240us
if (!(*(ow->port_in) & ow->pin)) return 2; // line should be "released" by slave
return 0;
}
void onewire_write_bit(onewire_t *ow, int bit)
{
sleepMicro(2); // recovery, min 1us
onewire_line_low(ow);
if (bit)
sleepMicro(5); // max 15us
else
sleepMicro(55); // min 60us
onewire_line_release(ow);
// rest of the write slot
if (bit)
sleepMicro(55);
else
sleepMicro(5);
}
int onewire_read_bit(onewire_t *ow)
{
int bit;
sleepMicro(1); // recovery, min 1us
onewire_line_low(ow);
sleepMicro(5); // hold min 1us
onewire_line_release(ow);
sleepMicro(10); // 15us window
bit = *(ow->port_in) & ow->pin;
sleepMicro(50); // rest of the read slot
return bit;
}
void onewire_write_byte(onewire_t *ow, uint8_t byte)
{
int i;
for(i = 0; i < 8; i++)
{
onewire_write_bit(ow, byte & 1);
byte >>= 1;
}
}
uint8_t onewire_read_byte(onewire_t *ow)
{
int i;
uint8_t byte = 0;
for(i = 0; i < 8; i++)
{
byte >>= 1;
if (onewire_read_bit(ow)) byte |= 0x80;
}
return byte;
}
/*
* Returns temperature in C * 16.
*/
int get_ext_temp_c()
{
int result = 0;
uint8_t one = 0;
uint8_t two = 0;
onewire_t ow;
ow.port_out = &P1OUT;
ow.port_in = &P1IN;
ow.port_ren = &P1REN;
ow.port_dir = &P1DIR;
ow.pin = BIT7;
//Start up the one wire line.
onewire_line_high(&ow);
sleepMicro(100);
if (onewire_reset(&ow) != 0)
{
return 0;
}
onewire_write_byte(&ow, 0xcc);
onewire_write_byte(&ow, 0x4e);
onewire_write_byte(&ow, 0x00);
onewire_write_byte(&ow, 0x00);
onewire_write_byte(&ow, 0xff); // 12 resolution bits
if (onewire_reset(&ow) != 0)
{
onewire_line_low(&ow);// Lets not waste power now
return 0;
}
onewire_write_byte(&ow, 0xcc);
onewire_write_byte(&ow, 0x44);
onewire_line_high(&ow);
sleep(80); // 750ms held high for parasitic power max conversion time
if (onewire_reset(&ow) != 0)
{
onewire_line_low(&ow);// Lets not waste power now
return 0;
}
onewire_write_byte(&ow, 0xcc);
onewire_write_byte(&ow, 0xbe);
one = onewire_read_byte(&ow);
two = onewire_read_byte(&ow);
if (onewire_reset(&ow) != 0)
{
onewire_line_low(&ow);// Lets not waste power now
return 0;
}
onewire_line_low(&ow);// Lets not waste power now
result = ( ((long)two) << 8) | one;
return result;
}
int get_ext_temp_f(int oversample_bits)
{
long i = 0;
long tmp = 0;
long max = 1L << (oversample_bits * 2);
for (i = 0; i < max; i++)
tmp += (long)get_ext_temp_c();
tmp = tmp >> oversample_bits;
tmp = tmp * 10000L / 16L;
tmp = tmp >> oversample_bits;//We want things to be base 10 intead of base 2, and we need to adjust for oversampling in our C to F conversion, as it is easier to read the temperature
tmp = 9L*tmp/5L+(32L*10000L);
return (int)(tmp / 100L);
}