annotate 1wire.c @ 20:76a6bad110e2

Add routine to split string into argv/argc
author Daniel O'Connor <darius@dons.net.au>
date Sat, 31 Jan 2015 23:30:35 +1030
parents b5e4591b6570
children
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 * Copyright (c) 2004
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
7 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
8 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
9 * 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
10 * modification, are permitted provided that the following conditions
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
11 * are met:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
12 * 1. Redistributions of source code must retain the above copyright
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
14 * 2. Redistributions in binary form must reproduce 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 in the
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
16 * documentation and/or other materials provided with the distribution.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
17 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
18 * 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
19 * 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
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
21 * 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
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
24 * 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
25 * 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
26 * 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
27 * 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
28 * SUCH DAMAGE.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
29 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
30
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 * No user servicable parts inside
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 * Modify 1wire-config.h
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
37 #include <stdio.h>
13
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
38 #include <stdint.h>
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
39 #include "1wire-config.h"
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
40 #include "1wire.h"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
41
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
42 static uint8_t OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
43 static uint8_t OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
44 static uint8_t OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
45
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
46 static const char __str1[] PROGMEM = "OK";
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
47 static const char __str2[] PROGMEM = "no HW support";
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
48 static const char __str3[] PROGMEM = "Invalid params";
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
49 static const char __str4[] PROGMEM = "module missing/broken";
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
50 PGM_P const OWProgROM_Status[] PROGMEM = {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
51 __str1,
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
52 __str2,
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
53 __str3,
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
54 __str4
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
55 };
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 * Configure the IO port as we need
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
59 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
60 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
61 OWInit(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
62 OWBUSINIT();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
63 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
64 }
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 * 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
68 * 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
69 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
70 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
71 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
72 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
73 OWTouchReset(void) {
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
74 uint8_t i;
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
75
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
76 OWDELAY_G;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
77
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
78 /* 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
79 * 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
80 * comms attempt
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
81 */
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
82 #if 1
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
83 OWSETREAD();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
84 if(OWREADBUS() == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
85 return 2;
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
86 #endif
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
87
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
88 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
89 OWDELAY_H;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
90 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
91 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
92
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
93 OWSETREAD();
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
94 i = OWREADBUS();
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
95
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
96 OWDELAY_J;
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
97 return(i);
0
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
100 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
101 * Send a 1-wire write bit.
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 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
104 OWWriteBit(uint8_t bit) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
105 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
106
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
107 if (bit) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
108 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
109 OWDELAY_A;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
110 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
111 OWDELAY_B;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
112 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
113 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
114 OWDELAY_C;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
115 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
116 OWDELAY_D;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
117 }
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
120 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
121 * 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
122 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
123 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
124 OWReadBit(void) {
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
125 uint8_t i;
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
126
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
127 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
128
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
129 OWSETBUSLOW();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
130 OWDELAY_A;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
131 OWSETBUSHIGH();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
132 OWDELAY_E;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
133 OWSETREAD();
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
134 i = OWREADBUS();
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
135 OWDELAY_F;
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
136 return(i);
0
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
139 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
140 * Write a byte to the 1-wire bus
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
141 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
142 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
143 OWWriteByte(uint8_t data) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
144 uint8_t i;
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 /* Send LSB first */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
147 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
148 OWWriteBit(data & 0x01);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
149 data >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
150 }
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
153 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
154 * Read a byte from the 1-wire bus
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
155 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
156 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
157 OWReadByte(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
158 int i, result = 0;
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 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
161 result >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
162 if (OWReadBit())
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
163 result |= 0x80;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
164 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
165 return(result);
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
168 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
169 * 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
170 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
171 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
172 OWTouchByte(uint8_t data) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
173 uint8_t i, result = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
174
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
175 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
176 result >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
177
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
178 /* 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
179 if (data & 0x01) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
180 if (OWReadBit())
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
181 result |= 0x80;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
182 } else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
183 OWWriteBit(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
184
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
185 data >>= 1;
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
188 return(result);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
189 }
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 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
192 * 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
193 * the same buffer
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 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
196 OWBlock(uint8_t *data, int len) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
197 int i;
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 for (i = 0; i < len; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
200 data[i] = OWTouchByte(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
201 }
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
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 * 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
206 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
207 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
208 OWSendCmd(uint8_t *ROM, uint8_t cmd) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
209 uint8_t i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
210
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
211 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
212
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
213 if (ROM == NULL)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
214 OWWriteByte(OW_SKIP_ROM_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
215 else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
216 OWWriteByte(OW_MATCH_ROM_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
217 for (i = 0; i < 8; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
218 OWWriteByte(ROM[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
219 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
220 OWWriteByte(cmd);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
221 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
222
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
223 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
224 * Search algorithm from App note 187 (and 162)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
225 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
226 * OWFirst/OWNext return..
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
227 * 1 when something is found,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
228 * 0 no more modules
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
229 * -1 if no presence pulse,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
230 * -2 if bad CRC,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
231 * -3 if bad wiring.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
232 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
233 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
234 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
235 /* Reset state */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
236 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
237 OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
238 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
239
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
240 /* Go looking */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
241 return (OWNext(ROM, do_reset, alarm_only));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
242 }
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 /* Returns 1 when something is found, 0 if nothing left */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
245 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
246 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
247 uint8_t bit_test, search_direction, bit_number;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
248 uint8_t last_zero, rom_byte_number, rom_byte_mask;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
249 uint8_t lastcrc8, crcaccum;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
250 int8_t next_result;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
251
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
252 /* Init for search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
253 bit_number = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
254 last_zero = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
255 rom_byte_number = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
256 rom_byte_mask = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
257 next_result = OW_NOMODULES;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
258 lastcrc8 = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
259 crcaccum = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
260
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
261 /* if the last call was not the last one */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
262 if (!OW_LastDevice) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
263 /* check if reset first is requested */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
264 if (do_reset) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
265 /* reset the 1-wire
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
266 * if there are no parts on 1-wire, return 0 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
267 OWPUTSP(PSTR("Resetting\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
268 switch (OWTouchReset()) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
269 case 0:
14
d8002c716678 - Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
270 OWPUTSP(PSTR("Found device(s)\r\n"));
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
271 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
272
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
273 case 1:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
274 /* reset the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
275 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
276 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
277 OWPUTSP(PSTR("No devices on bus\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
278 return OW_NOPRESENCE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
279 break;
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 case 2:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
282 /* reset the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
283 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
284 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
285 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
286 return OW_BADWIRE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
287 break;
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 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
290 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
291
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
292 /* If finding alarming devices issue a different command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
293 if (alarm_only)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
294 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
295 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
296 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
297
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
298 /* pause before beginning the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
299 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
300 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
301 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
302
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
303 /* loop to do the search */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
304 do {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
305 /* read a bit and its compliment */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
306 bit_test = OWReadBit() << 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
307 bit_test |= OWReadBit();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
308
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
309 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
310
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
311 /* check for no devices on 1-wire */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
312 if (bit_test == 3) {
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
313 /* 11 */
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
314 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test);
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
315 return(OW_NOMODULES);
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
316 }
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
317 /* all devices coupled have 0 or 1 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
318 if (bit_test != 0)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
319 /* 10 or 01 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
320 search_direction = !(bit_test & 0x01); /* bit write value for search */
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
321 else {
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
322 /* 00 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
323 /* if this discrepancy is before the Last Discrepancy
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
324 * on a previous OWNext then pick the same as last time */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
325 if (bit_number < OW_LastDiscrepancy)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
326 search_direction = (ROM[rom_byte_number] & rom_byte_mask) > 0;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
327 else
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
328 /* if equal to last pick 1, if not then pick 0 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
329 search_direction = bit_number == OW_LastDiscrepancy;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
330
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
331 /* if 0 was picked then record its position in LastZero */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
332 if (search_direction == 0) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
333 last_zero = bit_number;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
334
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
335 /* check for Last discrepancy in family */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
336 if (last_zero < 9)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
337 OW_LastFamilyDiscrepancy = last_zero;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
338 }
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
339 }
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
340
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
341 /* set or clear the bit in the ROM byte rom_byte_number
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
342 * with mask rom_byte_mask */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
343 if (search_direction == 1)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
344 ROM[rom_byte_number] |= rom_byte_mask;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
345 else
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
346 ROM[rom_byte_number] &= ~rom_byte_mask;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
347
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
348 /* serial number search direction write bit */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
349 OWWriteBit(search_direction);
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
350
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
351 /* increment the byte counter bit_number
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
352 * and shift the mask rom_byte_mask */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
353 bit_number++;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
354 rom_byte_mask <<= 1;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
355
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
356 /* if the mask is 0 then go to new ROM byte rom_byte_number
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
357 * and reset mask */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
358 if (rom_byte_mask == 0) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
359 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
360 lastcrc8 = crcaccum;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
361
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
362 rom_byte_number++;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
363 rom_byte_mask = 1;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
364 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
365 } 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
366
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
367 /* if the search was successful then */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
368 if (!(bit_number < 65) || lastcrc8) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
369 if (lastcrc8) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
370 OWPRINTFP(PSTR("Bad CRC (%d)\r\n"), lastcrc8);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
371 next_result = OW_BADCRC;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
372 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
373 /* search successful so set LastDiscrepancy,LastDevice,next_result */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
374 OW_LastDiscrepancy = last_zero;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
375 OW_LastDevice = (OW_LastDiscrepancy == 0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
376 OWPRINTFP(PSTR("Last device = %d\r\n"), OW_LastDevice);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
377 next_result = OW_FOUND;
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 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
381
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
382 /* 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
383 * like a first */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
384 if (next_result != OW_FOUND || ROM[0] == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
385 OW_LastDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
386 OW_LastDevice = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
387 OW_LastFamilyDiscrepancy = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
388 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
389
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
390 if (next_result == OW_FOUND && ROM[0] == 0x00)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
391 next_result = OW_BADWIRE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
392
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
393 return next_result;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
394
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
395 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
396
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
397 const uint8_t dscrc_table[] PROGMEM = {
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 };
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
415
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 * Update *crc based on the value of x
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
418 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
419 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
420 OWCRC(uint8_t x, uint8_t *crc) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
421 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
422 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
423
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
424 /*-----------------------------------------------------------------------------
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
425 * Program a DS2502's 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 * Arguments
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
428 * ROM - ROM ID (or NULL to send SKIP_ROM)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
429 * start - Start address (bytes)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
430 * len - Length of data to write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
431 * data - Data to write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
432 * exact - If true, only accept exact matches for programming,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
433 * otherwise only ensure the bits we requested were
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
434 * programmed [to 0]
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
435 * status - If true program status rather than memory
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
436 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
437 * Returns..
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
438 * 0 if all is OK
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
439 * 1 if the programming is not possible
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
440 * 2 if the parameters were invalid
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
441 * 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
442 * module doesn't exist)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
443 */
13
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
444 #if defined(OWSETVPPON) && defined(OWSETVPPOFF)
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
445 uint8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
446 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
447 uint8_t crc, i, tmp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
448
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
449 /* Stupid programmer detection */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
450 if (status) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
451 if (start + len > 3)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
452 return(2);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
453 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
454 if (start + len > 127)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
455 return(2);
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
458 if (len < 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
459 return(2);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
460
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
461 OWDELAY_I;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
462 if (OWTouchReset() != 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
463 cons_putsP(PSTR("No presence pulse\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
464 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
465 }
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 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
468
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
469 /* Send the command */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
470 if (status) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
471 OWSendCmd(ROM, OW_WRITE_STATUS);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
472 OWCRC(OW_WRITE_STATUS, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
473 } else {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
474 OWSendCmd(ROM, OW_WRITE_MEMORY);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
475 OWCRC(OW_WRITE_MEMORY, &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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
478 /* And the start address
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
479 * (2 bytes even though one would do)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
480 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
481 OWWriteByte(start);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
482 OWCRC(start, &crc);
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(0x00);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
485 OWCRC(0x00, &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 for (i = 0; i < len; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
488 cons_putsP(PSTR("Programming "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
489 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
490 cons_putsP(PSTR(" to "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
491 cons_puts_hex(start + i);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
492 cons_putsP(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
493
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
494 OWWriteByte(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
495 OWCRC(data[i], &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
496
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
497 tmp = OWReadByte();
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 if (crc != tmp) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
500 cons_putsP(PSTR("CRC mismatch "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
501 cons_puts_hex(crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
502 cons_putsP(PSTR(" vs "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
503 cons_puts_hex(tmp);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
504 cons_putsP(PSTR("\r\n"));
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 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
507 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
508 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
509
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
510 OWSETVPPON();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
511 OWDELAY_H;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
512 OWSETVPPOFF();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
513
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
514 tmp = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
515
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
516 /* Verify */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
517 if (data[i] != tmp) {
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
518 cons_putsP(PSTR("Readback mismatch "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
519 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
520 cons_putsP(PSTR(" vs "));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
521 cons_puts_hex(data[i]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
522 cons_putsP(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
523
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
524 OWTouchReset();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
525 return(3);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
526 }
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 /* 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
529 * next byte */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
530 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
531 OWCRC(start + i + 1, &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
532 }
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 return(0);
13
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
535 }
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
536 #else
13
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
537 uint8_t
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
538 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))) {
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
539 return(1);
13
6f8f7b87d2f1 - Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
540 }
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
541 #endif
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 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
544 * OWGetTemp
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
545 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
546 * Get the temperature from a 1wire bus module
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
547 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
548 * 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
549 * error.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
550 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
551 int16_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
552 OWGetTemp(uint8_t *ROM) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
553 int8_t i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
554 uint8_t crc, buf[9];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
555 int16_t temp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
556 int16_t tfrac;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
557
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
558 if (ROM[0] != OW_FAMILY_TEMP)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
559 return OW_TEMP_WRONG_FAM;
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 OWSendCmd(ROM, OW_CONVERTT_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
562
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
563 i = 0;
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 /* Wait for the conversion */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
566 while (OWReadBit() == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
567 i = 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
568
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
569 /* 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
570 if (i == 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
571 return OW_TEMP_NO_ROM;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
572 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
573
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
574 OWSendCmd(ROM, OW_RD_SCR_CMD);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
575 crc = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
576 for (i = 0; i < 8; i++) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
577 buf[i] = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
578 OWCRC(buf[i], &crc);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
579 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
580 buf[i] = OWReadByte();
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
581 if (crc != buf[8])
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
582 return OW_TEMP_CRC_ERR;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
583 temp = buf[0];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
584 if (buf[1] & 0x80)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
585 temp -= 256;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
586
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
587 /* Chop off 0.5 degree bit */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
588 temp >>= 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
589
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
590 /* Calulate the fractional remainder */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
591 tfrac = buf[7] - buf[6];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
592
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
593 /* Work in 100'th of degreess to save on floats */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
594 tfrac *= (int16_t)100;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
595
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
596 /* Divide by count */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
597 tfrac /= buf[7];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
598
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
599 /* Subtract 0.25 deg from temp */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
600 tfrac += 75;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
601 if (tfrac < 100)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
602 temp--;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
603 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
604 tfrac -= 100;
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 i = temp;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
607 temp *= 100;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
608 temp += tfrac;
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 return(temp);
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 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
614 * OWTempStatusStr
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
615 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
616 * Return a string for each OW_TEMP_xxx error code
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
617 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
618 * shrt = 1 returns short strings
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
619 *
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 const char *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
622 OWTempStatusStr(int16_t val, uint8_t shrt) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
623 if (val > OW_TEMP_BADVAL) {
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("OK");
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("OK");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
628 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
629
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
630 switch (val) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
631 case OW_TEMP_WRONG_FAM:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
632 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
633 return PSTR("WrFam");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
634 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
635 return PSTR("Wrong family");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
636 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
637 case OW_TEMP_CRC_ERR:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
638 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
639 return PSTR("CRCErr");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
640 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
641 return PSTR("CRC Error");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
642 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
643 case OW_TEMP_NO_ROM:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
644 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
645 return PSTR("NoROM");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
646 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
647 return PSTR("ROM did not reply");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
648 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
649 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
650 if (shrt)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
651 return PSTR("???");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
652 else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
653 return PSTR("Unknown error code");
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
654 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
655 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
656 }