Mercurial > ~darius > hgwebdir.cgi > avr
comparison 1wire.c @ 12:4b141cc7cbd4
- Reduce type widths where possible.
- Add URL for Dallas OWPD kit.
- Hide more stuff behind OW_DEBUG.
author | darius |
---|---|
date | Wed, 01 Sep 2004 17:45:24 +0930 |
parents | eb1faf51968e |
children | 026dc24d85e0 |
comparison
equal
deleted
inserted
replaced
11:ccc39c9f320b | 12:4b141cc7cbd4 |
---|---|
1 /* | 1 /* |
2 * Various 1 wire routines | 2 * Various 1 wire routines |
3 * Search routine is copied from the Dallas owpd library with mods. | 3 * Search routine is copied from the Dallas owpd library with mods |
4 * available from here http://www.ibutton.com/software/1wire/wirekit.html | |
4 * | 5 * |
5 * $Id$ | 6 * $Id$ |
6 * | 7 * |
7 * Copyright (c) 2004 | 8 * Copyright (c) 2004 |
8 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. | 9 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. |
34 #include <avr/pgmspace.h> | 35 #include <avr/pgmspace.h> |
35 | 36 |
36 #include "1wire.h" | 37 #include "1wire.h" |
37 #include "1wire-delay.h" | 38 #include "1wire-delay.h" |
38 | 39 |
40 #if OW_DEBUG | |
39 void uart_putsP(const char *addr); | 41 void uart_putsP(const char *addr); |
40 void uart_puts(const char *addr); | 42 void uart_puts(const char *addr); |
41 void uart_getc(); | 43 void uart_getc(); |
42 int uart_putc(char c); | 44 int uart_putc(char c); |
45 #endif | |
43 | 46 |
44 static uint8_t OW_LastDevice = 0; | 47 static uint8_t OW_LastDevice = 0; |
45 static uint8_t OW_LastDiscrepancy = 0; | 48 static uint8_t OW_LastDiscrepancy = 0; |
46 static uint8_t OW_LastFamilyDiscrepancy = 0; | 49 static uint8_t OW_LastFamilyDiscrepancy = 0; |
47 | 50 |
49 OWdelay(void) { | 52 OWdelay(void) { |
50 DELAY_I; | 53 DELAY_I; |
51 } | 54 } |
52 | 55 |
53 /*----------------------------------------------------------------------------- | 56 /*----------------------------------------------------------------------------- |
54 * Generate a 1-Wire reset, return 1 if no presence detect was found, | 57 * Generate a 1-Wire reset, return 0 if presence pulse was found, |
55 * return 0 otherwise. | 58 * return 1 otherwise. |
56 * (NOTE: Does not handle alarm presence from DS2404/DS1994) | 59 * (NOTE: Does not handle alarm presence from DS2404/DS1994) |
57 */ | 60 */ |
58 int | 61 uint8_t |
59 OWTouchReset(void) { | 62 OWTouchReset(void) { |
60 DELAY_G; | 63 DELAY_G; |
61 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 64 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
62 OWIREDDR |= _BV(OWIREOUTPIN); | 65 OWIREDDR |= _BV(OWIREOUTPIN); |
63 DELAY_H; | 66 DELAY_H; |
69 | 72 |
70 /*----------------------------------------------------------------------------- | 73 /*----------------------------------------------------------------------------- |
71 * Send a 1-wire write bit. | 74 * Send a 1-wire write bit. |
72 */ | 75 */ |
73 void | 76 void |
74 OWWriteBit(int bit) { | 77 OWWriteBit(uint8_t bit) { |
75 OWdelay(); | 78 OWdelay(); |
76 if (bit) { | 79 if (bit) { |
77 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 80 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
78 OWIREDDR |= _BV(OWIREOUTPIN); | 81 OWIREDDR |= _BV(OWIREOUTPIN); |
79 DELAY_A; | 82 DELAY_A; |
89 } | 92 } |
90 | 93 |
91 /*----------------------------------------------------------------------------- | 94 /*----------------------------------------------------------------------------- |
92 * Read a bit from the 1-wire bus and return it. | 95 * Read a bit from the 1-wire bus and return it. |
93 */ | 96 */ |
94 int | 97 uint8_t |
95 OWReadBit(void) { | 98 OWReadBit(void) { |
96 OWdelay(); | 99 OWdelay(); |
97 | 100 |
98 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | 101 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
99 OWIREDDR |= _BV(OWIREOUTPIN); | 102 OWIREDDR |= _BV(OWIREOUTPIN); |
106 /*----------------------------------------------------------------------------- | 109 /*----------------------------------------------------------------------------- |
107 * Write a byte to the 1-wire bus | 110 * Write a byte to the 1-wire bus |
108 */ | 111 */ |
109 void | 112 void |
110 OWWriteByte(uint8_t data) { | 113 OWWriteByte(uint8_t data) { |
111 int i; | 114 uint8_t i; |
112 | 115 |
113 /* Send LSB first */ | 116 /* Send LSB first */ |
114 for (i = 0; i < 8; i++) { | 117 for (i = 0; i < 8; i++) { |
115 OWWriteBit(data & 0x01); | 118 OWWriteBit(data & 0x01); |
116 data >>= 1; | 119 data >>= 1; |
118 } | 121 } |
119 | 122 |
120 /*----------------------------------------------------------------------------- | 123 /*----------------------------------------------------------------------------- |
121 * Read a byte from the 1-wire bus | 124 * Read a byte from the 1-wire bus |
122 */ | 125 */ |
123 int | 126 uint8_t |
124 OWReadByte(void) { | 127 OWReadByte(void) { |
125 int i, result = 0; | 128 int i, result = 0; |
126 | 129 |
127 for (i = 0; i < 8; i++) { | 130 for (i = 0; i < 8; i++) { |
128 result >>= 1; | 131 result >>= 1; |
133 } | 136 } |
134 | 137 |
135 /*----------------------------------------------------------------------------- | 138 /*----------------------------------------------------------------------------- |
136 * Write a 1-wire data byte and return the sampled result. | 139 * Write a 1-wire data byte and return the sampled result. |
137 */ | 140 */ |
138 int | 141 uint8_t |
139 OWTouchByte(uint8_t data) { | 142 OWTouchByte(uint8_t data) { |
140 int i, result = 0; | 143 uint8_t i, result = 0; |
141 | 144 |
142 for (i = 0; i < 8; i++) { | 145 for (i = 0; i < 8; i++) { |
143 result >>= 1; | 146 result >>= 1; |
144 | 147 |
145 /* If sending a 1 then read a bit, otherwise write a 0 */ | 148 /* If sending a 1 then read a bit, otherwise write a 0 */ |
171 /*----------------------------------------------------------------------------- | 174 /*----------------------------------------------------------------------------- |
172 * Send a 1 wire command to a device, or all if no ROM ID provided | 175 * Send a 1 wire command to a device, or all if no ROM ID provided |
173 */ | 176 */ |
174 void | 177 void |
175 OWSendCmd(uint8_t *ROM, uint8_t cmd) { | 178 OWSendCmd(uint8_t *ROM, uint8_t cmd) { |
176 int i; | 179 uint8_t i; |
177 | 180 |
178 OWTouchReset(); | 181 OWTouchReset(); |
179 | 182 |
180 if (ROM == NULL) | 183 if (ROM == NULL) |
181 OWWriteByte(OW_SKIP_ROM_CMD); | 184 OWWriteByte(OW_SKIP_ROM_CMD); |
196 * 0 no more modules | 199 * 0 no more modules |
197 * -1 if no presense pulse, | 200 * -1 if no presense pulse, |
198 * -2 if bad CRC, | 201 * -2 if bad CRC, |
199 * -3 if bad wiring. | 202 * -3 if bad wiring. |
200 */ | 203 */ |
201 int | 204 uint8_t |
202 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { | 205 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
203 /* Reset state */ | 206 /* Reset state */ |
204 OW_LastDiscrepancy = 0; | 207 OW_LastDiscrepancy = 0; |
205 OW_LastDevice = 0; | 208 OW_LastDevice = 0; |
206 OW_LastFamilyDiscrepancy = 0; | 209 OW_LastFamilyDiscrepancy = 0; |
208 /* Go looking */ | 211 /* Go looking */ |
209 return (OWNext(ROM, do_reset, alarm_only)); | 212 return (OWNext(ROM, do_reset, alarm_only)); |
210 } | 213 } |
211 | 214 |
212 /* Returns 1 when something is found, 0 if nothing left */ | 215 /* Returns 1 when something is found, 0 if nothing left */ |
213 int | 216 uint8_t |
214 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { | 217 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
215 uint8_t bit_test, search_direction, bit_number; | 218 uint8_t bit_test, search_direction, bit_number; |
216 uint8_t last_zero, rom_byte_number, rom_byte_mask; | 219 uint8_t last_zero, rom_byte_number, rom_byte_mask; |
217 uint8_t lastcrc8, crcaccum; | 220 uint8_t lastcrc8, crcaccum; |
218 int next_result; | 221 int8_t next_result; |
219 char errstr[30]; | |
220 | 222 |
221 /* Init for search */ | 223 /* Init for search */ |
222 bit_number = 1; | 224 bit_number = 1; |
223 last_zero = 0; | 225 last_zero = 0; |
224 rom_byte_number = 0; | 226 rom_byte_number = 0; |
328 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ | 330 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ |
329 | 331 |
330 /* if the search was successful then */ | 332 /* if the search was successful then */ |
331 if (!(bit_number < 65) || lastcrc8) { | 333 if (!(bit_number < 65) || lastcrc8) { |
332 if (lastcrc8) { | 334 if (lastcrc8) { |
335 #if OW_DEBUG | |
333 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); | 336 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); |
334 uart_puts(errstr); | 337 uart_puts(errstr); |
338 #endif | |
335 next_result = OW_BADCRC; | 339 next_result = OW_BADCRC; |
336 } else { | 340 } else { |
337 /* search successful so set LastDiscrepancy,LastDevice,next_result */ | 341 /* search successful so set LastDiscrepancy,LastDevice,next_result */ |
338 OW_LastDiscrepancy = last_zero; | 342 OW_LastDiscrepancy = last_zero; |
339 OW_LastDevice = (OW_LastDiscrepancy == 0); | 343 OW_LastDevice = (OW_LastDiscrepancy == 0); |