Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Slaves/Memory/DS2431/DS2431.cpp@115:a1ca2f3bf46d, 2016-08-22 (annotated)
- Committer:
- j3
- Date:
- Mon Aug 22 05:38:20 2016 +0000
- Revision:
- 115:a1ca2f3bf46d
- Parent:
- 110:a3b5e2a4fdf2
- Child:
- 116:8058bb54e959
Added support for DS2431
Who changed what in which revision?
User | Revision | Line number | New 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 | 115:a1ca2f3bf46d | 39 | enum DS2431_CMDS |
j3 | 115:a1ca2f3bf46d | 40 | { |
j3 | 115:a1ca2f3bf46d | 41 | WRITE_SCRATCHPAD = 0x0F, |
j3 | 115:a1ca2f3bf46d | 42 | READ_SCRATCHPAD = 0xAA, |
j3 | 115:a1ca2f3bf46d | 43 | COPY_SCRATCHPAD = 0x55, |
j3 | 115:a1ca2f3bf46d | 44 | READ_MEMORY = 0xF0 |
j3 | 115:a1ca2f3bf46d | 45 | }; |
j3 | 115:a1ca2f3bf46d | 46 | |
j3 | 115:a1ca2f3bf46d | 47 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 48 | DS2431::DS2431(RandomAccessRomIterator &selector):OneWireSlave(selector) |
j3 | 115:a1ca2f3bf46d | 49 | { |
j3 | 115:a1ca2f3bf46d | 50 | } |
j3 | 115:a1ca2f3bf46d | 51 | |
j3 | 115:a1ca2f3bf46d | 52 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 53 | OneWireSlave::CmdResult DS2431::writeScratchPad(uint16_t targetAddress, uint8_t *data) |
j3 | 115:a1ca2f3bf46d | 54 | { |
j3 | 115:a1ca2f3bf46d | 55 | OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; |
j3 | 115:a1ca2f3bf46d | 56 | |
j3 | 115:a1ca2f3bf46d | 57 | OneWireMaster::CmdResult owmResult = selectDevice(); |
j3 | 115:a1ca2f3bf46d | 58 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 59 | { |
j3 | 115:a1ca2f3bf46d | 60 | uint8_t sendBlock[11]; |
j3 | 115:a1ca2f3bf46d | 61 | sendBlock[0] = WRITE_SCRATCHPAD; |
j3 | 115:a1ca2f3bf46d | 62 | sendBlock[1] = (targetAddress &0xFF); |
j3 | 115:a1ca2f3bf46d | 63 | sendBlock[2] = ((targetAddress >> 8) &0xFF); |
j3 | 115:a1ca2f3bf46d | 64 | std::memcpy((sendBlock + 3), data, 8); |
j3 | 115:a1ca2f3bf46d | 65 | |
j3 | 115:a1ca2f3bf46d | 66 | owmResult = master().OWWriteBlock(sendBlock, 11); |
j3 | 115:a1ca2f3bf46d | 67 | |
j3 | 115:a1ca2f3bf46d | 68 | uint16_t invCRC16; |
j3 | 115:a1ca2f3bf46d | 69 | uint8_t recvbyte; |
j3 | 115:a1ca2f3bf46d | 70 | master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel); |
j3 | 115:a1ca2f3bf46d | 71 | invCRC16 = recvbyte; |
j3 | 115:a1ca2f3bf46d | 72 | master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel); |
j3 | 115:a1ca2f3bf46d | 73 | invCRC16 |= (recvbyte << 8); |
j3 | 115:a1ca2f3bf46d | 74 | |
j3 | 115:a1ca2f3bf46d | 75 | //calc our own inverted CRC16 to compare with one returned |
j3 | 115:a1ca2f3bf46d | 76 | uint16_t calculatedInvCRC16 = ~calculateCrc16(sendBlock, 0, 11); |
j3 | 115:a1ca2f3bf46d | 77 | |
j3 | 115:a1ca2f3bf46d | 78 | if(invCRC16 == calculatedInvCRC16) |
j3 | 115:a1ca2f3bf46d | 79 | { |
j3 | 115:a1ca2f3bf46d | 80 | result = OneWireSlave::Success; |
j3 | 115:a1ca2f3bf46d | 81 | } |
j3 | 115:a1ca2f3bf46d | 82 | } |
j3 | 115:a1ca2f3bf46d | 83 | |
j3 | 115:a1ca2f3bf46d | 84 | return result; |
j3 | 115:a1ca2f3bf46d | 85 | } |
j3 | 115:a1ca2f3bf46d | 86 | |
j3 | 115:a1ca2f3bf46d | 87 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 88 | OneWireSlave::CmdResult DS2431::readScratchPad(uint8_t *data) |
j3 | 115:a1ca2f3bf46d | 89 | { |
j3 | 115:a1ca2f3bf46d | 90 | OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; |
j3 | 115:a1ca2f3bf46d | 91 | |
j3 | 115:a1ca2f3bf46d | 92 | OneWireMaster::CmdResult owmResult = selectDevice(); |
j3 | 115:a1ca2f3bf46d | 93 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 94 | { |
j3 | 115:a1ca2f3bf46d | 95 | owmResult = master().OWWriteByteSetLevel(READ_SCRATCHPAD, OneWireMaster::NormalLevel); |
j3 | 115:a1ca2f3bf46d | 96 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 97 | { |
j3 | 115:a1ca2f3bf46d | 98 | uint8_t recvBlock[13]; |
j3 | 115:a1ca2f3bf46d | 99 | owmResult = master().OWReadBlock(recvBlock, 13); |
j3 | 115:a1ca2f3bf46d | 100 | |
j3 | 115:a1ca2f3bf46d | 101 | uint16_t invCRC16 = ((recvBlock[12] << 8) | recvBlock[11]); |
j3 | 115:a1ca2f3bf46d | 102 | |
j3 | 115:a1ca2f3bf46d | 103 | uint8_t idx = 12; |
j3 | 115:a1ca2f3bf46d | 104 | while(--idx) |
j3 | 115:a1ca2f3bf46d | 105 | { |
j3 | 115:a1ca2f3bf46d | 106 | recvBlock[idx] = recvBlock[idx - 1]; |
j3 | 115:a1ca2f3bf46d | 107 | } |
j3 | 115:a1ca2f3bf46d | 108 | recvBlock[0] = READ_SCRATCHPAD; |
j3 | 115:a1ca2f3bf46d | 109 | |
j3 | 115:a1ca2f3bf46d | 110 | //calc our own inverted CRC16 to compare with one returned |
j3 | 115:a1ca2f3bf46d | 111 | uint16_t calculatedInvCRC16 = ~calculateCrc16(recvBlock, 0, 12); |
j3 | 115:a1ca2f3bf46d | 112 | |
j3 | 115:a1ca2f3bf46d | 113 | if(invCRC16 == calculatedInvCRC16) |
j3 | 115:a1ca2f3bf46d | 114 | { |
j3 | 115:a1ca2f3bf46d | 115 | std::memcpy(data, (recvBlock + 1), 11); |
j3 | 115:a1ca2f3bf46d | 116 | result = OneWireSlave::Success; |
j3 | 115:a1ca2f3bf46d | 117 | } |
j3 | 115:a1ca2f3bf46d | 118 | } |
j3 | 115:a1ca2f3bf46d | 119 | } |
j3 | 115:a1ca2f3bf46d | 120 | |
j3 | 115:a1ca2f3bf46d | 121 | return result; |
j3 | 115:a1ca2f3bf46d | 122 | } |
j3 | 115:a1ca2f3bf46d | 123 | |
j3 | 115:a1ca2f3bf46d | 124 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 125 | OneWireSlave::CmdResult DS2431::copyScratchPad(uint16_t targetAddress, uint8_t esByte) |
j3 | 115:a1ca2f3bf46d | 126 | { |
j3 | 115:a1ca2f3bf46d | 127 | OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; |
j3 | 115:a1ca2f3bf46d | 128 | |
j3 | 115:a1ca2f3bf46d | 129 | OneWireMaster::CmdResult owmResult = selectDevice(); |
j3 | 115:a1ca2f3bf46d | 130 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 131 | { |
j3 | 115:a1ca2f3bf46d | 132 | uint8_t sendBlock[] = {COPY_SCRATCHPAD, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF), esByte}; |
j3 | 115:a1ca2f3bf46d | 133 | owmResult = master().OWWriteBlock(sendBlock, 4); |
j3 | 115:a1ca2f3bf46d | 134 | |
j3 | 115:a1ca2f3bf46d | 135 | master().OWSetLevel(OneWireMaster::StrongLevel); |
j3 | 115:a1ca2f3bf46d | 136 | wait_ms(10); |
j3 | 115:a1ca2f3bf46d | 137 | master().OWSetLevel(OneWireMaster::NormalLevel); |
j3 | 115:a1ca2f3bf46d | 138 | |
j3 | 115:a1ca2f3bf46d | 139 | uint8_t check; |
j3 | 115:a1ca2f3bf46d | 140 | master().OWReadByteSetLevel(check, OneWireMaster::NormalLevel); |
j3 | 115:a1ca2f3bf46d | 141 | if(check == 0xAA) |
j3 | 115:a1ca2f3bf46d | 142 | { |
j3 | 115:a1ca2f3bf46d | 143 | result = OneWireSlave::Success; |
j3 | 115:a1ca2f3bf46d | 144 | } |
j3 | 115:a1ca2f3bf46d | 145 | } |
j3 | 115:a1ca2f3bf46d | 146 | |
j3 | 115:a1ca2f3bf46d | 147 | return result; |
j3 | 115:a1ca2f3bf46d | 148 | } |
j3 | 115:a1ca2f3bf46d | 149 | |
j3 | 115:a1ca2f3bf46d | 150 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 151 | OneWireSlave::CmdResult DS2431::writeBlock(uint16_t targetAddress, uint8_t *data, uint8_t numBytes) |
j3 | 115:a1ca2f3bf46d | 152 | { |
j3 | 115:a1ca2f3bf46d | 153 | OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; |
j3 | 115:a1ca2f3bf46d | 154 | |
j3 | 115:a1ca2f3bf46d | 155 | if((targetAddress + numBytes) <= 0x88) |
j3 | 115:a1ca2f3bf46d | 156 | { |
j3 | 115:a1ca2f3bf46d | 157 | result = OneWireSlave::Success; |
j3 | 115:a1ca2f3bf46d | 158 | |
j3 | 115:a1ca2f3bf46d | 159 | uint8_t startOffset = (targetAddress & 0x0007); |
j3 | 115:a1ca2f3bf46d | 160 | uint16_t startRowAddress = (targetAddress & 0xFFF8); |
j3 | 115:a1ca2f3bf46d | 161 | uint8_t endOffset = ((targetAddress + numBytes) & 0x0007); |
j3 | 115:a1ca2f3bf46d | 162 | uint16_t endRowAddress = ((targetAddress + numBytes) & 0xFFF8); |
j3 | 115:a1ca2f3bf46d | 163 | uint8_t *dataIdx = data; |
j3 | 115:a1ca2f3bf46d | 164 | |
j3 | 115:a1ca2f3bf46d | 165 | uint8_t localData[12]; |
j3 | 115:a1ca2f3bf46d | 166 | |
j3 | 115:a1ca2f3bf46d | 167 | if(startOffset != 0) |
j3 | 115:a1ca2f3bf46d | 168 | { |
j3 | 115:a1ca2f3bf46d | 169 | result = this->readBlock(startRowAddress, localData, 8); |
j3 | 115:a1ca2f3bf46d | 170 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 171 | { |
j3 | 115:a1ca2f3bf46d | 172 | std::memcpy((localData + startOffset), data, (8 - startOffset)); |
j3 | 115:a1ca2f3bf46d | 173 | result = this->writeScratchPad(startRowAddress, localData); |
j3 | 115:a1ca2f3bf46d | 174 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 175 | { |
j3 | 115:a1ca2f3bf46d | 176 | result = this->readScratchPad(localData); |
j3 | 115:a1ca2f3bf46d | 177 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 178 | { |
j3 | 115:a1ca2f3bf46d | 179 | result = this->copyScratchPad(startRowAddress, localData[2]); |
j3 | 115:a1ca2f3bf46d | 180 | startRowAddress += 8; |
j3 | 115:a1ca2f3bf46d | 181 | dataIdx = (data + (8 - startOffset)); |
j3 | 115:a1ca2f3bf46d | 182 | } |
j3 | 115:a1ca2f3bf46d | 183 | } |
j3 | 115:a1ca2f3bf46d | 184 | } |
j3 | 115:a1ca2f3bf46d | 185 | } |
j3 | 115:a1ca2f3bf46d | 186 | |
j3 | 115:a1ca2f3bf46d | 187 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 188 | { |
j3 | 115:a1ca2f3bf46d | 189 | for(uint16_t row = startRowAddress; row < endRowAddress; row += 8) |
j3 | 115:a1ca2f3bf46d | 190 | { |
j3 | 115:a1ca2f3bf46d | 191 | std::memcpy(localData, dataIdx, 8); |
j3 | 115:a1ca2f3bf46d | 192 | |
j3 | 115:a1ca2f3bf46d | 193 | result = this->writeScratchPad(row, localData); |
j3 | 115:a1ca2f3bf46d | 194 | if(result != OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 195 | { |
j3 | 115:a1ca2f3bf46d | 196 | break; |
j3 | 115:a1ca2f3bf46d | 197 | } |
j3 | 115:a1ca2f3bf46d | 198 | |
j3 | 115:a1ca2f3bf46d | 199 | result = this->readScratchPad(localData); |
j3 | 115:a1ca2f3bf46d | 200 | if(result != OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 201 | { |
j3 | 115:a1ca2f3bf46d | 202 | break; |
j3 | 115:a1ca2f3bf46d | 203 | } |
j3 | 115:a1ca2f3bf46d | 204 | |
j3 | 115:a1ca2f3bf46d | 205 | result = this->copyScratchPad(row, localData[2]); |
j3 | 115:a1ca2f3bf46d | 206 | if(result != OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 207 | { |
j3 | 115:a1ca2f3bf46d | 208 | break; |
j3 | 115:a1ca2f3bf46d | 209 | } |
j3 | 115:a1ca2f3bf46d | 210 | |
j3 | 115:a1ca2f3bf46d | 211 | dataIdx += 8; |
j3 | 115:a1ca2f3bf46d | 212 | } |
j3 | 115:a1ca2f3bf46d | 213 | } |
j3 | 115:a1ca2f3bf46d | 214 | |
j3 | 115:a1ca2f3bf46d | 215 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 216 | { |
j3 | 115:a1ca2f3bf46d | 217 | if(endOffset != 0) |
j3 | 115:a1ca2f3bf46d | 218 | { |
j3 | 115:a1ca2f3bf46d | 219 | result = this->readBlock(endRowAddress, localData, 8); |
j3 | 115:a1ca2f3bf46d | 220 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 221 | { |
j3 | 115:a1ca2f3bf46d | 222 | std::memcpy(localData, dataIdx, endOffset); |
j3 | 115:a1ca2f3bf46d | 223 | result = this->writeScratchPad(endRowAddress, localData); |
j3 | 115:a1ca2f3bf46d | 224 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 225 | { |
j3 | 115:a1ca2f3bf46d | 226 | result = this->readScratchPad(localData); |
j3 | 115:a1ca2f3bf46d | 227 | if(result == OneWireSlave::Success) |
j3 | 115:a1ca2f3bf46d | 228 | { |
j3 | 115:a1ca2f3bf46d | 229 | result = this->copyScratchPad(endRowAddress, localData[2]); |
j3 | 115:a1ca2f3bf46d | 230 | } |
j3 | 115:a1ca2f3bf46d | 231 | } |
j3 | 115:a1ca2f3bf46d | 232 | } |
j3 | 115:a1ca2f3bf46d | 233 | } |
j3 | 115:a1ca2f3bf46d | 234 | } |
j3 | 115:a1ca2f3bf46d | 235 | } |
j3 | 115:a1ca2f3bf46d | 236 | |
j3 | 115:a1ca2f3bf46d | 237 | return result; |
j3 | 115:a1ca2f3bf46d | 238 | } |
j3 | 115:a1ca2f3bf46d | 239 | |
j3 | 115:a1ca2f3bf46d | 240 | //********************************************************************* |
j3 | 115:a1ca2f3bf46d | 241 | OneWireSlave::CmdResult DS2431::readBlock(uint16_t targetAddress, uint8_t *data, uint8_t numBytes) |
j3 | 115:a1ca2f3bf46d | 242 | { |
j3 | 115:a1ca2f3bf46d | 243 | OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; |
j3 | 115:a1ca2f3bf46d | 244 | |
j3 | 115:a1ca2f3bf46d | 245 | if((targetAddress + numBytes) <= 0x88) |
j3 | 115:a1ca2f3bf46d | 246 | { |
j3 | 115:a1ca2f3bf46d | 247 | OneWireMaster::CmdResult owmResult = selectDevice(); |
j3 | 115:a1ca2f3bf46d | 248 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 249 | { |
j3 | 115:a1ca2f3bf46d | 250 | uint8_t sendBlock[] = {READ_MEMORY, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF)}; |
j3 | 115:a1ca2f3bf46d | 251 | owmResult = master().OWWriteBlock(sendBlock, 3); |
j3 | 115:a1ca2f3bf46d | 252 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 253 | { |
j3 | 115:a1ca2f3bf46d | 254 | owmResult = master().OWReadBlock(data, numBytes); |
j3 | 115:a1ca2f3bf46d | 255 | if(owmResult == OneWireMaster::Success) |
j3 | 115:a1ca2f3bf46d | 256 | { |
j3 | 115:a1ca2f3bf46d | 257 | result = OneWireSlave::Success; |
j3 | 115:a1ca2f3bf46d | 258 | } |
j3 | 115:a1ca2f3bf46d | 259 | } |
j3 | 115:a1ca2f3bf46d | 260 | } |
j3 | 115:a1ca2f3bf46d | 261 | } |
j3 | 115:a1ca2f3bf46d | 262 | |
j3 | 115:a1ca2f3bf46d | 263 | return result; |
j3 | 115:a1ca2f3bf46d | 264 | } |