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 OneWireMaster.hpp Source File

OneWireMaster.hpp

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 #ifndef MaximInterfaceCore_OneWireMaster_hpp
00034 #define MaximInterfaceCore_OneWireMaster_hpp
00035 
00036 #include <stdint.h>
00037 #include "Config.hpp"
00038 #include "Result.hpp"
00039 #include "span.hpp"
00040 #include "system_error.hpp"
00041 
00042 namespace MaximInterfaceCore {
00043 
00044 /// 1-Wire master interface.
00045 class OneWireMaster {
00046 public:
00047   /// Speed of the 1-Wire bus.
00048   enum Speed { StandardSpeed = 0x00, OverdriveSpeed = 0x01 };
00049 
00050   /// Level of the 1-Wire bus.
00051   enum Level { NormalLevel = 0x00, StrongLevel = 0x02 };
00052 
00053   /// Result of all 1-Wire commands.
00054   enum ErrorValue {
00055     NoSlaveError = 1, ///< Slave not detected, typically due to no presence pulse.
00056     ShortDetectedError,
00057     InvalidSpeedError,
00058     InvalidLevelError
00059   };
00060 
00061   /// Result of the 1-Wire triplet command.
00062   struct TripletData {
00063     bool readBit;
00064     bool readBitComplement;
00065     bool writeBit;
00066   };
00067 
00068   virtual ~OneWireMaster() {}
00069 
00070   /// @brief
00071   /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
00072   /// @returns
00073   /// NoSlaveError if reset was performed but no presence pulse was detected.
00074   virtual Result<void> reset() = 0;
00075 
00076   /// @brief
00077   /// Send and receive one bit of communication and set a new level on the
00078   /// 1-Wire bus.
00079   /// @param sendBit Bit to send on the 1-Wire bus.
00080   /// @param afterLevel Level to set the 1-Wire bus to after communication.
00081   /// @returns Bit received from the 1-Wire bus.
00082   virtual Result<bool> touchBitSetLevel(bool sendBit, Level afterLevel) = 0;
00083 
00084   /// @brief
00085   /// Send one byte of communication and set a new level on the 1-Wire bus.
00086   /// @param sendByte Byte to send on the 1-Wire bus.
00087   /// @param afterLevel Level to set the 1-Wire bus to after communication.
00088   MaximInterfaceCore_EXPORT virtual Result<void>
00089   writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
00090 
00091   /// @brief
00092   /// Receive one byte of communication and set a new level on the 1-Wire bus.
00093   /// @param afterLevel Level to set the 1-Wire bus to after communication.
00094   /// @returns Data received from the 1-Wire bus.
00095   MaximInterfaceCore_EXPORT virtual Result<uint_least8_t>
00096   readByteSetLevel(Level afterLevel);
00097 
00098   /// @brief Send a block of communication on the 1-Wire bus.
00099   /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
00100   MaximInterfaceCore_EXPORT virtual Result<void>
00101   writeBlock(span<const uint_least8_t> sendBuf);
00102 
00103   /// @brief Receive a block of communication on the 1-Wire bus.
00104   /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
00105   MaximInterfaceCore_EXPORT virtual Result<void>
00106   readBlock(span<uint_least8_t> recvBuf);
00107 
00108   /// Set the 1-Wire bus communication speed.
00109   virtual Result<void> setSpeed(Speed newSpeed) = 0;
00110 
00111   /// Set the 1-Wire bus level.
00112   virtual Result<void> setLevel(Level newLevel) = 0;
00113 
00114   /// @brief 1-Wire Triplet operation.
00115   /// @details Perform one bit of a 1-Wire search. This command
00116   /// does two read bits and one write bit. The write bit is either
00117   /// the default direction (all devices have same bit) or in case
00118   /// of a discrepancy, the data.writeBit parameter is used.
00119   /// @param sendBit Bit to send in case both read bits are zero.
00120   MaximInterfaceCore_EXPORT virtual Result<TripletData> triplet(bool sendBit);
00121 
00122   /// @brief
00123   /// Send one bit of communication and set a new level on the 1-Wire bus.
00124   /// @param sendBit Bit to send on the 1-Wire bus.
00125   /// @param afterLevel Level to set the 1-Wire bus to after communication.
00126   Result<void> writeBitSetLevel(bool sendBit, Level afterLevel) {
00127     MaximInterfaceCore_TRY(touchBitSetLevel(sendBit, afterLevel));
00128     return none;
00129   }
00130 
00131   /// @brief
00132   /// Receive one bit of communication and set a new level on the 1-Wire bus.
00133   /// @param afterLevel Level to set the 1-Wire bus to after communication.
00134   /// @returns Received data from the 1-Wire bus.
00135   Result<bool> readBitSetLevel(Level afterLevel) {
00136     return touchBitSetLevel(1, afterLevel);
00137   }
00138 
00139   // Alternate forms of the read and write functions.
00140 
00141   Result<bool> touchBit(bool sendBit) {
00142     return touchBitSetLevel(sendBit, NormalLevel);
00143   }
00144 
00145   Result<void> writeBit(bool sendBit) {
00146     return writeBitSetLevel(sendBit, NormalLevel);
00147   }
00148 
00149   Result<bool> readBit() { return readBitSetLevel(NormalLevel); }
00150 
00151   Result<void> writeBitPower(bool sendBit) {
00152     return writeBitSetLevel(sendBit, StrongLevel);
00153   }
00154 
00155   Result<bool> readBitPower() { return readBitSetLevel(StrongLevel); }
00156 
00157   Result<void> writeByte(uint_least8_t sendByte) {
00158     return writeByteSetLevel(sendByte, NormalLevel);
00159   }
00160 
00161   Result<uint_least8_t> readByte() { return readByteSetLevel(NormalLevel); }
00162 
00163   Result<void> writeBytePower(uint_least8_t sendByte) {
00164     return writeByteSetLevel(sendByte, StrongLevel);
00165   }
00166 
00167   Result<uint_least8_t> readBytePower() {
00168     return readByteSetLevel(StrongLevel);
00169   }
00170 
00171   MaximInterfaceCore_EXPORT static const error_category & errorCategory();
00172 };
00173 
00174 template <> struct is_error_code_enum<OneWireMaster::ErrorValue> : true_type {};
00175 
00176 inline error_code make_error_code(OneWireMaster::ErrorValue e) {
00177   return error_code(e, OneWireMaster::errorCategory());
00178 }
00179 
00180 inline error_condition make_error_condition(OneWireMaster::ErrorValue e) {
00181   return error_condition(e, OneWireMaster::errorCategory());
00182 }
00183 
00184 } // namespace MaximInterfaceCore
00185 
00186 #endif