annotate 1wire.c @ 59:488085e0c7e1

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