Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Committer:
IanBenzMaxim
Date:
Thu Jan 11 13:50:39 2018 -0600
Revision:
3:f818ea5172ed
Parent:
0:f77ad7f72d04
Child:
6:a8c83a2e6fa4
Updated to version 1.1.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:f77ad7f72d04 1 /*******************************************************************************
IanBenzMaxim 0:f77ad7f72d04 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 0:f77ad7f72d04 3 *
IanBenzMaxim 0:f77ad7f72d04 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 0:f77ad7f72d04 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 0:f77ad7f72d04 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 0:f77ad7f72d04 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 0:f77ad7f72d04 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 0:f77ad7f72d04 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 0:f77ad7f72d04 10 *
IanBenzMaxim 0:f77ad7f72d04 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 0:f77ad7f72d04 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 0:f77ad7f72d04 13 *
IanBenzMaxim 0:f77ad7f72d04 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 0:f77ad7f72d04 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 0:f77ad7f72d04 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 0:f77ad7f72d04 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 0:f77ad7f72d04 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 0:f77ad7f72d04 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 0:f77ad7f72d04 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 0:f77ad7f72d04 21 *
IanBenzMaxim 0:f77ad7f72d04 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 24 * Products, Inc. Branding Policy.
IanBenzMaxim 0:f77ad7f72d04 25 *
IanBenzMaxim 0:f77ad7f72d04 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 0:f77ad7f72d04 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 0:f77ad7f72d04 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 0:f77ad7f72d04 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 0:f77ad7f72d04 30 * ownership rights.
IanBenzMaxim 0:f77ad7f72d04 31 *******************************************************************************/
IanBenzMaxim 0:f77ad7f72d04 32
IanBenzMaxim 0:f77ad7f72d04 33 #ifndef MaximInterface_OneWireMaster
IanBenzMaxim 0:f77ad7f72d04 34 #define MaximInterface_OneWireMaster
IanBenzMaxim 0:f77ad7f72d04 35
IanBenzMaxim 0:f77ad7f72d04 36 #include <stddef.h>
IanBenzMaxim 0:f77ad7f72d04 37 #include <stdint.h>
IanBenzMaxim 0:f77ad7f72d04 38 #include <MaximInterface/Utilities/Export.h>
IanBenzMaxim 0:f77ad7f72d04 39 #include <MaximInterface/Utilities/system_error.hpp>
IanBenzMaxim 0:f77ad7f72d04 40
IanBenzMaxim 0:f77ad7f72d04 41 namespace MaximInterface {
IanBenzMaxim 0:f77ad7f72d04 42
IanBenzMaxim 3:f818ea5172ed 43 /// 1-Wire master interface.
IanBenzMaxim 0:f77ad7f72d04 44 class OneWireMaster {
IanBenzMaxim 0:f77ad7f72d04 45 public:
IanBenzMaxim 0:f77ad7f72d04 46 /// Speed of the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 47 enum Speed { StandardSpeed = 0x00, OverdriveSpeed = 0x01 };
IanBenzMaxim 0:f77ad7f72d04 48
IanBenzMaxim 0:f77ad7f72d04 49 /// Level of the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 50 enum Level { NormalLevel = 0x00, StrongLevel = 0x02 };
IanBenzMaxim 0:f77ad7f72d04 51
IanBenzMaxim 0:f77ad7f72d04 52 /// Result of all 1-Wire commands.
IanBenzMaxim 0:f77ad7f72d04 53 enum ErrorValue {
IanBenzMaxim 0:f77ad7f72d04 54 NoSlaveError = 1, ///< Slave not detected, typically due to no presence pulse.
IanBenzMaxim 0:f77ad7f72d04 55 ShortDetectedError,
IanBenzMaxim 0:f77ad7f72d04 56 InvalidSpeedError,
IanBenzMaxim 0:f77ad7f72d04 57 InvalidLevelError
IanBenzMaxim 0:f77ad7f72d04 58 };
IanBenzMaxim 0:f77ad7f72d04 59
IanBenzMaxim 0:f77ad7f72d04 60 struct TripletData {
IanBenzMaxim 0:f77ad7f72d04 61 bool writeBit;
IanBenzMaxim 0:f77ad7f72d04 62 bool readBit;
IanBenzMaxim 0:f77ad7f72d04 63 bool readBitComplement;
IanBenzMaxim 0:f77ad7f72d04 64 };
IanBenzMaxim 0:f77ad7f72d04 65
IanBenzMaxim 0:f77ad7f72d04 66 virtual ~OneWireMaster() {}
IanBenzMaxim 0:f77ad7f72d04 67
IanBenzMaxim 0:f77ad7f72d04 68 /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
IanBenzMaxim 0:f77ad7f72d04 69 /// @returns NoSlaveError if reset was performed but no presence pulse was detected.
IanBenzMaxim 0:f77ad7f72d04 70 virtual error_code reset() = 0;
IanBenzMaxim 0:f77ad7f72d04 71
IanBenzMaxim 0:f77ad7f72d04 72 /// Send and receive one bit of communication and set a new level on the
IanBenzMaxim 0:f77ad7f72d04 73 /// 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 74 /// @param[in,out] sendRecvBit
IanBenzMaxim 0:f77ad7f72d04 75 /// Input containing the bit to send and output containing the received bit.
IanBenzMaxim 0:f77ad7f72d04 76 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 77 virtual error_code touchBitSetLevel(bool & sendRecvBit, Level afterLevel) = 0;
IanBenzMaxim 0:f77ad7f72d04 78
IanBenzMaxim 0:f77ad7f72d04 79 /// Send one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 80 /// @param sendByte Byte to send on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 81 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 82 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 0:f77ad7f72d04 83 writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
IanBenzMaxim 0:f77ad7f72d04 84
IanBenzMaxim 0:f77ad7f72d04 85 /// Receive one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 86 /// @param recvByte Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 87 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 88 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 0:f77ad7f72d04 89 readByteSetLevel(uint_least8_t & recvByte, Level afterLevel);
IanBenzMaxim 0:f77ad7f72d04 90
IanBenzMaxim 0:f77ad7f72d04 91 /// Send a block of communication on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 92 /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 93 /// @param sendLen Length of the buffer to send.
IanBenzMaxim 0:f77ad7f72d04 94 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 0:f77ad7f72d04 95 writeBlock(const uint_least8_t * sendBuf, size_t sendLen);
IanBenzMaxim 0:f77ad7f72d04 96
IanBenzMaxim 0:f77ad7f72d04 97 /// Receive a block of communication on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 98 /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 99 /// @param recvLen Length of the buffer to receive.
IanBenzMaxim 0:f77ad7f72d04 100 MaximInterface_EXPORT virtual error_code readBlock(uint_least8_t * recvBuf,
IanBenzMaxim 0:f77ad7f72d04 101 size_t recvLen);
IanBenzMaxim 0:f77ad7f72d04 102
IanBenzMaxim 0:f77ad7f72d04 103 /// Set the 1-Wire bus communication speed.
IanBenzMaxim 0:f77ad7f72d04 104 virtual error_code setSpeed(Speed newSpeed) = 0;
IanBenzMaxim 0:f77ad7f72d04 105
IanBenzMaxim 0:f77ad7f72d04 106 /// Set the 1-Wire bus level.
IanBenzMaxim 0:f77ad7f72d04 107 virtual error_code setLevel(Level newLevel) = 0;
IanBenzMaxim 0:f77ad7f72d04 108
IanBenzMaxim 0:f77ad7f72d04 109 /// 1-Wire Triplet operation.
IanBenzMaxim 0:f77ad7f72d04 110 /// @details Perform one bit of a 1-Wire search. This command
IanBenzMaxim 0:f77ad7f72d04 111 /// does two read bits and one write bit. The write bit is either
IanBenzMaxim 0:f77ad7f72d04 112 /// the default direction (all devices have same bit) or in case
IanBenzMaxim 0:f77ad7f72d04 113 /// of a discrepancy, the data.writeBit parameter is used.
IanBenzMaxim 0:f77ad7f72d04 114 ///@param[in,out] data
IanBenzMaxim 0:f77ad7f72d04 115 /// Input with desired writeBit in case both read bits are zero.
IanBenzMaxim 0:f77ad7f72d04 116 /// Output with all data fields set.
IanBenzMaxim 0:f77ad7f72d04 117 MaximInterface_EXPORT virtual error_code triplet(TripletData & data);
IanBenzMaxim 0:f77ad7f72d04 118
IanBenzMaxim 0:f77ad7f72d04 119 /// Send one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 120 /// @param sendBit Bit to send on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 121 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 122 error_code writeBitSetLevel(bool sendBit, Level afterLevel) {
IanBenzMaxim 0:f77ad7f72d04 123 return touchBitSetLevel(sendBit, afterLevel);
IanBenzMaxim 0:f77ad7f72d04 124 }
IanBenzMaxim 0:f77ad7f72d04 125
IanBenzMaxim 0:f77ad7f72d04 126 /// Receive one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 127 /// @param[out] recvBit Received data from the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 128 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 129 error_code readBitSetLevel(bool & recvBit, Level afterLevel) {
IanBenzMaxim 0:f77ad7f72d04 130 recvBit = 1;
IanBenzMaxim 0:f77ad7f72d04 131 return touchBitSetLevel(recvBit, afterLevel);
IanBenzMaxim 0:f77ad7f72d04 132 }
IanBenzMaxim 0:f77ad7f72d04 133
IanBenzMaxim 0:f77ad7f72d04 134 // Alternate forms of the read and write functions.
IanBenzMaxim 0:f77ad7f72d04 135 error_code writeBit(bool sendBit) {
IanBenzMaxim 0:f77ad7f72d04 136 return writeBitSetLevel(sendBit, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 137 }
IanBenzMaxim 0:f77ad7f72d04 138 error_code readBit(bool & recvBit) {
IanBenzMaxim 0:f77ad7f72d04 139 return readBitSetLevel(recvBit, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 140 }
IanBenzMaxim 0:f77ad7f72d04 141 error_code writeBitPower(bool sendBit) {
IanBenzMaxim 0:f77ad7f72d04 142 return writeBitSetLevel(sendBit, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 143 }
IanBenzMaxim 0:f77ad7f72d04 144 error_code readBitPower(bool & recvBit) {
IanBenzMaxim 0:f77ad7f72d04 145 return readBitSetLevel(recvBit, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 146 }
IanBenzMaxim 0:f77ad7f72d04 147 error_code writeByte(uint_least8_t sendByte) {
IanBenzMaxim 0:f77ad7f72d04 148 return writeByteSetLevel(sendByte, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 149 }
IanBenzMaxim 0:f77ad7f72d04 150 error_code readByte(uint_least8_t & recvByte) {
IanBenzMaxim 0:f77ad7f72d04 151 return readByteSetLevel(recvByte, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 152 }
IanBenzMaxim 0:f77ad7f72d04 153 error_code writeBytePower(uint_least8_t sendByte) {
IanBenzMaxim 0:f77ad7f72d04 154 return writeByteSetLevel(sendByte, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 155 }
IanBenzMaxim 0:f77ad7f72d04 156 error_code readBytePower(uint_least8_t & recvByte) {
IanBenzMaxim 0:f77ad7f72d04 157 return readByteSetLevel(recvByte, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 158 }
IanBenzMaxim 0:f77ad7f72d04 159
IanBenzMaxim 0:f77ad7f72d04 160 MaximInterface_EXPORT static const error_category & errorCategory();
IanBenzMaxim 0:f77ad7f72d04 161 };
IanBenzMaxim 0:f77ad7f72d04 162
IanBenzMaxim 0:f77ad7f72d04 163 inline error_code make_error_code(OneWireMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 164 return error_code(e, OneWireMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 165 }
IanBenzMaxim 0:f77ad7f72d04 166 inline error_condition make_error_condition(OneWireMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 167 return error_condition(e, OneWireMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 168 }
IanBenzMaxim 0:f77ad7f72d04 169
IanBenzMaxim 0:f77ad7f72d04 170 } // namespace MaximInterface
IanBenzMaxim 0:f77ad7f72d04 171
IanBenzMaxim 0:f77ad7f72d04 172 #endif