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:
Tue Sep 06 10:21:37 2016 -0500
Revision:
121:4bee07064d0d
Parent:
116:8058bb54e959
Child:
139:f0e0a7976846
Fixed potential issues flagged by GCC.

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 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 116:8058bb54e959 53 OneWireSlave::CmdResult DS2431::writeMemory(uint16_t targetAddress, const uint8_t *data, uint8_t numBytes)
j3 116:8058bb54e959 54 {
j3 116:8058bb54e959 55 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 116:8058bb54e959 56
j3 116:8058bb54e959 57 if((targetAddress + numBytes) <= 0x88)
j3 116:8058bb54e959 58 {
j3 116:8058bb54e959 59 result = OneWireSlave::Success;
j3 116:8058bb54e959 60
j3 116:8058bb54e959 61 uint8_t startOffset = (targetAddress & 0x0007);
j3 116:8058bb54e959 62 uint16_t startRowAddress = (targetAddress & 0xFFF8);
j3 116:8058bb54e959 63 uint8_t endOffset = ((targetAddress + numBytes) & 0x0007);
j3 116:8058bb54e959 64 uint16_t endRowAddress = ((targetAddress + numBytes) & 0xFFF8);
j3 116:8058bb54e959 65 const uint8_t *dataIdx = data;
j3 116:8058bb54e959 66
j3 116:8058bb54e959 67 DS2431::Scratchpad scratchpadData;
j3 116:8058bb54e959 68 uint8_t esByte;
j3 116:8058bb54e959 69
j3 116:8058bb54e959 70 if(startOffset != 0)
j3 116:8058bb54e959 71 {
j3 116:8058bb54e959 72 result = this->readMemory(startRowAddress, scratchpadData, 8);
j3 116:8058bb54e959 73 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 74 {
j3 116:8058bb54e959 75 std::memcpy((scratchpadData + startOffset), data, (8 - startOffset));
j3 116:8058bb54e959 76 result = this->writeScratchpad(startRowAddress, scratchpadData);
j3 116:8058bb54e959 77 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 78 {
j3 116:8058bb54e959 79 result = this->readScratchpad(scratchpadData , esByte);
j3 116:8058bb54e959 80 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 81 {
j3 116:8058bb54e959 82 result = this->copyScratchpad(startRowAddress, esByte);
j3 116:8058bb54e959 83 startRowAddress += 8;
j3 116:8058bb54e959 84 dataIdx = (data + (8 - startOffset));
j3 116:8058bb54e959 85 }
j3 116:8058bb54e959 86 }
j3 116:8058bb54e959 87 }
j3 116:8058bb54e959 88 }
j3 116:8058bb54e959 89
j3 116:8058bb54e959 90 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 91 {
j3 116:8058bb54e959 92 for(uint16_t row = startRowAddress; row < endRowAddress; row += 8)
j3 116:8058bb54e959 93 {
j3 116:8058bb54e959 94 std::memcpy(scratchpadData, dataIdx, 8);
j3 116:8058bb54e959 95
j3 116:8058bb54e959 96 result = this->writeScratchpad(row, scratchpadData);
j3 116:8058bb54e959 97 if(result != OneWireSlave::Success)
j3 116:8058bb54e959 98 {
j3 116:8058bb54e959 99 break;
j3 116:8058bb54e959 100 }
j3 116:8058bb54e959 101
j3 116:8058bb54e959 102 result = this->readScratchpad(scratchpadData, esByte);
j3 116:8058bb54e959 103 if(result != OneWireSlave::Success)
j3 116:8058bb54e959 104 {
j3 116:8058bb54e959 105 break;
j3 116:8058bb54e959 106 }
j3 116:8058bb54e959 107
j3 116:8058bb54e959 108 result = this->copyScratchpad(row, esByte);
j3 116:8058bb54e959 109 if(result != OneWireSlave::Success)
j3 116:8058bb54e959 110 {
j3 116:8058bb54e959 111 break;
j3 116:8058bb54e959 112 }
j3 116:8058bb54e959 113
j3 116:8058bb54e959 114 dataIdx += 8;
j3 116:8058bb54e959 115 }
j3 116:8058bb54e959 116 }
j3 116:8058bb54e959 117
j3 116:8058bb54e959 118 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 119 {
j3 116:8058bb54e959 120 if(endOffset != 0)
j3 116:8058bb54e959 121 {
j3 116:8058bb54e959 122 result = this->readMemory(endRowAddress, scratchpadData, 8);
j3 116:8058bb54e959 123 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 124 {
j3 116:8058bb54e959 125 std::memcpy(scratchpadData, dataIdx, endOffset);
j3 116:8058bb54e959 126 result = this->writeScratchpad(endRowAddress, scratchpadData);
j3 116:8058bb54e959 127 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 128 {
j3 116:8058bb54e959 129 result = this->readScratchpad(scratchpadData, esByte);
j3 116:8058bb54e959 130 if(result == OneWireSlave::Success)
j3 116:8058bb54e959 131 {
j3 116:8058bb54e959 132 result = this->copyScratchpad(endRowAddress, esByte);
j3 116:8058bb54e959 133 }
j3 116:8058bb54e959 134 }
j3 116:8058bb54e959 135 }
j3 116:8058bb54e959 136 }
j3 116:8058bb54e959 137 }
j3 116:8058bb54e959 138 }
j3 116:8058bb54e959 139
j3 116:8058bb54e959 140 return result;
j3 116:8058bb54e959 141 }
j3 116:8058bb54e959 142
j3 116:8058bb54e959 143 //*********************************************************************
j3 116:8058bb54e959 144 OneWireSlave::CmdResult DS2431::readMemory(uint16_t targetAddress, uint8_t *data, uint8_t numBytes)
j3 116:8058bb54e959 145 {
j3 116:8058bb54e959 146 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 116:8058bb54e959 147
j3 116:8058bb54e959 148 if((targetAddress + numBytes) <= 0x88)
j3 116:8058bb54e959 149 {
j3 116:8058bb54e959 150 OneWireMaster::CmdResult owmResult = selectDevice();
j3 116:8058bb54e959 151 if(owmResult == OneWireMaster::Success)
j3 116:8058bb54e959 152 {
IanBenzMaxim 121:4bee07064d0d 153 uint8_t sendBlock[] = { READ_MEMORY, static_cast<uint8_t>(targetAddress), static_cast<uint8_t>(targetAddress >> 8) };
j3 116:8058bb54e959 154 owmResult = master().OWWriteBlock(sendBlock, 3);
j3 116:8058bb54e959 155 if(owmResult == OneWireMaster::Success)
j3 116:8058bb54e959 156 {
j3 116:8058bb54e959 157 owmResult = master().OWReadBlock(data, numBytes);
j3 116:8058bb54e959 158 if(owmResult == OneWireMaster::Success)
j3 116:8058bb54e959 159 {
j3 116:8058bb54e959 160 result = OneWireSlave::Success;
j3 116:8058bb54e959 161 }
j3 116:8058bb54e959 162 }
j3 116:8058bb54e959 163 }
j3 116:8058bb54e959 164 }
j3 116:8058bb54e959 165
j3 116:8058bb54e959 166 return result;
j3 116:8058bb54e959 167 }
j3 116:8058bb54e959 168
j3 116:8058bb54e959 169 //*********************************************************************
j3 116:8058bb54e959 170 OneWireSlave::CmdResult DS2431::writeScratchpad(uint16_t targetAddress, const Scratchpad &data)
j3 115:a1ca2f3bf46d 171 {
j3 115:a1ca2f3bf46d 172 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 115:a1ca2f3bf46d 173
j3 115:a1ca2f3bf46d 174 OneWireMaster::CmdResult owmResult = selectDevice();
j3 115:a1ca2f3bf46d 175 if(owmResult == OneWireMaster::Success)
j3 115:a1ca2f3bf46d 176 {
j3 115:a1ca2f3bf46d 177 uint8_t sendBlock[11];
j3 115:a1ca2f3bf46d 178 sendBlock[0] = WRITE_SCRATCHPAD;
j3 115:a1ca2f3bf46d 179 sendBlock[1] = (targetAddress &0xFF);
j3 115:a1ca2f3bf46d 180 sendBlock[2] = ((targetAddress >> 8) &0xFF);
j3 115:a1ca2f3bf46d 181 std::memcpy((sendBlock + 3), data, 8);
j3 115:a1ca2f3bf46d 182
j3 115:a1ca2f3bf46d 183 owmResult = master().OWWriteBlock(sendBlock, 11);
j3 115:a1ca2f3bf46d 184
j3 115:a1ca2f3bf46d 185 uint16_t invCRC16;
j3 115:a1ca2f3bf46d 186 uint8_t recvbyte;
j3 115:a1ca2f3bf46d 187 master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel);
j3 115:a1ca2f3bf46d 188 invCRC16 = recvbyte;
j3 115:a1ca2f3bf46d 189 master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel);
j3 115:a1ca2f3bf46d 190 invCRC16 |= (recvbyte << 8);
j3 115:a1ca2f3bf46d 191
j3 115:a1ca2f3bf46d 192 //calc our own inverted CRC16 to compare with one returned
j3 115:a1ca2f3bf46d 193 uint16_t calculatedInvCRC16 = ~calculateCrc16(sendBlock, 0, 11);
j3 115:a1ca2f3bf46d 194
j3 115:a1ca2f3bf46d 195 if(invCRC16 == calculatedInvCRC16)
j3 115:a1ca2f3bf46d 196 {
j3 115:a1ca2f3bf46d 197 result = OneWireSlave::Success;
j3 115:a1ca2f3bf46d 198 }
j3 115:a1ca2f3bf46d 199 }
j3 115:a1ca2f3bf46d 200
j3 115:a1ca2f3bf46d 201 return result;
j3 115:a1ca2f3bf46d 202 }
j3 115:a1ca2f3bf46d 203
j3 115:a1ca2f3bf46d 204 //*********************************************************************
j3 116:8058bb54e959 205 OneWireSlave::CmdResult DS2431::readScratchpad(Scratchpad &data, uint8_t &esByte)
j3 115:a1ca2f3bf46d 206 {
j3 115:a1ca2f3bf46d 207 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 115:a1ca2f3bf46d 208
j3 115:a1ca2f3bf46d 209 OneWireMaster::CmdResult owmResult = selectDevice();
j3 115:a1ca2f3bf46d 210 if(owmResult == OneWireMaster::Success)
j3 115:a1ca2f3bf46d 211 {
j3 115:a1ca2f3bf46d 212 owmResult = master().OWWriteByteSetLevel(READ_SCRATCHPAD, OneWireMaster::NormalLevel);
j3 115:a1ca2f3bf46d 213 if(owmResult == OneWireMaster::Success)
j3 115:a1ca2f3bf46d 214 {
j3 115:a1ca2f3bf46d 215 uint8_t recvBlock[13];
j3 115:a1ca2f3bf46d 216 owmResult = master().OWReadBlock(recvBlock, 13);
j3 115:a1ca2f3bf46d 217
j3 115:a1ca2f3bf46d 218 uint16_t invCRC16 = ((recvBlock[12] << 8) | recvBlock[11]);
j3 115:a1ca2f3bf46d 219
j3 115:a1ca2f3bf46d 220 uint8_t idx = 12;
j3 115:a1ca2f3bf46d 221 while(--idx)
j3 115:a1ca2f3bf46d 222 {
j3 115:a1ca2f3bf46d 223 recvBlock[idx] = recvBlock[idx - 1];
j3 115:a1ca2f3bf46d 224 }
j3 115:a1ca2f3bf46d 225 recvBlock[0] = READ_SCRATCHPAD;
j3 115:a1ca2f3bf46d 226
j3 115:a1ca2f3bf46d 227 //calc our own inverted CRC16 to compare with one returned
j3 115:a1ca2f3bf46d 228 uint16_t calculatedInvCRC16 = ~calculateCrc16(recvBlock, 0, 12);
j3 115:a1ca2f3bf46d 229
j3 115:a1ca2f3bf46d 230 if(invCRC16 == calculatedInvCRC16)
j3 115:a1ca2f3bf46d 231 {
j3 116:8058bb54e959 232 esByte = recvBlock[3];
j3 116:8058bb54e959 233 std::memcpy(data, (recvBlock + 4), 8);
j3 115:a1ca2f3bf46d 234 result = OneWireSlave::Success;
j3 115:a1ca2f3bf46d 235 }
j3 115:a1ca2f3bf46d 236 }
j3 115:a1ca2f3bf46d 237 }
j3 115:a1ca2f3bf46d 238
j3 115:a1ca2f3bf46d 239 return result;
j3 115:a1ca2f3bf46d 240 }
j3 115:a1ca2f3bf46d 241
j3 115:a1ca2f3bf46d 242 //*********************************************************************
j3 116:8058bb54e959 243 OneWireSlave::CmdResult DS2431::copyScratchpad(uint16_t targetAddress, uint8_t esByte)
j3 115:a1ca2f3bf46d 244 {
j3 115:a1ca2f3bf46d 245 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 115:a1ca2f3bf46d 246
j3 115:a1ca2f3bf46d 247 OneWireMaster::CmdResult owmResult = selectDevice();
j3 115:a1ca2f3bf46d 248 if(owmResult == OneWireMaster::Success)
j3 115:a1ca2f3bf46d 249 {
IanBenzMaxim 121:4bee07064d0d 250 uint8_t sendBlock[] = { COPY_SCRATCHPAD, static_cast<uint8_t>(targetAddress), static_cast<uint8_t>(targetAddress >> 8), esByte };
j3 115:a1ca2f3bf46d 251 owmResult = master().OWWriteBlock(sendBlock, 4);
j3 115:a1ca2f3bf46d 252
j3 115:a1ca2f3bf46d 253 master().OWSetLevel(OneWireMaster::StrongLevel);
j3 115:a1ca2f3bf46d 254 wait_ms(10);
j3 115:a1ca2f3bf46d 255 master().OWSetLevel(OneWireMaster::NormalLevel);
j3 115:a1ca2f3bf46d 256
j3 115:a1ca2f3bf46d 257 uint8_t check;
j3 115:a1ca2f3bf46d 258 master().OWReadByteSetLevel(check, OneWireMaster::NormalLevel);
j3 115:a1ca2f3bf46d 259 if(check == 0xAA)
j3 115:a1ca2f3bf46d 260 {
j3 115:a1ca2f3bf46d 261 result = OneWireSlave::Success;
j3 115:a1ca2f3bf46d 262 }
j3 115:a1ca2f3bf46d 263 }
j3 115:a1ca2f3bf46d 264
j3 115:a1ca2f3bf46d 265 return result;
j3 115:a1ca2f3bf46d 266 }