1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

Committer:
j3
Date:
Wed Aug 03 22:12:13 2016 +0000
Revision:
106:d121acb88a34
Parent:
104:3f48daed532b
Child:
121:4bee07064d0d
fixed references to DS18B20 resolution in DS18B20.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 101:e7f76cb49584 1 /******************************************************************//**
j3 101:e7f76cb49584 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 101:e7f76cb49584 3 *
j3 101:e7f76cb49584 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 101:e7f76cb49584 5 * copy of this software and associated documentation files (the "Software"),
j3 101:e7f76cb49584 6 * to deal in the Software without restriction, including without limitation
j3 101:e7f76cb49584 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 101:e7f76cb49584 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 101:e7f76cb49584 9 * Software is furnished to do so, subject to the following conditions:
j3 101:e7f76cb49584 10 *
j3 101:e7f76cb49584 11 * The above copyright notice and this permission notice shall be included
j3 101:e7f76cb49584 12 * in all copies or substantial portions of the Software.
j3 101:e7f76cb49584 13 *
j3 101:e7f76cb49584 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 101:e7f76cb49584 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 101:e7f76cb49584 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 101:e7f76cb49584 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 101:e7f76cb49584 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 101:e7f76cb49584 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 101:e7f76cb49584 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 101:e7f76cb49584 21 *
j3 101:e7f76cb49584 22 * Except as contained in this notice, the name of Maxim Integrated
j3 101:e7f76cb49584 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 101:e7f76cb49584 24 * Products, Inc. Branding Policy.
j3 101:e7f76cb49584 25 *
j3 101:e7f76cb49584 26 * The mere transfer of this software does not imply any licenses
j3 101:e7f76cb49584 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 101:e7f76cb49584 28 * trademarks, maskwork rights, or any other form of intellectual
j3 101:e7f76cb49584 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 101:e7f76cb49584 30 * ownership rights.
j3 101:e7f76cb49584 31 **********************************************************************/
j3 101:e7f76cb49584 32
j3 101:e7f76cb49584 33
j3 104:3f48daed532b 34 #include "Slaves/Sensors/DS18B20/DS18B20.h"
j3 101:e7f76cb49584 35 #include "wait_api.h"
j3 101:e7f76cb49584 36
j3 101:e7f76cb49584 37
j3 101:e7f76cb49584 38 using namespace OneWire;
j3 101:e7f76cb49584 39 using namespace OneWire::crc;
j3 101:e7f76cb49584 40
j3 101:e7f76cb49584 41
j3 101:e7f76cb49584 42 enum DS18B20_CMDS
j3 101:e7f76cb49584 43 {
j3 101:e7f76cb49584 44 WRITE_SCRATCHPAD = 0x4E,
j3 101:e7f76cb49584 45 READ_SCRATCHPAD = 0xBE,
j3 101:e7f76cb49584 46 COPY_SCRATCHPAD = 0x48,
j3 101:e7f76cb49584 47 CONV_TEMPERATURE = 0x44,
j3 101:e7f76cb49584 48 READ_POWER_SUPPY = 0xB4,
j3 101:e7f76cb49584 49 RECALL = 0xB8
j3 101:e7f76cb49584 50 };
j3 101:e7f76cb49584 51
j3 101:e7f76cb49584 52
j3 101:e7f76cb49584 53 /**********************************************************************/
j3 101:e7f76cb49584 54 DS18B20::DS18B20(RandomAccessRomIterator &selector):OneWireSlave(selector)
j3 101:e7f76cb49584 55 {
j3 101:e7f76cb49584 56 }
j3 101:e7f76cb49584 57
j3 101:e7f76cb49584 58
j3 101:e7f76cb49584 59 /**********************************************************************/
j3 101:e7f76cb49584 60 OneWireSlave::CmdResult DS18B20::writeScratchPad(uint8_t th, uint8_t tl, Resolution res)
j3 101:e7f76cb49584 61 {
j3 101:e7f76cb49584 62 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 63
j3 101:e7f76cb49584 64 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 65
j3 101:e7f76cb49584 66 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 67 {
j3 101:e7f76cb49584 68 uint8_t sendBlock[] = {WRITE_SCRATCHPAD, th, tl, res};
j3 101:e7f76cb49584 69
j3 101:e7f76cb49584 70 owmResult = master().OWWriteBlock(sendBlock, 4);
j3 101:e7f76cb49584 71 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 72 {
j3 101:e7f76cb49584 73 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 74 }
j3 101:e7f76cb49584 75 else
j3 101:e7f76cb49584 76 {
j3 101:e7f76cb49584 77 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 78 }
j3 101:e7f76cb49584 79 }
j3 101:e7f76cb49584 80
j3 101:e7f76cb49584 81 return deviceResult;
j3 101:e7f76cb49584 82 }
j3 101:e7f76cb49584 83
j3 101:e7f76cb49584 84
j3 101:e7f76cb49584 85 /**********************************************************************/
j3 101:e7f76cb49584 86 OneWireSlave::CmdResult DS18B20::readScratchPad(uint8_t * scratchPadBuff)
j3 101:e7f76cb49584 87 {
j3 101:e7f76cb49584 88 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 89
j3 101:e7f76cb49584 90 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 91
j3 101:e7f76cb49584 92 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 93 {
j3 101:e7f76cb49584 94 owmResult = master().OWWriteByteSetLevel(READ_SCRATCHPAD, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 95 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 96 {
j3 101:e7f76cb49584 97 uint8_t rxBlock[9];
j3 101:e7f76cb49584 98 owmResult = master().OWReadBlock(rxBlock, 9);
j3 101:e7f76cb49584 99
j3 101:e7f76cb49584 100 uint8_t crcCheck = calculateCrc8(rxBlock, 8);
j3 101:e7f76cb49584 101 if ((owmResult == OneWireMaster::Success) && (crcCheck == rxBlock[8]))
j3 101:e7f76cb49584 102 {
j3 101:e7f76cb49584 103 std::memcpy(scratchPadBuff, rxBlock, 8);
j3 101:e7f76cb49584 104 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 105 }
j3 101:e7f76cb49584 106 else
j3 101:e7f76cb49584 107 {
j3 101:e7f76cb49584 108 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 109 }
j3 101:e7f76cb49584 110 }
j3 101:e7f76cb49584 111 else
j3 101:e7f76cb49584 112 {
j3 101:e7f76cb49584 113 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 114 }
j3 101:e7f76cb49584 115 }
j3 101:e7f76cb49584 116
j3 101:e7f76cb49584 117 return deviceResult;
j3 101:e7f76cb49584 118 }
j3 101:e7f76cb49584 119
j3 101:e7f76cb49584 120
j3 101:e7f76cb49584 121 /**********************************************************************/
j3 101:e7f76cb49584 122 OneWireSlave::CmdResult DS18B20::readPowerSupply(bool & localPower)
j3 101:e7f76cb49584 123 {
j3 101:e7f76cb49584 124 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 125
j3 101:e7f76cb49584 126 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 127 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 128 {
j3 101:e7f76cb49584 129 owmResult = master().OWWriteByteSetLevel(READ_POWER_SUPPY, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 130 if(owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 131 {
j3 101:e7f76cb49584 132 uint8_t rtnBit = 0;
j3 101:e7f76cb49584 133
j3 101:e7f76cb49584 134 owmResult = master().OWTouchBitSetLevel(rtnBit, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 135 if(owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 136 {
j3 101:e7f76cb49584 137 localPower = (rtnBit & 0x01);
j3 101:e7f76cb49584 138 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 139 }
j3 101:e7f76cb49584 140 else
j3 101:e7f76cb49584 141 {
j3 101:e7f76cb49584 142 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 143 }
j3 101:e7f76cb49584 144 }
j3 101:e7f76cb49584 145 else
j3 101:e7f76cb49584 146 {
j3 101:e7f76cb49584 147 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 148 }
j3 101:e7f76cb49584 149 }
j3 101:e7f76cb49584 150
j3 101:e7f76cb49584 151 return deviceResult;
j3 101:e7f76cb49584 152 }
j3 101:e7f76cb49584 153
j3 101:e7f76cb49584 154 /**********************************************************************/
j3 101:e7f76cb49584 155 OneWireSlave::CmdResult DS18B20::copyScratchPad( void )
j3 101:e7f76cb49584 156 {
j3 101:e7f76cb49584 157 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 158
j3 101:e7f76cb49584 159 bool hasLocalPower = false;
j3 101:e7f76cb49584 160 deviceResult = this->readPowerSupply(hasLocalPower);
j3 101:e7f76cb49584 161
j3 101:e7f76cb49584 162 if(deviceResult == OneWireSlave::Success)
j3 101:e7f76cb49584 163 {
j3 101:e7f76cb49584 164 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 165
j3 101:e7f76cb49584 166 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 167 {
j3 101:e7f76cb49584 168 if(hasLocalPower)
j3 101:e7f76cb49584 169 {
j3 101:e7f76cb49584 170 owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 171 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 172 {
j3 101:e7f76cb49584 173 uint8_t recvbit = 0;
j3 101:e7f76cb49584 174 do
j3 101:e7f76cb49584 175 {
j3 101:e7f76cb49584 176 owmResult = master().OWTouchBitSetLevel(recvbit, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 177 }
j3 101:e7f76cb49584 178 while((!recvbit) && (owmResult == OneWireMaster::Success));
j3 101:e7f76cb49584 179
j3 101:e7f76cb49584 180 if ((owmResult == OneWireMaster::Success) && (recvbit & 1))
j3 101:e7f76cb49584 181 {
j3 101:e7f76cb49584 182 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 183 }
j3 101:e7f76cb49584 184 else
j3 101:e7f76cb49584 185 {
j3 101:e7f76cb49584 186 deviceResult = OneWireSlave::TimeoutError;
j3 101:e7f76cb49584 187 }
j3 101:e7f76cb49584 188 }
j3 101:e7f76cb49584 189 else
j3 101:e7f76cb49584 190 {
j3 101:e7f76cb49584 191 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 192 }
j3 101:e7f76cb49584 193 }
j3 101:e7f76cb49584 194 else
j3 101:e7f76cb49584 195 {
j3 101:e7f76cb49584 196 owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::StrongLevel);
j3 101:e7f76cb49584 197 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 198 {
j3 101:e7f76cb49584 199 wait_ms(10);
j3 101:e7f76cb49584 200
j3 101:e7f76cb49584 201 owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 202 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 203 {
j3 101:e7f76cb49584 204 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 205 }
j3 101:e7f76cb49584 206 else
j3 101:e7f76cb49584 207 {
j3 101:e7f76cb49584 208 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 209 }
j3 101:e7f76cb49584 210 }
j3 101:e7f76cb49584 211 else
j3 101:e7f76cb49584 212 {
j3 101:e7f76cb49584 213 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 214 }
j3 101:e7f76cb49584 215 }
j3 101:e7f76cb49584 216 }
j3 101:e7f76cb49584 217 else
j3 101:e7f76cb49584 218 {
j3 101:e7f76cb49584 219 deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 220 }
j3 101:e7f76cb49584 221 }
j3 101:e7f76cb49584 222
j3 101:e7f76cb49584 223 return deviceResult;
j3 101:e7f76cb49584 224 }
j3 101:e7f76cb49584 225
j3 101:e7f76cb49584 226 /**********************************************************************/
j3 101:e7f76cb49584 227 OneWireSlave::CmdResult DS18B20::convertTemperature(float & temp)
j3 101:e7f76cb49584 228 {
j3 101:e7f76cb49584 229 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 230
j3 101:e7f76cb49584 231 bool hasLocalPower = false;
j3 101:e7f76cb49584 232 deviceResult = this->readPowerSupply(hasLocalPower);
j3 101:e7f76cb49584 233
j3 101:e7f76cb49584 234 uint8_t scratchPadBuff[8];
j3 101:e7f76cb49584 235
j3 101:e7f76cb49584 236 if (deviceResult == OneWireMaster::Success)
j3 101:e7f76cb49584 237 {
j3 101:e7f76cb49584 238 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 239 if(owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 240 {
j3 101:e7f76cb49584 241 if(hasLocalPower)
j3 101:e7f76cb49584 242 {
j3 101:e7f76cb49584 243 owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 244 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 245 {
j3 101:e7f76cb49584 246 uint8_t recvbit = 0;
j3 101:e7f76cb49584 247 do
j3 101:e7f76cb49584 248 {
j3 101:e7f76cb49584 249 owmResult = master().OWTouchBitSetLevel(recvbit, OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 250 }
j3 101:e7f76cb49584 251 while((owmResult == OneWireMaster::Success) && (!recvbit));
j3 101:e7f76cb49584 252
j3 101:e7f76cb49584 253 if((owmResult == OneWireMaster::Success) && (recvbit & 1))
j3 101:e7f76cb49584 254 {
j3 101:e7f76cb49584 255 deviceResult = this->readScratchPad(scratchPadBuff);
j3 101:e7f76cb49584 256 }
j3 101:e7f76cb49584 257 else
j3 101:e7f76cb49584 258 {
j3 101:e7f76cb49584 259 deviceResult = OneWireSlave::TimeoutError;
j3 101:e7f76cb49584 260 }
j3 101:e7f76cb49584 261 }
j3 101:e7f76cb49584 262 else
j3 101:e7f76cb49584 263 {
j3 101:e7f76cb49584 264 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 265 }
j3 101:e7f76cb49584 266 }
j3 101:e7f76cb49584 267 else
j3 101:e7f76cb49584 268 {
j3 101:e7f76cb49584 269 owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::StrongLevel);
j3 101:e7f76cb49584 270 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 271 {
j3 101:e7f76cb49584 272 wait_ms(750);
j3 101:e7f76cb49584 273
j3 101:e7f76cb49584 274 owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);
j3 101:e7f76cb49584 275 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 276 {
j3 101:e7f76cb49584 277 deviceResult = this->readScratchPad(scratchPadBuff);
j3 101:e7f76cb49584 278 }
j3 101:e7f76cb49584 279 else
j3 101:e7f76cb49584 280 {
j3 101:e7f76cb49584 281 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 282 }
j3 101:e7f76cb49584 283 }
j3 101:e7f76cb49584 284 else
j3 101:e7f76cb49584 285 {
j3 101:e7f76cb49584 286 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 287 }
j3 101:e7f76cb49584 288 }
j3 101:e7f76cb49584 289 }
j3 101:e7f76cb49584 290 else
j3 101:e7f76cb49584 291 {
j3 101:e7f76cb49584 292 deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 293 }
j3 101:e7f76cb49584 294 }
j3 101:e7f76cb49584 295
j3 101:e7f76cb49584 296 if(deviceResult == OneWireSlave::Success)
j3 101:e7f76cb49584 297 {
j3 101:e7f76cb49584 298 int16_t intTemp = ((scratchPadBuff[1] << 8) | scratchPadBuff[0]);
j3 101:e7f76cb49584 299
j3 101:e7f76cb49584 300 switch(scratchPadBuff[4])
j3 101:e7f76cb49584 301 {
j3 106:d121acb88a34 302 case DS18B20::NineBit:
j3 101:e7f76cb49584 303 temp = (intTemp * 0.5F);
j3 101:e7f76cb49584 304 break;
j3 101:e7f76cb49584 305
j3 106:d121acb88a34 306 case DS18B20::TenBit:
j3 101:e7f76cb49584 307 temp = (intTemp * 0.25F);
j3 101:e7f76cb49584 308 break;
j3 101:e7f76cb49584 309
j3 106:d121acb88a34 310 case DS18B20::ElevenBit:
j3 101:e7f76cb49584 311 temp = (intTemp * 0.125F);
j3 101:e7f76cb49584 312 break;
j3 101:e7f76cb49584 313
j3 106:d121acb88a34 314 case DS18B20::TwelveBit:
j3 101:e7f76cb49584 315 temp = (intTemp * 0.0625F);
j3 101:e7f76cb49584 316 break;
j3 101:e7f76cb49584 317
j3 101:e7f76cb49584 318 default:
j3 101:e7f76cb49584 319 deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 320 break;
j3 101:e7f76cb49584 321 }
j3 101:e7f76cb49584 322 }
j3 101:e7f76cb49584 323
j3 101:e7f76cb49584 324 return deviceResult;
j3 101:e7f76cb49584 325 }
j3 101:e7f76cb49584 326
j3 101:e7f76cb49584 327
j3 101:e7f76cb49584 328 /**********************************************************************/
j3 101:e7f76cb49584 329 OneWireSlave::CmdResult DS18B20::recallEEPROM( void )
j3 101:e7f76cb49584 330 {
j3 101:e7f76cb49584 331 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure;
j3 101:e7f76cb49584 332
j3 101:e7f76cb49584 333 OneWireMaster::CmdResult owmResult = selectDevice();
j3 101:e7f76cb49584 334
j3 101:e7f76cb49584 335 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 336 {
j3 101:e7f76cb49584 337 uint8_t cmd = RECALL;
j3 101:e7f76cb49584 338
j3 101:e7f76cb49584 339 owmResult = master().OWWriteBlock(&cmd, 1);
j3 101:e7f76cb49584 340 if (owmResult == OneWireMaster::Success)
j3 101:e7f76cb49584 341 {
j3 101:e7f76cb49584 342 deviceResult = OneWireSlave::Success;
j3 101:e7f76cb49584 343 }
j3 101:e7f76cb49584 344 else
j3 101:e7f76cb49584 345 {
j3 101:e7f76cb49584 346 deviceResult = OneWireSlave::CommunicationError;
j3 101:e7f76cb49584 347 }
j3 101:e7f76cb49584 348 }
j3 101:e7f76cb49584 349
j3 101:e7f76cb49584 350 return deviceResult;
j3 101:e7f76cb49584 351 }