Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS2413.cpp Source File

DS2413.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 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 <MaximInterfaceCore/Error.hpp>
00034 #include <MaximInterfaceCore/OneWireMaster.hpp>
00035 #include "DS2413.hpp"
00036 
00037 #define TRY MaximInterfaceCore_TRY
00038 #define TRY_VALUE MaximInterfaceCore_TRY_VALUE
00039 
00040 namespace MaximInterfaceDevices {
00041 
00042 using namespace Core;
00043 
00044 Result<DS2413::Status> DS2413::readStatus() const {
00045   return Result<DS2413::Status>(pioAccessRead());
00046 }
00047 
00048 Result<void> 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 Result<uint_least8_t> DS2413::pioAccessRead() const {
00060   TRY(selectRom(*master));
00061   TRY(master->writeByte(0xF5));
00062   uint_least8_t result;
00063   TRY_VALUE(result, master->readByte());
00064   return (result == ((result ^ 0xF0) >> 4)) ? makeResult(result)
00065                                             : CommunicationError;
00066 }
00067 
00068 Result<void> DS2413::pioAccessWrite(uint_least8_t val) {
00069   TRY(selectRom(*master));
00070   const uint_least8_t block[] = {0x5A, val,
00071                                  static_cast<uint_least8_t>(val ^ 0xFF)};
00072   TRY(master->writeBlock(block));
00073   TRY_VALUE(val, master->readByte());
00074   return (val == 0xAA) ? makeResult(none) : CommunicationError;
00075 }
00076 
00077 const error_category & DS2413::errorCategory() {
00078   static class : public error_category {
00079   public:
00080     virtual const char * name() const { return "MaximInterfaceDevices.DS2413"; }
00081 
00082     virtual std::string message(int condition) const {
00083       switch (condition) {
00084       case CommunicationError:
00085         return "Communication Error";
00086       }
00087       return defaultErrorMessage(condition);
00088     }
00089   } instance;
00090   return instance;
00091 }
00092 
00093 Result<void> writePioAOutputState(DS2413 & ds2413, bool pioAState) {
00094   DS2413::Status status;
00095   TRY_VALUE(status, ds2413.readStatus());
00096   if (pioAState != status[DS2413::PioAOutputState]) {
00097     TRY(ds2413.writeOutputState(pioAState, status[DS2413::PioBOutputState]));
00098   }
00099   return none;
00100 }
00101 
00102 Result<void> writePioBOutputState(DS2413 & ds2413, bool pioBState) {
00103   DS2413::Status status;
00104   TRY_VALUE(status, ds2413.readStatus());
00105   if (pioBState != status[DS2413::PioBOutputState]) {
00106     TRY(ds2413.writeOutputState(status[DS2413::PioAOutputState], pioBState));
00107   }
00108   return none;
00109 }
00110 
00111 } // namespace MaximInterfaceDevices