Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Revision:
0:f77ad7f72d04
Child:
3:f818ea5172ed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Links/OneWireMaster.hpp	Mon Nov 06 14:39:18 2017 -0600
@@ -0,0 +1,172 @@
+/*******************************************************************************
+* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+*******************************************************************************/
+
+#ifndef MaximInterface_OneWireMaster
+#define MaximInterface_OneWireMaster
+
+#include <stddef.h>
+#include <stdint.h>
+#include <MaximInterface/Utilities/Export.h>
+#include <MaximInterface/Utilities/system_error.hpp>
+
+namespace MaximInterface {
+
+/// Base class for all 1-Wire Masters.
+class OneWireMaster {
+public:
+  /// Speed of the 1-Wire bus.
+  enum Speed { StandardSpeed = 0x00, OverdriveSpeed = 0x01 };
+
+  /// Level of the 1-Wire bus.
+  enum Level { NormalLevel = 0x00, StrongLevel = 0x02 };
+
+  /// Result of all 1-Wire commands.
+  enum ErrorValue {
+    NoSlaveError = 1, ///< Slave not detected, typically due to no presence pulse.
+    ShortDetectedError,
+    InvalidSpeedError,
+    InvalidLevelError
+  };
+
+  struct TripletData {
+    bool writeBit;
+    bool readBit;
+    bool readBitComplement;
+  };
+
+  virtual ~OneWireMaster() {}
+
+  /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
+  /// @returns NoSlaveError if reset was performed but no presence pulse was detected.
+  virtual error_code reset() = 0;
+
+  /// Send and receive one bit of communication and set a new level on the
+  /// 1-Wire bus.
+  /// @param[in,out] sendRecvBit
+  /// Input containing the bit to send and output containing the received bit.
+  /// @param afterLevel Level to set the 1-Wire bus to after communication.
+  virtual error_code touchBitSetLevel(bool & sendRecvBit, Level afterLevel) = 0;
+
+  /// Send one byte of communication and set a new level on the 1-Wire bus.
+  /// @param sendByte Byte to send on the 1-Wire bus.
+  /// @param afterLevel Level to set the 1-Wire bus to after communication.
+  MaximInterface_EXPORT virtual error_code
+  writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
+
+  /// Receive one byte of communication and set a new level on the 1-Wire bus.
+  /// @param recvByte Buffer to receive the data from the 1-Wire bus.
+  /// @param afterLevel Level to set the 1-Wire bus to after communication.
+  MaximInterface_EXPORT virtual error_code
+  readByteSetLevel(uint_least8_t & recvByte, Level afterLevel);
+
+  /// Send a block of communication on the 1-Wire bus.
+  /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
+  /// @param sendLen Length of the buffer to send.
+  MaximInterface_EXPORT virtual error_code
+  writeBlock(const uint_least8_t * sendBuf, size_t sendLen);
+
+  /// Receive a block of communication on the 1-Wire bus.
+  /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
+  /// @param recvLen Length of the buffer to receive.
+  MaximInterface_EXPORT virtual error_code readBlock(uint_least8_t * recvBuf,
+                                                     size_t recvLen);
+
+  /// Set the 1-Wire bus communication speed.
+  virtual error_code setSpeed(Speed newSpeed) = 0;
+
+  /// Set the 1-Wire bus level.
+  virtual error_code setLevel(Level newLevel) = 0;
+
+  /// 1-Wire Triplet operation.
+  /// @details Perform one bit of a 1-Wire search. This command
+  /// does two read bits and one write bit. The write bit is either
+  /// the default direction (all devices have same bit) or in case
+  /// of a discrepancy, the data.writeBit parameter is used.
+  ///@param[in,out] data
+  /// Input with desired writeBit in case both read bits are zero.
+  /// Output with all data fields set.
+  MaximInterface_EXPORT virtual error_code triplet(TripletData & data);
+
+  /// Send one bit of communication and set a new level on the 1-Wire bus.
+  /// @param sendBit Bit to send on the 1-Wire bus.
+  /// @param afterLevel Level to set the 1-Wire bus to after communication.
+  error_code writeBitSetLevel(bool sendBit, Level afterLevel) {
+    return touchBitSetLevel(sendBit, afterLevel);
+  }
+
+  /// Receive one bit of communication and set a new level on the 1-Wire bus.
+  /// @param[out] recvBit Received data from the 1-Wire bus.
+  /// @param afterLevel Level to set the 1-Wire bus to after communication.
+  error_code readBitSetLevel(bool & recvBit, Level afterLevel) {
+    recvBit = 1;
+    return touchBitSetLevel(recvBit, afterLevel);
+  }
+
+  // Alternate forms of the read and write functions.
+  error_code writeBit(bool sendBit) {
+    return writeBitSetLevel(sendBit, NormalLevel);
+  }
+  error_code readBit(bool & recvBit) {
+    return readBitSetLevel(recvBit, NormalLevel);
+  }
+  error_code writeBitPower(bool sendBit) {
+    return writeBitSetLevel(sendBit, StrongLevel);
+  }
+  error_code readBitPower(bool & recvBit) {
+    return readBitSetLevel(recvBit, StrongLevel);
+  }
+  error_code writeByte(uint_least8_t sendByte) {
+    return writeByteSetLevel(sendByte, NormalLevel);
+  }
+  error_code readByte(uint_least8_t & recvByte) {
+    return readByteSetLevel(recvByte, NormalLevel);
+  }
+  error_code writeBytePower(uint_least8_t sendByte) {
+    return writeByteSetLevel(sendByte, StrongLevel);
+  }
+  error_code readBytePower(uint_least8_t & recvByte) {
+    return readByteSetLevel(recvByte, StrongLevel);
+  }
+
+  MaximInterface_EXPORT static const error_category & errorCategory();
+};
+
+inline error_code make_error_code(OneWireMaster::ErrorValue e) {
+  return error_code(e, OneWireMaster::errorCategory());
+}
+inline error_condition make_error_condition(OneWireMaster::ErrorValue e) {
+  return error_condition(e, OneWireMaster::errorCategory());
+}
+
+} // namespace MaximInterface
+
+#endif