Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
j3
Date:
Fri Feb 17 21:54:14 2017 +0000
Revision:
141:cf38f48a2a49
Parent:
139:f0e0a7976846
Updated OneWireMaster, DS2465, and DS2431 to synchronize with svn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 110:a3b5e2a4fdf2 1 /******************************************************************//**
j3 110:a3b5e2a4fdf2 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 110:a3b5e2a4fdf2 3 *
j3 110:a3b5e2a4fdf2 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 110:a3b5e2a4fdf2 5 * copy of this software and associated documentation files (the "Software"),
j3 110:a3b5e2a4fdf2 6 * to deal in the Software without restriction, including without limitation
j3 110:a3b5e2a4fdf2 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 110:a3b5e2a4fdf2 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 110:a3b5e2a4fdf2 9 * Software is furnished to do so, subject to the following conditions:
j3 110:a3b5e2a4fdf2 10 *
j3 110:a3b5e2a4fdf2 11 * The above copyright notice and this permission notice shall be included
j3 110:a3b5e2a4fdf2 12 * in all copies or substantial portions of the Software.
j3 110:a3b5e2a4fdf2 13 *
j3 110:a3b5e2a4fdf2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 110:a3b5e2a4fdf2 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 110:a3b5e2a4fdf2 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 110:a3b5e2a4fdf2 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 110:a3b5e2a4fdf2 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 110:a3b5e2a4fdf2 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 110:a3b5e2a4fdf2 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 110:a3b5e2a4fdf2 21 *
j3 110:a3b5e2a4fdf2 22 * Except as contained in this notice, the name of Maxim Integrated
j3 110:a3b5e2a4fdf2 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 110:a3b5e2a4fdf2 24 * Products, Inc. Branding Policy.
j3 110:a3b5e2a4fdf2 25 *
j3 110:a3b5e2a4fdf2 26 * The mere transfer of this software does not imply any licenses
j3 110:a3b5e2a4fdf2 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 110:a3b5e2a4fdf2 28 * trademarks, maskwork rights, or any other form of intellectual
j3 110:a3b5e2a4fdf2 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 110:a3b5e2a4fdf2 30 * ownership rights.
j3 110:a3b5e2a4fdf2 31 **********************************************************************/
j3 110:a3b5e2a4fdf2 32
j3 115:a1ca2f3bf46d 33 #include "wait_api.h"
j3 110:a3b5e2a4fdf2 34 #include "Slaves/Memory/DS2431/DS2431.h"
j3 110:a3b5e2a4fdf2 35
j3 115:a1ca2f3bf46d 36 using namespace OneWire;
j3 115:a1ca2f3bf46d 37 using namespace OneWire::crc;
j3 115:a1ca2f3bf46d 38
j3 141:cf38f48a2a49 39 enum Command
j3 115:a1ca2f3bf46d 40 {
j3 141:cf38f48a2a49 41 WriteScratchpad = 0x0F,
j3 141:cf38f48a2a49 42 ReadScratchpad = 0xAA,
j3 141:cf38f48a2a49 43 CopyScratchpad = 0x55,
j3 141:cf38f48a2a49 44 ReadMemory = 0xF0
j3 115:a1ca2f3bf46d 45 };
j3 115:a1ca2f3bf46d 46
j3 141:cf38f48a2a49 47 static const DS2431::Address beginReservedAddress = 0x0088;
j3 141:cf38f48a2a49 48
j3 115:a1ca2f3bf46d 49 //*********************************************************************
j3 141:cf38f48a2a49 50 DS2431::DS2431(RandomAccessRomIterator & selector) : OneWireSlave(selector)
j3 115:a1ca2f3bf46d 51 {
j3 115:a1ca2f3bf46d 52 }
j3 115:a1ca2f3bf46d 53
j3 115:a1ca2f3bf46d 54 //*********************************************************************
j3 141:cf38f48a2a49 55 OneWireSlave::CmdResult DS2431::writeMemory(Address targetAddress, const Scratchpad & data)
j3 141:cf38f48a2a49 56 {
j3 141:cf38f48a2a49 57 if (((targetAddress & 0x7) != 0x0) || ((targetAddress + data.size()) > beginReservedAddress))
j3 141:cf38f48a2a49 58 {
j3 141:cf38f48a2a49 59 return OneWireSlave::OperationFailure;
j3 141:cf38f48a2a49 60 }
j3 141:cf38f48a2a49 61 OneWireSlave::CmdResult result = writeScratchpad(targetAddress, data);
j3 141:cf38f48a2a49 62 if (result != OneWireSlave::Success)
j3 116:8058bb54e959 63 {
j3 141:cf38f48a2a49 64 return result;
j3 116:8058bb54e959 65 }
j3 141:cf38f48a2a49 66 Scratchpad readData;
j3 141:cf38f48a2a49 67 uint8_t esByte;
j3 141:cf38f48a2a49 68 result = readScratchpad(readData, esByte);
j3 141:cf38f48a2a49 69 if (result != OneWireSlave::Success)
j3 141:cf38f48a2a49 70 {
j3 141:cf38f48a2a49 71 return result;
j3 141:cf38f48a2a49 72 }
j3 141:cf38f48a2a49 73 result = copyScratchpad(targetAddress, esByte);
j3 116:8058bb54e959 74 return result;
j3 116:8058bb54e959 75 }
j3 116:8058bb54e959 76
j3 116:8058bb54e959 77 //*********************************************************************
j3 141:cf38f48a2a49 78 OneWireSlave::CmdResult DS2431::readMemory(Address targetAddress, uint8_t numBytes, uint8_t * data)
j3 141:cf38f48a2a49 79 {
j3 141:cf38f48a2a49 80 if ((targetAddress + numBytes) > beginReservedAddress)
j3 141:cf38f48a2a49 81 {
j3 141:cf38f48a2a49 82 return OneWireSlave::OperationFailure;
j3 141:cf38f48a2a49 83 }
j3 141:cf38f48a2a49 84 OneWireMaster::CmdResult owmResult = selectDevice();
j3 141:cf38f48a2a49 85 if (owmResult != OneWireMaster::Success)
j3 116:8058bb54e959 86 {
j3 141:cf38f48a2a49 87 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 88 }
j3 141:cf38f48a2a49 89 uint8_t sendBlock[] = { ReadMemory, static_cast<uint8_t>(targetAddress), static_cast<uint8_t>(targetAddress >> 8) };
j3 141:cf38f48a2a49 90 owmResult = master().OWWriteBlock(sendBlock, sizeof(sendBlock) / sizeof(sendBlock[0]));
j3 141:cf38f48a2a49 91 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 92 {
j3 141:cf38f48a2a49 93 return OneWireSlave::CommunicationError;
j3 116:8058bb54e959 94 }
j3 141:cf38f48a2a49 95 owmResult = master().OWReadBlock(data, numBytes);
j3 141:cf38f48a2a49 96 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 97 {
j3 141:cf38f48a2a49 98 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 99 }
j3 141:cf38f48a2a49 100 // else
j3 141:cf38f48a2a49 101 return OneWireSlave::Success;
j3 116:8058bb54e959 102 }
j3 116:8058bb54e959 103
j3 116:8058bb54e959 104 //*********************************************************************
j3 141:cf38f48a2a49 105 OneWireSlave::CmdResult DS2431::writeScratchpad(Address targetAddress, const Scratchpad & data)
j3 141:cf38f48a2a49 106 {
j3 115:a1ca2f3bf46d 107 OneWireMaster::CmdResult owmResult = selectDevice();
j3 141:cf38f48a2a49 108 if(owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 109 {
j3 141:cf38f48a2a49 110 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 111 }
j3 141:cf38f48a2a49 112 uint8_t sendBlock[3 + Scratchpad::csize] = { WriteScratchpad, static_cast<uint8_t>(targetAddress), static_cast<uint8_t>(targetAddress >> 8) };
j3 141:cf38f48a2a49 113 std::memcpy((sendBlock + 3), data.data(), data.size());
j3 141:cf38f48a2a49 114 owmResult = master().OWWriteBlock(sendBlock, sizeof(sendBlock) / sizeof(sendBlock[0]));
j3 141:cf38f48a2a49 115 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 116 {
j3 141:cf38f48a2a49 117 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 118 }
j3 141:cf38f48a2a49 119 uint8_t recvbyte;
j3 141:cf38f48a2a49 120 owmResult = master().OWReadByte(recvbyte);
j3 141:cf38f48a2a49 121 if (owmResult != OneWireMaster::Success)
j3 115:a1ca2f3bf46d 122 {
j3 141:cf38f48a2a49 123 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 124 }
j3 141:cf38f48a2a49 125 uint16_t invCRC16 = recvbyte;
j3 141:cf38f48a2a49 126 owmResult = master().OWReadByte(recvbyte);
j3 141:cf38f48a2a49 127 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 128 {
j3 141:cf38f48a2a49 129 return OneWireSlave::CommunicationError;
j3 115:a1ca2f3bf46d 130 }
j3 141:cf38f48a2a49 131 invCRC16 |= (recvbyte << 8);
j3 141:cf38f48a2a49 132 //calc our own inverted CRC16 to compare with one returned
j3 141:cf38f48a2a49 133 uint16_t calculatedInvCRC16 = ~calculateCrc16(sendBlock, sizeof(sendBlock) / sizeof(sendBlock[0]));
j3 141:cf38f48a2a49 134 if (invCRC16 != calculatedInvCRC16)
j3 141:cf38f48a2a49 135 {
j3 141:cf38f48a2a49 136 return OneWireSlave::CrcError;
j3 141:cf38f48a2a49 137 }
j3 141:cf38f48a2a49 138 // else
j3 141:cf38f48a2a49 139 return OneWireSlave::Success;
j3 115:a1ca2f3bf46d 140 }
j3 115:a1ca2f3bf46d 141
j3 115:a1ca2f3bf46d 142 //*********************************************************************
j3 141:cf38f48a2a49 143 OneWireSlave::CmdResult DS2431::readScratchpad(Scratchpad & data, uint8_t & esByte)
j3 141:cf38f48a2a49 144 {
j3 115:a1ca2f3bf46d 145 OneWireMaster::CmdResult owmResult = selectDevice();
j3 141:cf38f48a2a49 146 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 147 {
j3 141:cf38f48a2a49 148 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 149 }
j3 141:cf38f48a2a49 150 owmResult = master().OWWriteByte(ReadScratchpad);
j3 141:cf38f48a2a49 151 if (owmResult != OneWireMaster::Success)
j3 115:a1ca2f3bf46d 152 {
j3 141:cf38f48a2a49 153 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 154 }
j3 141:cf38f48a2a49 155 uint8_t recvBlock[13];
j3 141:cf38f48a2a49 156 const size_t recvBlockSize = sizeof(recvBlock) / sizeof(recvBlock[0]);
j3 141:cf38f48a2a49 157 owmResult = master().OWReadBlock(recvBlock, recvBlockSize);
j3 141:cf38f48a2a49 158 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 159 {
j3 141:cf38f48a2a49 160 return OneWireSlave::CommunicationError;
j3 115:a1ca2f3bf46d 161 }
j3 141:cf38f48a2a49 162 uint16_t invCRC16 = ((recvBlock[recvBlockSize - 1] << 8) | recvBlock[recvBlockSize - 2]);
j3 141:cf38f48a2a49 163 // Shift contents down
j3 141:cf38f48a2a49 164 std::memmove(recvBlock + 1, recvBlock, recvBlockSize - 1);
j3 141:cf38f48a2a49 165 recvBlock[0] = ReadScratchpad;
j3 141:cf38f48a2a49 166 //calc our own inverted CRC16 to compare with one returned
j3 141:cf38f48a2a49 167 uint16_t calculatedInvCRC16 = ~calculateCrc16(recvBlock, recvBlockSize - 1);
j3 141:cf38f48a2a49 168 if (invCRC16 != calculatedInvCRC16)
j3 141:cf38f48a2a49 169 {
j3 141:cf38f48a2a49 170 return OneWireSlave::CrcError;
j3 141:cf38f48a2a49 171 }
j3 141:cf38f48a2a49 172 esByte = recvBlock[3];
j3 141:cf38f48a2a49 173 std::memcpy(data.data(), (recvBlock + 4), data.size());
j3 141:cf38f48a2a49 174 return OneWireSlave::Success;
j3 115:a1ca2f3bf46d 175 }
j3 115:a1ca2f3bf46d 176
j3 115:a1ca2f3bf46d 177 //*********************************************************************
j3 141:cf38f48a2a49 178 OneWireSlave::CmdResult DS2431::copyScratchpad(Address targetAddress, uint8_t esByte)
j3 141:cf38f48a2a49 179 {
j3 115:a1ca2f3bf46d 180 OneWireMaster::CmdResult owmResult = selectDevice();
j3 141:cf38f48a2a49 181 if(owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 182 {
j3 141:cf38f48a2a49 183 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 184 }
j3 141:cf38f48a2a49 185 uint8_t sendBlock[] = { CopyScratchpad, static_cast<uint8_t>(targetAddress), static_cast<uint8_t>(targetAddress >> 8) };
j3 141:cf38f48a2a49 186 owmResult = master().OWWriteBlock(sendBlock, sizeof(sendBlock) / sizeof(sendBlock[0]));
j3 141:cf38f48a2a49 187 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 188 {
j3 141:cf38f48a2a49 189 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 190 }
j3 141:cf38f48a2a49 191 owmResult = master().OWWriteByteSetLevel(esByte, OneWireMaster::StrongLevel);
j3 141:cf38f48a2a49 192 if (owmResult != OneWireMaster::Success)
j3 115:a1ca2f3bf46d 193 {
j3 141:cf38f48a2a49 194 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 195 }
j3 141:cf38f48a2a49 196 wait_ms(10);
j3 141:cf38f48a2a49 197 owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);
j3 141:cf38f48a2a49 198 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 199 {
j3 141:cf38f48a2a49 200 return OneWireSlave::CommunicationError;
j3 115:a1ca2f3bf46d 201 }
j3 141:cf38f48a2a49 202 uint8_t check;
j3 141:cf38f48a2a49 203 owmResult = master().OWReadByte(check);
j3 141:cf38f48a2a49 204 if (owmResult != OneWireMaster::Success)
j3 141:cf38f48a2a49 205 {
j3 141:cf38f48a2a49 206 return OneWireSlave::CommunicationError;
j3 141:cf38f48a2a49 207 }
j3 141:cf38f48a2a49 208 if (check != 0xAA)
j3 141:cf38f48a2a49 209 {
j3 141:cf38f48a2a49 210 return OneWireSlave::OperationFailure;
j3 141:cf38f48a2a49 211 }
j3 141:cf38f48a2a49 212 // else
j3 141:cf38f48a2a49 213 return OneWireSlave::Success;
j3 115:a1ca2f3bf46d 214 }