annotate 1wire.c @ 6:3da232f97e81

Invert the bit mask, we only want to turn off this bit, not everything.
author darius@inchoate.localdomain
date Mon, 20 Apr 2009 13:52:06 +0800
parents 3879f487b661
children 6f8f7b87d2f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
1 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
2 * Various 1 wire routines
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
3 * Search routine is copied from the Dallas owpd library with mods
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
4 * available from here http://www.ibutton.com/software/1wire/wirekit.html
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
5 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
6 * $Id$
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
7 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
8 * Copyright (c) 2004
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
9 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
10 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
11 * Redistribution and use in source and binary forms, with or without
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
12 * modification, are permitted provided that the following conditions
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
13 * are met:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
14 * 1. Redistributions of source code must retain the above copyright
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
16 * 2. Redistributions in binary form must reproduce the above copyright
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
17 * notice, this list of conditions and the following disclaimer in the
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
18 * documentation and/or other materials provided with the distribution.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
19 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
30 * SUCH DAMAGE.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
31 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
32
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
33 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
34 * No user servicable parts inside
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
35 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
36 * Modify 1wire-config.h
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
37 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
38
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
39 #include <stdio.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
40 #include <avr/io.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
41 #include <avr/pgmspace.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
42 #include <util/delay.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
43 #include "1wire.h"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
44 #include "1wire-config.h"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
45 #include "cons.h"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
46
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
47 static uint8_t OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
48 static uint8_t OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
49 static uint8_t OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
50
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
51 const PROGMEM char *OWProgROM_Status[] = {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
52 "OK",
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
53 "no HW support",
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
54 "Invalid params",
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
55 "module missing/broken"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
56 };
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
57
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
58 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
59 * Configure the IO port as we need
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
60 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
61 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
62 OWInit(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
63 OWBUSINIT();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
64 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
65 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
66
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
67 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
68 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
69 * wasn't, or 2 if the line appears to be being held low.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
70 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
71 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
72 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
73 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
74 OWTouchReset(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
75 OWDELAY_G;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
76
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
77 /* Check the bus isn't being held low (ie it's broken) Do it after
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
78 * the delay so we guarantee we don't see a slave from a previous
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
79 * comms attempt
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
80 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
81 #if 0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
82 OWSETREAD();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
83 if(OWREADBUS() == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
84 return 2;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
85 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
86
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
87 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
88 OWDELAY_H;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
89 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
90 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
91
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
92 OWSETREAD();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
93 return(OWREADBUS());
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
94 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
95
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
96 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
97 * Send a 1-wire write bit.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
98 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
99 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
100 OWWriteBit(uint8_t bit) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
101 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
102
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
103 if (bit) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
104 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
105 OWDELAY_A;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
106 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
107 OWDELAY_B;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
108 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
109 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
110 OWDELAY_C;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
111 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
112 OWDELAY_D;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
113 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
114 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
115
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
116 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
117 * Read a bit from the 1-wire bus and return it.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
118 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
119 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
120 OWReadBit(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
121 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
122
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
123 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
124 OWDELAY_A;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
125 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
126 OWDELAY_E;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
127 OWSETREAD();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
128 return(OWREADBUS());
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
129 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
130
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
131 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
132 * Write a byte to the 1-wire bus
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
133 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
134 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
135 OWWriteByte(uint8_t data) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
136 uint8_t i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
137
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
138 /* Send LSB first */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
139 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
140 OWWriteBit(data & 0x01);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
141 data >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
142 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
143 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
144
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
145 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
146 * Read a byte from the 1-wire bus
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
147 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
148 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
149 OWReadByte(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
150 int i, result = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
151
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
152 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
153 result >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
154 if (OWReadBit())
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
155 result |= 0x80;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
156 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
157 return(result);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
158 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
159
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
160 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
161 * Write a 1-wire data byte and return the sampled result.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
162 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
163 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
164 OWTouchByte(uint8_t data) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
165 uint8_t i, result = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
166
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
167 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
168 result >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
169
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
170 /* If sending a 1 then read a bit, otherwise write a 0 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
171 if (data & 0x01) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
172 if (OWReadBit())
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
173 result |= 0x80;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
174 } else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
175 OWWriteBit(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
176
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
177 data >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
178 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
179
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
180 return(result);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
181 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
182
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
183 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
184 * Write a block of bytes to the 1-wire bus and return the sampled result in
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
185 * the same buffer
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
186 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
187 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
188 OWBlock(uint8_t *data, int len) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
189 int i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
190
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
191 for (i = 0; i < len; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
192 data[i] = OWTouchByte(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
193 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
194
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
195
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
196 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
197 * Send a 1 wire command to a device, or all if no ROM ID provided
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
198 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
199 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
200 OWSendCmd(uint8_t *ROM, uint8_t cmd) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
201 uint8_t i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
202
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
203 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
204
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
205 if (ROM == NULL)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
206 OWWriteByte(OW_SKIP_ROM_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
207 else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
208 OWWriteByte(OW_MATCH_ROM_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
209 for (i = 0; i < 8; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
210 OWWriteByte(ROM[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
211 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
212 OWWriteByte(cmd);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
213 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
214
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
215 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
216 * Search algorithm from App note 187 (and 162)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
217 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
218 * OWFirst/OWNext return..
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
219 * 1 when something is found,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
220 * 0 no more modules
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
221 * -1 if no presence pulse,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
222 * -2 if bad CRC,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
223 * -3 if bad wiring.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
224 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
225 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
226 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
227 /* Reset state */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
228 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
229 OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
230 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
231
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
232 /* Go looking */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
233 return (OWNext(ROM, do_reset, alarm_only));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
234 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
235
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
236 /* Returns 1 when something is found, 0 if nothing left */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
237 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
238 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
239 uint8_t bit_test, search_direction, bit_number;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
240 uint8_t last_zero, rom_byte_number, rom_byte_mask;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
241 uint8_t lastcrc8, crcaccum;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
242 int8_t next_result;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
243
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
244 /* Init for search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
245 bit_number = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
246 last_zero = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
247 rom_byte_number = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
248 rom_byte_mask = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
249 next_result = OW_NOMODULES;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
250 lastcrc8 = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
251 crcaccum = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
252
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
253 /* if the last call was not the last one */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
254 if (!OW_LastDevice) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
255 /* check if reset first is requested */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
256 if (do_reset) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
257 /* reset the 1-wire
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
258 * if there are no parts on 1-wire, return 0 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
259 OWPUTSP(PSTR("Resetting\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
260 switch (OWTouchReset()) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
261 case 0:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
262 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
263
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
264 case 1:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
265 /* reset the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
266 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
267 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
268 OWPUTSP(PSTR("No devices on bus\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
269 return OW_NOPRESENCE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
270 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
271
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
272 case 2:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
273 /* reset the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
274 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
275 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
276 OWPUTSP(PSTR("Bus appears to be being held low\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
277 return OW_BADWIRE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
278 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
279
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
280 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
281 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
282
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
283 /* If finding alarming devices issue a different command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
284 if (alarm_only)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
285 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
286 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
287 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
288
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
289 /* pause before beginning the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
290 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
291 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
292 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
293
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
294 /* loop to do the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
295 do {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
296 /* read a bit and its compliment */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
297 bit_test = OWReadBit() << 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
298 bit_test |= OWReadBit();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
299
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
300 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
301
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
302 /* check for no devices on 1-wire */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
303 if (bit_test == 3) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
304 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
305 return(OW_BADWIRE);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
306 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
307 else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
308 /* all devices coupled have 0 or 1 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
309 if (bit_test > 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
310 search_direction = !(bit_test & 0x01); /* bit write value for search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
311 else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
312 /* if this discrepancy is before the Last Discrepancy
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
313 * on a previous OWNext then pick the same as last time */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
314 if (bit_number < OW_LastDiscrepancy)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
315 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
316 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
317 /* if equal to last pick 1, if not then pick 0 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
318 search_direction = (bit_number == OW_LastDiscrepancy);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
319
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
320 /* if 0 was picked then record its position in LastZero */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
321 if (search_direction == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
322 last_zero = bit_number;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
323
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
324 /* check for Last discrepancy in family */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
325 if (last_zero < 9)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
326 OW_LastFamilyDiscrepancy = last_zero;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
327 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
328 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
329
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
330 /* set or clear the bit in the ROM byte rom_byte_number
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
331 * with mask rom_byte_mask */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
332 if (search_direction == 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
333 ROM[rom_byte_number] |= rom_byte_mask;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
334 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
335 ROM[rom_byte_number] &= ~rom_byte_mask;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
336
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
337 /* serial number search direction write bit */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
338 OWWriteBit(search_direction);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
339
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
340 /* increment the byte counter bit_number
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
341 * and shift the mask rom_byte_mask */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
342 bit_number++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
343 rom_byte_mask <<= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
344
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
345 /* if the mask is 0 then go to new ROM byte rom_byte_number
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
346 * and reset mask */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
347 if (rom_byte_mask == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
348 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
349 lastcrc8 = crcaccum;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
350
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
351 rom_byte_number++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
352 rom_byte_mask = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
353 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
354 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
355 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
356
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
357 /* if the search was successful then */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
358 if (!(bit_number < 65) || lastcrc8) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
359 if (lastcrc8) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
360 OWPRINTFP(PSTR("Bad CRC (%d)\r\n"), lastcrc8);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
361 next_result = OW_BADCRC;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
362 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
363 /* search successful so set LastDiscrepancy,LastDevice,next_result */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
364 OW_LastDiscrepancy = last_zero;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
365 OW_LastDevice = (OW_LastDiscrepancy == 0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
366 OWPRINTFP(PSTR("Last device = %d\r\n"), OW_LastDevice);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
367 next_result = OW_FOUND;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
368 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
369 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
370 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
371
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
372 /* if no device found then reset counters so next 'next' will be
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
373 * like a first */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
374 if (next_result != OW_FOUND || ROM[0] == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
375 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
376 OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
377 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
378 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
379
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
380 if (next_result == OW_FOUND && ROM[0] == 0x00)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
381 next_result = OW_BADWIRE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
382
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
383 return next_result;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
384
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
385 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
386
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
387 uint8_t PROGMEM dscrc_table[] = {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
388 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
389 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
390 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
391 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
392 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
393 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
394 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
395 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
396 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
397 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
398 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
399 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
400 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
401 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
402 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
403 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
404 };
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
405
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
406 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
407 * Update *crc based on the value of x
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
408 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
409 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
410 OWCRC(uint8_t x, uint8_t *crc) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
411 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
412 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
413
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
414 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
415 * Program a DS2502's memory
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
416 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
417 * Arguments
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
418 * ROM - ROM ID (or NULL to send SKIP_ROM)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
419 * start - Start address (bytes)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
420 * len - Length of data to write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
421 * data - Data to write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
422 * exact - If true, only accept exact matches for programming,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
423 * otherwise only ensure the bits we requested were
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
424 * programmed [to 0]
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
425 * status - If true program status rather than memory
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
426 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
427 * Returns..
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
428 * 0 if all is OK
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
429 * 1 if the programming is not possible
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
430 * 2 if the parameters were invalid
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
431 * 3 if the DS2502 didn't respond appropriately (also happens if the
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
432 * module doesn't exist)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
433 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
434 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
435 OWProgROM(uint8_t *ROM, uint8_t start, uint8_t len, uint8_t *data, uint8_t exact, uint8_t status) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
436 #if defined(OWSETVPPON) && defined(OWSETVPPOFF)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
437 uint8_t crc, i, tmp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
438
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
439 /* Stupid programmer detection */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
440 if (status) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
441 if (start + len > 3)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
442 return(2);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
443 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
444 if (start + len > 127)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
445 return(2);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
446 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
447
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
448 if (len < 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
449 return(2);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
450
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
451 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
452 if (OWTouchReset() != 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
453 cons_putsP(PSTR("No presence pulse\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
454 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
455 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
456
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
457 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
458
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
459 /* Send the command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
460 if (status) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
461 OWSendCmd(ROM, OW_WRITE_STATUS);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
462 OWCRC(OW_WRITE_STATUS, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
463 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
464 OWSendCmd(ROM, OW_WRITE_MEMORY);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
465 OWCRC(OW_WRITE_MEMORY, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
466 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
467
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
468 /* And the start address
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
469 * (2 bytes even though one would do)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
470 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
471 OWWriteByte(start);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
472 OWCRC(start, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
473
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
474 OWWriteByte(0x00);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
475 OWCRC(0x00, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
476
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
477 for (i = 0; i < len; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
478 cons_putsP(PSTR("Programming "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
479 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
480 cons_putsP(PSTR(" to "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
481 cons_puts_hex(start + i);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
482 cons_putsP(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
483
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
484 OWWriteByte(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
485 OWCRC(data[i], &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
486
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
487 tmp = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
488
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
489 if (crc != tmp) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
490 cons_putsP(PSTR("CRC mismatch "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
491 cons_puts_hex(crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
492 cons_putsP(PSTR(" vs "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
493 cons_puts_hex(tmp);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
494 cons_putsP(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
495
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
496 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
497 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
498 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
499
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
500 OWSETVPPON();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
501 OWDELAY_H;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
502 OWSETVPPOFF();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
503
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
504 tmp = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
505
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
506 /* Check the bits we turned off are off */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
507 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
508 for (i = 0; i < 8; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
509 if (!(data[i] & 1 << i) && (tmp & 1 << i))
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
510 return(-3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
511 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
512 if ((!data[i] & tmp) != 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
513 cons_putsP(PSTR("Readback mismatch "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
514 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
515 cons_putsP(PSTR(" vs "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
516 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
517 cons_putsP(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
518
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
519 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
520 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
521 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
522
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
523 /* The DS2502 loads it's CRC register with the address of the
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
524 * next byte */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
525 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
526 OWCRC(start + i + 1, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
527 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
528
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
529 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
530 #else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
531 return(1);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
532 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
533 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
534
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
535 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
536 * OWGetTemp
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
537 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
538 * Get the temperature from a 1wire bus module
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
539 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
540 * Returns temperature in hundredths of a degree or OW_TEMP_xxx on
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
541 * error.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
542 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
543 int16_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
544 OWGetTemp(uint8_t *ROM) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
545 int8_t i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
546 uint8_t crc, buf[9];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
547 int16_t temp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
548 int16_t tfrac;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
549
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
550 if (ROM[0] != OW_FAMILY_TEMP)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
551 return OW_TEMP_WRONG_FAM;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
552
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
553 OWSendCmd(ROM, OW_CONVERTT_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
554
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
555 i = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
556
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
557 /* Wait for the conversion */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
558 while (OWReadBit() == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
559 i = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
560
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
561 /* Check that we talked to a module and it did something */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
562 if (i == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
563 return OW_TEMP_NO_ROM;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
564 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
565
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
566 OWSendCmd(ROM, OW_RD_SCR_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
567 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
568 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
569 buf[i] = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
570 OWCRC(buf[i], &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
571 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
572 buf[i] = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
573 if (crc != buf[8])
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
574 return OW_TEMP_CRC_ERR;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
575 temp = buf[0];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
576 if (buf[1] & 0x80)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
577 temp -= 256;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
578
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
579 /* Chop off 0.5 degree bit */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
580 temp >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
581
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
582 /* Calulate the fractional remainder */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
583 tfrac = buf[7] - buf[6];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
584
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
585 /* Work in 100'th of degreess to save on floats */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
586 tfrac *= (int16_t)100;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
587
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
588 /* Divide by count */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
589 tfrac /= buf[7];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
590
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
591 /* Subtract 0.25 deg from temp */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
592 tfrac += 75;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
593 if (tfrac < 100)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
594 temp--;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
595 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
596 tfrac -= 100;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
597
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
598 i = temp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
599 temp *= 100;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
600 temp += tfrac;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
601
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
602 return(temp);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
603 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
604
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
605 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
606 * OWTempStatusStr
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
607 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
608 * Return a string for each OW_TEMP_xxx error code
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
609 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
610 * shrt = 1 returns short strings
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
611 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
612 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
613 const char *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
614 OWTempStatusStr(int16_t val, uint8_t shrt) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
615 if (val > OW_TEMP_BADVAL) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
616 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
617 return PSTR("OK");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
618 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
619 return PSTR("OK");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
620 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
621
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
622 switch (val) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
623 case OW_TEMP_WRONG_FAM:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
624 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
625 return PSTR("WrFam");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
626 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
627 return PSTR("Wrong family");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
628 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
629 case OW_TEMP_CRC_ERR:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
630 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
631 return PSTR("CRCErr");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
632 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
633 return PSTR("CRC Error");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
634 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
635 case OW_TEMP_NO_ROM:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
636 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
637 return PSTR("NoROM");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
638 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
639 return PSTR("ROM did not reply");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
640 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
641 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
642 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
643 return PSTR("???");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
644 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
645 return PSTR("Unknown error code");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
646 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
647 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
648 }