Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS2413.cpp Source File

DS2413.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 "DS2413.hpp"
00036 
00037 namespace MaximInterface {
00038 
00039 error_code DS2413::readStatus(Status & status) const {
00040   uint_least8_t val;
00041   const error_code result = pioAccessRead(val);
00042   if (!result) {
00043     status = val;
00044   }
00045   return result;
00046 }
00047 
00048 error_code DS2413::writeOutputState(bool pioAState, bool pioBState) {
00049   uint_least8_t val = 0xFC;
00050   if (pioAState) {
00051     val |= 0x1;
00052   }
00053   if (pioBState) {
00054     val |= 0x2;
00055   }
00056   return pioAccessWrite(val);
00057 }
00058 
00059 error_code DS2413::pioAccessRead(uint_least8_t & val) const {
00060   error_code result = selectRom(*master);
00061   if (!result) {
00062     result = master->writeByte(0xF5);
00063     if (!result) {
00064       result = master->readByte(val);
00065       if (!result && (val != ((val ^ 0xF0) >> 4))) {
00066         result = make_error_code(CommunicationError);
00067       }
00068     }
00069   }
00070   return result;
00071 }
00072 
00073 error_code DS2413::pioAccessWrite(uint_least8_t val) {
00074   error_code result = selectRom(*master);
00075   if (!result) {
00076     uint_least8_t block[] = {0x5A, val, static_cast<uint_least8_t>(val ^ 0xFF)};
00077     result = master->writeBlock(block);
00078     if (!result) {
00079       result = master->readByte(block[0]);
00080       if (!result && block[0] != 0xAA) {
00081         result = make_error_code(CommunicationError);
00082       }
00083     }
00084   }
00085   return result;
00086 }
00087 
00088 const error_category & DS2413::errorCategory() {
00089   static class : public error_category {
00090   public:
00091     virtual const char * name() const { return "DS2413"; }
00092 
00093     virtual std::string message(int condition) const {
00094       switch (condition) {
00095       case CommunicationError:
00096         return "Communication Error";
00097       }
00098       return defaultErrorMessage(condition);
00099     }
00100   } instance;
00101   return instance;
00102 }
00103 
00104 error_code writePioAOutputState(DS2413 & ds2413, bool pioAState) {
00105   DS2413::Status status;
00106   error_code result = ds2413.readStatus(status);
00107   if (!result && pioAState != status[DS2413::PioAOutputState]) {
00108     result =
00109         ds2413.writeOutputState(pioAState, status[DS2413::PioBOutputState]);
00110   }
00111   return result;
00112 }
00113 
00114 error_code writePioBOutputState(DS2413 & ds2413, bool pioBState) {
00115   DS2413::Status status;
00116   error_code result = ds2413.readStatus(status);
00117   if (!result && pioBState != status[DS2413::PioBOutputState]) {
00118     result =
00119         ds2413.writeOutputState(status[DS2413::PioAOutputState], pioBState);
00120   }
00121   return result;
00122 }
00123 
00124 } // namespace MaximInterface