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:
IanBenzMaxim
Date:
Wed Mar 23 15:25:40 2016 -0500
Revision:
26:a361e3f42ba5
Parent:
24:8942d8478d68
Child:
27:d5aaefa252f1
Rework the OneWireMaster virtual interface for simplicity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 5:ce108eeb878d 1 /******************************************************************//**
j3 5:ce108eeb878d 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 5:ce108eeb878d 3 *
j3 5:ce108eeb878d 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 5:ce108eeb878d 5 * copy of this software and associated documentation files (the "Software"),
j3 5:ce108eeb878d 6 * to deal in the Software without restriction, including without limitation
j3 5:ce108eeb878d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 5:ce108eeb878d 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 5:ce108eeb878d 9 * Software is furnished to do so, subject to the following conditions:
j3 5:ce108eeb878d 10 *
j3 5:ce108eeb878d 11 * The above copyright notice and this permission notice shall be included
j3 5:ce108eeb878d 12 * in all copies or substantial portions of the Software.
j3 5:ce108eeb878d 13 *
j3 5:ce108eeb878d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 5:ce108eeb878d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 5:ce108eeb878d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 5:ce108eeb878d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 5:ce108eeb878d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 5:ce108eeb878d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 5:ce108eeb878d 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 5:ce108eeb878d 21 *
j3 5:ce108eeb878d 22 * Except as contained in this notice, the name of Maxim Integrated
j3 5:ce108eeb878d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 5:ce108eeb878d 24 * Products, Inc. Branding Policy.
j3 5:ce108eeb878d 25 *
j3 5:ce108eeb878d 26 * The mere transfer of this software does not imply any licenses
j3 5:ce108eeb878d 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 5:ce108eeb878d 28 * trademarks, maskwork rights, or any other form of intellectual
j3 5:ce108eeb878d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 5:ce108eeb878d 30 * ownership rights.
j3 5:ce108eeb878d 31 **********************************************************************/
j3 5:ce108eeb878d 32
j3 5:ce108eeb878d 33
j3 15:f6cb0d906fb6 34 #include "OneWireMaster.h"
j3 15:f6cb0d906fb6 35
j3 15:f6cb0d906fb6 36
j3 17:b646b1e3970b 37 const uint16_t OneWireMaster::_oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
j3 15:f6cb0d906fb6 38
j3 15:f6cb0d906fb6 39
j3 23:e8e403d61359 40 OneWireMaster::CmdResult OneWireMaster::OWFirst(RomId & romId)
j3 15:f6cb0d906fb6 41 {
j3 17:b646b1e3970b 42 // reset the search state
j3 17:b646b1e3970b 43 _last_discrepancy = 0;
j3 17:b646b1e3970b 44 _last_device_flag = 0;
j3 17:b646b1e3970b 45 _last_family_discrepancy = 0;
j3 17:b646b1e3970b 46
j3 17:b646b1e3970b 47 return OWSearch(romId);
j3 15:f6cb0d906fb6 48 }
j3 15:f6cb0d906fb6 49
j3 15:f6cb0d906fb6 50
j3 15:f6cb0d906fb6 51 //*********************************************************************
j3 23:e8e403d61359 52 OneWireMaster::CmdResult OneWireMaster::OWNext(RomId & romId)
j3 15:f6cb0d906fb6 53 {
j3 17:b646b1e3970b 54 // leave the search state alone
j3 17:b646b1e3970b 55 return OWSearch(romId);
j3 15:f6cb0d906fb6 56 }
j3 15:f6cb0d906fb6 57
j3 15:f6cb0d906fb6 58
j3 15:f6cb0d906fb6 59 //*********************************************************************
j3 23:e8e403d61359 60 OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId)
j3 15:f6cb0d906fb6 61 {
j3 23:e8e403d61359 62 OneWireMaster::CmdResult result;
j3 17:b646b1e3970b 63 RomId romIdCopy(romId);
j3 17:b646b1e3970b 64 int ld_backup, ldf_backup, lfd_backup;
j3 15:f6cb0d906fb6 65
j3 15:f6cb0d906fb6 66 // keep a backup copy of the current state
j3 15:f6cb0d906fb6 67 ld_backup = _last_discrepancy;
j3 15:f6cb0d906fb6 68 ldf_backup = _last_device_flag;
j3 15:f6cb0d906fb6 69 lfd_backup = _last_family_discrepancy;
j3 15:f6cb0d906fb6 70
j3 15:f6cb0d906fb6 71 // set search to find the same device
j3 15:f6cb0d906fb6 72 _last_discrepancy = 64;
IanBenzMaxim 21:00c94aeb533e 73 _last_device_flag = false;
j3 15:f6cb0d906fb6 74
j3 17:b646b1e3970b 75 result = OWSearch(romIdCopy);
j3 23:e8e403d61359 76 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 77 {
j3 15:f6cb0d906fb6 78 // check if same device found
j3 17:b646b1e3970b 79 if (romId != romIdCopy)
j3 15:f6cb0d906fb6 80 {
j3 23:e8e403d61359 81 result = OneWireMaster::OperationFailure;
j3 15:f6cb0d906fb6 82 }
j3 15:f6cb0d906fb6 83 }
j3 15:f6cb0d906fb6 84
j3 15:f6cb0d906fb6 85 // restore the search state
j3 15:f6cb0d906fb6 86 _last_discrepancy = ld_backup;
j3 15:f6cb0d906fb6 87 _last_device_flag = ldf_backup;
j3 15:f6cb0d906fb6 88 _last_family_discrepancy = lfd_backup;
j3 17:b646b1e3970b 89
j3 15:f6cb0d906fb6 90 // return the result of the verify
j3 17:b646b1e3970b 91 return result;
j3 15:f6cb0d906fb6 92 }
j3 15:f6cb0d906fb6 93
j3 15:f6cb0d906fb6 94
j3 15:f6cb0d906fb6 95 //*********************************************************************
j3 17:b646b1e3970b 96 void OneWireMaster::OWTargetSetup(RomId & romId)
j3 15:f6cb0d906fb6 97 {
j3 15:f6cb0d906fb6 98 // set the search state to find SearchFamily type devices
j3 17:b646b1e3970b 99 for (int i = 1; i < 8; i++)
j3 15:f6cb0d906fb6 100 {
j3 17:b646b1e3970b 101 romId[i] = 0;
j3 15:f6cb0d906fb6 102 }
j3 15:f6cb0d906fb6 103
j3 15:f6cb0d906fb6 104 _last_discrepancy = 64;
j3 15:f6cb0d906fb6 105 _last_family_discrepancy = 0;
j3 15:f6cb0d906fb6 106 _last_device_flag = false;
j3 15:f6cb0d906fb6 107 }
j3 5:ce108eeb878d 108
j3 5:ce108eeb878d 109
j3 5:ce108eeb878d 110 //*********************************************************************
j3 15:f6cb0d906fb6 111 void OneWireMaster::OWFamilySkipSetup(void)
j3 15:f6cb0d906fb6 112 {
j3 15:f6cb0d906fb6 113 // set the Last discrepancy to last family discrepancy
j3 15:f6cb0d906fb6 114 _last_discrepancy = _last_family_discrepancy;
j3 17:b646b1e3970b 115
j3 15:f6cb0d906fb6 116 // clear the last family discrpepancy
j3 15:f6cb0d906fb6 117 _last_family_discrepancy = 0;
j3 17:b646b1e3970b 118
j3 15:f6cb0d906fb6 119 // check for end of list
j3 17:b646b1e3970b 120 if (_last_discrepancy == 0)
j3 15:f6cb0d906fb6 121 {
j3 15:f6cb0d906fb6 122 _last_device_flag = true;
j3 15:f6cb0d906fb6 123 }
j3 15:f6cb0d906fb6 124 }
j3 15:f6cb0d906fb6 125
j3 15:f6cb0d906fb6 126
j3 15:f6cb0d906fb6 127 //*********************************************************************
j3 23:e8e403d61359 128 OneWireMaster::CmdResult OneWireMaster::OWReadROM(RomId & romId)
j3 15:f6cb0d906fb6 129 {
j3 23:e8e403d61359 130 OneWireMaster::CmdResult result;
j3 17:b646b1e3970b 131 uint8_t buf[2 + RomId::byteLen];
j3 17:b646b1e3970b 132
j3 17:b646b1e3970b 133 result = OWReset();
j3 23:e8e403d61359 134 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 135 {
IanBenzMaxim 24:8942d8478d68 136 result = OWWriteByte(READ_ROM);
j3 17:b646b1e3970b 137 }
j3 17:b646b1e3970b 138
j3 17:b646b1e3970b 139 // read the ROM
j3 23:e8e403d61359 140 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 141 {
j3 17:b646b1e3970b 142 result = OWReadBlock(buf, RomId::byteLen);
j3 15:f6cb0d906fb6 143 }
j3 15:f6cb0d906fb6 144
j3 17:b646b1e3970b 145 // verify CRC8
IanBenzMaxim 24:8942d8478d68 146 if ((result == OneWireMaster::Success) && (RomId::calculateCRC8(buf, RomId::byteLen) == 0))
j3 17:b646b1e3970b 147 {
j3 17:b646b1e3970b 148 romId = RomId(reinterpret_cast<std::uint8_t (&)[RomId::byteLen]>(buf[0]));
j3 17:b646b1e3970b 149 }
j3 17:b646b1e3970b 150
j3 17:b646b1e3970b 151 return result;
j3 15:f6cb0d906fb6 152 }
j3 15:f6cb0d906fb6 153
j3 15:f6cb0d906fb6 154
j3 15:f6cb0d906fb6 155 //*********************************************************************
j3 23:e8e403d61359 156 OneWireMaster::CmdResult OneWireMaster::OWSkipROM(void)
j3 15:f6cb0d906fb6 157 {
j3 23:e8e403d61359 158 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 159
j3 17:b646b1e3970b 160 result = OWReset();
j3 23:e8e403d61359 161 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 162 {
IanBenzMaxim 24:8942d8478d68 163 result = OWWriteByte(SKIP_ROM);
j3 15:f6cb0d906fb6 164 }
j3 15:f6cb0d906fb6 165
j3 17:b646b1e3970b 166 return result;
j3 15:f6cb0d906fb6 167 }
j3 15:f6cb0d906fb6 168
j3 15:f6cb0d906fb6 169
j3 15:f6cb0d906fb6 170 //*********************************************************************
j3 23:e8e403d61359 171 OneWireMaster::CmdResult OneWireMaster::OWMatchROM(const RomId & romId)
j3 15:f6cb0d906fb6 172 {
j3 23:e8e403d61359 173 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 174
j3 17:b646b1e3970b 175 uint8_t buf[1 + RomId::byteLen];
j3 17:b646b1e3970b 176
j3 17:b646b1e3970b 177 // use MatchROM
j3 17:b646b1e3970b 178 result = OWReset();
j3 23:e8e403d61359 179 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 180 {
IanBenzMaxim 24:8942d8478d68 181 buf[0] = MATCH_ROM;
j3 17:b646b1e3970b 182 std::memcpy(&buf[1], romId, RomId::byteLen);
j3 17:b646b1e3970b 183 // send command and rom
j3 17:b646b1e3970b 184 result = OWWriteBlock(buf, 1 + RomId::byteLen);
j3 15:f6cb0d906fb6 185 }
j3 17:b646b1e3970b 186
j3 17:b646b1e3970b 187 return result;
j3 15:f6cb0d906fb6 188 }
j3 15:f6cb0d906fb6 189
j3 15:f6cb0d906fb6 190
j3 15:f6cb0d906fb6 191 //*********************************************************************
j3 23:e8e403d61359 192 OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipROM(void)
j3 15:f6cb0d906fb6 193 {
j3 23:e8e403d61359 194 OneWireMaster::CmdResult result = OWSpeed(SPEED_STANDARD);
j3 15:f6cb0d906fb6 195
j3 23:e8e403d61359 196 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 197 {
j3 17:b646b1e3970b 198 result = OWReset();
j3 15:f6cb0d906fb6 199 }
j3 15:f6cb0d906fb6 200
j3 23:e8e403d61359 201 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 202 {
IanBenzMaxim 24:8942d8478d68 203 result = OWWriteByte(OVERDRIVE_SKIP_ROM);
j3 17:b646b1e3970b 204 }
j3 17:b646b1e3970b 205
j3 23:e8e403d61359 206 if (result == OneWireMaster::Success)
j3 17:b646b1e3970b 207 {
j3 17:b646b1e3970b 208 result = OWSpeed(SPEED_OVERDRIVE);
j3 17:b646b1e3970b 209 }
j3 17:b646b1e3970b 210
j3 17:b646b1e3970b 211 return result;
j3 15:f6cb0d906fb6 212 }
j3 15:f6cb0d906fb6 213
j3 15:f6cb0d906fb6 214
j3 15:f6cb0d906fb6 215 //*********************************************************************
j3 23:e8e403d61359 216 OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchROM(const RomId & romId)
j3 15:f6cb0d906fb6 217 {
j3 23:e8e403d61359 218 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 219
j3 17:b646b1e3970b 220 // use overdrive MatchROM
j3 17:b646b1e3970b 221 OWSpeed(SPEED_STANDARD);
j3 17:b646b1e3970b 222
j3 17:b646b1e3970b 223 result = OWReset();
j3 23:e8e403d61359 224 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 225 {
IanBenzMaxim 24:8942d8478d68 226 result = OWWriteByte(OVERDRIVE_MATCH_ROM);
j3 23:e8e403d61359 227 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 228 {
j3 15:f6cb0d906fb6 229 OWSpeed(SPEED_OVERDRIVE);
j3 17:b646b1e3970b 230 // send ROM
j3 17:b646b1e3970b 231 result = OWWriteBlock(romId, RomId::byteLen);
j3 15:f6cb0d906fb6 232 }
j3 15:f6cb0d906fb6 233 }
j3 17:b646b1e3970b 234 return result;
j3 15:f6cb0d906fb6 235 }
j3 15:f6cb0d906fb6 236
j3 15:f6cb0d906fb6 237
j3 15:f6cb0d906fb6 238 //*********************************************************************
j3 23:e8e403d61359 239 OneWireMaster::CmdResult OneWireMaster::OWResume(void)
j3 15:f6cb0d906fb6 240 {
j3 23:e8e403d61359 241 OneWireMaster::CmdResult result;
j3 15:f6cb0d906fb6 242
j3 17:b646b1e3970b 243 result = OWReset();
j3 23:e8e403d61359 244 if (result == OneWireMaster::Success)
j3 15:f6cb0d906fb6 245 {
IanBenzMaxim 24:8942d8478d68 246 result = OWWriteByte(RESUME);
j3 15:f6cb0d906fb6 247 }
j3 15:f6cb0d906fb6 248
j3 17:b646b1e3970b 249 return result;
j3 15:f6cb0d906fb6 250 }
j3 15:f6cb0d906fb6 251
j3 15:f6cb0d906fb6 252
j3 17:b646b1e3970b 253 //--------------------------------------------------------------------------
j3 17:b646b1e3970b 254 // Calculate a new CRC16 from the input data shorteger. Return the current
j3 17:b646b1e3970b 255 // CRC16 and also update the global variable CRC16.
j3 17:b646b1e3970b 256 //
j3 17:b646b1e3970b 257 uint16_t OneWireMaster::calculateCRC16(uint16_t CRC16, uint16_t data)
j3 15:f6cb0d906fb6 258 {
j3 17:b646b1e3970b 259 data = (data ^ (CRC16 & 0xff)) & 0xff;
j3 17:b646b1e3970b 260 CRC16 >>= 8;
j3 17:b646b1e3970b 261
j3 17:b646b1e3970b 262 if (_oddparity[data & 0xf] ^ _oddparity[data >> 4])
j3 17:b646b1e3970b 263 CRC16 ^= 0xc001;
j3 17:b646b1e3970b 264
j3 17:b646b1e3970b 265 data <<= 6;
j3 17:b646b1e3970b 266 CRC16 ^= data;
j3 17:b646b1e3970b 267 data <<= 1;
j3 17:b646b1e3970b 268 CRC16 ^= data;
j3 17:b646b1e3970b 269
j3 17:b646b1e3970b 270 return CRC16;
j3 15:f6cb0d906fb6 271 }
j3 15:f6cb0d906fb6 272
j3 17:b646b1e3970b 273 uint16_t OneWireMaster::calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc)
j3 17:b646b1e3970b 274 {
j3 17:b646b1e3970b 275 for (size_t i = data_offset; i < (data_len + data_offset); i++)
j3 17:b646b1e3970b 276 crc = calculateCRC16(crc, data[i]);
j3 17:b646b1e3970b 277 return crc;
IanBenzMaxim 21:00c94aeb533e 278 }