one wire driver

Dependents:   09_PT1000 10_PT1000 11_PT1000

Committer:
rs27
Date:
Sat Jul 26 07:27:00 2014 +0000
Revision:
5:d94037eb31ed
Parent:
4:c29e67d55f86
one wire driver

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 5:d94037eb31ed 5 #include "monitor.h"
rs27 0:c57706d25cf0 6
rs27 1:0950824b1ca3 7 extern Serial pc;
rs27 1:0950824b1ca3 8
rs27 0:c57706d25cf0 9 //-----------------------------------------------------------------------------
rs27 0:c57706d25cf0 10 // CRC = X^8 + X^5 + X^4 + 1
rs27 0:c57706d25cf0 11
rs27 0:c57706d25cf0 12 #define CRC_TABLE_ITEMS 256
rs27 0:c57706d25cf0 13
rs27 0:c57706d25cf0 14 const uint8_t crc_table[CRC_TABLE_ITEMS] =
rs27 0:c57706d25cf0 15 {
rs27 0:c57706d25cf0 16 0 , 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
rs27 0:c57706d25cf0 17 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
rs27 0:c57706d25cf0 18 35 ,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
rs27 0:c57706d25cf0 19 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
rs27 0:c57706d25cf0 20 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
rs27 0:c57706d25cf0 21 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
rs27 0:c57706d25cf0 22 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
rs27 0:c57706d25cf0 23 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
rs27 0:c57706d25cf0 24 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
rs27 0:c57706d25cf0 25 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
rs27 0:c57706d25cf0 26 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
rs27 0:c57706d25cf0 27 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
rs27 0:c57706d25cf0 28 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
rs27 0:c57706d25cf0 29 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
rs27 0:c57706d25cf0 30 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
rs27 0:c57706d25cf0 31 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
rs27 0:c57706d25cf0 32 };
rs27 0:c57706d25cf0 33
rs27 0:c57706d25cf0 34
rs27 0:c57706d25cf0 35 //==========================================================================
rs27 0:c57706d25cf0 36 // destructor
rs27 0:c57706d25cf0 37
rs27 0:c57706d25cf0 38 DS2482::DS2482(PinName sda, PinName scl, int address) : i2c(sda, scl)
rs27 0:c57706d25cf0 39 {
rs27 0:c57706d25cf0 40 addr = address;
rs27 0:c57706d25cf0 41 i2c.frequency(100000);
rs27 0:c57706d25cf0 42 }
rs27 0:c57706d25cf0 43
rs27 0:c57706d25cf0 44 //-----------------------------------------------------------------------------
rs27 0:c57706d25cf0 45 // Calculate the CRC8 of the byte value provided with the current
rs27 0:c57706d25cf0 46 // global 'crc8' value.
rs27 0:c57706d25cf0 47 // Returns current global crc8 value
rs27 0:c57706d25cf0 48 //
rs27 0:c57706d25cf0 49 uint8_t DS2482::crc_calc(uint8_t x)
rs27 0:c57706d25cf0 50 {
rs27 0:c57706d25cf0 51 crc_value = crc_table[crc_value ^ x];
rs27 0:c57706d25cf0 52 return crc_value;
rs27 0:c57706d25cf0 53 }
rs27 0:c57706d25cf0 54
rs27 1:0950824b1ca3 55 uint8_t DS2482::crc_calc_buffer(uint8_t *pbuffer, uint8_t count)
rs27 1:0950824b1ca3 56 {
rs27 1:0950824b1ca3 57 uint8_t n;
rs27 1:0950824b1ca3 58
rs27 1:0950824b1ca3 59 crc_value = 0;
rs27 1:0950824b1ca3 60
rs27 1:0950824b1ca3 61 for (n = 0; n < count; n++)
rs27 1:0950824b1ca3 62 {
rs27 1:0950824b1ca3 63 crc_value = crc_table[crc_value ^ *pbuffer++];
rs27 1:0950824b1ca3 64 }
rs27 1:0950824b1ca3 65
rs27 1:0950824b1ca3 66 return crc_value;
rs27 1:0950824b1ca3 67 }
rs27 1:0950824b1ca3 68
rs27 0:c57706d25cf0 69 //==========================================================================
rs27 0:c57706d25cf0 70 // I2C DS2482
rs27 0:c57706d25cf0 71 //
rs27 0:c57706d25cf0 72 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 73 // DS2482 Detect routine that sets the I2C address and then performs a
rs27 0:c57706d25cf0 74 // device reset followed by writing the configuration byte to default values:
rs27 0:c57706d25cf0 75 // 1-Wire speed (c1WS) = standard (0)
rs27 0:c57706d25cf0 76 // Strong pullup (cSPU) = off (0)
rs27 0:c57706d25cf0 77 // Presence pulse masking (cPPM) = off (0)
rs27 0:c57706d25cf0 78 // Active pullup (cAPU) = on (CONFIG_APU = 0x01)
rs27 0:c57706d25cf0 79 //
rs27 0:c57706d25cf0 80 // Returns: TRUE if device was detected and written
rs27 0:c57706d25cf0 81 // FALSE device not detected or failure to write configuration byte
rs27 0:c57706d25cf0 82 //uint8_t DS2482_detect(unsigned char addr)
rs27 0:c57706d25cf0 83 //
rs27 0:c57706d25cf0 84 uint8_t DS2482::detect(void)
rs27 0:c57706d25cf0 85 {
rs27 0:c57706d25cf0 86
rs27 0:c57706d25cf0 87 if (!reset()) // reset the DS2482 ON selected address
rs27 0:c57706d25cf0 88 {
rs27 0:c57706d25cf0 89 #if OW_MASTER_START
rs27 1:0950824b1ca3 90 pc.printf("\r\n--- DS2482 bus0 reset not executed \n");
rs27 1:0950824b1ca3 91 wait(0.1);
rs27 0:c57706d25cf0 92 #endif
rs27 0:c57706d25cf0 93
rs27 0:c57706d25cf0 94 return false;
rs27 0:c57706d25cf0 95 }
rs27 0:c57706d25cf0 96
rs27 0:c57706d25cf0 97 // default configuration
rs27 0:c57706d25cf0 98 c1WS = 0;
rs27 0:c57706d25cf0 99 cSPU = 0;
rs27 0:c57706d25cf0 100 cPPM = 0;
rs27 0:c57706d25cf0 101 cAPU = DS2482_CFG_APU;
rs27 0:c57706d25cf0 102
rs27 0:c57706d25cf0 103 // write the default configuration setup
rs27 0:c57706d25cf0 104 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 105 {
rs27 0:c57706d25cf0 106 #if OW_MASTER_START
rs27 1:0950824b1ca3 107 pc.printf("\r\n--- DS2482 configuration failure \n");
rs27 1:0950824b1ca3 108 wait(0.1);
rs27 0:c57706d25cf0 109 #endif
rs27 0:c57706d25cf0 110
rs27 0:c57706d25cf0 111 return false;
rs27 0:c57706d25cf0 112 }
rs27 0:c57706d25cf0 113
rs27 0:c57706d25cf0 114 #if OW_MASTER_START
rs27 1:0950824b1ca3 115 pc.printf("\r\n*** DS2482 detect OK \n");
rs27 1:0950824b1ca3 116 wait(0.1);
rs27 0:c57706d25cf0 117 #endif
rs27 0:c57706d25cf0 118
rs27 0:c57706d25cf0 119 return true;
rs27 0:c57706d25cf0 120 }
rs27 0:c57706d25cf0 121
rs27 0:c57706d25cf0 122 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 123 // Perform a device reset on the DS2482
rs27 0:c57706d25cf0 124 //
rs27 0:c57706d25cf0 125 // Returns: TRUE if device was reset
rs27 0:c57706d25cf0 126 // FALSE device not detected or failure to perform reset
rs27 0:c57706d25cf0 127 //
rs27 0:c57706d25cf0 128 int DS2482::reset(void)
rs27 0:c57706d25cf0 129 {
rs27 0:c57706d25cf0 130 char cmd[2];
rs27 0:c57706d25cf0 131
rs27 0:c57706d25cf0 132 cmd[0] = DS2482_CMD_DRST;
rs27 1:0950824b1ca3 133 // pc.printf("\nreset write");
rs27 0:c57706d25cf0 134 i2c.write(addr, cmd, 1);
rs27 1:0950824b1ca3 135 // pc.printf("\nreset read");
rs27 0:c57706d25cf0 136 i2c.read(addr, cmd, 1);
rs27 1:0950824b1ca3 137 // pc.printf(" cmd = %02x \n",cmd[0]);
rs27 1:0950824b1ca3 138 wait(0.1);
rs27 0:c57706d25cf0 139 return ((cmd[0] & 0xF7) == 0x10);
rs27 0:c57706d25cf0 140 }
rs27 0:c57706d25cf0 141
rs27 0:c57706d25cf0 142 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 143 // Write the configuration register in the DS2482. The configuration
rs27 0:c57706d25cf0 144 // options are provided in the lower nibble of the provided config byte.
rs27 0:c57706d25cf0 145 // The uppper nibble in bitwise inverted when written to the DS2482.
rs27 0:c57706d25cf0 146 //
rs27 0:c57706d25cf0 147 // Returns: TRUE: config written and response correct
rs27 0:c57706d25cf0 148 // FALSE: response incorrect
rs27 0:c57706d25cf0 149 //
rs27 0:c57706d25cf0 150 uint8_t DS2482::write_config(uint8_t config)
rs27 0:c57706d25cf0 151 {
rs27 0:c57706d25cf0 152 char cmd[2];
rs27 0:c57706d25cf0 153 char read_config;
rs27 0:c57706d25cf0 154
rs27 0:c57706d25cf0 155 cmd[0] = DS2482_CMD_WCFG;
rs27 0:c57706d25cf0 156 cmd[1] = config | (~config << 4);
rs27 0:c57706d25cf0 157 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 158
rs27 0:c57706d25cf0 159 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 160 read_config = cmd[0];
rs27 0:c57706d25cf0 161
rs27 0:c57706d25cf0 162 if (config != read_config) // check for failure due to incorrect read back
rs27 0:c57706d25cf0 163 {
rs27 0:c57706d25cf0 164 // handle error
rs27 0:c57706d25cf0 165 // ...
rs27 0:c57706d25cf0 166 #if OW_MASTER_START
rs27 1:0950824b1ca3 167 pc.printf("\r\n---check for failure due to incorrect config read back");
rs27 0:c57706d25cf0 168 #endif
rs27 0:c57706d25cf0 169
rs27 0:c57706d25cf0 170 reset();
rs27 0:c57706d25cf0 171 return false;
rs27 0:c57706d25cf0 172 }
rs27 0:c57706d25cf0 173 return true;
rs27 0:c57706d25cf0 174
rs27 0:c57706d25cf0 175 }
rs27 0:c57706d25cf0 176
rs27 0:c57706d25cf0 177 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 178 // DS2482 1-Wire Operations
rs27 0:c57706d25cf0 179 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 180 //
rs27 0:c57706d25cf0 181 //OWReset
rs27 0:c57706d25cf0 182 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 183 // Reset all of the devices on the 1-Wire Net and return the result.
rs27 0:c57706d25cf0 184 //
rs27 0:c57706d25cf0 185 // Returns: TRUE(1): presence pulse(s) detected, device(s) reset
rs27 0:c57706d25cf0 186 // FALSE(0): no presence pulses detected
rs27 0:c57706d25cf0 187 //
rs27 0:c57706d25cf0 188 uint8_t DS2482::OWReset(void)
rs27 0:c57706d25cf0 189 {
rs27 0:c57706d25cf0 190 uint8_t poll_count = 0;
rs27 1:0950824b1ca3 191 char cmd[2];
rs27 0:c57706d25cf0 192
rs27 0:c57706d25cf0 193 cmd[0] = DS2482_CMD_1WRS;
rs27 0:c57706d25cf0 194 i2c.write(addr,cmd,1);
rs27 0:c57706d25cf0 195
rs27 0:c57706d25cf0 196 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 197 pc.printf("\r\n*** Reset all devices on the 1-Wire Net\r\n");
rs27 0:c57706d25cf0 198 #endif
rs27 1:0950824b1ca3 199
rs27 0:c57706d25cf0 200 do
rs27 0:c57706d25cf0 201 {
rs27 1:0950824b1ca3 202 i2c.read(addr,cmd,1);
rs27 1:0950824b1ca3 203 // pc.printf("\n read %04x",cmd[0]);
rs27 0:c57706d25cf0 204 }
rs27 0:c57706d25cf0 205 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 1:0950824b1ca3 206
rs27 0:c57706d25cf0 207 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 208 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 209 {
rs27 0:c57706d25cf0 210 // handle error
rs27 0:c57706d25cf0 211 // ...
rs27 0:c57706d25cf0 212 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 213 pc.printf("\r\n---poll limit reached");
rs27 0:c57706d25cf0 214 #endif
rs27 0:c57706d25cf0 215
rs27 0:c57706d25cf0 216 reset();
rs27 1:0950824b1ca3 217 return false;
rs27 0:c57706d25cf0 218 }
rs27 0:c57706d25cf0 219
rs27 0:c57706d25cf0 220 // check for short condition
rs27 0:c57706d25cf0 221 if (cmd[0] & DS2482_STATUS_SD)
rs27 0:c57706d25cf0 222 {
rs27 0:c57706d25cf0 223 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 224 pc.printf("\r\n---1-Wire Net short detected %04x",cmd[0]);
rs27 0:c57706d25cf0 225 #endif
rs27 0:c57706d25cf0 226
rs27 0:c57706d25cf0 227 short_detected = true;
rs27 0:c57706d25cf0 228 }
rs27 0:c57706d25cf0 229 else
rs27 0:c57706d25cf0 230 {
rs27 0:c57706d25cf0 231 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 232 pc.printf("\r\n*** 1-Wire electrical net is OK");
rs27 0:c57706d25cf0 233 #endif
rs27 0:c57706d25cf0 234
rs27 0:c57706d25cf0 235 short_detected = false;
rs27 0:c57706d25cf0 236 }
rs27 1:0950824b1ca3 237
rs27 0:c57706d25cf0 238 // check for presence detect
rs27 0:c57706d25cf0 239 if (cmd[0] & DS2482_STATUS_PPD)
rs27 0:c57706d25cf0 240 {
rs27 0:c57706d25cf0 241 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 242 pc.printf("\r\n*** 1-Wire Device detected");
rs27 0:c57706d25cf0 243 #endif
rs27 0:c57706d25cf0 244
rs27 0:c57706d25cf0 245 return true;
rs27 0:c57706d25cf0 246 }
rs27 0:c57706d25cf0 247 else
rs27 0:c57706d25cf0 248 {
rs27 0:c57706d25cf0 249 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 250 pc.printf("\r\n---No Device detected");
rs27 0:c57706d25cf0 251 #endif
rs27 0:c57706d25cf0 252
rs27 0:c57706d25cf0 253 return false;
rs27 0:c57706d25cf0 254 }
rs27 0:c57706d25cf0 255 }
rs27 0:c57706d25cf0 256
rs27 0:c57706d25cf0 257 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 258 // Send 1 bit of communication to the 1-Wire Net.
rs27 0:c57706d25cf0 259 // The parameter 'sendbit' least significant bit is used.
rs27 0:c57706d25cf0 260 //
rs27 0:c57706d25cf0 261 // 'sendbit' - 1 bit to send (least significant byte)
rs27 0:c57706d25cf0 262 //
rs27 0:c57706d25cf0 263 void DS2482::OWWriteBit(uint8_t sendbit)
rs27 0:c57706d25cf0 264 {
rs27 0:c57706d25cf0 265 OWTouchBit(sendbit);
rs27 0:c57706d25cf0 266 }
rs27 0:c57706d25cf0 267
rs27 0:c57706d25cf0 268 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 269 // Reads 1 bit of communication from the 1-Wire Net and returns the
rs27 0:c57706d25cf0 270 // result
rs27 0:c57706d25cf0 271 //
rs27 0:c57706d25cf0 272 // Returns: 1 bit read from 1-Wire Net
rs27 0:c57706d25cf0 273 //
rs27 0:c57706d25cf0 274 uint8_t DS2482::OWReadBit(void)
rs27 0:c57706d25cf0 275 {
rs27 0:c57706d25cf0 276 return OWTouchBit(0x01);
rs27 0:c57706d25cf0 277 }
rs27 0:c57706d25cf0 278
rs27 0:c57706d25cf0 279 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 280 // Send 1 bit of communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 281 // result 1 bit read from the 1-Wire Net. The parameter 'sendbit'
rs27 0:c57706d25cf0 282 // least significant bit is used and the least significant bit
rs27 0:c57706d25cf0 283 // of the result is the return bit.
rs27 0:c57706d25cf0 284 //
rs27 0:c57706d25cf0 285 // 'sendbit' - the least significant bit is the bit to send
rs27 0:c57706d25cf0 286 //
rs27 0:c57706d25cf0 287 // Returns: 0: 0 bit read from sendbit
rs27 0:c57706d25cf0 288 // 1: 1 bit read from sendbit
rs27 0:c57706d25cf0 289 //
rs27 0:c57706d25cf0 290 uint8_t DS2482::OWTouchBit(uint8_t sendbit)
rs27 0:c57706d25cf0 291 {
rs27 0:c57706d25cf0 292 char cmd[2];
rs27 0:c57706d25cf0 293 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 294
rs27 0:c57706d25cf0 295 cmd[0] = DS2482_CMD_1WSB;
rs27 0:c57706d25cf0 296 cmd[1] = sendbit ? 0x80 : 0x00;
rs27 0:c57706d25cf0 297 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 298
rs27 0:c57706d25cf0 299 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 300 // abort if poll limit reached
rs27 0:c57706d25cf0 301
rs27 0:c57706d25cf0 302 do
rs27 0:c57706d25cf0 303 {
rs27 0:c57706d25cf0 304 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 305 }
rs27 0:c57706d25cf0 306 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 307
rs27 0:c57706d25cf0 308 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 309 pc.printf("\r\n*** Send 1 bit to the 1-Wire Net");
rs27 0:c57706d25cf0 310 #endif
rs27 0:c57706d25cf0 311
rs27 0:c57706d25cf0 312 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 313 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 314 {
rs27 0:c57706d25cf0 315 // handle error
rs27 0:c57706d25cf0 316 // ...
rs27 0:c57706d25cf0 317 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 318 pc.printf("\r\n---handle error OW Write Bit");
rs27 0:c57706d25cf0 319 #endif
rs27 0:c57706d25cf0 320
rs27 0:c57706d25cf0 321 reset();
rs27 0:c57706d25cf0 322 return 0;
rs27 0:c57706d25cf0 323 }
rs27 0:c57706d25cf0 324
rs27 0:c57706d25cf0 325 // return bit state
rs27 0:c57706d25cf0 326 if (cmd[0] & DS2482_STATUS_SBR)
rs27 0:c57706d25cf0 327 {
rs27 0:c57706d25cf0 328 return 1;
rs27 0:c57706d25cf0 329 }
rs27 0:c57706d25cf0 330 else
rs27 0:c57706d25cf0 331 {
rs27 0:c57706d25cf0 332 return 0;
rs27 0:c57706d25cf0 333 }
rs27 0:c57706d25cf0 334 }
rs27 0:c57706d25cf0 335
rs27 0:c57706d25cf0 336 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 337 // Send 8 bits of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 338 // 8 bits read from the 1-Wire Net are the same (write operation).
rs27 0:c57706d25cf0 339 // The parameter 'sendbyte' least significant 8 bits are used.
rs27 0:c57706d25cf0 340 //
rs27 0:c57706d25cf0 341 // 'sendbyte' - 8 bits to send (least significant byte)
rs27 0:c57706d25cf0 342 //
rs27 0:c57706d25cf0 343 // Returns: TRUE: bytes written and echo was the same
rs27 0:c57706d25cf0 344 // FALSE: echo was not the same
rs27 0:c57706d25cf0 345 //
rs27 0:c57706d25cf0 346 void DS2482::OWWriteByte(uint8_t sendbyte)
rs27 0:c57706d25cf0 347 {
rs27 0:c57706d25cf0 348 char cmd[2];
rs27 0:c57706d25cf0 349 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 350
rs27 0:c57706d25cf0 351 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 352 pc.printf("\r\n*** Send 8 bits of WRITE to the 1-Wire Net");
rs27 0:c57706d25cf0 353 #endif
rs27 0:c57706d25cf0 354
rs27 0:c57706d25cf0 355 cmd[0] = DS2482_CMD_1WWB;
rs27 0:c57706d25cf0 356 cmd[1] = sendbyte;
rs27 0:c57706d25cf0 357
rs27 0:c57706d25cf0 358 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 359
rs27 0:c57706d25cf0 360 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 361 // abort if poll limit reached
rs27 0:c57706d25cf0 362
rs27 0:c57706d25cf0 363 do
rs27 0:c57706d25cf0 364 {
rs27 0:c57706d25cf0 365 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 366 }
rs27 0:c57706d25cf0 367 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 368
rs27 0:c57706d25cf0 369 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 370 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 371 {
rs27 0:c57706d25cf0 372 // handle error
rs27 0:c57706d25cf0 373 // ...
rs27 0:c57706d25cf0 374 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 375 pc.printf("\r\n---handle error OW Write Byte");
rs27 0:c57706d25cf0 376 #endif
rs27 0:c57706d25cf0 377
rs27 0:c57706d25cf0 378 reset();
rs27 0:c57706d25cf0 379 }
rs27 1:0950824b1ca3 380
rs27 1:0950824b1ca3 381 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 382 pc.printf(" done");
rs27 1:0950824b1ca3 383 #endif
rs27 0:c57706d25cf0 384 }
rs27 0:c57706d25cf0 385
rs27 0:c57706d25cf0 386 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 387 // Send 8 bits of read communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 388 // result 8 bits read from the 1-Wire Net.
rs27 0:c57706d25cf0 389 //
rs27 0:c57706d25cf0 390 // Returns: 8 bits read from 1-Wire Net
rs27 0:c57706d25cf0 391 //
rs27 0:c57706d25cf0 392 uint8_t DS2482::OWReadByte(void)
rs27 0:c57706d25cf0 393 {
rs27 0:c57706d25cf0 394 uint8_t poll_count = 0;
rs27 0:c57706d25cf0 395 char cmd[2];
rs27 0:c57706d25cf0 396
rs27 0:c57706d25cf0 397 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 398 pc.printf("\r\n*** Read 8 bits from the 1-Wire Net");
rs27 0:c57706d25cf0 399 #endif
rs27 0:c57706d25cf0 400
rs27 0:c57706d25cf0 401 cmd[0] = DS2482_CMD_1WRB; // DS2482 1-Wire Read Byte
rs27 0:c57706d25cf0 402 i2c.write(addr, cmd, 1);
rs27 0:c57706d25cf0 403
rs27 0:c57706d25cf0 404 // loop checking 1WB bit for completion of 1-Wire operation
rs27 0:c57706d25cf0 405 // abort if poll limit reached
rs27 0:c57706d25cf0 406
rs27 0:c57706d25cf0 407 do
rs27 0:c57706d25cf0 408 {
rs27 1:0950824b1ca3 409 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 410 }
rs27 0:c57706d25cf0 411 while ((cmd[0] & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 412
rs27 0:c57706d25cf0 413 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 414 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 415 {
rs27 0:c57706d25cf0 416 // handle error
rs27 0:c57706d25cf0 417 // ...
rs27 0:c57706d25cf0 418 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 419 pc.printf("\r\n---handle error OW Read Byte");
rs27 0:c57706d25cf0 420 #endif
rs27 0:c57706d25cf0 421
rs27 0:c57706d25cf0 422 reset();
rs27 0:c57706d25cf0 423 return 0;
rs27 0:c57706d25cf0 424 }
rs27 0:c57706d25cf0 425
rs27 0:c57706d25cf0 426 cmd[0] = DS2482_CMD_SRP; // DS2482 Set Read Pointer
rs27 0:c57706d25cf0 427 cmd[1] = DS2482_READPTR_RDR; // DS2482 Read Data Register
rs27 1:0950824b1ca3 428 i2c.write(addr, cmd, 2);
rs27 0:c57706d25cf0 429
rs27 1:0950824b1ca3 430 i2c.read(addr, cmd, 1);
rs27 0:c57706d25cf0 431
rs27 0:c57706d25cf0 432 return cmd[0];
rs27 0:c57706d25cf0 433 }
rs27 0:c57706d25cf0 434
rs27 0:c57706d25cf0 435 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 436 // The 'OWBlock' transfers a block of data to and from the
rs27 0:c57706d25cf0 437 // 1-Wire Net. The result is returned in the same buffer.
rs27 0:c57706d25cf0 438 //
rs27 0:c57706d25cf0 439 // 'tran_buf' - pointer to a block of unsigned
rs27 0:c57706d25cf0 440 // chars of length 'tran_len' that will be sent
rs27 0:c57706d25cf0 441 // to the 1-Wire Net
rs27 0:c57706d25cf0 442 // 'tran_len' - length in bytes to transfer
rs27 0:c57706d25cf0 443 //
rs27 0:c57706d25cf0 444 void DS2482::OWBlock(uint8_t *tran_buf, uint8_t tran_len)
rs27 0:c57706d25cf0 445 {
rs27 0:c57706d25cf0 446 uint8_t i;
rs27 0:c57706d25cf0 447
rs27 0:c57706d25cf0 448 for (i = 0; i < tran_len; i++)
rs27 0:c57706d25cf0 449 {
rs27 0:c57706d25cf0 450 tran_buf[i] = OWTouchByte(tran_buf[i]);
rs27 0:c57706d25cf0 451 }
rs27 0:c57706d25cf0 452 }
rs27 0:c57706d25cf0 453
rs27 0:c57706d25cf0 454 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 455 // Send 8 bits of communication to the 1-Wire Net and return the
rs27 0:c57706d25cf0 456 // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte'
rs27 0:c57706d25cf0 457 // least significant 8 bits are used and the least significant 8 bits
rs27 0:c57706d25cf0 458 // of the result are the return byte.
rs27 0:c57706d25cf0 459 //
rs27 0:c57706d25cf0 460 // 'sendbyte' - 8 bits to send (least significant byte)
rs27 0:c57706d25cf0 461 //
rs27 0:c57706d25cf0 462 // Returns: 8 bits read from sendbyte
rs27 0:c57706d25cf0 463 //
rs27 0:c57706d25cf0 464 uint8_t DS2482::OWTouchByte(uint8_t sendbyte)
rs27 0:c57706d25cf0 465 {
rs27 0:c57706d25cf0 466 if (sendbyte == 0xFF)
rs27 0:c57706d25cf0 467 {
rs27 0:c57706d25cf0 468 return OWReadByte();
rs27 0:c57706d25cf0 469 }
rs27 0:c57706d25cf0 470 else
rs27 0:c57706d25cf0 471 {
rs27 0:c57706d25cf0 472 OWWriteByte(sendbyte);
rs27 0:c57706d25cf0 473 return sendbyte;
rs27 0:c57706d25cf0 474 }
rs27 0:c57706d25cf0 475 }
rs27 0:c57706d25cf0 476
rs27 0:c57706d25cf0 477 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 478 // Search state
rs27 0:c57706d25cf0 479 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 480 // Find the 'first' devices on the 1-Wire network
rs27 0:c57706d25cf0 481 // Return TRUE : device found, ROM number in ROM_NO buffer
rs27 0:c57706d25cf0 482 // FALSE : no device present
rs27 0:c57706d25cf0 483 //
rs27 0:c57706d25cf0 484 uint8_t DS2482::OWFirst(void)
rs27 0:c57706d25cf0 485 {
rs27 0:c57706d25cf0 486 // reset the search state
rs27 0:c57706d25cf0 487 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 488 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 489 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 490
rs27 0:c57706d25cf0 491 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 492 pc.printf("\r\n*** Find the 'first' device on the 1-Wire network");
rs27 0:c57706d25cf0 493 #endif
rs27 0:c57706d25cf0 494
rs27 0:c57706d25cf0 495 return OWSearch();
rs27 0:c57706d25cf0 496 }
rs27 0:c57706d25cf0 497
rs27 0:c57706d25cf0 498 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 499 // Find the 'next' devices on the 1-Wire network
rs27 0:c57706d25cf0 500 // Return TRUE : device found, ROM number in ROM_NO buffer
rs27 0:c57706d25cf0 501 // FALSE : device not found, end of search
rs27 0:c57706d25cf0 502 //
rs27 0:c57706d25cf0 503 uint8_t DS2482::OWNext(void)
rs27 0:c57706d25cf0 504 {
rs27 0:c57706d25cf0 505 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 506 pc.printf("\r\n*** Find the 'next' device on the 1-Wire network");
rs27 0:c57706d25cf0 507 #endif
rs27 0:c57706d25cf0 508
rs27 0:c57706d25cf0 509 // leave the search state alone
rs27 0:c57706d25cf0 510 return OWSearch();
rs27 0:c57706d25cf0 511 }
rs27 0:c57706d25cf0 512
rs27 0:c57706d25cf0 513 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 514 // Verify the device with the ROM number in ROM_NO buffer is present.
rs27 0:c57706d25cf0 515 // Return TRUE : device present
rs27 0:c57706d25cf0 516 // FALSE : device not present
rs27 0:c57706d25cf0 517 //
rs27 0:c57706d25cf0 518 int DS2482::OWVerify(void)
rs27 0:c57706d25cf0 519 {
rs27 0:c57706d25cf0 520 uint8_t rom_backup[8], status;
rs27 0:c57706d25cf0 521 int i,ld_backup,ldf_backup,lfd_backup;
rs27 0:c57706d25cf0 522
rs27 0:c57706d25cf0 523 // keep a backup copy of the current state
rs27 0:c57706d25cf0 524 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 525 {
rs27 0:c57706d25cf0 526 rom_backup[i] = ROM_NO[i];
rs27 0:c57706d25cf0 527 }
rs27 0:c57706d25cf0 528
rs27 0:c57706d25cf0 529 ld_backup = LastDiscrepancy;
rs27 0:c57706d25cf0 530 ldf_backup = LastDeviceFlag;
rs27 0:c57706d25cf0 531 lfd_backup = LastFamilyDiscrepancy;
rs27 0:c57706d25cf0 532
rs27 0:c57706d25cf0 533 // set search to find the same device
rs27 0:c57706d25cf0 534 LastDiscrepancy = 64;
rs27 0:c57706d25cf0 535 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 536
rs27 0:c57706d25cf0 537 if (OWSearch())
rs27 0:c57706d25cf0 538 {
rs27 0:c57706d25cf0 539 // check if same device found
rs27 0:c57706d25cf0 540 status = TRUE;
rs27 0:c57706d25cf0 541
rs27 0:c57706d25cf0 542 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 543 {
rs27 0:c57706d25cf0 544 if (rom_backup[i] != ROM_NO[i])
rs27 0:c57706d25cf0 545 {
rs27 0:c57706d25cf0 546 status = FALSE;
rs27 0:c57706d25cf0 547 break;
rs27 0:c57706d25cf0 548 }
rs27 0:c57706d25cf0 549 }
rs27 0:c57706d25cf0 550 }
rs27 0:c57706d25cf0 551 else
rs27 0:c57706d25cf0 552 {
rs27 0:c57706d25cf0 553 status = FALSE;
rs27 0:c57706d25cf0 554 }
rs27 0:c57706d25cf0 555
rs27 0:c57706d25cf0 556 // restore the search state
rs27 0:c57706d25cf0 557 for (i = 0; i < 8; i++)
rs27 0:c57706d25cf0 558 {
rs27 0:c57706d25cf0 559 ROM_NO[i] = rom_backup[i];
rs27 0:c57706d25cf0 560 }
rs27 0:c57706d25cf0 561 LastDiscrepancy = ld_backup;
rs27 0:c57706d25cf0 562 LastDeviceFlag = ldf_backup;
rs27 0:c57706d25cf0 563 LastFamilyDiscrepancy = lfd_backup;
rs27 0:c57706d25cf0 564
rs27 0:c57706d25cf0 565 // return the result of the verify
rs27 0:c57706d25cf0 566 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 567 pc.printf("\r\n*** 1-Wire Verify device with the ROM number in ROM_NO");
rs27 0:c57706d25cf0 568 #endif
rs27 0:c57706d25cf0 569
rs27 0:c57706d25cf0 570 return status;
rs27 0:c57706d25cf0 571 }
rs27 0:c57706d25cf0 572
rs27 0:c57706d25cf0 573 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 574 // Setup the search to find the device type 'family_code' on the next call
rs27 0:c57706d25cf0 575 // to OWNext() if it is present.
rs27 0:c57706d25cf0 576 //
rs27 0:c57706d25cf0 577 void DS2482::OWTargetSetup(uint8_t family_code)
rs27 0:c57706d25cf0 578 {
rs27 0:c57706d25cf0 579 uint8_t i;
rs27 0:c57706d25cf0 580
rs27 0:c57706d25cf0 581 // set the search state to find SearchFamily type devices
rs27 0:c57706d25cf0 582 ROM_NO[0] = family_code;
rs27 0:c57706d25cf0 583
rs27 0:c57706d25cf0 584 for (i = 1; i < 8; i++)
rs27 0:c57706d25cf0 585 {
rs27 0:c57706d25cf0 586 ROM_NO[i] = 0;
rs27 0:c57706d25cf0 587 }
rs27 0:c57706d25cf0 588
rs27 0:c57706d25cf0 589 LastDiscrepancy = 64;
rs27 0:c57706d25cf0 590 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 591 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 592 }
rs27 0:c57706d25cf0 593
rs27 0:c57706d25cf0 594 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 595 // Setup the search to skip the current device type on the next call
rs27 0:c57706d25cf0 596 // to OWNext().
rs27 0:c57706d25cf0 597 //
rs27 0:c57706d25cf0 598 void DS2482::OWFamilySkipSetup(void)
rs27 0:c57706d25cf0 599 {
rs27 0:c57706d25cf0 600 // set the Last discrepancy to last family discrepancy
rs27 0:c57706d25cf0 601 LastDiscrepancy = LastFamilyDiscrepancy;
rs27 0:c57706d25cf0 602
rs27 0:c57706d25cf0 603 // clear the last family discrpepancy
rs27 0:c57706d25cf0 604 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 605
rs27 0:c57706d25cf0 606 // check for end of list
rs27 0:c57706d25cf0 607 if (LastDiscrepancy == 0)
rs27 0:c57706d25cf0 608 {
rs27 0:c57706d25cf0 609 LastDeviceFlag = TRUE;
rs27 0:c57706d25cf0 610 }
rs27 0:c57706d25cf0 611 }
rs27 0:c57706d25cf0 612
rs27 0:c57706d25cf0 613 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 614 // The 'OWSearch' function does a general search. This function
rs27 0:c57706d25cf0 615 // continues from the previous search state. The search state
rs27 0:c57706d25cf0 616 // can be reset by using the 'OWFirst' function.
rs27 0:c57706d25cf0 617 // This function contains one parameter 'alarm_only'.
rs27 0:c57706d25cf0 618 // When 'alarm_only' is TRUE (1) the find alarm command
rs27 0:c57706d25cf0 619 // 0xEC is sent instead of the normal search command 0xF0.
rs27 0:c57706d25cf0 620 // Using the find alarm command 0xEC will limit the search to only
rs27 0:c57706d25cf0 621 // 1-Wire devices that are in an 'alarm' state.
rs27 0:c57706d25cf0 622 //
rs27 0:c57706d25cf0 623 // Returns: TRUE (1) : when a 1-Wire device was found and its
rs27 0:c57706d25cf0 624 // Serial Number placed in the global ROM
rs27 0:c57706d25cf0 625 // FALSE (0): when no new device was found. Either the
rs27 0:c57706d25cf0 626 // last search was the last device or there
rs27 0:c57706d25cf0 627 // are no devices on the 1-Wire Net.
rs27 0:c57706d25cf0 628 //
rs27 0:c57706d25cf0 629 uint8_t DS2482::OWSearch()
rs27 0:c57706d25cf0 630 {
rs27 0:c57706d25cf0 631 int id_bit_number;
rs27 0:c57706d25cf0 632 int last_zero, rom_byte_number, search_result;
rs27 0:c57706d25cf0 633 int id_bit, cmp_id_bit;
rs27 0:c57706d25cf0 634 uint8_t rom_byte_mask, search_direction, status;
rs27 0:c57706d25cf0 635
rs27 0:c57706d25cf0 636 // initialize for search
rs27 0:c57706d25cf0 637 id_bit_number = 1;
rs27 0:c57706d25cf0 638 last_zero = 0;
rs27 0:c57706d25cf0 639 rom_byte_number = 0;
rs27 0:c57706d25cf0 640 rom_byte_mask = 1;
rs27 0:c57706d25cf0 641 search_result = FALSE;
rs27 0:c57706d25cf0 642 crc_value = 0;
rs27 0:c57706d25cf0 643
rs27 1:0950824b1ca3 644 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 645 pc.printf("\r\n*** one wire search");
rs27 1:0950824b1ca3 646 #endif
rs27 1:0950824b1ca3 647
rs27 0:c57706d25cf0 648 if (!LastDeviceFlag) // if the last call was not the last one
rs27 0:c57706d25cf0 649 {
rs27 0:c57706d25cf0 650 // 1-Wire reset
rs27 0:c57706d25cf0 651 if (!OWReset())
rs27 0:c57706d25cf0 652 {
rs27 0:c57706d25cf0 653 // reset the search
rs27 0:c57706d25cf0 654 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 655 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 656 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 657 return FALSE;
rs27 0:c57706d25cf0 658 }
rs27 0:c57706d25cf0 659
rs27 0:c57706d25cf0 660 // issue the search command
rs27 0:c57706d25cf0 661 OWWriteByte(OW_SEARCH_ROM);
rs27 0:c57706d25cf0 662
rs27 0:c57706d25cf0 663 // loop to do the search
rs27 0:c57706d25cf0 664 do
rs27 0:c57706d25cf0 665 {
rs27 0:c57706d25cf0 666 // if this discrepancy if before the Last Discrepancy
rs27 0:c57706d25cf0 667 // on a previous next then pick the same as last time
rs27 0:c57706d25cf0 668 if (id_bit_number < LastDiscrepancy)
rs27 0:c57706d25cf0 669 {
rs27 0:c57706d25cf0 670 if ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0)
rs27 0:c57706d25cf0 671 search_direction = 1;
rs27 0:c57706d25cf0 672 else
rs27 0:c57706d25cf0 673 search_direction = 0;
rs27 0:c57706d25cf0 674 }
rs27 0:c57706d25cf0 675 else
rs27 0:c57706d25cf0 676 {
rs27 0:c57706d25cf0 677 // if equal to last pick 1, if not then pick 0
rs27 0:c57706d25cf0 678 if (id_bit_number == LastDiscrepancy)
rs27 0:c57706d25cf0 679 search_direction = 1;
rs27 0:c57706d25cf0 680 else
rs27 0:c57706d25cf0 681 search_direction = 0;
rs27 0:c57706d25cf0 682 }
rs27 0:c57706d25cf0 683
rs27 0:c57706d25cf0 684 // Perform a triple operation on the DS2482 which will perform 2 read bits and 1 write bit
rs27 1:0950824b1ca3 685 //pc.printf("\n *** preform triplet operation ");
rs27 0:c57706d25cf0 686 status = search_triplet(search_direction);
rs27 1:0950824b1ca3 687 //pc.printf("%04x",status);
rs27 1:0950824b1ca3 688
rs27 0:c57706d25cf0 689 // check bit results in status byte
rs27 0:c57706d25cf0 690 id_bit = ((status & DS2482_STATUS_SBR) == DS2482_STATUS_SBR);
rs27 0:c57706d25cf0 691 cmp_id_bit = ((status & DS2482_STATUS_TSB) == DS2482_STATUS_TSB);
rs27 0:c57706d25cf0 692 search_direction = ((status & DS2482_STATUS_DIR) == DS2482_STATUS_DIR) ? 1 : 0;
rs27 0:c57706d25cf0 693
rs27 0:c57706d25cf0 694 // check for no devices on 1-Wire
rs27 0:c57706d25cf0 695 if ((id_bit) && (cmp_id_bit))
rs27 0:c57706d25cf0 696 {
rs27 0:c57706d25cf0 697 break;
rs27 0:c57706d25cf0 698 }
rs27 0:c57706d25cf0 699 else
rs27 0:c57706d25cf0 700 {
rs27 0:c57706d25cf0 701 if ((!id_bit) && (!cmp_id_bit) && (search_direction == 0))
rs27 0:c57706d25cf0 702 {
rs27 0:c57706d25cf0 703 last_zero = id_bit_number;
rs27 0:c57706d25cf0 704
rs27 0:c57706d25cf0 705 // check for Last discrepancy in family
rs27 0:c57706d25cf0 706 if (last_zero < 9)
rs27 0:c57706d25cf0 707 {
rs27 0:c57706d25cf0 708 LastFamilyDiscrepancy = last_zero;
rs27 0:c57706d25cf0 709 }
rs27 0:c57706d25cf0 710 }
rs27 0:c57706d25cf0 711
rs27 0:c57706d25cf0 712 // set or clear the bit in the ROM byte rom_byte_number
rs27 0:c57706d25cf0 713 // with mask rom_byte_mask
rs27 0:c57706d25cf0 714 if (search_direction == 1)
rs27 0:c57706d25cf0 715 {
rs27 0:c57706d25cf0 716 ROM_NO[rom_byte_number] |= rom_byte_mask;
rs27 0:c57706d25cf0 717 }
rs27 0:c57706d25cf0 718 else
rs27 0:c57706d25cf0 719 {
rs27 0:c57706d25cf0 720 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
rs27 0:c57706d25cf0 721 }
rs27 0:c57706d25cf0 722
rs27 0:c57706d25cf0 723 // increment the byte counter id_bit_number
rs27 0:c57706d25cf0 724 // and shift the mask rom_byte_mask
rs27 0:c57706d25cf0 725 id_bit_number++;
rs27 0:c57706d25cf0 726 rom_byte_mask <<= 1;
rs27 0:c57706d25cf0 727
rs27 0:c57706d25cf0 728 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
rs27 0:c57706d25cf0 729 if (rom_byte_mask == 0)
rs27 0:c57706d25cf0 730 {
rs27 0:c57706d25cf0 731 crc_calc(ROM_NO[rom_byte_number]); // accumulate the CRC
rs27 0:c57706d25cf0 732 rom_byte_number++;
rs27 0:c57706d25cf0 733 rom_byte_mask = 1;
rs27 0:c57706d25cf0 734 }
rs27 0:c57706d25cf0 735 }
rs27 0:c57706d25cf0 736 }
rs27 0:c57706d25cf0 737 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
rs27 0:c57706d25cf0 738
rs27 0:c57706d25cf0 739 // if the search was successful then
rs27 0:c57706d25cf0 740 if (!((id_bit_number < 65) || (crc_value != 0)))
rs27 0:c57706d25cf0 741 {
rs27 0:c57706d25cf0 742 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
rs27 0:c57706d25cf0 743 LastDiscrepancy = last_zero;
rs27 0:c57706d25cf0 744
rs27 0:c57706d25cf0 745 // check for last device
rs27 0:c57706d25cf0 746 if (LastDiscrepancy == 0)
rs27 0:c57706d25cf0 747 {
rs27 0:c57706d25cf0 748 LastDeviceFlag = TRUE;
rs27 0:c57706d25cf0 749 }
rs27 0:c57706d25cf0 750
rs27 0:c57706d25cf0 751 search_result = TRUE;
rs27 0:c57706d25cf0 752 }
rs27 0:c57706d25cf0 753 }
rs27 0:c57706d25cf0 754
rs27 0:c57706d25cf0 755 // if no device found then reset counters so next 'search' will be like a first
rs27 0:c57706d25cf0 756 if (!search_result || (ROM_NO[0] == 0))
rs27 0:c57706d25cf0 757 {
rs27 0:c57706d25cf0 758 LastDiscrepancy = 0;
rs27 0:c57706d25cf0 759 LastDeviceFlag = FALSE;
rs27 0:c57706d25cf0 760 LastFamilyDiscrepancy = 0;
rs27 0:c57706d25cf0 761 search_result = FALSE;
rs27 0:c57706d25cf0 762 }
rs27 0:c57706d25cf0 763
rs27 1:0950824b1ca3 764 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 765 pc.printf(" pass ");
rs27 1:0950824b1ca3 766 #endif
rs27 1:0950824b1ca3 767
rs27 0:c57706d25cf0 768 return search_result;
rs27 0:c57706d25cf0 769 }
rs27 0:c57706d25cf0 770
rs27 0:c57706d25cf0 771 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 772 // Use the DS2482 help command '1-Wire triplet' to perform one bit of a
rs27 0:c57706d25cf0 773 // 1-Wire search.
rs27 0:c57706d25cf0 774 // This command does two read bits and one write bit. The write bit
rs27 0:c57706d25cf0 775 // is either the default direction (all device have same bit) or in case of
rs27 0:c57706d25cf0 776 // a discrepancy, the 'search_direction' parameter is used.
rs27 0:c57706d25cf0 777 //
rs27 0:c57706d25cf0 778 // Returns – The DS2482 status byte result from the triplet command
rs27 0:c57706d25cf0 779 //
rs27 0:c57706d25cf0 780 uint8_t DS2482::search_triplet(uint8_t search_direction)
rs27 0:c57706d25cf0 781 {
rs27 1:0950824b1ca3 782 char cmd[2];
rs27 1:0950824b1ca3 783 uint8_t status, poll_count = 0;
rs27 0:c57706d25cf0 784
rs27 1:0950824b1ca3 785 cmd[0] = DS2482_CMD_1WT;
rs27 1:0950824b1ca3 786 cmd[1] = (search_direction ? 0x80 : 0x00);
rs27 1:0950824b1ca3 787 i2c.write(addr,cmd,2);
rs27 1:0950824b1ca3 788
rs27 0:c57706d25cf0 789 do
rs27 0:c57706d25cf0 790 {
rs27 1:0950824b1ca3 791 i2c.read(addr,cmd,1);
rs27 1:0950824b1ca3 792 status = cmd[0];
rs27 0:c57706d25cf0 793 }
rs27 0:c57706d25cf0 794 while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));
rs27 0:c57706d25cf0 795
rs27 0:c57706d25cf0 796 // check for failure due to poll limit reached
rs27 0:c57706d25cf0 797 if (poll_count >= POLL_LIMIT)
rs27 0:c57706d25cf0 798 {
rs27 0:c57706d25cf0 799 // handle error
rs27 0:c57706d25cf0 800 // ...
rs27 0:c57706d25cf0 801 reset();
rs27 0:c57706d25cf0 802 return 0;
rs27 0:c57706d25cf0 803 }
rs27 0:c57706d25cf0 804 // return status byte
rs27 0:c57706d25cf0 805 return status;
rs27 0:c57706d25cf0 806 }
rs27 0:c57706d25cf0 807
rs27 0:c57706d25cf0 808 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 809 // Set the 1-Wire Net communication speed.
rs27 0:c57706d25cf0 810 //
rs27 0:c57706d25cf0 811 // 'new_speed' - new speed defined as
rs27 0:c57706d25cf0 812 // MODE_STANDARD 0x00
rs27 0:c57706d25cf0 813 // MODE_OVERDRIVE 0x01
rs27 0:c57706d25cf0 814 //
rs27 0:c57706d25cf0 815 // Returns: current 1-Wire Net speed
rs27 0:c57706d25cf0 816 //
rs27 0:c57706d25cf0 817 uint8_t DS2482::OWSpeed(uint8_t new_speed)
rs27 0:c57706d25cf0 818 {
rs27 0:c57706d25cf0 819 // set the speed
rs27 0:c57706d25cf0 820 if (new_speed == MODE_OVERDRIVE)
rs27 0:c57706d25cf0 821 {
rs27 0:c57706d25cf0 822 c1WS = DS2482_CFG_1WS;
rs27 0:c57706d25cf0 823 }
rs27 0:c57706d25cf0 824 else
rs27 0:c57706d25cf0 825 {
rs27 0:c57706d25cf0 826 c1WS = FALSE;
rs27 0:c57706d25cf0 827 }
rs27 0:c57706d25cf0 828
rs27 0:c57706d25cf0 829 // write the new config
rs27 0:c57706d25cf0 830 write_config(c1WS | cSPU | cPPM | cAPU);
rs27 0:c57706d25cf0 831
rs27 0:c57706d25cf0 832
rs27 0:c57706d25cf0 833 return new_speed;
rs27 0:c57706d25cf0 834 }
rs27 0:c57706d25cf0 835
rs27 0:c57706d25cf0 836 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 837 // Set the 1-Wire Net line level pullup to normal. The DS2482 only
rs27 0:c57706d25cf0 838 // allows enabling strong pullup on a bit or byte event.
rs27 0:c57706d25cf0 839 // Consequently this function only allows the MODE_STANDARD argument.
rs27 0:c57706d25cf0 840 // To enable strong pullup
rs27 0:c57706d25cf0 841 // use OWWriteBytePower or OWReadBitPower.
rs27 0:c57706d25cf0 842 //
rs27 0:c57706d25cf0 843 // 'new_level' - new level defined as
rs27 0:c57706d25cf0 844 // MODE_STANDARD 0x00
rs27 0:c57706d25cf0 845 //
rs27 0:c57706d25cf0 846 // Returns: current 1-Wire Net level
rs27 0:c57706d25cf0 847 //
rs27 0:c57706d25cf0 848 uint8_t DS2482::OWLevel(uint8_t new_level)
rs27 0:c57706d25cf0 849 {
rs27 0:c57706d25cf0 850 // function only will turn back to non-strong pullup
rs27 0:c57706d25cf0 851 if (new_level != MODE_STANDARD)
rs27 0:c57706d25cf0 852 {
rs27 0:c57706d25cf0 853 return MODE_STRONG;
rs27 0:c57706d25cf0 854 }
rs27 0:c57706d25cf0 855
rs27 0:c57706d25cf0 856 // clear the strong pullup bit in the global config state
rs27 0:c57706d25cf0 857 cSPU = FALSE;
rs27 0:c57706d25cf0 858
rs27 0:c57706d25cf0 859 // write the new config
rs27 0:c57706d25cf0 860 write_config(c1WS | cSPU | cPPM | cAPU);
rs27 0:c57706d25cf0 861
rs27 0:c57706d25cf0 862
rs27 0:c57706d25cf0 863 return MODE_STANDARD;
rs27 0:c57706d25cf0 864 }
rs27 0:c57706d25cf0 865
rs27 0:c57706d25cf0 866 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 867 // Send 1 bit of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 868 // response matches the 'applyPowerResponse' bit and apply power delivery
rs27 0:c57706d25cf0 869 // to the 1-Wire net. Note that some implementations may apply the power
rs27 0:c57706d25cf0 870 // first and then turn it off if the response is incorrect.
rs27 0:c57706d25cf0 871 //
rs27 0:c57706d25cf0 872 // 'applyPowerResponse' - 1 bit response to check, if correct then start
rs27 0:c57706d25cf0 873 // power delivery
rs27 0:c57706d25cf0 874 //
rs27 0:c57706d25cf0 875 // Returns: TRUE: bit written and response correct, strong pullup now on
rs27 0:c57706d25cf0 876 // FALSE: response incorrect
rs27 0:c57706d25cf0 877 //
rs27 0:c57706d25cf0 878 uint8_t DS2482::OWReadBitPower(uint8_t applyPowerResponse)
rs27 0:c57706d25cf0 879 {
rs27 0:c57706d25cf0 880 uint8_t rdbit;
rs27 0:c57706d25cf0 881
rs27 0:c57706d25cf0 882 // set strong pullup enable
rs27 0:c57706d25cf0 883 cSPU = DS2482_CFG_SPU;
rs27 0:c57706d25cf0 884
rs27 0:c57706d25cf0 885 // write the new config
rs27 0:c57706d25cf0 886 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 887 {
rs27 0:c57706d25cf0 888 return FALSE;
rs27 0:c57706d25cf0 889 }
rs27 0:c57706d25cf0 890
rs27 0:c57706d25cf0 891 // perform read bit
rs27 0:c57706d25cf0 892 rdbit = OWReadBit();
rs27 0:c57706d25cf0 893
rs27 0:c57706d25cf0 894 // check if response was correct, if not then turn off strong pullup
rs27 0:c57706d25cf0 895 if (rdbit != applyPowerResponse)
rs27 0:c57706d25cf0 896 {
rs27 0:c57706d25cf0 897 OWLevel(MODE_STANDARD);
rs27 0:c57706d25cf0 898 return FALSE;
rs27 0:c57706d25cf0 899 }
rs27 0:c57706d25cf0 900
rs27 0:c57706d25cf0 901 return TRUE;
rs27 0:c57706d25cf0 902 }
rs27 0:c57706d25cf0 903
rs27 0:c57706d25cf0 904 //--------------------------------------------------------------------------
rs27 0:c57706d25cf0 905 // Send 8 bits of communication to the 1-Wire Net and verify that the
rs27 0:c57706d25cf0 906 // 8 bits read from the 1-Wire Net are the same (write operation).
rs27 0:c57706d25cf0 907 // The parameter 'sendbyte' least significant 8 bits are used. After the
rs27 0:c57706d25cf0 908 // 8 bits are sent change the level of the 1-Wire net.
rs27 0:c57706d25cf0 909 //
rs27 0:c57706d25cf0 910 // 'sendbyte' - 8 bits to send (least significant bit)
rs27 0:c57706d25cf0 911 //
rs27 0:c57706d25cf0 912 // Returns: TRUE: bytes written and echo was the same, strong pullup now on
rs27 0:c57706d25cf0 913 // FALSE: echo was not the same
rs27 0:c57706d25cf0 914 //
rs27 0:c57706d25cf0 915 uint8_t DS2482::OWWriteBytePower(uint8_t sendbyte)
rs27 0:c57706d25cf0 916 {
rs27 0:c57706d25cf0 917 // set strong pullup enable
rs27 0:c57706d25cf0 918 cSPU = DS2482_CFG_SPU;
rs27 0:c57706d25cf0 919
rs27 0:c57706d25cf0 920 // write the new config
rs27 0:c57706d25cf0 921 if (!write_config(c1WS | cSPU | cPPM | cAPU))
rs27 0:c57706d25cf0 922 {
rs27 0:c57706d25cf0 923 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 924 pc.printf("\r\nSPU off");
rs27 0:c57706d25cf0 925 #endif
rs27 0:c57706d25cf0 926
rs27 0:c57706d25cf0 927 return FALSE;
rs27 0:c57706d25cf0 928 }
rs27 0:c57706d25cf0 929
rs27 0:c57706d25cf0 930 // perform write byte
rs27 0:c57706d25cf0 931 OWWriteByte(sendbyte);
rs27 0:c57706d25cf0 932
rs27 0:c57706d25cf0 933 return TRUE;
rs27 0:c57706d25cf0 934 }
rs27 0:c57706d25cf0 935
rs27 0:c57706d25cf0 936
rs27 0:c57706d25cf0 937 // end I2C DS2482
rs27 0:c57706d25cf0 938 //======================================================================
rs27 0:c57706d25cf0 939
rs27 1:0950824b1ca3 940 //---------------------------------------------------------------------------
rs27 1:0950824b1ca3 941 //-------------------------DS18XX OPERATIONS---------------------------------
rs27 1:0950824b1ca3 942 //---------------------------------------------------------------------------
rs27 1:0950824b1ca3 943 // find ALL devices on bus 1
rs27 1:0950824b1ca3 944 //
rs27 1:0950824b1ca3 945 void DS2482::DS18XX_Read_Address(void)
rs27 1:0950824b1ca3 946 {
rs27 1:0950824b1ca3 947 uint8_t status, i, j;
rs27 1:0950824b1ca3 948
rs27 1:0950824b1ca3 949 //--------------------------------------------------------------
rs27 1:0950824b1ca3 950 // Device Tabele für bus 1 und bus 2 löschen
rs27 1:0950824b1ca3 951
rs27 1:0950824b1ca3 952 for (i = 0; i < OW_MAX_DEVICES; i++)
rs27 1:0950824b1ca3 953 {
rs27 1:0950824b1ca3 954 for (j = 0; j < 8; j++)
rs27 1:0950824b1ca3 955 {
rs27 1:0950824b1ca3 956 ow.device_table[i].rom[j] = 0;
rs27 1:0950824b1ca3 957 }
rs27 1:0950824b1ca3 958 }
rs27 1:0950824b1ca3 959
rs27 1:0950824b1ca3 960 // pc.printf("\n --- DS18xx_Read_Address --- ");
rs27 1:0950824b1ca3 961 // wait(0.1);
rs27 1:0950824b1ca3 962
rs27 1:0950824b1ca3 963 ow.devices = 0;
rs27 1:0950824b1ca3 964
rs27 1:0950824b1ca3 965 //--------------------------------------------------------------
rs27 1:0950824b1ca3 966 // die Bausteine am Bus suchen
rs27 2:e9048135901b 967
rs27 1:0950824b1ca3 968 //pc.printf("\n -- detect -- \n");
rs27 1:0950824b1ca3 969 //wait(0.5);
rs27 1:0950824b1ca3 970 detect(); // reset and init the 1-wire master
rs27 1:0950824b1ca3 971
rs27 1:0950824b1ca3 972 //pc.printf("\n -- OWFirst --\n");
rs27 1:0950824b1ca3 973 //wait(0.1);
rs27 1:0950824b1ca3 974 status = OWFirst();
rs27 1:0950824b1ca3 975
rs27 1:0950824b1ca3 976 if(status == 0)
rs27 1:0950824b1ca3 977 {
rs27 1:0950824b1ca3 978 #if OW_MASTER_START
rs27 1:0950824b1ca3 979 pc.printf("\r\nno 1-Wire slaves found");
rs27 1:0950824b1ca3 980 #endif
rs27 1:0950824b1ca3 981 }
rs27 1:0950824b1ca3 982
rs27 1:0950824b1ca3 983 while(status)
rs27 1:0950824b1ca3 984 {
rs27 1:0950824b1ca3 985 // TODO hier ins eeprom schreiben
rs27 1:0950824b1ca3 986 #if OW_MASTER_START
rs27 1:0950824b1ca3 987 pc.printf("\n*** #%02d ROM_NO= ",ow.devices);
rs27 1:0950824b1ca3 988 wait(0.1);
rs27 1:0950824b1ca3 989 #endif
rs27 1:0950824b1ca3 990
rs27 1:0950824b1ca3 991 for (uint8_t i = 0; i < 8; i++)
rs27 1:0950824b1ca3 992 {
rs27 1:0950824b1ca3 993 ow.device_table[ow.devices].rom[i] = ROM_NO[i];
rs27 1:0950824b1ca3 994
rs27 1:0950824b1ca3 995 #if OW_MASTER_START
rs27 1:0950824b1ca3 996 pc.printf(" 0x%02x",ROM_NO[i]);
rs27 1:0950824b1ca3 997 wait (0.1);
rs27 1:0950824b1ca3 998 #endif
rs27 1:0950824b1ca3 999 }
rs27 1:0950824b1ca3 1000 ow.device_table[ow.devices].status = 0x01; // Wandler gefunden
rs27 1:0950824b1ca3 1001
rs27 1:0950824b1ca3 1002 status = OWNext();
rs27 1:0950824b1ca3 1003 ow.devices++; // Zeiger auf den nächsten freien Baustein
rs27 1:0950824b1ca3 1004
rs27 1:0950824b1ca3 1005 // maximal 16 Bausteine
rs27 1:0950824b1ca3 1006 if (ow.devices >= OW_MAX_DEVICES)
rs27 1:0950824b1ca3 1007 {
rs27 1:0950824b1ca3 1008 ow.devices--; // zeiget auf den letzten gültigen Baustein
rs27 1:0950824b1ca3 1009 return;
rs27 1:0950824b1ca3 1010 }
rs27 1:0950824b1ca3 1011 }
rs27 1:0950824b1ca3 1012
rs27 1:0950824b1ca3 1013 #if OW_MASTER_START
rs27 1:0950824b1ca3 1014 pc.printf("\r\n");
rs27 1:0950824b1ca3 1015 #endif
rs27 1:0950824b1ca3 1016
rs27 1:0950824b1ca3 1017 return;
rs27 1:0950824b1ca3 1018 }
rs27 1:0950824b1ca3 1019
rs27 1:0950824b1ca3 1020 //---------------------------------------------------------------------------
rs27 1:0950824b1ca3 1021 //
rs27 1:0950824b1ca3 1022 void DS2482::start_conversion(void)
rs27 1:0950824b1ca3 1023 {
rs27 1:0950824b1ca3 1024 //--------------------------------------------------------------
rs27 1:0950824b1ca3 1025 // alle Bausteine an bus 0 starten
rs27 1:0950824b1ca3 1026
rs27 1:0950824b1ca3 1027 // find a DS18S20
rs27 1:0950824b1ca3 1028 OWFirst();
rs27 1:0950824b1ca3 1029 OWReset();
rs27 1:0950824b1ca3 1030
rs27 1:0950824b1ca3 1031 // address all devices
rs27 1:0950824b1ca3 1032 OWWriteByte(OW_SKIP_ROM);
rs27 1:0950824b1ca3 1033
rs27 1:0950824b1ca3 1034 // Start Conversion and Config Strong Pull-Up
rs27 1:0950824b1ca3 1035 if (!OWWriteBytePower(OW_CONVERT_TEMP))
rs27 1:0950824b1ca3 1036 {
rs27 1:0950824b1ca3 1037 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 1038 pc.printf("Fail convert command\r\n");
rs27 1:0950824b1ca3 1039 #endif
rs27 1:0950824b1ca3 1040 }
rs27 1:0950824b1ca3 1041
rs27 1:0950824b1ca3 1042 //--------------------------------------------------------------
rs27 1:0950824b1ca3 1043 // alle Bausteine an bus 1 starten
rs27 1:0950824b1ca3 1044
rs27 1:0950824b1ca3 1045 // find a DS18S20
rs27 1:0950824b1ca3 1046 OWFirst();
rs27 1:0950824b1ca3 1047 OWReset();
rs27 1:0950824b1ca3 1048
rs27 1:0950824b1ca3 1049 // address all devices
rs27 1:0950824b1ca3 1050 OWWriteByte(OW_SKIP_ROM);
rs27 1:0950824b1ca3 1051
rs27 1:0950824b1ca3 1052 // Start Conversion and Config Strong Pull-Up
rs27 1:0950824b1ca3 1053 if (!OWWriteBytePower(OW_CONVERT_TEMP))
rs27 1:0950824b1ca3 1054 {
rs27 1:0950824b1ca3 1055 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 1056 pc.printf("Fail convert command\r\n");
rs27 1:0950824b1ca3 1057 #endif
rs27 1:0950824b1ca3 1058 }
rs27 1:0950824b1ca3 1059
rs27 1:0950824b1ca3 1060 }
rs27 1:0950824b1ca3 1061
rs27 1:0950824b1ca3 1062 //-----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1063
rs27 1:0950824b1ca3 1064 bool DS2482::ow_read_rom(void)
rs27 1:0950824b1ca3 1065 {
rs27 1:0950824b1ca3 1066 uint8_t n;
rs27 1:0950824b1ca3 1067
rs27 1:0950824b1ca3 1068 // Write Read ROM Code command
rs27 1:0950824b1ca3 1069 OWWriteByte(OW_CMD_READ_ROM);
rs27 1:0950824b1ca3 1070
rs27 1:0950824b1ca3 1071 // Read 8 bytes of ROM code
rs27 1:0950824b1ca3 1072 for (n = 0; n < 8; n++)
rs27 1:0950824b1ca3 1073 {
rs27 1:0950824b1ca3 1074 ow_rom_code[n] = OWReadByte();
rs27 1:0950824b1ca3 1075 }
rs27 1:0950824b1ca3 1076
rs27 1:0950824b1ca3 1077 // Do Cyclic Redundancy Check
rs27 1:0950824b1ca3 1078 if (crc_calc_buffer(&ow_rom_code[0],7) != ow_rom_code[7])
rs27 1:0950824b1ca3 1079 {
rs27 1:0950824b1ca3 1080 ow_flags |= OW_CRC_ERROR;
rs27 1:0950824b1ca3 1081
rs27 1:0950824b1ca3 1082 return false;
rs27 1:0950824b1ca3 1083 }
rs27 1:0950824b1ca3 1084
rs27 1:0950824b1ca3 1085 return true;
rs27 1:0950824b1ca3 1086 }
rs27 1:0950824b1ca3 1087
rs27 1:0950824b1ca3 1088 //-----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1089
rs27 1:0950824b1ca3 1090 bool DS2482::ow_read_scratchpad(void)
rs27 1:0950824b1ca3 1091 {
rs27 1:0950824b1ca3 1092 uint8_t i, crc;
rs27 1:0950824b1ca3 1093
rs27 1:0950824b1ca3 1094 ow_flags = 0;
rs27 1:0950824b1ca3 1095
rs27 1:0950824b1ca3 1096 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1097
rs27 1:0950824b1ca3 1098 // select the device
rs27 1:0950824b1ca3 1099 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1100 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1101
rs27 1:0950824b1ca3 1102 for (i = 0; i < 8; i++)
rs27 1:0950824b1ca3 1103 {
rs27 1:0950824b1ca3 1104 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 1:0950824b1ca3 1105 }
rs27 1:0950824b1ca3 1106
rs27 1:0950824b1ca3 1107 OWWriteByte(OW_CMD_READ_SCRATCHPAD); // Read Scratch pad 0xBE
rs27 1:0950824b1ca3 1108
rs27 1:0950824b1ca3 1109 // Read 9 bytes of scratchpad memory
rs27 1:0950824b1ca3 1110 for (i = 0; i < OW_SCRATCHPAD_BYTES; i++)
rs27 1:0950824b1ca3 1111 {
rs27 1:0950824b1ca3 1112 crc = OWReadByte();
rs27 1:0950824b1ca3 1113 ow_scratchpad[i] = crc;
rs27 1:0950824b1ca3 1114 }
rs27 1:0950824b1ca3 1115
rs27 1:0950824b1ca3 1116 #if OW_MASTER_DEBUG
rs27 1:0950824b1ca3 1117
rs27 1:0950824b1ca3 1118 pc.printf("\n*** Scratch_Val ");
rs27 1:0950824b1ca3 1119
rs27 1:0950824b1ca3 1120 for (i = 1; i < OW_SCRATCHPAD_BYTES; i++)
rs27 1:0950824b1ca3 1121 {
rs27 1:0950824b1ca3 1122 pc.printf(":0x%02x",ow_scratchpad[i]);
rs27 1:0950824b1ca3 1123 }
rs27 1:0950824b1ca3 1124
rs27 1:0950824b1ca3 1125 pc.printf("\r\n");
rs27 1:0950824b1ca3 1126
rs27 1:0950824b1ca3 1127 #endif
rs27 1:0950824b1ca3 1128
rs27 1:0950824b1ca3 1129
rs27 1:0950824b1ca3 1130 return true;
rs27 1:0950824b1ca3 1131 }
rs27 1:0950824b1ca3 1132
rs27 1:0950824b1ca3 1133 //-----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1134
rs27 1:0950824b1ca3 1135 bool DS2482::ow_read_scratchpad_ds2438(uint8_t page)
rs27 1:0950824b1ca3 1136 {
rs27 1:0950824b1ca3 1137 uint8_t n, crc;
rs27 1:0950824b1ca3 1138
rs27 1:0950824b1ca3 1139 ow_flags = 0;
rs27 1:0950824b1ca3 1140
rs27 1:0950824b1ca3 1141 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1142
rs27 1:0950824b1ca3 1143 // select the device
rs27 1:0950824b1ca3 1144 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1145 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1146
rs27 1:0950824b1ca3 1147 for (n = 0; n < 8; n++)
rs27 1:0950824b1ca3 1148 {
rs27 1:0950824b1ca3 1149 OWWriteByte(ow.device_table[ow.device_table_index].rom[n]);
rs27 1:0950824b1ca3 1150 }
rs27 1:0950824b1ca3 1151
rs27 1:0950824b1ca3 1152 // Write command to read scratchpad memory
rs27 1:0950824b1ca3 1153 OWWriteByte(0xbe); // 0xBE
rs27 1:0950824b1ca3 1154 OWWriteByte(page); // 0x03
rs27 1:0950824b1ca3 1155
rs27 1:0950824b1ca3 1156 // Read 9 bytes of scratchpad memory
rs27 1:0950824b1ca3 1157 for (n = 0; n < OW_SCRATCHPAD_BYTES; n++)
rs27 1:0950824b1ca3 1158 {
rs27 1:0950824b1ca3 1159 crc = OWReadByte();
rs27 1:0950824b1ca3 1160 ow_scratchpad[n] = crc;
rs27 1:0950824b1ca3 1161 // printf_P(PSTR("%02x "),crc);
rs27 1:0950824b1ca3 1162 }
rs27 1:0950824b1ca3 1163
rs27 1:0950824b1ca3 1164 crc = crc_calc_buffer(ow_scratchpad,8);
rs27 1:0950824b1ca3 1165
rs27 1:0950824b1ca3 1166 // Calculate CRC
rs27 1:0950824b1ca3 1167 if (crc != ow_scratchpad[8])
rs27 1:0950824b1ca3 1168 {
rs27 1:0950824b1ca3 1169 ow_flags |= OW_CRC_ERROR;
rs27 1:0950824b1ca3 1170 // Read 9 bytes of scratchpad memory
rs27 1:0950824b1ca3 1171 pc.printf("\now_read_scratchpad: ");
rs27 1:0950824b1ca3 1172 for (n = 0; n < OW_SCRATCHPAD_BYTES; n++)
rs27 1:0950824b1ca3 1173 {
rs27 1:0950824b1ca3 1174 pc.printf("%02x ",ow_scratchpad[n]);
rs27 1:0950824b1ca3 1175 }
rs27 1:0950824b1ca3 1176 pc.printf(": CRC error %02x",crc);
rs27 1:0950824b1ca3 1177 return false;
rs27 1:0950824b1ca3 1178 }
rs27 1:0950824b1ca3 1179 return true;
rs27 1:0950824b1ca3 1180 }
rs27 1:0950824b1ca3 1181
rs27 1:0950824b1ca3 1182 //----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1183
rs27 1:0950824b1ca3 1184 bool DS2482::ow_write_scratchpad_ds18x20 (uint8_t th, uint8_t tl)
rs27 1:0950824b1ca3 1185 {
rs27 1:0950824b1ca3 1186 uint8_t i;
rs27 1:0950824b1ca3 1187
rs27 1:0950824b1ca3 1188 // Do bus reset
rs27 1:0950824b1ca3 1189 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1190
rs27 1:0950824b1ca3 1191 // select the device
rs27 1:0950824b1ca3 1192 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1193 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1194
rs27 1:0950824b1ca3 1195 for (i = 0; i < 8; i++)
rs27 1:0950824b1ca3 1196 {
rs27 1:0950824b1ca3 1197 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 1:0950824b1ca3 1198 }
rs27 1:0950824b1ca3 1199
rs27 1:0950824b1ca3 1200
rs27 1:0950824b1ca3 1201 // Write command to read scratchpad memory
rs27 1:0950824b1ca3 1202 OWWriteByte(OW_CMD_WRITE_SCRATCHPAD);
rs27 1:0950824b1ca3 1203
rs27 1:0950824b1ca3 1204 // Write 1 byte at scratchpad memory
rs27 1:0950824b1ca3 1205 OWWriteByte(th); // Th
rs27 1:0950824b1ca3 1206 wait_us(8); // ist für das Timing der 8 MHZ CPU notwendig
rs27 1:0950824b1ca3 1207
rs27 1:0950824b1ca3 1208 // Write 1 byte at scratchpad memory
rs27 1:0950824b1ca3 1209 OWWriteByte(tl); // Th
rs27 1:0950824b1ca3 1210 wait_us(8); // ist für das Timing der 8 MHZ CPU notwendig
rs27 1:0950824b1ca3 1211
rs27 1:0950824b1ca3 1212 // Write 1 byte at scratchpad memory
rs27 1:0950824b1ca3 1213 OWWriteByte(0x7F); // 12 Bit Auflösung
rs27 1:0950824b1ca3 1214 wait_us(8); // ist für das Timing der 8 MHZ CPU notwendig
rs27 1:0950824b1ca3 1215
rs27 1:0950824b1ca3 1216 return true;
rs27 1:0950824b1ca3 1217 }
rs27 1:0950824b1ca3 1218
rs27 1:0950824b1ca3 1219 //----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1220 bool DS2482::ow_write_scratchpad_ds2438 (uint8_t th, uint8_t tl)
rs27 1:0950824b1ca3 1221 {
rs27 1:0950824b1ca3 1222
rs27 1:0950824b1ca3 1223 uint8_t i;
rs27 1:0950824b1ca3 1224
rs27 1:0950824b1ca3 1225 // Do bus reset
rs27 1:0950824b1ca3 1226 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1227
rs27 1:0950824b1ca3 1228 // select the device
rs27 1:0950824b1ca3 1229 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1230 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1231
rs27 1:0950824b1ca3 1232 for (i = 0; i < 8; i++)
rs27 1:0950824b1ca3 1233 {
rs27 1:0950824b1ca3 1234 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 1:0950824b1ca3 1235 }
rs27 1:0950824b1ca3 1236
rs27 1:0950824b1ca3 1237 // Write command to read scratchpad memory
rs27 1:0950824b1ca3 1238 OWWriteByte(0x4E);
rs27 1:0950824b1ca3 1239 OWWriteByte(0x03);
rs27 1:0950824b1ca3 1240
rs27 1:0950824b1ca3 1241 // Write 3 bytes to scratchpad memory
rs27 1:0950824b1ca3 1242 OWWriteByte(th); // Th
rs27 1:0950824b1ca3 1243 wait_us(8);
rs27 1:0950824b1ca3 1244
rs27 1:0950824b1ca3 1245 OWWriteByte(tl); // Tl
rs27 1:0950824b1ca3 1246 wait_us(8);
rs27 1:0950824b1ca3 1247
rs27 1:0950824b1ca3 1248 return true;
rs27 1:0950824b1ca3 1249 }
rs27 1:0950824b1ca3 1250
rs27 1:0950824b1ca3 1251 //----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1252 bool DS2482::ow_write_eeprom_ds18x20 (void)
rs27 1:0950824b1ca3 1253 {
rs27 1:0950824b1ca3 1254
rs27 2:e9048135901b 1255 uint8_t i;
rs27 1:0950824b1ca3 1256
rs27 1:0950824b1ca3 1257 // Do bus reset
rs27 1:0950824b1ca3 1258 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1259
rs27 1:0950824b1ca3 1260 // select the device
rs27 1:0950824b1ca3 1261 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1262 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1263
rs27 1:0950824b1ca3 1264 for (i = 0; i < 8; i++)
rs27 1:0950824b1ca3 1265 {
rs27 1:0950824b1ca3 1266 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 1:0950824b1ca3 1267 }
rs27 1:0950824b1ca3 1268
rs27 1:0950824b1ca3 1269 // Write command to write eeprom
rs27 1:0950824b1ca3 1270 OWWriteByte(0x48);
rs27 1:0950824b1ca3 1271
rs27 1:0950824b1ca3 1272 return true;
rs27 1:0950824b1ca3 1273
rs27 1:0950824b1ca3 1274 }
rs27 1:0950824b1ca3 1275
rs27 1:0950824b1ca3 1276 //----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1277 bool DS2482::ow_write_eeprom_ds2438 (void)
rs27 1:0950824b1ca3 1278 {
rs27 1:0950824b1ca3 1279 uint8_t i;
rs27 1:0950824b1ca3 1280
rs27 1:0950824b1ca3 1281 // Do bus reset
rs27 1:0950824b1ca3 1282 if (!OWReset()) return false;
rs27 1:0950824b1ca3 1283
rs27 1:0950824b1ca3 1284 // select the device
rs27 1:0950824b1ca3 1285 // den Baustein wird über den ROM Code adressiert
rs27 1:0950824b1ca3 1286 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 1:0950824b1ca3 1287
rs27 1:0950824b1ca3 1288 for (i = 0; i < 8; i++)
rs27 1:0950824b1ca3 1289 {
rs27 1:0950824b1ca3 1290 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 1:0950824b1ca3 1291 }
rs27 1:0950824b1ca3 1292
rs27 1:0950824b1ca3 1293 // Write command to read scratchpad memory
rs27 1:0950824b1ca3 1294 OWWriteByte(0x48);
rs27 1:0950824b1ca3 1295 OWWriteByte(0x03);
rs27 1:0950824b1ca3 1296
rs27 1:0950824b1ca3 1297 return true;
rs27 1:0950824b1ca3 1298 }
rs27 1:0950824b1ca3 1299
rs27 1:0950824b1ca3 1300
rs27 1:0950824b1ca3 1301 //----------------------------------------------------------------------------
rs27 1:0950824b1ca3 1302
rs27 1:0950824b1ca3 1303 void DS2482::ow_read_address (void)
rs27 1:0950824b1ca3 1304 {
rs27 1:0950824b1ca3 1305 uint8_t n;
rs27 1:0950824b1ca3 1306
rs27 1:0950824b1ca3 1307 // ow Adressen aus den Bausteinen lesen
rs27 1:0950824b1ca3 1308 for (n = 0; n < OW_MAX_DEVICES; n++)
rs27 1:0950824b1ca3 1309 {
rs27 1:0950824b1ca3 1310 if ((ow.device_table[n].status & 0x0f) == 1)
rs27 1:0950824b1ca3 1311 {
rs27 1:0950824b1ca3 1312 // printf_P(PSTR("\n device table %d"),n);
rs27 1:0950824b1ca3 1313 ow.device_table_index = n;
rs27 1:0950824b1ca3 1314
rs27 1:0950824b1ca3 1315 if ((ow.device_table[n].rom[0] == 0x10) || (ow.device_table[n].rom[0] == 0x28))
rs27 1:0950824b1ca3 1316 {
rs27 1:0950824b1ca3 1317 if (ow_read_scratchpad())
rs27 1:0950824b1ca3 1318 {
rs27 1:0950824b1ca3 1319 if(ow_scratchpad[2] == ow_scratchpad[3]) ow.device_table[n].adr = ow_scratchpad[3];
rs27 1:0950824b1ca3 1320 }
rs27 1:0950824b1ca3 1321 }
rs27 1:0950824b1ca3 1322
rs27 1:0950824b1ca3 1323 if ((ow.device_table[n].rom[0] == 0xa6) || (ow.device_table[n].rom[0] == 0x26))
rs27 1:0950824b1ca3 1324 {
rs27 1:0950824b1ca3 1325 if (ow_read_scratchpad_ds2438(0x03))
rs27 1:0950824b1ca3 1326 {
rs27 1:0950824b1ca3 1327 if(ow_scratchpad[0] == ow_scratchpad[1]) ow.device_table[n].adr = ow_scratchpad[0];
rs27 1:0950824b1ca3 1328
rs27 1:0950824b1ca3 1329 }
rs27 1:0950824b1ca3 1330 }
rs27 1:0950824b1ca3 1331
rs27 1:0950824b1ca3 1332 }
rs27 1:0950824b1ca3 1333
rs27 1:0950824b1ca3 1334 } // end for(...
rs27 1:0950824b1ca3 1335 }
rs27 1:0950824b1ca3 1336
rs27 2:e9048135901b 1337 //-----------------------------------------------------------------------------
rs27 2:e9048135901b 1338
rs27 2:e9048135901b 1339 bool DS2482::ds1820_start_conversion(uint8_t command)
rs27 2:e9048135901b 1340 {
rs27 2:e9048135901b 1341 uint8_t i;
rs27 2:e9048135901b 1342
rs27 2:e9048135901b 1343 // Do bus reset
rs27 2:e9048135901b 1344 if (!OWReset()) return false;
rs27 2:e9048135901b 1345
rs27 2:e9048135901b 1346 // Check if a match ROM code must be done or just skip ROM code (0xFF)
rs27 2:e9048135901b 1347 if (command > 0xFE)
rs27 2:e9048135901b 1348 {
rs27 2:e9048135901b 1349 OWWriteByte(OW_CMD_SKIP_ROM); // Send Skip ROM Code command
rs27 2:e9048135901b 1350 }
rs27 2:e9048135901b 1351 else
rs27 2:e9048135901b 1352 {
rs27 2:e9048135901b 1353 // select the device
rs27 2:e9048135901b 1354 // den Baustein wird über den ROM Code adressiert
rs27 2:e9048135901b 1355 OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
rs27 2:e9048135901b 1356
rs27 2:e9048135901b 1357 for (i = 0; i < 8; i++)
rs27 2:e9048135901b 1358 {
rs27 2:e9048135901b 1359 OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
rs27 2:e9048135901b 1360 }
rs27 2:e9048135901b 1361 }
rs27 2:e9048135901b 1362
rs27 2:e9048135901b 1363 // Send the "Convert T" command to start conversion
rs27 2:e9048135901b 1364 OWWriteByte(OW_CMD_CONVERT_T);
rs27 2:e9048135901b 1365
rs27 2:e9048135901b 1366 // Set flag to mark a measurement in progress
rs27 3:e773b0d83471 1367 ds1820_request_pending = true;
rs27 2:e9048135901b 1368
rs27 2:e9048135901b 1369 return TRUE;
rs27 2:e9048135901b 1370 }
rs27 2:e9048135901b 1371
rs27 2:e9048135901b 1372
rs27 2:e9048135901b 1373 //-----------------------------------------------------------------------------
rs27 2:e9048135901b 1374
rs27 2:e9048135901b 1375 bool DS2482::ds18B20_read_hrtemp(void)
rs27 2:e9048135901b 1376 {
rs27 2:e9048135901b 1377
rs27 2:e9048135901b 1378 union ds18B20_temp_union
rs27 2:e9048135901b 1379 {
rs27 2:e9048135901b 1380 int16_t word;
rs27 2:e9048135901b 1381 uint8_t byte[2];
rs27 2:e9048135901b 1382 } ds18B20_temp;
rs27 2:e9048135901b 1383
rs27 2:e9048135901b 1384 // uint8_t temp_lo;
rs27 2:e9048135901b 1385 // uint8_t temp_hi;
rs27 2:e9048135901b 1386 // int8_t digit;
rs27 2:e9048135901b 1387 float ds1820_float_result;
rs27 2:e9048135901b 1388
rs27 2:e9048135901b 1389 // Preset float result to zero in case of function termination (return false)
rs27 2:e9048135901b 1390 //ds1820_float_result = 0.0;
rs27 2:e9048135901b 1391 ow.device_table[ow.device_table_index].result = 0;
rs27 2:e9048135901b 1392
rs27 2:e9048135901b 1393 // Return false if reading scratchpad memory fails
rs27 2:e9048135901b 1394 if (!ow_read_scratchpad())
rs27 2:e9048135901b 1395 {
rs27 2:e9048135901b 1396 ow.device_table[ow.device_table_index].status &= 0xf0;
rs27 2:e9048135901b 1397 ow.device_table[ow.device_table_index].status |= 4;
rs27 2:e9048135901b 1398 return FALSE;
rs27 2:e9048135901b 1399 }
rs27 2:e9048135901b 1400
rs27 2:e9048135901b 1401 ds18B20_temp.byte[0] = ow_scratchpad[DS1820_LSB];
rs27 2:e9048135901b 1402 ds18B20_temp.byte[1] = ow_scratchpad[DS1820_MSB];
rs27 2:e9048135901b 1403
rs27 2:e9048135901b 1404 //ds18B20_temp.word <<= 4; // Vorzeichenbit auf MSB Bit schieben
rs27 2:e9048135901b 1405 //ds18B20_temp.word /= 16; // die letzten 4 Stellen wieder löschen
rs27 2:e9048135901b 1406
rs27 2:e9048135901b 1407 ds1820_float_result = ds18B20_temp.word * 0.0625;
rs27 2:e9048135901b 1408
rs27 5:d94037eb31ed 1409 //pc.printf("\nDS18B20 T-Kanal: %d = %2.2f ",ow.device_table_index,ds1820_float_result);
rs27 2:e9048135901b 1410
rs27 5:d94037eb31ed 1411 ow.device_table[ow.device_table_index].result = (int16_t)(ds1820_float_result * 100);
rs27 2:e9048135901b 1412
rs27 2:e9048135901b 1413 // Clear flag to mark that no measurement is in progress
rs27 4:c29e67d55f86 1414 ds1820_request_pending = false;
rs27 2:e9048135901b 1415
rs27 2:e9048135901b 1416 // Flag für erfolgreiches Lesen setzen
rs27 2:e9048135901b 1417 ow.device_table[ow.device_table_index].status &= 0xf0;
rs27 2:e9048135901b 1418 ow.device_table[ow.device_table_index].status |= 3;
rs27 2:e9048135901b 1419
rs27 3:e773b0d83471 1420 return true;
rs27 2:e9048135901b 1421 }
rs27 2:e9048135901b 1422
rs27 2:e9048135901b 1423
rs27 2:e9048135901b 1424 //-----------------------------------------------------------------------------
rs27 2:e9048135901b 1425