Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1920.cpp Source File

DS1920.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 <MaximInterface/Links/OneWireMaster.hpp>
00034 #include <MaximInterface/Utilities/Error.hpp>
00035 #include "DS1920.hpp"
00036 
00037 namespace MaximInterface {
00038 
00039 error_code DS1920::writeScratchpad(uint_least8_t th, uint_least8_t tl) {  
00040   error_code result = selectRom(*master);
00041   if (!result) {
00042     const uint_least8_t sendBlock[] = {0x4E, th, tl};
00043     result = master->writeBlock(sendBlock);
00044   }
00045   return result;
00046 }
00047 
00048 error_code DS1920::readScratchpad(Scratchpad::span scratchpad) {  
00049   error_code result = selectRom(*master);
00050   if (!result) {
00051     result = master->writeByte(0xBE);
00052     if (!result) {
00053       result = master->readBlock(scratchpad);
00054       if (!result) {
00055         uint_least8_t receivedCrc;
00056         result = master->readByte(receivedCrc);
00057         if (!result && (receivedCrc != calculateCrc8(scratchpad))) {
00058           result = make_error_code(CrcError);
00059         }
00060       }
00061     }
00062   }
00063   return result;
00064 }
00065 
00066 error_code DS1920::copyScratchpad() {  
00067   error_code result = selectRom(*master);
00068   if (!result) {
00069     result = master->writeByteSetLevel(0x48, OneWireMaster::StrongLevel);
00070     if (!result) {
00071       sleep->invoke(10);
00072       result = master->setLevel(OneWireMaster::NormalLevel);
00073     }
00074   }
00075   return result;
00076 }
00077 
00078 error_code DS1920::convertTemperature() {  
00079   error_code result = selectRom(*master);
00080   if (!result) {
00081     result = master->writeByteSetLevel(0x44, OneWireMaster::StrongLevel);
00082     if (!result) {
00083       sleep->invoke(750);
00084       result = master->setLevel(OneWireMaster::NormalLevel);
00085     }
00086   }
00087   return result;
00088 }
00089 
00090 error_code DS1920::recallEeprom() {  
00091   error_code result = selectRom(*master);
00092   if (!result) {
00093     result = master->writeByte(0xB8);
00094   }
00095   return result;
00096 }
00097 
00098 const error_category & DS1920::errorCategory() {
00099   static class : public error_category {
00100   public:
00101     virtual const char * name() const { return "DS1920"; }
00102 
00103     virtual std::string message(int condition) const {
00104       switch (condition) {
00105       case CrcError:
00106         return "CRC Error";
00107 
00108       case DataError:
00109         return "Data Error";
00110 
00111       default:
00112         return defaultErrorMessage(condition);
00113       }
00114     }
00115   } instance;
00116   return instance;
00117 }
00118 
00119 error_code readTemperature(DS1920 & ds1920, int & temperature) {
00120   error_code result = ds1920.convertTemperature();
00121   if (result) {
00122     return result;
00123   }
00124   DS1920::Scratchpad::array scratchpad;
00125   result = ds1920.readScratchpad(scratchpad);
00126   if (result) {
00127     return result;
00128   }
00129 
00130   unsigned int tempData =
00131       (static_cast<unsigned int>(scratchpad[1]) << 8) | scratchpad[0];
00132   const unsigned int signMask = 0xFF00;
00133   if ((tempData & signMask) == signMask) {
00134     temperature = -0x100;
00135     tempData &= ~signMask;
00136   } else if ((tempData & signMask) == 0) {
00137     temperature = 0;
00138   } else {
00139     return make_error_code(DS1920::DataError);
00140   }
00141   temperature += static_cast<int>(tempData);
00142   return error_code();
00143 }
00144 
00145 } // namespace MaximInterface