one wire driver

Dependents:   09_PT1000 10_PT1000 11_PT1000

Committer:
rs27
Date:
Sat Apr 27 18:56:53 2013 +0000
Revision:
0:c57706d25cf0
Child:
1:0950824b1ca3
alle i2c Befehle in i2c.write und i2c.read abge?ndert;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rs27 0:c57706d25cf0 1
rs27 0:c57706d25cf0 2
rs27 0:c57706d25cf0 3 #include "mbed.h"
rs27 0:c57706d25cf0 4 #include "DS2482.h"
rs27 0:c57706d25cf0 5
rs27 0:c57706d25cf0 6 //-----------------------------------------------------------------------------
rs27 0:c57706d25cf0 7 // CRC = X^8 + X^5 + X^4 + 1
rs27 0:c57706d25cf0 8
rs27 0:c57706d25cf0 9 #define CRC_TABLE_ITEMS 256
rs27 0:c57706d25cf0 10
rs27 0:c57706d25cf0 11 const uint8_t crc_table[CRC_TABLE_ITEMS] =
rs27 0:c57706d25cf0 12 {
rs27 0:c57706d25cf0 13 0 , 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
rs27 0:c57706d25cf0 14 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
rs27 0:c57706d25cf0 15 35 ,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
rs27 0:c57706d25cf0 16 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
rs27 0:c57706d25cf0 17 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
rs27 0:c57706d25cf0 18 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
rs27 0:c57706d25cf0 19 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
rs27 0:c57706d25cf0 20 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
rs27 0:c57706d25cf0 21 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
rs27 0:c57706d25cf0 22 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
rs27 0:c57706d25cf0 23 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
rs27 0:c57706d25cf0 24 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
rs27 0:c57706d25cf0 25 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
rs27 0:c57706d25cf0 26 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
rs27 0:c57706d25cf0 27 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
rs27 0:c57706d25cf0 28 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
rs27 0:c57706d25cf0 29 };
rs27 0:c57706d25cf0 30
rs27 0:c57706d25cf0 31
rs27 0:c57706d25cf0 32 //==========================================================================
rs27 0:c57706d25cf0 33 // destructor
rs27 0:c57706d25cf0 34
rs27 0:c57706d25cf0 35 DS2482::DS2482(PinName sda, PinName scl, int address) : i2c(sda, scl)
rs27 0:c57706d25cf0 36 {
rs27 0:c57706d25cf0 37 addr = address;
rs27 0:c57706d25cf0 38 i2c.frequency(100000);
rs27 0:c57706d25cf0 39 }
rs27 0:c57706d25cf0 40
rs27 0:c57706d25cf0 41 //-----------------------------------------------------------------------------
rs27 0:c57706d25cf0 42 // Calculate the CRC8 of the byte value provided with the current
rs27 0:c57706d25cf0 43 // global 'crc8' value.
rs27 0:c57706d25cf0 44 // Returns current global crc8 value
rs27 0:c57706d25cf0 45 //
rs27 0:c57706d25cf0 46 uint8_t DS2482::crc_calc(uint8_t x)
rs27 0:c57706d25cf0 47 {
rs27 0:c57706d25cf0 48 crc_value = crc_table[crc_value ^ x];
rs27 0:c57706d25cf0 49 return crc_value;
rs27 0:c57706d25cf0 50 }
rs27 0:c57706d25cf0 51
rs27 0:c57706d25cf0 52 //==========================================================================
rs27 0:c57706d25cf0 53 // I2C DS2482
rs27 0:c57706d25cf0 54 //
rs27 0:c57706d25cf0 55 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 56 // DS2482 Detect routine that sets the I2C address and then performs a
rs27 0:c57706d25cf0 57 // device reset followed by writing the configuration byte to default values:
rs27 0:c57706d25cf0 58 // 1-Wire speed (c1WS) = standard (0)
rs27 0:c57706d25cf0 59 // Strong pullup (cSPU) = off (0)
rs27 0:c57706d25cf0 60 // Presence pulse masking (cPPM) = off (0)
rs27 0:c57706d25cf0 61 // Active pullup (cAPU) = on (CONFIG_APU = 0x01)
rs27 0:c57706d25cf0 62 //
rs27 0:c57706d25cf0 63 // Returns: TRUE if device was detected and written
rs27 0:c57706d25cf0 64 // FALSE device not detected or failure to write configuration byte
rs27 0:c57706d25cf0 65 //uint8_t DS2482_detect(unsigned char addr)
rs27 0:c57706d25cf0 66 //
rs27 0:c57706d25cf0 67 uint8_t DS2482::detect(void)
rs27 0:c57706d25cf0 68 {
rs27 0:c57706d25cf0 69
rs27 0:c57706d25cf0 70 if (!reset()) // reset the DS2482 ON selected address
rs27 0:c57706d25cf0 71 {
rs27 0:c57706d25cf0 72 #if OW_MASTER_START
rs27 0:c57706d25cf0 73 printf("\r\n--- DS2482 bus0 reset not executed");
rs27 0:c57706d25cf0 74 #endif
rs27 0:c57706d25cf0 75
rs27 0:c57706d25cf0 76 return false;
rs27 0:c57706d25cf0 77 }
rs27 0:c57706d25cf0 78
rs27 0:c57706d25cf0 79 // default configuration
rs27 0:c57706d25cf0 80 c1WS = 0;
rs27 0:c57706d25cf0 81 cSPU = 0;
rs27 0:c57706d25cf0 82 cPPM = 0;
rs27 0:c57706d25cf0 83 cAPU = DS2482_CFG_APU;
rs27 0:c57706d25cf0 84
rs27 0:c57706d25cf0 85 // write the default configuration setup
rs27 0:c57706d25cf0 86 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 87 {
rs27 0:c57706d25cf0 88 #if OW_MASTER_START
rs27 0:c57706d25cf0 89 printf("\r\n--- DS2482 configuration failure");
rs27 0:c57706d25cf0 90 #endif
rs27 0:c57706d25cf0 91
rs27 0:c57706d25cf0 92 return false;
rs27 0:c57706d25cf0 93 }
rs27 0:c57706d25cf0 94
rs27 0:c57706d25cf0 95 #if OW_MASTER_START
rs27 0:c57706d25cf0 96 printf("\r\n*** DS2482 detect OK");
rs27 0:c57706d25cf0 97 #endif
rs27 0:c57706d25cf0 98
rs27 0:c57706d25cf0 99 return true;
rs27 0:c57706d25cf0 100 }
rs27 0:c57706d25cf0 101
rs27 0:c57706d25cf0 102 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 103 // Perform a device reset on the DS2482
rs27 0:c57706d25cf0 104 //
rs27 0:c57706d25cf0 105 // Returns: TRUE if device was reset
rs27 0:c57706d25cf0 106 // FALSE device not detected or failure to perform reset
rs27 0:c57706d25cf0 107 //
rs27 0:c57706d25cf0 108 int DS2482::reset(void)
rs27 0:c57706d25cf0 109 {
rs27 0:c57706d25cf0 110 char cmd[2];
rs27 0:c57706d25cf0 111
rs27 0:c57706d25cf0 112 cmd[0] = DS2482_CMD_DRST;
rs27 0:c57706d25cf0 113 i2c.write(addr, cmd, 1);
rs27 0:c57706d25cf0 114 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 115 printf("\ncmd = %02x",cmd[0]);
rs27 0:c57706d25cf0 116 return ((cmd[0] & 0xF7) == 0x10);
rs27 0:c57706d25cf0 117 }
rs27 0:c57706d25cf0 118
rs27 0:c57706d25cf0 119 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 120 // Write the configuration register in the DS2482. The configuration
rs27 0:c57706d25cf0 121 // options are provided in the lower nibble of the provided config byte.
rs27 0:c57706d25cf0 122 // The uppper nibble in bitwise inverted when written to the DS2482.
rs27 0:c57706d25cf0 123 //
rs27 0:c57706d25cf0 124 // Returns: TRUE: config written and response correct
rs27 0:c57706d25cf0 125 // FALSE: response incorrect
rs27 0:c57706d25cf0 126 //
rs27 0:c57706d25cf0 127 uint8_t DS2482::write_config(uint8_t config)
rs27 0:c57706d25cf0 128 {
rs27 0:c57706d25cf0 129 char cmd[2];
rs27 0:c57706d25cf0 130 char read_config;
rs27 0:c57706d25cf0 131
rs27 0:c57706d25cf0 132 cmd[0] = DS2482_CMD_WCFG;
rs27 0:c57706d25cf0 133 cmd[1] = config | (~config << 4);
rs27 0:c57706d25cf0 134 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 135
rs27 0:c57706d25cf0 136 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 137 read_config = cmd[0];
rs27 0:c57706d25cf0 138
rs27 0:c57706d25cf0 139 if (config != read_config) // check for failure due to incorrect read back
rs27 0:c57706d25cf0 140 {
rs27 0:c57706d25cf0 141 // handle error
rs27 0:c57706d25cf0 142 // ...
rs27 0:c57706d25cf0 143 #if OW_MASTER_START
rs27 0:c57706d25cf0 144 printf("\r\n---check for failure due to incorrect config read back");
rs27 0:c57706d25cf0 145 #endif
rs27 0:c57706d25cf0 146
rs27 0:c57706d25cf0 147 reset();
rs27 0:c57706d25cf0 148 return false;
rs27 0:c57706d25cf0 149 }
rs27 0:c57706d25cf0 150 return true;
rs27 0:c57706d25cf0 151
rs27 0:c57706d25cf0 152 }
rs27 0:c57706d25cf0 153
rs27 0:c57706d25cf0 154 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 155 // DS2482 1-Wire Operations
rs27 0:c57706d25cf0 156 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 157 //
rs27 0:c57706d25cf0 158 //OWReset
rs27 0:c57706d25cf0 159 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 160 // Reset all of the devices on the 1-Wire Net and return the result.
rs27 0:c57706d25cf0 161 //
rs27 0:c57706d25cf0 162 // Returns: TRUE(1): presence pulse(s) detected, device(s) reset
rs27 0:c57706d25cf0 163 // FALSE(0): no presence pulses detected
rs27 0:c57706d25cf0 164 //
rs27 0:c57706d25cf0 165 uint8_t DS2482::OWReset(void)
rs27 0:c57706d25cf0 166 {
rs27 0:c57706d25cf0 167 uint8_t status;
rs27 0:c57706d25cf0 168 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 169 char cmd(2);
rs27 0:c57706d25cf0 170
rs27 0:c57706d25cf0 171 cmd[0] = DS2482_CMD_1WRS;
rs27 0:c57706d25cf0 172 i2c.write(addr,cmd,1);
rs27 0:c57706d25cf0 173
rs27 0:c57706d25cf0 174 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 175 printf_P(PSTR("\r\n*** Reset all devices on the 1-Wire Net\r\n"));
rs27 0:c57706d25cf0 176 #endif
rs27 0:c57706d25cf0 177
rs27 0:c57706d25cf0 178 do
rs27 0:c57706d25cf0 179 {
rs27 0:c57706d25cf0 180 i2c.read(addr,cmd,1);
rs27 0:c57706d25cf0 181 }
rs27 0:c57706d25cf0 182 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 183
rs27 0:c57706d25cf0 184 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 185 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 186 {
rs27 0:c57706d25cf0 187 // handle error
rs27 0:c57706d25cf0 188 // ...
rs27 0:c57706d25cf0 189 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 190 printf("\r\n---poll limit reached");
rs27 0:c57706d25cf0 191 #endif
rs27 0:c57706d25cf0 192
rs27 0:c57706d25cf0 193 reset();
rs27 0:c57706d25cf0 194 return FALSE;
rs27 0:c57706d25cf0 195 }
rs27 0:c57706d25cf0 196
rs27 0:c57706d25cf0 197 // check for short condition
rs27 0:c57706d25cf0 198 if (cmd[0] & DS2482_STATUS_SD)
rs27 0:c57706d25cf0 199 {
rs27 0:c57706d25cf0 200 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 201 printf("\r\n---1-Wire Net short detected");
rs27 0:c57706d25cf0 202 #endif
rs27 0:c57706d25cf0 203
rs27 0:c57706d25cf0 204 short_detected = true;
rs27 0:c57706d25cf0 205 }
rs27 0:c57706d25cf0 206 else
rs27 0:c57706d25cf0 207 {
rs27 0:c57706d25cf0 208 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 209 printf("\r\n*** 1-Wire electrical net is OK");
rs27 0:c57706d25cf0 210 #endif
rs27 0:c57706d25cf0 211
rs27 0:c57706d25cf0 212 short_detected = false;
rs27 0:c57706d25cf0 213 }
rs27 0:c57706d25cf0 214 // check for presence detect
rs27 0:c57706d25cf0 215 if (cmd[0] & DS2482_STATUS_PPD)
rs27 0:c57706d25cf0 216 {
rs27 0:c57706d25cf0 217 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 218 printf("\r\n*** 1-Wire Device detected");
rs27 0:c57706d25cf0 219 #endif
rs27 0:c57706d25cf0 220
rs27 0:c57706d25cf0 221 return true;
rs27 0:c57706d25cf0 222 }
rs27 0:c57706d25cf0 223 else
rs27 0:c57706d25cf0 224 {
rs27 0:c57706d25cf0 225 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 226 printf("\r\n---No Device detected");
rs27 0:c57706d25cf0 227 #endif
rs27 0:c57706d25cf0 228
rs27 0:c57706d25cf0 229 return false;
rs27 0:c57706d25cf0 230 }
rs27 0:c57706d25cf0 231 }
rs27 0:c57706d25cf0 232
rs27 0:c57706d25cf0 233 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 234 // Send 1 bit of communication to the 1-Wire Net.
rs27 0:c57706d25cf0 235 // The parameter 'sendbit' least significant bit is used.
rs27 0:c57706d25cf0 236 //
rs27 0:c57706d25cf0 237 // 'sendbit' - 1 bit to send (least significant byte)
rs27 0:c57706d25cf0 238 //
rs27 0:c57706d25cf0 239 void DS2482::OWWriteBit(uint8_t sendbit)
rs27 0:c57706d25cf0 240 {
rs27 0:c57706d25cf0 241 OWTouchBit(sendbit);
rs27 0:c57706d25cf0 242 }
rs27 0:c57706d25cf0 243
rs27 0:c57706d25cf0 244 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 245 // Reads 1 bit of communication from the 1-Wire Net and returns the
rs27 0:c57706d25cf0 246 // result
rs27 0:c57706d25cf0 247 //
rs27 0:c57706d25cf0 248 // Returns: 1 bit read from 1-Wire Net
rs27 0:c57706d25cf0 249 //
rs27 0:c57706d25cf0 250 uint8_t DS2482::OWReadBit(void)
rs27 0:c57706d25cf0 251 {
rs27 0:c57706d25cf0 252 return OWTouchBit(0x01);
rs27 0:c57706d25cf0 253 }
rs27 0:c57706d25cf0 254
rs27 0:c57706d25cf0 255 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 256 // Send 1 bit of communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 257 // result 1 bit read from the 1-Wire Net. The parameter 'sendbit'
rs27 0:c57706d25cf0 258 // least significant bit is used and the least significant bit
rs27 0:c57706d25cf0 259 // of the result is the return bit.
rs27 0:c57706d25cf0 260 //
rs27 0:c57706d25cf0 261 // 'sendbit' - the least significant bit is the bit to send
rs27 0:c57706d25cf0 262 //
rs27 0:c57706d25cf0 263 // Returns: 0: 0 bit read from sendbit
rs27 0:c57706d25cf0 264 // 1: 1 bit read from sendbit
rs27 0:c57706d25cf0 265 //
rs27 0:c57706d25cf0 266 uint8_t DS2482::OWTouchBit(uint8_t sendbit)
rs27 0:c57706d25cf0 267 {
rs27 0:c57706d25cf0 268 char cmd[2];
rs27 0:c57706d25cf0 269 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 270
rs27 0:c57706d25cf0 271 cmd[0] = DS2482_CMD_1WSB;
rs27 0:c57706d25cf0 272 cmd[1] = sendbit ? 0x80 : 0x00;
rs27 0:c57706d25cf0 273 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 274
rs27 0:c57706d25cf0 275 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 276 // abort if poll limit reached
rs27 0:c57706d25cf0 277
rs27 0:c57706d25cf0 278 do
rs27 0:c57706d25cf0 279 {
rs27 0:c57706d25cf0 280 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 281 }
rs27 0:c57706d25cf0 282 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 283
rs27 0:c57706d25cf0 284 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 285 printf("\r\n*** Send 1 bit to the 1-Wire Net");
rs27 0:c57706d25cf0 286 #endif
rs27 0:c57706d25cf0 287
rs27 0:c57706d25cf0 288 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 289 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 290 {
rs27 0:c57706d25cf0 291 // handle error
rs27 0:c57706d25cf0 292 // ...
rs27 0:c57706d25cf0 293 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 294 printf("\r\n---handle error OW Write Bit");
rs27 0:c57706d25cf0 295 #endif
rs27 0:c57706d25cf0 296
rs27 0:c57706d25cf0 297 reset();
rs27 0:c57706d25cf0 298 return 0;
rs27 0:c57706d25cf0 299 }
rs27 0:c57706d25cf0 300
rs27 0:c57706d25cf0 301 // return bit state
rs27 0:c57706d25cf0 302 if (cmd[0] & DS2482_STATUS_SBR)
rs27 0:c57706d25cf0 303 {
rs27 0:c57706d25cf0 304 return 1;
rs27 0:c57706d25cf0 305 }
rs27 0:c57706d25cf0 306 else
rs27 0:c57706d25cf0 307 {
rs27 0:c57706d25cf0 308 return 0;
rs27 0:c57706d25cf0 309 }
rs27 0:c57706d25cf0 310 }
rs27 0:c57706d25cf0 311
rs27 0:c57706d25cf0 312 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 313 // Send 8 bits of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 314 // 8 bits read from the 1-Wire Net are the same (write operation).
rs27 0:c57706d25cf0 315 // The parameter 'sendbyte' least significant 8 bits are used.
rs27 0:c57706d25cf0 316 //
rs27 0:c57706d25cf0 317 // 'sendbyte' - 8 bits to send (least significant byte)
rs27 0:c57706d25cf0 318 //
rs27 0:c57706d25cf0 319 // Returns: TRUE: bytes written and echo was the same
rs27 0:c57706d25cf0 320 // FALSE: echo was not the same
rs27 0:c57706d25cf0 321 //
rs27 0:c57706d25cf0 322 void DS2482::OWWriteByte(uint8_t sendbyte)
rs27 0:c57706d25cf0 323 {
rs27 0:c57706d25cf0 324 char cmd[2];
rs27 0:c57706d25cf0 325 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 326
rs27 0:c57706d25cf0 327 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 328 printf("\r\n*** Send 8 bits of WRITE to the 1-Wire Net");
rs27 0:c57706d25cf0 329 #endif
rs27 0:c57706d25cf0 330
rs27 0:c57706d25cf0 331 cmd[0] = DS2482_CMD_1WWB;
rs27 0:c57706d25cf0 332 cmd[1] = sendbyte;
rs27 0:c57706d25cf0 333
rs27 0:c57706d25cf0 334 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 335
rs27 0:c57706d25cf0 336 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 337 // abort if poll limit reached
rs27 0:c57706d25cf0 338
rs27 0:c57706d25cf0 339 do
rs27 0:c57706d25cf0 340 {
rs27 0:c57706d25cf0 341 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 342 }
rs27 0:c57706d25cf0 343 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 344
rs27 0:c57706d25cf0 345 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 346 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 347 {
rs27 0:c57706d25cf0 348 // handle error
rs27 0:c57706d25cf0 349 // ...
rs27 0:c57706d25cf0 350 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 351 printf("\r\n---handle error OW Write Byte");
rs27 0:c57706d25cf0 352 #endif
rs27 0:c57706d25cf0 353
rs27 0:c57706d25cf0 354 reset();
rs27 0:c57706d25cf0 355 }
rs27 0:c57706d25cf0 356 }
rs27 0:c57706d25cf0 357
rs27 0:c57706d25cf0 358 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 359 // Send 8 bits of read communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 360 // result 8 bits read from the 1-Wire Net.
rs27 0:c57706d25cf0 361 //
rs27 0:c57706d25cf0 362 // Returns: 8 bits read from 1-Wire Net
rs27 0:c57706d25cf0 363 //
rs27 0:c57706d25cf0 364 uint8_t DS2482::OWReadByte(void)
rs27 0:c57706d25cf0 365 {
rs27 0:c57706d25cf0 366 uint8_t data, status;
rs27 0:c57706d25cf0 367 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 368 char cmd[2];
rs27 0:c57706d25cf0 369
rs27 0:c57706d25cf0 370 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 371 printf("\r\n*** Read 8 bits from the 1-Wire Net");
rs27 0:c57706d25cf0 372 #endif
rs27 0:c57706d25cf0 373
rs27 0:c57706d25cf0 374 cmd[0] = DS2482_CMD_1WRB; // DS2482 1-Wire Read Byte
rs27 0:c57706d25cf0 375 i2c.write(addr, cmd, 1);
rs27 0:c57706d25cf0 376
rs27 0:c57706d25cf0 377 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 378 // abort if poll limit reached
rs27 0:c57706d25cf0 379
rs27 0:c57706d25cf0 380 do
rs27 0:c57706d25cf0 381 {
rs27 0:c57706d25cf0 382 i2c.read(adr, cmd, 1);
rs27 0:c57706d25cf0 383 }
rs27 0:c57706d25cf0 384 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 385
rs27 0:c57706d25cf0 386 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 387 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 388 {
rs27 0:c57706d25cf0 389 // handle error
rs27 0:c57706d25cf0 390 // ...
rs27 0:c57706d25cf0 391 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 392 printf("\r\n---handle error OW Read Byte");
rs27 0:c57706d25cf0 393 #endif
rs27 0:c57706d25cf0 394
rs27 0:c57706d25cf0 395 reset();
rs27 0:c57706d25cf0 396 return 0;
rs27 0:c57706d25cf0 397 }
rs27 0:c57706d25cf0 398
rs27 0:c57706d25cf0 399 cmd[0] = DS2482_CMD_SRP; // DS2482 Set Read Pointer
rs27 0:c57706d25cf0 400 cmd[1] = DS2482_READPTR_RDR; // DS2482 Read Data Register
rs27 0:c57706d25cf0 401 i2c.write[addr, cmd, 2);
rs27 0:c57706d25cf0 402
rs27 0:c57706d25cf0 403 i2c.read(addr, cmd, 1));
rs27 0:c57706d25cf0 404
rs27 0:c57706d25cf0 405 return cmd[0];
rs27 0:c57706d25cf0 406 }
rs27 0:c57706d25cf0 407
rs27 0:c57706d25cf0 408 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 409 // The 'OWBlock' transfers a block of data to and from the
rs27 0:c57706d25cf0 410 // 1-Wire Net. The result is returned in the same buffer.
rs27 0:c57706d25cf0 411 //
rs27 0:c57706d25cf0 412 // 'tran_buf' - pointer to a block of unsigned
rs27 0:c57706d25cf0 413 // chars of length 'tran_len' that will be sent
rs27 0:c57706d25cf0 414 // to the 1-Wire Net
rs27 0:c57706d25cf0 415 // 'tran_len' - length in bytes to transfer
rs27 0:c57706d25cf0 416 //
rs27 0:c57706d25cf0 417 void DS2482::OWBlock(uint8_t *tran_buf, uint8_t tran_len)
rs27 0:c57706d25cf0 418 {
rs27 0:c57706d25cf0 419 uint8_t i;
rs27 0:c57706d25cf0 420
rs27 0:c57706d25cf0 421 for (i = 0; i < tran_len; i++)
rs27 0:c57706d25cf0 422 {
rs27 0:c57706d25cf0 423 tran_buf[i] = OWTouchByte(tran_buf[i]);
rs27 0:c57706d25cf0 424 }
rs27 0:c57706d25cf0 425 }
rs27 0:c57706d25cf0 426
rs27 0:c57706d25cf0 427 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 428 // Send 8 bits of communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 429 // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte'
rs27 0:c57706d25cf0 430 // least significant 8 bits are used and the least significant 8 bits
rs27 0:c57706d25cf0 431 // of the result are the return byte.
rs27 0:c57706d25cf0 432 //
rs27 0:c57706d25cf0 433 // 'sendbyte' - 8 bits to send (least significant byte)
rs27 0:c57706d25cf0 434 //
rs27 0:c57706d25cf0 435 // Returns: 8 bits read from sendbyte
rs27 0:c57706d25cf0 436 //
rs27 0:c57706d25cf0 437 uint8_t DS2482::OWTouchByte(uint8_t sendbyte)
rs27 0:c57706d25cf0 438 {
rs27 0:c57706d25cf0 439 if (sendbyte == 0xFF)
rs27 0:c57706d25cf0 440 {
rs27 0:c57706d25cf0 441 return OWReadByte();
rs27 0:c57706d25cf0 442 }
rs27 0:c57706d25cf0 443 else
rs27 0:c57706d25cf0 444 {
rs27 0:c57706d25cf0 445 OWWriteByte(sendbyte);
rs27 0:c57706d25cf0 446 return sendbyte;
rs27 0:c57706d25cf0 447 }
rs27 0:c57706d25cf0 448 }
rs27 0:c57706d25cf0 449
rs27 0:c57706d25cf0 450 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 451 // Search state
rs27 0:c57706d25cf0 452 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 453 // Find the 'first' devices on the 1-Wire network
rs27 0:c57706d25cf0 454 // Return TRUE : device found, ROM number in ROM_NO buffer
rs27 0:c57706d25cf0 455 // FALSE : no device present
rs27 0:c57706d25cf0 456 //
rs27 0:c57706d25cf0 457 uint8_t DS2482::OWFirst(void)
rs27 0:c57706d25cf0 458 {
rs27 0:c57706d25cf0 459 // reset the search state
rs27 0:c57706d25cf0 460 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 461 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 462 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 463
rs27 0:c57706d25cf0 464 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 465 printf("\r\n*** Find the 'first' device on the 1-Wire network");
rs27 0:c57706d25cf0 466 #endif
rs27 0:c57706d25cf0 467
rs27 0:c57706d25cf0 468 return OWSearch();
rs27 0:c57706d25cf0 469 }
rs27 0:c57706d25cf0 470
rs27 0:c57706d25cf0 471 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 472 // Find the 'next' devices on the 1-Wire network
rs27 0:c57706d25cf0 473 // Return TRUE : device found, ROM number in ROM_NO buffer
rs27 0:c57706d25cf0 474 // FALSE : device not found, end of search
rs27 0:c57706d25cf0 475 //
rs27 0:c57706d25cf0 476 uint8_t DS2482::OWNext(void)
rs27 0:c57706d25cf0 477 {
rs27 0:c57706d25cf0 478 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 479 printf("\r\n*** Find the 'next' device on the 1-Wire network");
rs27 0:c57706d25cf0 480 #endif
rs27 0:c57706d25cf0 481
rs27 0:c57706d25cf0 482 // leave the search state alone
rs27 0:c57706d25cf0 483 return OWSearch();
rs27 0:c57706d25cf0 484 }
rs27 0:c57706d25cf0 485
rs27 0:c57706d25cf0 486 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 487 // Verify the device with the ROM number in ROM_NO buffer is present.
rs27 0:c57706d25cf0 488 // Return TRUE : device present
rs27 0:c57706d25cf0 489 // FALSE : device not present
rs27 0:c57706d25cf0 490 //
rs27 0:c57706d25cf0 491 int DS2482::OWVerify(void)
rs27 0:c57706d25cf0 492 {
rs27 0:c57706d25cf0 493 uint8_t rom_backup[8], status;
rs27 0:c57706d25cf0 494 int i,ld_backup,ldf_backup,lfd_backup;
rs27 0:c57706d25cf0 495
rs27 0:c57706d25cf0 496 // keep a backup copy of the current state
rs27 0:c57706d25cf0 497 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 498 {
rs27 0:c57706d25cf0 499 rom_backup[i] = ROM_NO[i];
rs27 0:c57706d25cf0 500 }
rs27 0:c57706d25cf0 501
rs27 0:c57706d25cf0 502 ld_backup = LastDiscrepancy;
rs27 0:c57706d25cf0 503 ldf_backup = LastDeviceFlag;
rs27 0:c57706d25cf0 504 lfd_backup = LastFamilyDiscrepancy;
rs27 0:c57706d25cf0 505
rs27 0:c57706d25cf0 506 // set search to find the same device
rs27 0:c57706d25cf0 507 LastDiscrepancy = 64;
rs27 0:c57706d25cf0 508 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 509
rs27 0:c57706d25cf0 510 if (OWSearch())
rs27 0:c57706d25cf0 511 {
rs27 0:c57706d25cf0 512 // check if same device found
rs27 0:c57706d25cf0 513 status = TRUE;
rs27 0:c57706d25cf0 514
rs27 0:c57706d25cf0 515 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 516 {
rs27 0:c57706d25cf0 517 if (rom_backup[i] != ROM_NO[i])
rs27 0:c57706d25cf0 518 {
rs27 0:c57706d25cf0 519 status = FALSE;
rs27 0:c57706d25cf0 520 break;
rs27 0:c57706d25cf0 521 }
rs27 0:c57706d25cf0 522 }
rs27 0:c57706d25cf0 523 }
rs27 0:c57706d25cf0 524 else
rs27 0:c57706d25cf0 525 {
rs27 0:c57706d25cf0 526 status = FALSE;
rs27 0:c57706d25cf0 527 }
rs27 0:c57706d25cf0 528
rs27 0:c57706d25cf0 529 // restore the search state
rs27 0:c57706d25cf0 530 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 531 {
rs27 0:c57706d25cf0 532 ROM_NO[i] = rom_backup[i];
rs27 0:c57706d25cf0 533 }
rs27 0:c57706d25cf0 534 LastDiscrepancy = ld_backup;
rs27 0:c57706d25cf0 535 LastDeviceFlag = ldf_backup;
rs27 0:c57706d25cf0 536 LastFamilyDiscrepancy = lfd_backup;
rs27 0:c57706d25cf0 537
rs27 0:c57706d25cf0 538 // return the result of the verify
rs27 0:c57706d25cf0 539 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 540 printf("\r\n*** 1-Wire Verify device with the ROM number in ROM_NO");
rs27 0:c57706d25cf0 541 #endif
rs27 0:c57706d25cf0 542
rs27 0:c57706d25cf0 543 return status;
rs27 0:c57706d25cf0 544 }
rs27 0:c57706d25cf0 545
rs27 0:c57706d25cf0 546 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 547 // Setup the search to find the device type 'family_code' on the next call
rs27 0:c57706d25cf0 548 // to OWNext() if it is present.
rs27 0:c57706d25cf0 549 //
rs27 0:c57706d25cf0 550 void DS2482::OWTargetSetup(uint8_t family_code)
rs27 0:c57706d25cf0 551 {
rs27 0:c57706d25cf0 552 uint8_t i;
rs27 0:c57706d25cf0 553
rs27 0:c57706d25cf0 554 // set the search state to find SearchFamily type devices
rs27 0:c57706d25cf0 555 ROM_NO[0] = family_code;
rs27 0:c57706d25cf0 556
rs27 0:c57706d25cf0 557 for (i = 1; i < 8; i++)
rs27 0:c57706d25cf0 558 {
rs27 0:c57706d25cf0 559 ROM_NO[i] = 0;
rs27 0:c57706d25cf0 560 }
rs27 0:c57706d25cf0 561
rs27 0:c57706d25cf0 562 LastDiscrepancy = 64;
rs27 0:c57706d25cf0 563 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 564 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 565 }
rs27 0:c57706d25cf0 566
rs27 0:c57706d25cf0 567 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 568 // Setup the search to skip the current device type on the next call
rs27 0:c57706d25cf0 569 // to OWNext().
rs27 0:c57706d25cf0 570 //
rs27 0:c57706d25cf0 571 void DS2482::OWFamilySkipSetup(void)
rs27 0:c57706d25cf0 572 {
rs27 0:c57706d25cf0 573 // set the Last discrepancy to last family discrepancy
rs27 0:c57706d25cf0 574 LastDiscrepancy = LastFamilyDiscrepancy;
rs27 0:c57706d25cf0 575
rs27 0:c57706d25cf0 576 // clear the last family discrpepancy
rs27 0:c57706d25cf0 577 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 578
rs27 0:c57706d25cf0 579 // check for end of list
rs27 0:c57706d25cf0 580 if (LastDiscrepancy == 0)
rs27 0:c57706d25cf0 581 {
rs27 0:c57706d25cf0 582 LastDeviceFlag = TRUE;
rs27 0:c57706d25cf0 583 }
rs27 0:c57706d25cf0 584 }
rs27 0:c57706d25cf0 585
rs27 0:c57706d25cf0 586 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 587 // The 'OWSearch' function does a general search. This function
rs27 0:c57706d25cf0 588 // continues from the previous search state. The search state
rs27 0:c57706d25cf0 589 // can be reset by using the 'OWFirst' function.
rs27 0:c57706d25cf0 590 // This function contains one parameter 'alarm_only'.
rs27 0:c57706d25cf0 591 // When 'alarm_only' is TRUE (1) the find alarm command
rs27 0:c57706d25cf0 592 // 0xEC is sent instead of the normal search command 0xF0.
rs27 0:c57706d25cf0 593 // Using the find alarm command 0xEC will limit the search to only
rs27 0:c57706d25cf0 594 // 1-Wire devices that are in an 'alarm' state.
rs27 0:c57706d25cf0 595 //
rs27 0:c57706d25cf0 596 // Returns: TRUE (1) : when a 1-Wire device was found and its
rs27 0:c57706d25cf0 597 // Serial Number placed in the global ROM
rs27 0:c57706d25cf0 598 // FALSE (0): when no new device was found. Either the
rs27 0:c57706d25cf0 599 // last search was the last device or there
rs27 0:c57706d25cf0 600 // are no devices on the 1-Wire Net.
rs27 0:c57706d25cf0 601 //
rs27 0:c57706d25cf0 602 uint8_t DS2482::OWSearch()
rs27 0:c57706d25cf0 603 {
rs27 0:c57706d25cf0 604 int id_bit_number;
rs27 0:c57706d25cf0 605 int last_zero, rom_byte_number, search_result;
rs27 0:c57706d25cf0 606 int id_bit, cmp_id_bit;
rs27 0:c57706d25cf0 607 uint8_t rom_byte_mask, search_direction, status;
rs27 0:c57706d25cf0 608
rs27 0:c57706d25cf0 609 // initialize for search
rs27 0:c57706d25cf0 610 id_bit_number = 1;
rs27 0:c57706d25cf0 611 last_zero = 0;
rs27 0:c57706d25cf0 612 rom_byte_number = 0;
rs27 0:c57706d25cf0 613 rom_byte_mask = 1;
rs27 0:c57706d25cf0 614 search_result = FALSE;
rs27 0:c57706d25cf0 615 crc_value = 0;
rs27 0:c57706d25cf0 616
rs27 0:c57706d25cf0 617 if (!LastDeviceFlag) // if the last call was not the last one
rs27 0:c57706d25cf0 618 {
rs27 0:c57706d25cf0 619 // 1-Wire reset
rs27 0:c57706d25cf0 620 if (!OWReset())
rs27 0:c57706d25cf0 621 {
rs27 0:c57706d25cf0 622 // reset the search
rs27 0:c57706d25cf0 623 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 624 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 625 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 626 return FALSE;
rs27 0:c57706d25cf0 627 }
rs27 0:c57706d25cf0 628
rs27 0:c57706d25cf0 629 // issue the search command
rs27 0:c57706d25cf0 630 OWWriteByte(OW_SEARCH_ROM);
rs27 0:c57706d25cf0 631
rs27 0:c57706d25cf0 632 // loop to do the search
rs27 0:c57706d25cf0 633 do
rs27 0:c57706d25cf0 634 {
rs27 0:c57706d25cf0 635 // if this discrepancy if before the Last Discrepancy
rs27 0:c57706d25cf0 636 // on a previous next then pick the same as last time
rs27 0:c57706d25cf0 637 if (id_bit_number < LastDiscrepancy)
rs27 0:c57706d25cf0 638 {
rs27 0:c57706d25cf0 639 if ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0)
rs27 0:c57706d25cf0 640 search_direction = 1;
rs27 0:c57706d25cf0 641 else
rs27 0:c57706d25cf0 642 search_direction = 0;
rs27 0:c57706d25cf0 643 }
rs27 0:c57706d25cf0 644 else
rs27 0:c57706d25cf0 645 {
rs27 0:c57706d25cf0 646 // if equal to last pick 1, if not then pick 0
rs27 0:c57706d25cf0 647 if (id_bit_number == LastDiscrepancy)
rs27 0:c57706d25cf0 648 search_direction = 1;
rs27 0:c57706d25cf0 649 else
rs27 0:c57706d25cf0 650 search_direction = 0;
rs27 0:c57706d25cf0 651 }
rs27 0:c57706d25cf0 652
rs27 0:c57706d25cf0 653 // Perform a triple operation on the DS2482 which will perform 2 read bits and 1 write bit
rs27 0:c57706d25cf0 654 status = search_triplet(search_direction);
rs27 0:c57706d25cf0 655
rs27 0:c57706d25cf0 656 // check bit results in status byte
rs27 0:c57706d25cf0 657 id_bit = ((status & DS2482_STATUS_SBR) == DS2482_STATUS_SBR);
rs27 0:c57706d25cf0 658 cmp_id_bit = ((status & DS2482_STATUS_TSB) == DS2482_STATUS_TSB);
rs27 0:c57706d25cf0 659 search_direction = ((status & DS2482_STATUS_DIR) == DS2482_STATUS_DIR) ? 1 : 0;
rs27 0:c57706d25cf0 660
rs27 0:c57706d25cf0 661 // check for no devices on 1-Wire
rs27 0:c57706d25cf0 662 if ((id_bit) && (cmp_id_bit))
rs27 0:c57706d25cf0 663 {
rs27 0:c57706d25cf0 664 break;
rs27 0:c57706d25cf0 665 }
rs27 0:c57706d25cf0 666 else
rs27 0:c57706d25cf0 667 {
rs27 0:c57706d25cf0 668 if ((!id_bit) && (!cmp_id_bit) && (search_direction == 0))
rs27 0:c57706d25cf0 669 {
rs27 0:c57706d25cf0 670 last_zero = id_bit_number;
rs27 0:c57706d25cf0 671
rs27 0:c57706d25cf0 672 // check for Last discrepancy in family
rs27 0:c57706d25cf0 673 if (last_zero < 9)
rs27 0:c57706d25cf0 674 {
rs27 0:c57706d25cf0 675 LastFamilyDiscrepancy = last_zero;
rs27 0:c57706d25cf0 676 }
rs27 0:c57706d25cf0 677 }
rs27 0:c57706d25cf0 678
rs27 0:c57706d25cf0 679 // set or clear the bit in the ROM byte rom_byte_number
rs27 0:c57706d25cf0 680 // with mask rom_byte_mask
rs27 0:c57706d25cf0 681 if (search_direction == 1)
rs27 0:c57706d25cf0 682 {
rs27 0:c57706d25cf0 683 ROM_NO[rom_byte_number] |= rom_byte_mask;
rs27 0:c57706d25cf0 684 }
rs27 0:c57706d25cf0 685 else
rs27 0:c57706d25cf0 686 {
rs27 0:c57706d25cf0 687 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
rs27 0:c57706d25cf0 688 }
rs27 0:c57706d25cf0 689
rs27 0:c57706d25cf0 690 // increment the byte counter id_bit_number
rs27 0:c57706d25cf0 691 // and shift the mask rom_byte_mask
rs27 0:c57706d25cf0 692 id_bit_number++;
rs27 0:c57706d25cf0 693 rom_byte_mask <<= 1;
rs27 0:c57706d25cf0 694
rs27 0:c57706d25cf0 695 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
rs27 0:c57706d25cf0 696 if (rom_byte_mask == 0)
rs27 0:c57706d25cf0 697 {
rs27 0:c57706d25cf0 698 crc_calc(ROM_NO[rom_byte_number]); // accumulate the CRC
rs27 0:c57706d25cf0 699 rom_byte_number++;
rs27 0:c57706d25cf0 700 rom_byte_mask = 1;
rs27 0:c57706d25cf0 701 }
rs27 0:c57706d25cf0 702 }
rs27 0:c57706d25cf0 703 }
rs27 0:c57706d25cf0 704 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
rs27 0:c57706d25cf0 705
rs27 0:c57706d25cf0 706 // if the search was successful then
rs27 0:c57706d25cf0 707 if (!((id_bit_number < 65) || (crc_value != 0)))
rs27 0:c57706d25cf0 708 {
rs27 0:c57706d25cf0 709 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
rs27 0:c57706d25cf0 710 LastDiscrepancy = last_zero;
rs27 0:c57706d25cf0 711
rs27 0:c57706d25cf0 712 // check for last device
rs27 0:c57706d25cf0 713 if (LastDiscrepancy == 0)
rs27 0:c57706d25cf0 714 {
rs27 0:c57706d25cf0 715 LastDeviceFlag = TRUE;
rs27 0:c57706d25cf0 716 }
rs27 0:c57706d25cf0 717
rs27 0:c57706d25cf0 718 search_result = TRUE;
rs27 0:c57706d25cf0 719 }
rs27 0:c57706d25cf0 720 }
rs27 0:c57706d25cf0 721
rs27 0:c57706d25cf0 722 // if no device found then reset counters so next 'search' will be like a first
rs27 0:c57706d25cf0 723 if (!search_result || (ROM_NO[0] == 0))
rs27 0:c57706d25cf0 724 {
rs27 0:c57706d25cf0 725 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 726 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 727 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 728 search_result = FALSE;
rs27 0:c57706d25cf0 729 }
rs27 0:c57706d25cf0 730
rs27 0:c57706d25cf0 731 return search_result;
rs27 0:c57706d25cf0 732 }
rs27 0:c57706d25cf0 733
rs27 0:c57706d25cf0 734 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 735 // Use the DS2482 help command '1-Wire triplet' to perform one bit of a
rs27 0:c57706d25cf0 736 // 1-Wire search.
rs27 0:c57706d25cf0 737 // This command does two read bits and one write bit. The write bit
rs27 0:c57706d25cf0 738 // is either the default direction (all device have same bit) or in case of
rs27 0:c57706d25cf0 739 // a discrepancy, the 'search_direction' parameter is used.
rs27 0:c57706d25cf0 740 //
rs27 0:c57706d25cf0 741 // Returns – The DS2482 status byte result from the triplet command
rs27 0:c57706d25cf0 742 //
rs27 0:c57706d25cf0 743 uint8_t DS2482::search_triplet(uint8_t search_direction)
rs27 0:c57706d25cf0 744 {
rs27 0:c57706d25cf0 745 uint8_t status;
rs27 0:c57706d25cf0 746 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 747
rs27 0:c57706d25cf0 748 i2c.start();
rs27 0:c57706d25cf0 749 i2c.write(OW_I2C_DEVICE + I2C_WRITE); // set device address & write mode
rs27 0:c57706d25cf0 750 i2c.write(DS2482_CMD_1WT);
rs27 0:c57706d25cf0 751 i2c.write(search_direction ? 0x80 : 0x00);
rs27 0:c57706d25cf0 752
rs27 0:c57706d25cf0 753 i2c.start();
rs27 0:c57706d25cf0 754 i2c.write(OW_I2C_DEVICE + I2C_READ);
rs27 0:c57706d25cf0 755
rs27 0:c57706d25cf0 756 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 757 // abort if poll limit reached
rs27 0:c57706d25cf0 758 status = i2c.read(ACK);
rs27 0:c57706d25cf0 759
rs27 0:c57706d25cf0 760 do
rs27 0:c57706d25cf0 761 {
rs27 0:c57706d25cf0 762 status = i2c.read(ACK);
rs27 0:c57706d25cf0 763 }
rs27 0:c57706d25cf0 764 while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 765
rs27 0:c57706d25cf0 766 status = i2c.read(NAK);
rs27 0:c57706d25cf0 767 i2c.stop();
rs27 0:c57706d25cf0 768
rs27 0:c57706d25cf0 769 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 770 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 771 {
rs27 0:c57706d25cf0 772 // handle error
rs27 0:c57706d25cf0 773 // ...
rs27 0:c57706d25cf0 774 reset();
rs27 0:c57706d25cf0 775 return 0;
rs27 0:c57706d25cf0 776 }
rs27 0:c57706d25cf0 777 // return status byte
rs27 0:c57706d25cf0 778 return status;
rs27 0:c57706d25cf0 779 }
rs27 0:c57706d25cf0 780
rs27 0:c57706d25cf0 781 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 782 // Set the 1-Wire Net communication speed.
rs27 0:c57706d25cf0 783 //
rs27 0:c57706d25cf0 784 // 'new_speed' - new speed defined as
rs27 0:c57706d25cf0 785 // MODE_STANDARD 0x00
rs27 0:c57706d25cf0 786 // MODE_OVERDRIVE 0x01
rs27 0:c57706d25cf0 787 //
rs27 0:c57706d25cf0 788 // Returns: current 1-Wire Net speed
rs27 0:c57706d25cf0 789 //
rs27 0:c57706d25cf0 790 uint8_t DS2482::OWSpeed(uint8_t new_speed)
rs27 0:c57706d25cf0 791 {
rs27 0:c57706d25cf0 792 // set the speed
rs27 0:c57706d25cf0 793 if (new_speed == MODE_OVERDRIVE)
rs27 0:c57706d25cf0 794 {
rs27 0:c57706d25cf0 795 c1WS = DS2482_CFG_1WS;
rs27 0:c57706d25cf0 796 }
rs27 0:c57706d25cf0 797 else
rs27 0:c57706d25cf0 798 {
rs27 0:c57706d25cf0 799 c1WS = FALSE;
rs27 0:c57706d25cf0 800 }
rs27 0:c57706d25cf0 801
rs27 0:c57706d25cf0 802 // write the new config
rs27 0:c57706d25cf0 803 write_config(c1WS | cSPU | cPPM | cAPU);
rs27 0:c57706d25cf0 804
rs27 0:c57706d25cf0 805
rs27 0:c57706d25cf0 806 return new_speed;
rs27 0:c57706d25cf0 807 }
rs27 0:c57706d25cf0 808
rs27 0:c57706d25cf0 809 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 810 // Set the 1-Wire Net line level pullup to normal. The DS2482 only
rs27 0:c57706d25cf0 811 // allows enabling strong pullup on a bit or byte event.
rs27 0:c57706d25cf0 812 // Consequently this function only allows the MODE_STANDARD argument.
rs27 0:c57706d25cf0 813 // To enable strong pullup
rs27 0:c57706d25cf0 814 // use OWWriteBytePower or OWReadBitPower.
rs27 0:c57706d25cf0 815 //
rs27 0:c57706d25cf0 816 // 'new_level' - new level defined as
rs27 0:c57706d25cf0 817 // MODE_STANDARD 0x00
rs27 0:c57706d25cf0 818 //
rs27 0:c57706d25cf0 819 // Returns: current 1-Wire Net level
rs27 0:c57706d25cf0 820 //
rs27 0:c57706d25cf0 821 uint8_t DS2482::OWLevel(uint8_t new_level)
rs27 0:c57706d25cf0 822 {
rs27 0:c57706d25cf0 823 // function only will turn back to non-strong pullup
rs27 0:c57706d25cf0 824 if (new_level != MODE_STANDARD)
rs27 0:c57706d25cf0 825 {
rs27 0:c57706d25cf0 826 return MODE_STRONG;
rs27 0:c57706d25cf0 827 }
rs27 0:c57706d25cf0 828
rs27 0:c57706d25cf0 829 // clear the strong pullup bit in the global config state
rs27 0:c57706d25cf0 830 cSPU = FALSE;
rs27 0:c57706d25cf0 831
rs27 0:c57706d25cf0 832 // write the new config
rs27 0:c57706d25cf0 833 write_config(c1WS | cSPU | cPPM | cAPU);
rs27 0:c57706d25cf0 834
rs27 0:c57706d25cf0 835
rs27 0:c57706d25cf0 836 return MODE_STANDARD;
rs27 0:c57706d25cf0 837 }
rs27 0:c57706d25cf0 838
rs27 0:c57706d25cf0 839 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 840 // Send 1 bit of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 841 // response matches the 'applyPowerResponse' bit and apply power delivery
rs27 0:c57706d25cf0 842 // to the 1-Wire net. Note that some implementations may apply the power
rs27 0:c57706d25cf0 843 // first and then turn it off if the response is incorrect.
rs27 0:c57706d25cf0 844 //
rs27 0:c57706d25cf0 845 // 'applyPowerResponse' - 1 bit response to check, if correct then start
rs27 0:c57706d25cf0 846 // power delivery
rs27 0:c57706d25cf0 847 //
rs27 0:c57706d25cf0 848 // Returns: TRUE: bit written and response correct, strong pullup now on
rs27 0:c57706d25cf0 849 // FALSE: response incorrect
rs27 0:c57706d25cf0 850 //
rs27 0:c57706d25cf0 851 uint8_t DS2482::OWReadBitPower(uint8_t applyPowerResponse)
rs27 0:c57706d25cf0 852 {
rs27 0:c57706d25cf0 853 uint8_t rdbit;
rs27 0:c57706d25cf0 854
rs27 0:c57706d25cf0 855 // set strong pullup enable
rs27 0:c57706d25cf0 856 cSPU = DS2482_CFG_SPU;
rs27 0:c57706d25cf0 857
rs27 0:c57706d25cf0 858 // write the new config
rs27 0:c57706d25cf0 859 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 860 {
rs27 0:c57706d25cf0 861 return FALSE;
rs27 0:c57706d25cf0 862 }
rs27 0:c57706d25cf0 863
rs27 0:c57706d25cf0 864 // perform read bit
rs27 0:c57706d25cf0 865 rdbit = OWReadBit();
rs27 0:c57706d25cf0 866
rs27 0:c57706d25cf0 867 // check if response was correct, if not then turn off strong pullup
rs27 0:c57706d25cf0 868 if (rdbit != applyPowerResponse)
rs27 0:c57706d25cf0 869 {
rs27 0:c57706d25cf0 870 OWLevel(MODE_STANDARD);
rs27 0:c57706d25cf0 871 return FALSE;
rs27 0:c57706d25cf0 872 }
rs27 0:c57706d25cf0 873
rs27 0:c57706d25cf0 874 return TRUE;
rs27 0:c57706d25cf0 875 }
rs27 0:c57706d25cf0 876
rs27 0:c57706d25cf0 877 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 878 // Send 8 bits of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 879 // 8 bits read from the 1-Wire Net are the same (write operation).
rs27 0:c57706d25cf0 880 // The parameter 'sendbyte' least significant 8 bits are used. After the
rs27 0:c57706d25cf0 881 // 8 bits are sent change the level of the 1-Wire net.
rs27 0:c57706d25cf0 882 //
rs27 0:c57706d25cf0 883 // 'sendbyte' - 8 bits to send (least significant bit)
rs27 0:c57706d25cf0 884 //
rs27 0:c57706d25cf0 885 // Returns: TRUE: bytes written and echo was the same, strong pullup now on
rs27 0:c57706d25cf0 886 // FALSE: echo was not the same
rs27 0:c57706d25cf0 887 //
rs27 0:c57706d25cf0 888 uint8_t DS2482::OWWriteBytePower(uint8_t sendbyte)
rs27 0:c57706d25cf0 889 {
rs27 0:c57706d25cf0 890 // set strong pullup enable
rs27 0:c57706d25cf0 891 cSPU = DS2482_CFG_SPU;
rs27 0:c57706d25cf0 892
rs27 0:c57706d25cf0 893 // write the new config
rs27 0:c57706d25cf0 894 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 895 {
rs27 0:c57706d25cf0 896 #if OW_MASTER_DEBUG
rs27 0:c57706d25cf0 897 printf("\r\nSPU off");
rs27 0:c57706d25cf0 898 #endif
rs27 0:c57706d25cf0 899
rs27 0:c57706d25cf0 900 return FALSE;
rs27 0:c57706d25cf0 901 }
rs27 0:c57706d25cf0 902
rs27 0:c57706d25cf0 903 // perform write byte
rs27 0:c57706d25cf0 904 OWWriteByte(sendbyte);
rs27 0:c57706d25cf0 905
rs27 0:c57706d25cf0 906 return TRUE;
rs27 0:c57706d25cf0 907 }
rs27 0:c57706d25cf0 908
rs27 0:c57706d25cf0 909
rs27 0:c57706d25cf0 910 // end I2C DS2482
rs27 0:c57706d25cf0 911 //======================================================================
rs27 0:c57706d25cf0 912