Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RomCommands.cpp Source File

RomCommands.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #include "RomCommands.hpp"
00034 
00035 namespace MaximInterface {
00036 
00037 enum RomCmd {
00038   ReadRomCmd = 0x33,
00039   MatchRomCmd = 0x55,
00040   SearchRomCmd = 0xF0,
00041   SkipRomCmd = 0xCC,
00042   ResumeRomCmd = 0xA5,
00043   OverdriveSkipRomCmd = 0x3C,
00044   OverdriveMatchRomCmd = 0x69
00045 };
00046 
00047 void skipCurrentFamily(SearchRomState & searchState) {
00048   // Set the last discrepancy to last family discrepancy.
00049   searchState.lastDiscrepancy = searchState.lastFamilyDiscrepancy;
00050   // Clear the last family discrepancy.
00051   searchState.lastFamilyDiscrepancy = 0;
00052   // Check for end of list.
00053   if (searchState.lastDiscrepancy == 0) {
00054     searchState.lastDevice = true;
00055   }
00056 }
00057 
00058 error_code verifyRom(OneWireMaster & master, RomId::const_span romId) {
00059   SearchRomState searchState(romId);
00060   error_code result = searchRom(master, searchState);
00061   if (result) {
00062     return result;
00063   }
00064   // Check if same device found.
00065   if (romId != make_span(searchState.romId)) {
00066     result = make_error_code(OneWireMaster::NoSlaveError);
00067   }
00068   return result;
00069 }
00070 
00071 error_code readRom(OneWireMaster & master, RomId::span romId) {
00072   error_code result = master.reset();
00073   if (result) {
00074     return result;
00075   }
00076   result = master.writeByte(ReadRomCmd);
00077   if (result) {
00078     return result;
00079   }
00080   result = master.readBlock(romId);
00081   if (result) {
00082     return result;
00083   }
00084   if (!valid(romId)) {
00085     result = make_error_code(OneWireMaster::NoSlaveError);
00086   }
00087   return result;
00088 }
00089 
00090 error_code skipRom(OneWireMaster & master) {
00091   error_code result = master.reset();
00092   if (result) {
00093     return result;
00094   }
00095   result = master.writeByte(SkipRomCmd);
00096   return result;
00097 }
00098 
00099 error_code matchRom(OneWireMaster & master, RomId::const_span romId) {
00100   error_code result = master.reset();
00101   if (result) {
00102     return result;
00103   }
00104   result = master.writeByte(MatchRomCmd);
00105   if (result) {
00106     return result;
00107   }
00108   result = master.writeBlock(romId);
00109   return result;
00110 }
00111 
00112 error_code overdriveSkipRom(OneWireMaster & master) {
00113   error_code result = master.reset();
00114   if (result) {
00115     return result;
00116   }
00117   result = master.writeByte(OverdriveSkipRomCmd);
00118   if (result) {
00119     return result;
00120   }
00121   result = master.setSpeed(OneWireMaster::OverdriveSpeed);
00122   return result;
00123 }
00124 
00125 error_code overdriveMatchRom(OneWireMaster & master, RomId::const_span romId) {
00126   error_code result = master.reset();
00127   if (result) {
00128     return result;
00129   }
00130   result = master.writeByte(OverdriveMatchRomCmd);
00131   if (result) {
00132     return result;
00133   }
00134   result = master.setSpeed(OneWireMaster::OverdriveSpeed);
00135   if (result) {
00136     return result;
00137   }
00138   result = master.writeBlock(romId);
00139   return result;
00140 }
00141 
00142 error_code resumeRom(OneWireMaster & master) {
00143   error_code result = master.reset();
00144   if (result) {
00145     return result;
00146   }
00147   result = master.writeByte(ResumeRomCmd);
00148   return result;
00149 }
00150 
00151 error_code searchRom(OneWireMaster & master, SearchRomState & searchState) {
00152   if (searchState.lastDevice) {
00153     searchState = SearchRomState();
00154   }
00155 
00156   error_code result = master.reset();
00157   if (result) {
00158     return result;
00159   }
00160   result = master.writeByte(SearchRomCmd);
00161   if (result) {
00162     return result;
00163   }
00164 
00165   SearchRomState newSearchState;
00166   newSearchState.lastFamilyDiscrepancy = searchState.lastFamilyDiscrepancy;
00167   for (int idBitNumber = 1; idBitNumber <= 64; ++idBitNumber) {
00168     const int idByteNumber = (idBitNumber - 1) / 8;
00169     const unsigned int idBitMask = 1 << ((idBitNumber - 1) % 8);
00170 
00171     OneWireMaster::TripletData tripletData;
00172     if (idBitNumber == searchState.lastDiscrepancy) {
00173       tripletData.writeBit = 1;
00174     } else if (idBitNumber > searchState.lastDiscrepancy) {
00175       tripletData.writeBit = 0;
00176     } else { // idBitNumber < searchState.lastDiscrepancy
00177       tripletData.writeBit =
00178           (searchState.romId[idByteNumber] & idBitMask) == idBitMask;
00179     }
00180 
00181     result = master.triplet(tripletData);
00182     if (result) {
00183       return result;
00184     }
00185     if (tripletData.readBit && tripletData.readBitComplement) {
00186       return make_error_code(OneWireMaster::NoSlaveError);
00187     }
00188 
00189     if (tripletData.writeBit) {
00190       newSearchState.romId[idByteNumber] |= idBitMask;
00191     } else if (!tripletData.readBit && !tripletData.readBitComplement) {
00192       newSearchState.lastDiscrepancy = idBitNumber;
00193       if (idBitNumber <= 8) {
00194         newSearchState.lastFamilyDiscrepancy = idBitNumber;
00195       }
00196     }
00197   }
00198 
00199   if (valid(newSearchState.romId)) {
00200     if (newSearchState.lastDiscrepancy == 0) {
00201       newSearchState.lastDevice = true;
00202     }
00203     searchState = newSearchState;
00204   } else {
00205     result = make_error_code(OneWireMaster::NoSlaveError);
00206   }
00207   return result;
00208 }
00209 
00210 } // namespace MaximInterface