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.
OneWire_Masters/OneWireMaster.cpp@24:8942d8478d68, 2016-03-22 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Tue Mar 22 14:24:53 2016 -0500
- Revision:
- 24:8942d8478d68
- Parent:
- 23:e8e403d61359
- Child:
- 26:a361e3f42ba5
Began cleanup of DS2465 class. Added relative include paths to help preprocessor.
Who changed what in which revision?
User | Revision | Line number | New 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 | 15:f6cb0d906fb6 | 40 | //********************************************************************* |
j3 | 23:e8e403d61359 | 41 | OneWireMaster::CmdResult OneWireMaster::OWWriteBit(uint8_t sendbit) |
j3 | 15:f6cb0d906fb6 | 42 | { |
j3 | 17:b646b1e3970b | 43 | return OWTouchBit(sendbit); |
j3 | 15:f6cb0d906fb6 | 44 | } |
j3 | 15:f6cb0d906fb6 | 45 | |
j3 | 15:f6cb0d906fb6 | 46 | |
j3 | 15:f6cb0d906fb6 | 47 | //********************************************************************* |
j3 | 23:e8e403d61359 | 48 | OneWireMaster::CmdResult OneWireMaster::OWReadBit(uint8_t & recvbit) |
j3 | 15:f6cb0d906fb6 | 49 | { |
j3 | 17:b646b1e3970b | 50 | recvbit = 0x01; |
j3 | 17:b646b1e3970b | 51 | return(OWTouchBit(recvbit)); |
j3 | 15:f6cb0d906fb6 | 52 | } |
j3 | 15:f6cb0d906fb6 | 53 | |
j3 | 15:f6cb0d906fb6 | 54 | |
j3 | 15:f6cb0d906fb6 | 55 | //********************************************************************* |
j3 | 23:e8e403d61359 | 56 | OneWireMaster::CmdResult OneWireMaster::OWTouchByte(uint8_t & sendrecvbyte) |
j3 | 15:f6cb0d906fb6 | 57 | { |
j3 | 23:e8e403d61359 | 58 | OneWireMaster::CmdResult result = OWWriteByte(sendrecvbyte); |
j3 | 17:b646b1e3970b | 59 | |
j3 | 23:e8e403d61359 | 60 | if (result == OneWireMaster::Success) |
j3 | 17:b646b1e3970b | 61 | { |
j3 | 17:b646b1e3970b | 62 | OWReadByte(sendrecvbyte); |
j3 | 17:b646b1e3970b | 63 | } |
j3 | 15:f6cb0d906fb6 | 64 | |
j3 | 17:b646b1e3970b | 65 | return result; |
j3 | 15:f6cb0d906fb6 | 66 | } |
j3 | 15:f6cb0d906fb6 | 67 | |
j3 | 15:f6cb0d906fb6 | 68 | |
j3 | 15:f6cb0d906fb6 | 69 | //********************************************************************* |
j3 | 23:e8e403d61359 | 70 | OneWireMaster::CmdResult OneWireMaster::OWBlock(uint8_t *tran_buf, uint8_t tran_len) |
j3 | 15:f6cb0d906fb6 | 71 | { |
j3 | 23:e8e403d61359 | 72 | OneWireMaster::CmdResult result; |
j3 | 15:f6cb0d906fb6 | 73 | |
j3 | 17:b646b1e3970b | 74 | for (uint8_t i = 0; i < tran_len; i++) |
j3 | 17:b646b1e3970b | 75 | { |
j3 | 17:b646b1e3970b | 76 | result = OWTouchByte(tran_buf[i]); |
j3 | 17:b646b1e3970b | 77 | |
j3 | 23:e8e403d61359 | 78 | if (result != OneWireMaster::Success) |
j3 | 17:b646b1e3970b | 79 | { |
j3 | 17:b646b1e3970b | 80 | break; |
j3 | 17:b646b1e3970b | 81 | } |
j3 | 17:b646b1e3970b | 82 | } |
j3 | 15:f6cb0d906fb6 | 83 | |
j3 | 17:b646b1e3970b | 84 | return result; |
j3 | 17:b646b1e3970b | 85 | } |
j3 | 17:b646b1e3970b | 86 | |
j3 | 17:b646b1e3970b | 87 | |
j3 | 23:e8e403d61359 | 88 | OneWireMaster::CmdResult OneWireMaster::OWFirst(RomId & romId) |
j3 | 15:f6cb0d906fb6 | 89 | { |
j3 | 17:b646b1e3970b | 90 | // reset the search state |
j3 | 17:b646b1e3970b | 91 | _last_discrepancy = 0; |
j3 | 17:b646b1e3970b | 92 | _last_device_flag = 0; |
j3 | 17:b646b1e3970b | 93 | _last_family_discrepancy = 0; |
j3 | 17:b646b1e3970b | 94 | |
j3 | 17:b646b1e3970b | 95 | return OWSearch(romId); |
j3 | 15:f6cb0d906fb6 | 96 | } |
j3 | 15:f6cb0d906fb6 | 97 | |
j3 | 15:f6cb0d906fb6 | 98 | |
j3 | 15:f6cb0d906fb6 | 99 | //********************************************************************* |
j3 | 23:e8e403d61359 | 100 | OneWireMaster::CmdResult OneWireMaster::OWNext(RomId & romId) |
j3 | 15:f6cb0d906fb6 | 101 | { |
j3 | 17:b646b1e3970b | 102 | // leave the search state alone |
j3 | 17:b646b1e3970b | 103 | return OWSearch(romId); |
j3 | 15:f6cb0d906fb6 | 104 | } |
j3 | 15:f6cb0d906fb6 | 105 | |
j3 | 15:f6cb0d906fb6 | 106 | |
j3 | 15:f6cb0d906fb6 | 107 | //********************************************************************* |
j3 | 23:e8e403d61359 | 108 | OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId) |
j3 | 15:f6cb0d906fb6 | 109 | { |
j3 | 23:e8e403d61359 | 110 | OneWireMaster::CmdResult result; |
j3 | 17:b646b1e3970b | 111 | RomId romIdCopy(romId); |
j3 | 17:b646b1e3970b | 112 | int ld_backup, ldf_backup, lfd_backup; |
j3 | 15:f6cb0d906fb6 | 113 | |
j3 | 15:f6cb0d906fb6 | 114 | // keep a backup copy of the current state |
j3 | 15:f6cb0d906fb6 | 115 | ld_backup = _last_discrepancy; |
j3 | 15:f6cb0d906fb6 | 116 | ldf_backup = _last_device_flag; |
j3 | 15:f6cb0d906fb6 | 117 | lfd_backup = _last_family_discrepancy; |
j3 | 15:f6cb0d906fb6 | 118 | |
j3 | 15:f6cb0d906fb6 | 119 | // set search to find the same device |
j3 | 15:f6cb0d906fb6 | 120 | _last_discrepancy = 64; |
IanBenzMaxim | 21:00c94aeb533e | 121 | _last_device_flag = false; |
j3 | 15:f6cb0d906fb6 | 122 | |
j3 | 17:b646b1e3970b | 123 | result = OWSearch(romIdCopy); |
j3 | 23:e8e403d61359 | 124 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 125 | { |
j3 | 15:f6cb0d906fb6 | 126 | // check if same device found |
j3 | 17:b646b1e3970b | 127 | if (romId != romIdCopy) |
j3 | 15:f6cb0d906fb6 | 128 | { |
j3 | 23:e8e403d61359 | 129 | result = OneWireMaster::OperationFailure; |
j3 | 15:f6cb0d906fb6 | 130 | } |
j3 | 15:f6cb0d906fb6 | 131 | } |
j3 | 15:f6cb0d906fb6 | 132 | |
j3 | 15:f6cb0d906fb6 | 133 | // restore the search state |
j3 | 15:f6cb0d906fb6 | 134 | _last_discrepancy = ld_backup; |
j3 | 15:f6cb0d906fb6 | 135 | _last_device_flag = ldf_backup; |
j3 | 15:f6cb0d906fb6 | 136 | _last_family_discrepancy = lfd_backup; |
j3 | 17:b646b1e3970b | 137 | |
j3 | 15:f6cb0d906fb6 | 138 | // return the result of the verify |
j3 | 17:b646b1e3970b | 139 | return result; |
j3 | 15:f6cb0d906fb6 | 140 | } |
j3 | 15:f6cb0d906fb6 | 141 | |
j3 | 15:f6cb0d906fb6 | 142 | |
j3 | 15:f6cb0d906fb6 | 143 | //********************************************************************* |
j3 | 17:b646b1e3970b | 144 | void OneWireMaster::OWTargetSetup(RomId & romId) |
j3 | 15:f6cb0d906fb6 | 145 | { |
j3 | 15:f6cb0d906fb6 | 146 | // set the search state to find SearchFamily type devices |
j3 | 17:b646b1e3970b | 147 | for (int i = 1; i < 8; i++) |
j3 | 15:f6cb0d906fb6 | 148 | { |
j3 | 17:b646b1e3970b | 149 | romId[i] = 0; |
j3 | 15:f6cb0d906fb6 | 150 | } |
j3 | 15:f6cb0d906fb6 | 151 | |
j3 | 15:f6cb0d906fb6 | 152 | _last_discrepancy = 64; |
j3 | 15:f6cb0d906fb6 | 153 | _last_family_discrepancy = 0; |
j3 | 15:f6cb0d906fb6 | 154 | _last_device_flag = false; |
j3 | 15:f6cb0d906fb6 | 155 | } |
j3 | 5:ce108eeb878d | 156 | |
j3 | 5:ce108eeb878d | 157 | |
j3 | 5:ce108eeb878d | 158 | //********************************************************************* |
j3 | 15:f6cb0d906fb6 | 159 | void OneWireMaster::OWFamilySkipSetup(void) |
j3 | 15:f6cb0d906fb6 | 160 | { |
j3 | 15:f6cb0d906fb6 | 161 | // set the Last discrepancy to last family discrepancy |
j3 | 15:f6cb0d906fb6 | 162 | _last_discrepancy = _last_family_discrepancy; |
j3 | 17:b646b1e3970b | 163 | |
j3 | 15:f6cb0d906fb6 | 164 | // clear the last family discrpepancy |
j3 | 15:f6cb0d906fb6 | 165 | _last_family_discrepancy = 0; |
j3 | 17:b646b1e3970b | 166 | |
j3 | 15:f6cb0d906fb6 | 167 | // check for end of list |
j3 | 17:b646b1e3970b | 168 | if (_last_discrepancy == 0) |
j3 | 15:f6cb0d906fb6 | 169 | { |
j3 | 15:f6cb0d906fb6 | 170 | _last_device_flag = true; |
j3 | 15:f6cb0d906fb6 | 171 | } |
j3 | 15:f6cb0d906fb6 | 172 | } |
j3 | 15:f6cb0d906fb6 | 173 | |
j3 | 15:f6cb0d906fb6 | 174 | |
j3 | 15:f6cb0d906fb6 | 175 | //********************************************************************* |
j3 | 23:e8e403d61359 | 176 | OneWireMaster::CmdResult OneWireMaster::OWReadROM(RomId & romId) |
j3 | 15:f6cb0d906fb6 | 177 | { |
j3 | 23:e8e403d61359 | 178 | OneWireMaster::CmdResult result; |
j3 | 17:b646b1e3970b | 179 | uint8_t buf[2 + RomId::byteLen]; |
j3 | 17:b646b1e3970b | 180 | |
j3 | 17:b646b1e3970b | 181 | result = OWReset(); |
j3 | 23:e8e403d61359 | 182 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 183 | { |
IanBenzMaxim | 24:8942d8478d68 | 184 | result = OWWriteByte(READ_ROM); |
j3 | 17:b646b1e3970b | 185 | } |
j3 | 17:b646b1e3970b | 186 | |
j3 | 17:b646b1e3970b | 187 | // read the ROM |
j3 | 23:e8e403d61359 | 188 | if (result == OneWireMaster::Success) |
j3 | 17:b646b1e3970b | 189 | { |
j3 | 17:b646b1e3970b | 190 | result = OWReadBlock(buf, RomId::byteLen); |
j3 | 15:f6cb0d906fb6 | 191 | } |
j3 | 15:f6cb0d906fb6 | 192 | |
j3 | 17:b646b1e3970b | 193 | // verify CRC8 |
IanBenzMaxim | 24:8942d8478d68 | 194 | if ((result == OneWireMaster::Success) && (RomId::calculateCRC8(buf, RomId::byteLen) == 0)) |
j3 | 17:b646b1e3970b | 195 | { |
j3 | 17:b646b1e3970b | 196 | romId = RomId(reinterpret_cast<std::uint8_t (&)[RomId::byteLen]>(buf[0])); |
j3 | 17:b646b1e3970b | 197 | } |
j3 | 17:b646b1e3970b | 198 | |
j3 | 17:b646b1e3970b | 199 | return result; |
j3 | 15:f6cb0d906fb6 | 200 | } |
j3 | 15:f6cb0d906fb6 | 201 | |
j3 | 15:f6cb0d906fb6 | 202 | |
j3 | 15:f6cb0d906fb6 | 203 | //********************************************************************* |
j3 | 23:e8e403d61359 | 204 | OneWireMaster::CmdResult OneWireMaster::OWSkipROM(void) |
j3 | 15:f6cb0d906fb6 | 205 | { |
j3 | 23:e8e403d61359 | 206 | OneWireMaster::CmdResult result; |
j3 | 15:f6cb0d906fb6 | 207 | |
j3 | 17:b646b1e3970b | 208 | result = OWReset(); |
j3 | 23:e8e403d61359 | 209 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 210 | { |
IanBenzMaxim | 24:8942d8478d68 | 211 | result = OWWriteByte(SKIP_ROM); |
j3 | 15:f6cb0d906fb6 | 212 | } |
j3 | 15:f6cb0d906fb6 | 213 | |
j3 | 17:b646b1e3970b | 214 | return result; |
j3 | 15:f6cb0d906fb6 | 215 | } |
j3 | 15:f6cb0d906fb6 | 216 | |
j3 | 15:f6cb0d906fb6 | 217 | |
j3 | 15:f6cb0d906fb6 | 218 | //********************************************************************* |
j3 | 23:e8e403d61359 | 219 | OneWireMaster::CmdResult OneWireMaster::OWMatchROM(const RomId & romId) |
j3 | 15:f6cb0d906fb6 | 220 | { |
j3 | 23:e8e403d61359 | 221 | OneWireMaster::CmdResult result; |
j3 | 15:f6cb0d906fb6 | 222 | |
j3 | 17:b646b1e3970b | 223 | uint8_t buf[1 + RomId::byteLen]; |
j3 | 17:b646b1e3970b | 224 | |
j3 | 17:b646b1e3970b | 225 | // use MatchROM |
j3 | 17:b646b1e3970b | 226 | result = OWReset(); |
j3 | 23:e8e403d61359 | 227 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 228 | { |
IanBenzMaxim | 24:8942d8478d68 | 229 | buf[0] = MATCH_ROM; |
j3 | 17:b646b1e3970b | 230 | std::memcpy(&buf[1], romId, RomId::byteLen); |
j3 | 17:b646b1e3970b | 231 | // send command and rom |
j3 | 17:b646b1e3970b | 232 | result = OWWriteBlock(buf, 1 + RomId::byteLen); |
j3 | 15:f6cb0d906fb6 | 233 | } |
j3 | 17:b646b1e3970b | 234 | |
j3 | 17:b646b1e3970b | 235 | return result; |
j3 | 15:f6cb0d906fb6 | 236 | } |
j3 | 15:f6cb0d906fb6 | 237 | |
j3 | 15:f6cb0d906fb6 | 238 | |
j3 | 15:f6cb0d906fb6 | 239 | //********************************************************************* |
j3 | 23:e8e403d61359 | 240 | OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipROM(void) |
j3 | 15:f6cb0d906fb6 | 241 | { |
j3 | 23:e8e403d61359 | 242 | OneWireMaster::CmdResult result = OWSpeed(SPEED_STANDARD); |
j3 | 15:f6cb0d906fb6 | 243 | |
j3 | 23:e8e403d61359 | 244 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 245 | { |
j3 | 17:b646b1e3970b | 246 | result = OWReset(); |
j3 | 15:f6cb0d906fb6 | 247 | } |
j3 | 15:f6cb0d906fb6 | 248 | |
j3 | 23:e8e403d61359 | 249 | if (result == OneWireMaster::Success) |
j3 | 17:b646b1e3970b | 250 | { |
IanBenzMaxim | 24:8942d8478d68 | 251 | result = OWWriteByte(OVERDRIVE_SKIP_ROM); |
j3 | 17:b646b1e3970b | 252 | } |
j3 | 17:b646b1e3970b | 253 | |
j3 | 23:e8e403d61359 | 254 | if (result == OneWireMaster::Success) |
j3 | 17:b646b1e3970b | 255 | { |
j3 | 17:b646b1e3970b | 256 | result = OWSpeed(SPEED_OVERDRIVE); |
j3 | 17:b646b1e3970b | 257 | } |
j3 | 17:b646b1e3970b | 258 | |
j3 | 17:b646b1e3970b | 259 | return result; |
j3 | 15:f6cb0d906fb6 | 260 | } |
j3 | 15:f6cb0d906fb6 | 261 | |
j3 | 15:f6cb0d906fb6 | 262 | |
j3 | 15:f6cb0d906fb6 | 263 | //********************************************************************* |
j3 | 23:e8e403d61359 | 264 | OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchROM(const RomId & romId) |
j3 | 15:f6cb0d906fb6 | 265 | { |
j3 | 23:e8e403d61359 | 266 | OneWireMaster::CmdResult result; |
j3 | 15:f6cb0d906fb6 | 267 | |
j3 | 17:b646b1e3970b | 268 | // use overdrive MatchROM |
j3 | 17:b646b1e3970b | 269 | OWSpeed(SPEED_STANDARD); |
j3 | 17:b646b1e3970b | 270 | |
j3 | 17:b646b1e3970b | 271 | result = OWReset(); |
j3 | 23:e8e403d61359 | 272 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 273 | { |
IanBenzMaxim | 24:8942d8478d68 | 274 | result = OWWriteByte(OVERDRIVE_MATCH_ROM); |
j3 | 23:e8e403d61359 | 275 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 276 | { |
j3 | 15:f6cb0d906fb6 | 277 | OWSpeed(SPEED_OVERDRIVE); |
j3 | 17:b646b1e3970b | 278 | // send ROM |
j3 | 17:b646b1e3970b | 279 | result = OWWriteBlock(romId, RomId::byteLen); |
j3 | 15:f6cb0d906fb6 | 280 | } |
j3 | 15:f6cb0d906fb6 | 281 | } |
j3 | 17:b646b1e3970b | 282 | return result; |
j3 | 15:f6cb0d906fb6 | 283 | } |
j3 | 15:f6cb0d906fb6 | 284 | |
j3 | 15:f6cb0d906fb6 | 285 | |
j3 | 15:f6cb0d906fb6 | 286 | //********************************************************************* |
j3 | 23:e8e403d61359 | 287 | OneWireMaster::CmdResult OneWireMaster::OWResume(void) |
j3 | 15:f6cb0d906fb6 | 288 | { |
j3 | 23:e8e403d61359 | 289 | OneWireMaster::CmdResult result; |
j3 | 15:f6cb0d906fb6 | 290 | |
j3 | 17:b646b1e3970b | 291 | result = OWReset(); |
j3 | 23:e8e403d61359 | 292 | if (result == OneWireMaster::Success) |
j3 | 15:f6cb0d906fb6 | 293 | { |
IanBenzMaxim | 24:8942d8478d68 | 294 | result = OWWriteByte(RESUME); |
j3 | 15:f6cb0d906fb6 | 295 | } |
j3 | 15:f6cb0d906fb6 | 296 | |
j3 | 17:b646b1e3970b | 297 | return result; |
j3 | 15:f6cb0d906fb6 | 298 | } |
j3 | 15:f6cb0d906fb6 | 299 | |
j3 | 15:f6cb0d906fb6 | 300 | |
j3 | 17:b646b1e3970b | 301 | //-------------------------------------------------------------------------- |
j3 | 17:b646b1e3970b | 302 | // Calculate a new CRC16 from the input data shorteger. Return the current |
j3 | 17:b646b1e3970b | 303 | // CRC16 and also update the global variable CRC16. |
j3 | 17:b646b1e3970b | 304 | // |
j3 | 17:b646b1e3970b | 305 | uint16_t OneWireMaster::calculateCRC16(uint16_t CRC16, uint16_t data) |
j3 | 15:f6cb0d906fb6 | 306 | { |
j3 | 17:b646b1e3970b | 307 | data = (data ^ (CRC16 & 0xff)) & 0xff; |
j3 | 17:b646b1e3970b | 308 | CRC16 >>= 8; |
j3 | 17:b646b1e3970b | 309 | |
j3 | 17:b646b1e3970b | 310 | if (_oddparity[data & 0xf] ^ _oddparity[data >> 4]) |
j3 | 17:b646b1e3970b | 311 | CRC16 ^= 0xc001; |
j3 | 17:b646b1e3970b | 312 | |
j3 | 17:b646b1e3970b | 313 | data <<= 6; |
j3 | 17:b646b1e3970b | 314 | CRC16 ^= data; |
j3 | 17:b646b1e3970b | 315 | data <<= 1; |
j3 | 17:b646b1e3970b | 316 | CRC16 ^= data; |
j3 | 17:b646b1e3970b | 317 | |
j3 | 17:b646b1e3970b | 318 | return CRC16; |
j3 | 15:f6cb0d906fb6 | 319 | } |
j3 | 15:f6cb0d906fb6 | 320 | |
j3 | 17:b646b1e3970b | 321 | uint16_t OneWireMaster::calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc) |
j3 | 17:b646b1e3970b | 322 | { |
j3 | 17:b646b1e3970b | 323 | for (size_t i = data_offset; i < (data_len + data_offset); i++) |
j3 | 17:b646b1e3970b | 324 | crc = calculateCRC16(crc, data[i]); |
j3 | 17:b646b1e3970b | 325 | return crc; |
IanBenzMaxim | 21:00c94aeb533e | 326 | } |