Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ownet.cpp@1:dcf2f8359398, 2010-12-08 (annotated)
- Committer:
- svlach
- Date:
- Wed Dec 08 21:23:21 2010 +0000
- Revision:
- 1:dcf2f8359398
- Parent:
- 0:70c3bea805ee
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
svlach | 0:70c3bea805ee | 1 | //--------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 2 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
svlach | 0:70c3bea805ee | 3 | // |
svlach | 0:70c3bea805ee | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
svlach | 0:70c3bea805ee | 5 | // copy of this software and associated documentation files (the "Software"), |
svlach | 0:70c3bea805ee | 6 | // to deal in the Software without restriction, including without limitation |
svlach | 0:70c3bea805ee | 7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
svlach | 0:70c3bea805ee | 8 | // and/or sell copies of the Software, and to permit persons to whom the |
svlach | 0:70c3bea805ee | 9 | // Software is furnished to do so, subject to the following conditions: |
svlach | 0:70c3bea805ee | 10 | // |
svlach | 0:70c3bea805ee | 11 | // The above copyright notice and this permission notice shall be included |
svlach | 0:70c3bea805ee | 12 | // in all copies or substantial portions of the Software. |
svlach | 0:70c3bea805ee | 13 | // |
svlach | 0:70c3bea805ee | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
svlach | 0:70c3bea805ee | 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
svlach | 0:70c3bea805ee | 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
svlach | 0:70c3bea805ee | 17 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
svlach | 0:70c3bea805ee | 18 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
svlach | 0:70c3bea805ee | 19 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
svlach | 0:70c3bea805ee | 20 | // OTHER DEALINGS IN THE SOFTWARE. |
svlach | 0:70c3bea805ee | 21 | // |
svlach | 0:70c3bea805ee | 22 | // Except as contained in this notice, the name of Dallas Semiconductor |
svlach | 0:70c3bea805ee | 23 | // shall not be used except as stated in the Dallas Semiconductor |
svlach | 0:70c3bea805ee | 24 | // Branding Policy. |
svlach | 0:70c3bea805ee | 25 | //--------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 26 | // |
svlach | 0:70c3bea805ee | 27 | // ownet.C - Network functions for 1-Wire net devices. |
svlach | 0:70c3bea805ee | 28 | // |
svlach | 0:70c3bea805ee | 29 | // Version: 3.00 |
svlach | 0:70c3bea805ee | 30 | // |
svlach | 0:70c3bea805ee | 31 | // History: 1.00 -> 1.01 Change to owFamilySearchSetup, LastDiscrepancy[portnum] |
svlach | 0:70c3bea805ee | 32 | // was set to 64 instead of 8 to enable devices with |
svlach | 0:70c3bea805ee | 33 | // early contention to go in the '0' direction first. |
svlach | 0:70c3bea805ee | 34 | // 1.02 -> 1.03 Initialized goodbits in owVerify |
svlach | 0:70c3bea805ee | 35 | // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for |
svlach | 0:70c3bea805ee | 36 | // multiple ports. |
svlach | 0:70c3bea805ee | 37 | // 2.00 -> 2.01 Added support for owError library |
svlach | 0:70c3bea805ee | 38 | // 2.01 -> 3.00 Make search functions consistent with AN187 |
svlach | 0:70c3bea805ee | 39 | // |
svlach | 0:70c3bea805ee | 40 | |
svlach | 0:70c3bea805ee | 41 | #include <stdio.h> |
svlach | 1:dcf2f8359398 | 42 | #include "ownet.h" |
svlach | 0:70c3bea805ee | 43 | |
svlach | 0:70c3bea805ee | 44 | // exportable functions defined in ownet.c |
svlach | 0:70c3bea805ee | 45 | SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *); |
svlach | 0:70c3bea805ee | 46 | |
svlach | 0:70c3bea805ee | 47 | // global variables for this module to hold search state information |
svlach | 0:70c3bea805ee | 48 | static SMALLINT LastDiscrepancy[MAX_PORTNUM]; |
svlach | 0:70c3bea805ee | 49 | static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM]; |
svlach | 0:70c3bea805ee | 50 | static SMALLINT LastDevice[MAX_PORTNUM]; |
svlach | 0:70c3bea805ee | 51 | uchar SerialNum[MAX_PORTNUM][8]; |
svlach | 0:70c3bea805ee | 52 | |
svlach | 0:70c3bea805ee | 53 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 54 | // The 'owFirst' finds the first device on the 1-Wire Net This function |
svlach | 0:70c3bea805ee | 55 | // contains one parameter 'alarm_only'. When |
svlach | 0:70c3bea805ee | 56 | // 'alarm_only' is TRUE (1) the find alarm command 0xEC is |
svlach | 0:70c3bea805ee | 57 | // sent instead of the normal search command 0xF0. |
svlach | 0:70c3bea805ee | 58 | // Using the find alarm command 0xEC will limit the search to only |
svlach | 0:70c3bea805ee | 59 | // 1-Wire devices that are in an 'alarm' state. |
svlach | 0:70c3bea805ee | 60 | // |
svlach | 0:70c3bea805ee | 61 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 62 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 63 | // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not |
svlach | 0:70c3bea805ee | 64 | // perform reset before search. |
svlach | 0:70c3bea805ee | 65 | // 'alarm_only' - TRUE (1) the find alarm command 0xEC is |
svlach | 0:70c3bea805ee | 66 | // sent instead of the normal search command 0xF0 |
svlach | 0:70c3bea805ee | 67 | // |
svlach | 0:70c3bea805ee | 68 | // Returns: TRUE (1) : when a 1-Wire device was found and it's |
svlach | 0:70c3bea805ee | 69 | // Serial Number placed in the global SerialNum[portnum] |
svlach | 0:70c3bea805ee | 70 | // FALSE (0): There are no devices on the 1-Wire Net. |
svlach | 0:70c3bea805ee | 71 | // |
svlach | 0:70c3bea805ee | 72 | SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only) |
svlach | 0:70c3bea805ee | 73 | { |
svlach | 0:70c3bea805ee | 74 | // reset the search state |
svlach | 0:70c3bea805ee | 75 | LastDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 76 | LastDevice[portnum] = FALSE; |
svlach | 0:70c3bea805ee | 77 | LastFamilyDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 78 | |
svlach | 0:70c3bea805ee | 79 | return owNext(portnum,do_reset,alarm_only); |
svlach | 0:70c3bea805ee | 80 | } |
svlach | 0:70c3bea805ee | 81 | |
svlach | 0:70c3bea805ee | 82 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 83 | // The 'owNext' function does a general search. This function |
svlach | 0:70c3bea805ee | 84 | // continues from the previos search state. The search state |
svlach | 0:70c3bea805ee | 85 | // can be reset by using the 'owFirst' function. |
svlach | 0:70c3bea805ee | 86 | // This function contains one parameter 'alarm_only'. |
svlach | 0:70c3bea805ee | 87 | // When 'alarm_only' is TRUE (1) the find alarm command |
svlach | 0:70c3bea805ee | 88 | // 0xEC is sent instead of the normal search command 0xF0. |
svlach | 0:70c3bea805ee | 89 | // Using the find alarm command 0xEC will limit the search to only |
svlach | 0:70c3bea805ee | 90 | // 1-Wire devices that are in an 'alarm' state. |
svlach | 0:70c3bea805ee | 91 | // |
svlach | 0:70c3bea805ee | 92 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 93 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 94 | // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not |
svlach | 0:70c3bea805ee | 95 | // perform reset before search. |
svlach | 0:70c3bea805ee | 96 | // 'alarm_only' - TRUE (1) the find alarm command 0xEC is |
svlach | 0:70c3bea805ee | 97 | // sent instead of the normal search command 0xF0 |
svlach | 0:70c3bea805ee | 98 | // |
svlach | 0:70c3bea805ee | 99 | // Returns: TRUE (1) : when a 1-Wire device was found and it's |
svlach | 0:70c3bea805ee | 100 | // Serial Number placed in the global SerialNum[portnum] |
svlach | 0:70c3bea805ee | 101 | // FALSE (0): when no new device was found. Either the |
svlach | 0:70c3bea805ee | 102 | // last search was the last device or there |
svlach | 0:70c3bea805ee | 103 | // are no devices on the 1-Wire Net. |
svlach | 0:70c3bea805ee | 104 | // |
svlach | 0:70c3bea805ee | 105 | SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only) |
svlach | 0:70c3bea805ee | 106 | { |
svlach | 0:70c3bea805ee | 107 | uchar bit_test, search_direction, bit_number; |
svlach | 0:70c3bea805ee | 108 | uchar last_zero, serial_byte_number, next_result; |
svlach | 0:70c3bea805ee | 109 | uchar serial_byte_mask; |
svlach | 0:70c3bea805ee | 110 | uchar lastcrc8=0; |
svlach | 0:70c3bea805ee | 111 | |
svlach | 0:70c3bea805ee | 112 | // initialize for search |
svlach | 0:70c3bea805ee | 113 | bit_number = 1; |
svlach | 0:70c3bea805ee | 114 | last_zero = 0; |
svlach | 0:70c3bea805ee | 115 | serial_byte_number = 0; |
svlach | 0:70c3bea805ee | 116 | serial_byte_mask = 1; |
svlach | 0:70c3bea805ee | 117 | next_result = 0; |
svlach | 0:70c3bea805ee | 118 | setcrc8(portnum,0); |
svlach | 0:70c3bea805ee | 119 | |
svlach | 0:70c3bea805ee | 120 | // if the last call was not the last one |
svlach | 0:70c3bea805ee | 121 | if (!LastDevice[portnum]) |
svlach | 0:70c3bea805ee | 122 | { |
svlach | 0:70c3bea805ee | 123 | // check if reset first is requested |
svlach | 0:70c3bea805ee | 124 | if (do_reset) |
svlach | 0:70c3bea805ee | 125 | { |
svlach | 0:70c3bea805ee | 126 | // reset the 1-wire |
svlach | 0:70c3bea805ee | 127 | // if there are no parts on 1-wire, return FALSE |
svlach | 0:70c3bea805ee | 128 | if (!owTouchReset(portnum)) |
svlach | 0:70c3bea805ee | 129 | { |
svlach | 0:70c3bea805ee | 130 | // printf("owTouchReset failed\r\n"); |
svlach | 0:70c3bea805ee | 131 | // reset the search |
svlach | 0:70c3bea805ee | 132 | LastDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 133 | LastFamilyDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 134 | OWERROR(OWERROR_NO_DEVICES_ON_NET); |
svlach | 0:70c3bea805ee | 135 | return FALSE; |
svlach | 0:70c3bea805ee | 136 | } |
svlach | 0:70c3bea805ee | 137 | } |
svlach | 0:70c3bea805ee | 138 | |
svlach | 0:70c3bea805ee | 139 | // If finding alarming devices issue a different command |
svlach | 0:70c3bea805ee | 140 | if (alarm_only) |
svlach | 0:70c3bea805ee | 141 | owWriteByte(portnum,0xEC); // issue the alarming search command |
svlach | 0:70c3bea805ee | 142 | else |
svlach | 0:70c3bea805ee | 143 | owWriteByte(portnum,0xF0); // issue the search command |
svlach | 0:70c3bea805ee | 144 | |
svlach | 0:70c3bea805ee | 145 | //pause before beginning the search |
svlach | 0:70c3bea805ee | 146 | //usDelay(100); |
svlach | 0:70c3bea805ee | 147 | |
svlach | 0:70c3bea805ee | 148 | // loop to do the search |
svlach | 0:70c3bea805ee | 149 | do |
svlach | 0:70c3bea805ee | 150 | { |
svlach | 0:70c3bea805ee | 151 | // read a bit and its compliment |
svlach | 0:70c3bea805ee | 152 | bit_test = owTouchBit(portnum,1) << 1; |
svlach | 0:70c3bea805ee | 153 | bit_test |= owTouchBit(portnum,1); |
svlach | 0:70c3bea805ee | 154 | |
svlach | 0:70c3bea805ee | 155 | // check for no devices on 1-wire |
svlach | 0:70c3bea805ee | 156 | if (bit_test == 3) |
svlach | 0:70c3bea805ee | 157 | break; |
svlach | 0:70c3bea805ee | 158 | else |
svlach | 0:70c3bea805ee | 159 | { |
svlach | 0:70c3bea805ee | 160 | // all devices coupled have 0 or 1 |
svlach | 0:70c3bea805ee | 161 | if (bit_test > 0) |
svlach | 0:70c3bea805ee | 162 | search_direction = !(bit_test & 0x01); // bit write value for search |
svlach | 0:70c3bea805ee | 163 | else |
svlach | 0:70c3bea805ee | 164 | { |
svlach | 0:70c3bea805ee | 165 | // if this discrepancy if before the Last Discrepancy |
svlach | 0:70c3bea805ee | 166 | // on a previous next then pick the same as last time |
svlach | 0:70c3bea805ee | 167 | if (bit_number < LastDiscrepancy[portnum]) |
svlach | 0:70c3bea805ee | 168 | search_direction = ((SerialNum[portnum][serial_byte_number] & serial_byte_mask) > 0); |
svlach | 0:70c3bea805ee | 169 | else |
svlach | 0:70c3bea805ee | 170 | // if equal to last pick 1, if not then pick 0 |
svlach | 0:70c3bea805ee | 171 | search_direction = (bit_number == LastDiscrepancy[portnum]); |
svlach | 0:70c3bea805ee | 172 | |
svlach | 0:70c3bea805ee | 173 | // if 0 was picked then record its position in LastZero |
svlach | 0:70c3bea805ee | 174 | if (search_direction == 0) |
svlach | 0:70c3bea805ee | 175 | { |
svlach | 0:70c3bea805ee | 176 | last_zero = bit_number; |
svlach | 0:70c3bea805ee | 177 | |
svlach | 0:70c3bea805ee | 178 | // check for Last discrepancy in family |
svlach | 0:70c3bea805ee | 179 | if (last_zero < 9) |
svlach | 0:70c3bea805ee | 180 | LastFamilyDiscrepancy[portnum] = last_zero; |
svlach | 0:70c3bea805ee | 181 | } |
svlach | 0:70c3bea805ee | 182 | } |
svlach | 0:70c3bea805ee | 183 | |
svlach | 0:70c3bea805ee | 184 | // set or clear the bit in the SerialNum[portnum] byte serial_byte_number |
svlach | 0:70c3bea805ee | 185 | // with mask serial_byte_mask |
svlach | 0:70c3bea805ee | 186 | if (search_direction == 1) |
svlach | 0:70c3bea805ee | 187 | SerialNum[portnum][serial_byte_number] |= serial_byte_mask; |
svlach | 0:70c3bea805ee | 188 | else |
svlach | 0:70c3bea805ee | 189 | SerialNum[portnum][serial_byte_number] &= ~serial_byte_mask; |
svlach | 0:70c3bea805ee | 190 | |
svlach | 0:70c3bea805ee | 191 | // serial number search direction write bit |
svlach | 0:70c3bea805ee | 192 | owTouchBit(portnum,search_direction); |
svlach | 0:70c3bea805ee | 193 | |
svlach | 0:70c3bea805ee | 194 | // increment the byte counter bit_number |
svlach | 0:70c3bea805ee | 195 | // and shift the mask serial_byte_mask |
svlach | 0:70c3bea805ee | 196 | bit_number++; |
svlach | 0:70c3bea805ee | 197 | serial_byte_mask <<= 1; |
svlach | 0:70c3bea805ee | 198 | |
svlach | 0:70c3bea805ee | 199 | // if the mask is 0 then go to new SerialNum[portnum] byte serial_byte_number |
svlach | 0:70c3bea805ee | 200 | // and reset mask |
svlach | 0:70c3bea805ee | 201 | if (serial_byte_mask == 0) |
svlach | 0:70c3bea805ee | 202 | { |
svlach | 0:70c3bea805ee | 203 | // The below has been added to accomidate the valid CRC with the |
svlach | 0:70c3bea805ee | 204 | // possible changing serial number values of the DS28E04. |
svlach | 0:70c3bea805ee | 205 | if (((SerialNum[portnum][0] & 0x7F) == 0x1C) && (serial_byte_number == 1)) |
svlach | 0:70c3bea805ee | 206 | lastcrc8 = docrc8(portnum,0x7F); |
svlach | 0:70c3bea805ee | 207 | else |
svlach | 0:70c3bea805ee | 208 | lastcrc8 = docrc8(portnum,SerialNum[portnum][serial_byte_number]); // accumulate the CRC |
svlach | 0:70c3bea805ee | 209 | |
svlach | 0:70c3bea805ee | 210 | serial_byte_number++; |
svlach | 0:70c3bea805ee | 211 | serial_byte_mask = 1; |
svlach | 0:70c3bea805ee | 212 | } |
svlach | 0:70c3bea805ee | 213 | } |
svlach | 0:70c3bea805ee | 214 | } |
svlach | 0:70c3bea805ee | 215 | while(serial_byte_number < 8); // loop until through all SerialNum[portnum] bytes 0-7 |
svlach | 0:70c3bea805ee | 216 | |
svlach | 0:70c3bea805ee | 217 | // if the search was successful then |
svlach | 0:70c3bea805ee | 218 | if (!((bit_number < 65) || lastcrc8)) |
svlach | 0:70c3bea805ee | 219 | { |
svlach | 0:70c3bea805ee | 220 | // search successful so set LastDiscrepancy[portnum],LastDevice[portnum],next_result |
svlach | 0:70c3bea805ee | 221 | LastDiscrepancy[portnum] = last_zero; |
svlach | 0:70c3bea805ee | 222 | LastDevice[portnum] = (LastDiscrepancy[portnum] == 0); |
svlach | 0:70c3bea805ee | 223 | next_result = TRUE; |
svlach | 0:70c3bea805ee | 224 | } |
svlach | 0:70c3bea805ee | 225 | } |
svlach | 0:70c3bea805ee | 226 | |
svlach | 0:70c3bea805ee | 227 | // if no device found then reset counters so next 'next' will be |
svlach | 0:70c3bea805ee | 228 | // like a first |
svlach | 0:70c3bea805ee | 229 | if (!next_result || !SerialNum[portnum][0]) |
svlach | 0:70c3bea805ee | 230 | { |
svlach | 0:70c3bea805ee | 231 | LastDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 232 | LastDevice[portnum] = FALSE; |
svlach | 0:70c3bea805ee | 233 | LastFamilyDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 234 | next_result = FALSE; |
svlach | 0:70c3bea805ee | 235 | } |
svlach | 0:70c3bea805ee | 236 | |
svlach | 0:70c3bea805ee | 237 | return next_result; |
svlach | 0:70c3bea805ee | 238 | } |
svlach | 0:70c3bea805ee | 239 | |
svlach | 0:70c3bea805ee | 240 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 241 | // The 'owSerialNum' function either reads or sets the SerialNum buffer |
svlach | 0:70c3bea805ee | 242 | // that is used in the search functions 'owFirst' and 'owNext'. |
svlach | 0:70c3bea805ee | 243 | // This function contains two parameters, 'serialnum_buf' is a pointer |
svlach | 0:70c3bea805ee | 244 | // to a buffer provided by the caller. 'serialnum_buf' should point to |
svlach | 0:70c3bea805ee | 245 | // an array of 8 unsigned chars. The second parameter is a flag called |
svlach | 0:70c3bea805ee | 246 | // 'do_read' that is TRUE (1) if the operation is to read and FALSE |
svlach | 0:70c3bea805ee | 247 | // (0) if the operation is to set the internal SerialNum buffer from |
svlach | 0:70c3bea805ee | 248 | // the data in the provided buffer. |
svlach | 0:70c3bea805ee | 249 | // |
svlach | 0:70c3bea805ee | 250 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 251 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 252 | // 'serialnum_buf' - buffer to that contains the serial number to set |
svlach | 0:70c3bea805ee | 253 | // when do_read = FALSE (0) and buffer to get the serial |
svlach | 0:70c3bea805ee | 254 | // number when do_read = TRUE (1). |
svlach | 0:70c3bea805ee | 255 | // 'do_read' - flag to indicate reading (1) or setting (0) the current |
svlach | 0:70c3bea805ee | 256 | // serial number. |
svlach | 0:70c3bea805ee | 257 | // |
svlach | 0:70c3bea805ee | 258 | void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read) |
svlach | 0:70c3bea805ee | 259 | { |
svlach | 0:70c3bea805ee | 260 | uchar i; |
svlach | 0:70c3bea805ee | 261 | |
svlach | 0:70c3bea805ee | 262 | // read the internal buffer and place in 'serialnum_buf' |
svlach | 0:70c3bea805ee | 263 | if (do_read) |
svlach | 0:70c3bea805ee | 264 | { |
svlach | 0:70c3bea805ee | 265 | for (i = 0; i < 8; i++) |
svlach | 0:70c3bea805ee | 266 | serialnum_buf[i] = SerialNum[portnum][i]; |
svlach | 0:70c3bea805ee | 267 | } |
svlach | 0:70c3bea805ee | 268 | // set the internal buffer from the data in 'serialnum_buf' |
svlach | 0:70c3bea805ee | 269 | else |
svlach | 0:70c3bea805ee | 270 | { |
svlach | 0:70c3bea805ee | 271 | for (i = 0; i < 8; i++) |
svlach | 0:70c3bea805ee | 272 | SerialNum[portnum][i] = serialnum_buf[i]; |
svlach | 0:70c3bea805ee | 273 | } |
svlach | 0:70c3bea805ee | 274 | } |
svlach | 0:70c3bea805ee | 275 | |
svlach | 0:70c3bea805ee | 276 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 277 | // Setup the search algorithm to find a certain family of devices |
svlach | 0:70c3bea805ee | 278 | // the next time a search function is called 'owNext'. |
svlach | 0:70c3bea805ee | 279 | // |
svlach | 0:70c3bea805ee | 280 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
svlach | 0:70c3bea805ee | 281 | // OpenCOM to indicate the port number. |
svlach | 0:70c3bea805ee | 282 | // 'search_family' - family code type to set the search algorithm to find |
svlach | 0:70c3bea805ee | 283 | // next. |
svlach | 0:70c3bea805ee | 284 | // |
svlach | 0:70c3bea805ee | 285 | void owFamilySearchSetup(int portnum, SMALLINT search_family) |
svlach | 0:70c3bea805ee | 286 | { |
svlach | 0:70c3bea805ee | 287 | uchar i; |
svlach | 0:70c3bea805ee | 288 | |
svlach | 0:70c3bea805ee | 289 | // set the search state to find SearchFamily type devices |
svlach | 0:70c3bea805ee | 290 | SerialNum[portnum][0] = search_family; |
svlach | 0:70c3bea805ee | 291 | for (i = 1; i < 8; i++) |
svlach | 0:70c3bea805ee | 292 | SerialNum[portnum][i] = 0; |
svlach | 0:70c3bea805ee | 293 | LastDiscrepancy[portnum] = 64; |
svlach | 0:70c3bea805ee | 294 | LastDevice[portnum] = FALSE; |
svlach | 0:70c3bea805ee | 295 | } |
svlach | 0:70c3bea805ee | 296 | |
svlach | 0:70c3bea805ee | 297 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 298 | // Set the current search state to skip the current family code. |
svlach | 0:70c3bea805ee | 299 | // |
svlach | 0:70c3bea805ee | 300 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 301 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 302 | // |
svlach | 0:70c3bea805ee | 303 | void owSkipFamily(int portnum) |
svlach | 0:70c3bea805ee | 304 | { |
svlach | 0:70c3bea805ee | 305 | // set the Last discrepancy to last family discrepancy |
svlach | 0:70c3bea805ee | 306 | LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum]; |
svlach | 0:70c3bea805ee | 307 | LastFamilyDiscrepancy[portnum] = 0; |
svlach | 0:70c3bea805ee | 308 | |
svlach | 0:70c3bea805ee | 309 | // check for end of list |
svlach | 0:70c3bea805ee | 310 | if (LastDiscrepancy[portnum] == 0) |
svlach | 0:70c3bea805ee | 311 | LastDevice[portnum] = TRUE; |
svlach | 0:70c3bea805ee | 312 | } |
svlach | 0:70c3bea805ee | 313 | |
svlach | 0:70c3bea805ee | 314 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 315 | // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial |
svlach | 0:70c3bea805ee | 316 | // Number command followed by the current SerialNum code. After this |
svlach | 0:70c3bea805ee | 317 | // function is complete the 1-Wire device is ready to accept device-specific |
svlach | 0:70c3bea805ee | 318 | // commands. |
svlach | 0:70c3bea805ee | 319 | // |
svlach | 0:70c3bea805ee | 320 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 321 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 322 | // |
svlach | 0:70c3bea805ee | 323 | // Returns: TRUE (1) : reset indicates present and device is ready |
svlach | 0:70c3bea805ee | 324 | // for commands. |
svlach | 0:70c3bea805ee | 325 | // FALSE (0): reset does not indicate presence or echos 'writes' |
svlach | 0:70c3bea805ee | 326 | // are not correct. |
svlach | 0:70c3bea805ee | 327 | // |
svlach | 0:70c3bea805ee | 328 | SMALLINT owAccess(int portnum) |
svlach | 0:70c3bea805ee | 329 | { |
svlach | 0:70c3bea805ee | 330 | uchar sendpacket[9]; |
svlach | 0:70c3bea805ee | 331 | uchar i; |
svlach | 0:70c3bea805ee | 332 | |
svlach | 0:70c3bea805ee | 333 | // reset the 1-wire |
svlach | 0:70c3bea805ee | 334 | if (owTouchReset(portnum)) |
svlach | 0:70c3bea805ee | 335 | { |
svlach | 0:70c3bea805ee | 336 | // create a buffer to use with block function |
svlach | 0:70c3bea805ee | 337 | // match Serial Number command 0x55 |
svlach | 0:70c3bea805ee | 338 | sendpacket[0] = 0x55; |
svlach | 0:70c3bea805ee | 339 | // Serial Number |
svlach | 0:70c3bea805ee | 340 | for (i = 1; i < 9; i++) |
svlach | 0:70c3bea805ee | 341 | sendpacket[i] = SerialNum[portnum][i-1]; |
svlach | 0:70c3bea805ee | 342 | |
svlach | 0:70c3bea805ee | 343 | // send/recieve the transfer buffer |
svlach | 0:70c3bea805ee | 344 | if (owBlock(portnum,FALSE,sendpacket,9)) |
svlach | 0:70c3bea805ee | 345 | { |
svlach | 0:70c3bea805ee | 346 | // verify that the echo of the writes was correct |
svlach | 0:70c3bea805ee | 347 | for (i = 1; i < 9; i++) |
svlach | 0:70c3bea805ee | 348 | if (sendpacket[i] != SerialNum[portnum][i-1]) |
svlach | 0:70c3bea805ee | 349 | return FALSE; |
svlach | 0:70c3bea805ee | 350 | if (sendpacket[0] != 0x55) |
svlach | 0:70c3bea805ee | 351 | { |
svlach | 0:70c3bea805ee | 352 | OWERROR(OWERROR_WRITE_VERIFY_FAILED); |
svlach | 0:70c3bea805ee | 353 | return FALSE; |
svlach | 0:70c3bea805ee | 354 | } |
svlach | 0:70c3bea805ee | 355 | else |
svlach | 0:70c3bea805ee | 356 | return TRUE; |
svlach | 0:70c3bea805ee | 357 | } |
svlach | 0:70c3bea805ee | 358 | else |
svlach | 0:70c3bea805ee | 359 | OWERROR(OWERROR_BLOCK_FAILED); |
svlach | 0:70c3bea805ee | 360 | } |
svlach | 0:70c3bea805ee | 361 | else |
svlach | 0:70c3bea805ee | 362 | OWERROR(OWERROR_NO_DEVICES_ON_NET); |
svlach | 0:70c3bea805ee | 363 | |
svlach | 0:70c3bea805ee | 364 | // reset or match echo failed |
svlach | 0:70c3bea805ee | 365 | return FALSE; |
svlach | 0:70c3bea805ee | 366 | } |
svlach | 0:70c3bea805ee | 367 | |
svlach | 0:70c3bea805ee | 368 | //---------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 369 | // The function 'owVerify' verifies that the current device |
svlach | 0:70c3bea805ee | 370 | // is in contact with the 1-Wire Net. |
svlach | 0:70c3bea805ee | 371 | // Using the find alarm command 0xEC will verify that the device |
svlach | 0:70c3bea805ee | 372 | // is in contact with the 1-Wire Net and is in an 'alarm' state. |
svlach | 0:70c3bea805ee | 373 | // |
svlach | 0:70c3bea805ee | 374 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 375 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 376 | // 'alarm_only' - TRUE (1) the find alarm command 0xEC |
svlach | 0:70c3bea805ee | 377 | // is sent instead of the normal search |
svlach | 0:70c3bea805ee | 378 | // command 0xF0. |
svlach | 0:70c3bea805ee | 379 | // |
svlach | 0:70c3bea805ee | 380 | // Returns: TRUE (1) : when the 1-Wire device was verified |
svlach | 0:70c3bea805ee | 381 | // to be on the 1-Wire Net |
svlach | 0:70c3bea805ee | 382 | // with alarm_only == FALSE |
svlach | 0:70c3bea805ee | 383 | // or verified to be on the 1-Wire Net |
svlach | 0:70c3bea805ee | 384 | // AND in an alarm state when |
svlach | 0:70c3bea805ee | 385 | // alarm_only == TRUE. |
svlach | 0:70c3bea805ee | 386 | // FALSE (0): the 1-Wire device was not on the |
svlach | 0:70c3bea805ee | 387 | // 1-Wire Net or if alarm_only |
svlach | 0:70c3bea805ee | 388 | // == TRUE, the device may be on the |
svlach | 0:70c3bea805ee | 389 | // 1-Wire Net but in a non-alarm state. |
svlach | 0:70c3bea805ee | 390 | // |
svlach | 0:70c3bea805ee | 391 | SMALLINT owVerify(int portnum, SMALLINT alarm_only) |
svlach | 0:70c3bea805ee | 392 | { |
svlach | 0:70c3bea805ee | 393 | uchar i,sendlen=0,goodbits=0,cnt=0,s,tst; |
svlach | 0:70c3bea805ee | 394 | uchar sendpacket[50]; |
svlach | 0:70c3bea805ee | 395 | |
svlach | 0:70c3bea805ee | 396 | // construct the search |
svlach | 0:70c3bea805ee | 397 | if (alarm_only) |
svlach | 0:70c3bea805ee | 398 | sendpacket[sendlen++] = 0xEC; // issue the alarming search command |
svlach | 0:70c3bea805ee | 399 | else |
svlach | 0:70c3bea805ee | 400 | sendpacket[sendlen++] = 0xF0; // issue the search command |
svlach | 0:70c3bea805ee | 401 | // set all bits at first |
svlach | 0:70c3bea805ee | 402 | for (i = 1; i <= 24; i++) |
svlach | 0:70c3bea805ee | 403 | sendpacket[sendlen++] = 0xFF; |
svlach | 0:70c3bea805ee | 404 | // now set or clear apropriate bits for search |
svlach | 0:70c3bea805ee | 405 | for (i = 0; i < 64; i++) |
svlach | 0:70c3bea805ee | 406 | bitacc(WRITE_FUNCTION,bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),(int)((i+1)*3-1),&sendpacket[1]); |
svlach | 0:70c3bea805ee | 407 | |
svlach | 0:70c3bea805ee | 408 | // send/recieve the transfer buffer |
svlach | 0:70c3bea805ee | 409 | if (owBlock(portnum,TRUE,sendpacket,sendlen)) |
svlach | 0:70c3bea805ee | 410 | { |
svlach | 0:70c3bea805ee | 411 | // check results to see if it was a success |
svlach | 0:70c3bea805ee | 412 | for (i = 0; i < 192; i += 3) |
svlach | 0:70c3bea805ee | 413 | { |
svlach | 0:70c3bea805ee | 414 | tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) | |
svlach | 0:70c3bea805ee | 415 | bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]); |
svlach | 0:70c3bea805ee | 416 | |
svlach | 0:70c3bea805ee | 417 | s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]); |
svlach | 0:70c3bea805ee | 418 | |
svlach | 0:70c3bea805ee | 419 | if (tst == 0x03) // no device on line |
svlach | 0:70c3bea805ee | 420 | { |
svlach | 0:70c3bea805ee | 421 | goodbits = 0; // number of good bits set to zero |
svlach | 0:70c3bea805ee | 422 | break; // quit |
svlach | 0:70c3bea805ee | 423 | } |
svlach | 0:70c3bea805ee | 424 | |
svlach | 0:70c3bea805ee | 425 | if (((s == 0x01) && (tst == 0x02)) || |
svlach | 0:70c3bea805ee | 426 | ((s == 0x00) && (tst == 0x01)) ) // correct bit |
svlach | 0:70c3bea805ee | 427 | goodbits++; // count as a good bit |
svlach | 0:70c3bea805ee | 428 | } |
svlach | 0:70c3bea805ee | 429 | |
svlach | 0:70c3bea805ee | 430 | // check too see if there were enough good bits to be successful |
svlach | 0:70c3bea805ee | 431 | if (goodbits >= 8) |
svlach | 0:70c3bea805ee | 432 | return TRUE; |
svlach | 0:70c3bea805ee | 433 | } |
svlach | 0:70c3bea805ee | 434 | else |
svlach | 0:70c3bea805ee | 435 | OWERROR(OWERROR_BLOCK_FAILED); |
svlach | 0:70c3bea805ee | 436 | |
svlach | 0:70c3bea805ee | 437 | // block fail or device not present |
svlach | 0:70c3bea805ee | 438 | return FALSE; |
svlach | 0:70c3bea805ee | 439 | } |
svlach | 0:70c3bea805ee | 440 | |
svlach | 0:70c3bea805ee | 441 | //---------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 442 | // Perform a overdrive MATCH command to select the 1-Wire device with |
svlach | 0:70c3bea805ee | 443 | // the address in the ID data register. |
svlach | 0:70c3bea805ee | 444 | // |
svlach | 0:70c3bea805ee | 445 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to |
svlach | 0:70c3bea805ee | 446 | // indicate the symbolic port number. |
svlach | 0:70c3bea805ee | 447 | // |
svlach | 0:70c3bea805ee | 448 | // Returns: TRUE: If the device is present on the 1-Wire Net and |
svlach | 0:70c3bea805ee | 449 | // can do overdrive then the device is selected. |
svlach | 0:70c3bea805ee | 450 | // FALSE: Device is not present or not capable of overdrive. |
svlach | 0:70c3bea805ee | 451 | // |
svlach | 0:70c3bea805ee | 452 | // *Note: This function could be converted to send DS2480 |
svlach | 0:70c3bea805ee | 453 | // commands in one packet. |
svlach | 0:70c3bea805ee | 454 | // |
svlach | 0:70c3bea805ee | 455 | SMALLINT owOverdriveAccess(int portnum) |
svlach | 0:70c3bea805ee | 456 | { |
svlach | 0:70c3bea805ee | 457 | uchar sendpacket[8]; |
svlach | 0:70c3bea805ee | 458 | uchar i, bad_echo = FALSE; |
svlach | 0:70c3bea805ee | 459 | |
svlach | 0:70c3bea805ee | 460 | // make sure normal level |
svlach | 0:70c3bea805ee | 461 | owLevel(portnum,MODE_NORMAL); |
svlach | 0:70c3bea805ee | 462 | |
svlach | 0:70c3bea805ee | 463 | // force to normal communication speed |
svlach | 0:70c3bea805ee | 464 | owSpeed(portnum,MODE_NORMAL); |
svlach | 0:70c3bea805ee | 465 | |
svlach | 0:70c3bea805ee | 466 | // call the 1-Wire Net reset function |
svlach | 0:70c3bea805ee | 467 | if (owTouchReset(portnum)) |
svlach | 0:70c3bea805ee | 468 | { |
svlach | 0:70c3bea805ee | 469 | // send the match command 0x69 |
svlach | 0:70c3bea805ee | 470 | if (owWriteByte(portnum,0x69)) |
svlach | 0:70c3bea805ee | 471 | { |
svlach | 0:70c3bea805ee | 472 | // switch to overdrive communication speed |
svlach | 0:70c3bea805ee | 473 | owSpeed(portnum,MODE_OVERDRIVE); |
svlach | 0:70c3bea805ee | 474 | |
svlach | 0:70c3bea805ee | 475 | // create a buffer to use with block function |
svlach | 0:70c3bea805ee | 476 | // Serial Number |
svlach | 0:70c3bea805ee | 477 | for (i = 0; i < 8; i++) |
svlach | 0:70c3bea805ee | 478 | sendpacket[i] = SerialNum[portnum][i]; |
svlach | 0:70c3bea805ee | 479 | |
svlach | 0:70c3bea805ee | 480 | // send/recieve the transfer buffer |
svlach | 0:70c3bea805ee | 481 | if (owBlock(portnum,FALSE,sendpacket,8)) |
svlach | 0:70c3bea805ee | 482 | { |
svlach | 0:70c3bea805ee | 483 | // verify that the echo of the writes was correct |
svlach | 0:70c3bea805ee | 484 | for (i = 0; i < 8; i++) |
svlach | 0:70c3bea805ee | 485 | if (sendpacket[i] != SerialNum[portnum][i]) |
svlach | 0:70c3bea805ee | 486 | bad_echo = TRUE; |
svlach | 0:70c3bea805ee | 487 | // if echo ok then success |
svlach | 0:70c3bea805ee | 488 | if (!bad_echo) |
svlach | 0:70c3bea805ee | 489 | return TRUE; |
svlach | 0:70c3bea805ee | 490 | else |
svlach | 0:70c3bea805ee | 491 | OWERROR(OWERROR_WRITE_VERIFY_FAILED); |
svlach | 0:70c3bea805ee | 492 | } |
svlach | 0:70c3bea805ee | 493 | else |
svlach | 0:70c3bea805ee | 494 | OWERROR(OWERROR_BLOCK_FAILED); |
svlach | 0:70c3bea805ee | 495 | } |
svlach | 0:70c3bea805ee | 496 | else |
svlach | 0:70c3bea805ee | 497 | OWERROR(OWERROR_WRITE_BYTE_FAILED); |
svlach | 0:70c3bea805ee | 498 | } |
svlach | 0:70c3bea805ee | 499 | else |
svlach | 0:70c3bea805ee | 500 | OWERROR(OWERROR_NO_DEVICES_ON_NET); |
svlach | 0:70c3bea805ee | 501 | |
svlach | 0:70c3bea805ee | 502 | // failure, force back to normal communication speed |
svlach | 0:70c3bea805ee | 503 | owSpeed(portnum,MODE_NORMAL); |
svlach | 0:70c3bea805ee | 504 | |
svlach | 0:70c3bea805ee | 505 | return FALSE; |
svlach | 0:70c3bea805ee | 506 | } |
svlach | 0:70c3bea805ee | 507 | |
svlach | 0:70c3bea805ee | 508 | //-------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 509 | // Bit utility to read and write a bit in the buffer 'buf'. |
svlach | 0:70c3bea805ee | 510 | // |
svlach | 0:70c3bea805ee | 511 | // 'op' - operation (1) to set and (0) to read |
svlach | 0:70c3bea805ee | 512 | // 'state' - set (1) or clear (0) if operation is write (1) |
svlach | 0:70c3bea805ee | 513 | // 'loc' - bit number location to read or write |
svlach | 0:70c3bea805ee | 514 | // 'buf' - pointer to array of bytes that contains the bit |
svlach | 0:70c3bea805ee | 515 | // to read or write |
svlach | 0:70c3bea805ee | 516 | // |
svlach | 0:70c3bea805ee | 517 | // Returns: 1 if operation is set (1) |
svlach | 0:70c3bea805ee | 518 | // 0/1 state of bit number 'loc' if operation is reading |
svlach | 0:70c3bea805ee | 519 | // |
svlach | 0:70c3bea805ee | 520 | SMALLINT bitacc(SMALLINT op, SMALLINT state, SMALLINT loc, uchar *buf) |
svlach | 0:70c3bea805ee | 521 | { |
svlach | 0:70c3bea805ee | 522 | SMALLINT nbyt,nbit; |
svlach | 0:70c3bea805ee | 523 | |
svlach | 0:70c3bea805ee | 524 | nbyt = (loc / 8); |
svlach | 0:70c3bea805ee | 525 | nbit = loc - (nbyt * 8); |
svlach | 0:70c3bea805ee | 526 | |
svlach | 0:70c3bea805ee | 527 | if (op == WRITE_FUNCTION) |
svlach | 0:70c3bea805ee | 528 | { |
svlach | 0:70c3bea805ee | 529 | if (state) |
svlach | 0:70c3bea805ee | 530 | buf[nbyt] |= (0x01 << nbit); |
svlach | 0:70c3bea805ee | 531 | else |
svlach | 0:70c3bea805ee | 532 | buf[nbyt] &= ~(0x01 << nbit); |
svlach | 0:70c3bea805ee | 533 | |
svlach | 0:70c3bea805ee | 534 | return 1; |
svlach | 0:70c3bea805ee | 535 | } |
svlach | 0:70c3bea805ee | 536 | else |
svlach | 0:70c3bea805ee | 537 | return ((buf[nbyt] >> nbit) & 0x01); |
svlach | 0:70c3bea805ee | 538 | } |