one wire driver

Dependents:   09_PT1000 10_PT1000 11_PT1000

Committer:
rs27
Date:
Sat May 04 12:24:55 2013 +0000
Revision:
3:e773b0d83471
Parent:
2:e9048135901b
Child:
4:c29e67d55f86
test;

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