Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Committer:
IanBenzMaxim
Date:
Wed Jan 23 13:11:04 2019 -0600
Revision:
6:a8c83a2e6fa4
Parent:
3:f818ea5172ed
Child:
7:471901a04573
Updated to version 1.6 and removed platform files that are incompatible with mbed.

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 <stdint.h>
IanBenzMaxim 0:f77ad7f72d04 37 #include <MaximInterface/Utilities/Export.h>
IanBenzMaxim 6:a8c83a2e6fa4 38 #include <MaximInterface/Utilities/span.hpp>
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 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 6:a8c83a2e6fa4 94 writeBlock(span<const uint_least8_t> sendBuf);
IanBenzMaxim 0:f77ad7f72d04 95
IanBenzMaxim 0:f77ad7f72d04 96 /// Receive a block of communication on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 97 /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 6:a8c83a2e6fa4 98 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 6:a8c83a2e6fa4 99 readBlock(span<uint_least8_t> recvBuf);
IanBenzMaxim 0:f77ad7f72d04 100
IanBenzMaxim 0:f77ad7f72d04 101 /// Set the 1-Wire bus communication speed.
IanBenzMaxim 0:f77ad7f72d04 102 virtual error_code setSpeed(Speed newSpeed) = 0;
IanBenzMaxim 0:f77ad7f72d04 103
IanBenzMaxim 0:f77ad7f72d04 104 /// Set the 1-Wire bus level.
IanBenzMaxim 0:f77ad7f72d04 105 virtual error_code setLevel(Level newLevel) = 0;
IanBenzMaxim 0:f77ad7f72d04 106
IanBenzMaxim 0:f77ad7f72d04 107 /// 1-Wire Triplet operation.
IanBenzMaxim 0:f77ad7f72d04 108 /// @details Perform one bit of a 1-Wire search. This command
IanBenzMaxim 0:f77ad7f72d04 109 /// does two read bits and one write bit. The write bit is either
IanBenzMaxim 0:f77ad7f72d04 110 /// the default direction (all devices have same bit) or in case
IanBenzMaxim 0:f77ad7f72d04 111 /// of a discrepancy, the data.writeBit parameter is used.
IanBenzMaxim 0:f77ad7f72d04 112 ///@param[in,out] data
IanBenzMaxim 0:f77ad7f72d04 113 /// Input with desired writeBit in case both read bits are zero.
IanBenzMaxim 0:f77ad7f72d04 114 /// Output with all data fields set.
IanBenzMaxim 0:f77ad7f72d04 115 MaximInterface_EXPORT virtual error_code triplet(TripletData & data);
IanBenzMaxim 0:f77ad7f72d04 116
IanBenzMaxim 0:f77ad7f72d04 117 /// Send one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 118 /// @param sendBit Bit to send on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 119 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 120 error_code writeBitSetLevel(bool sendBit, Level afterLevel) {
IanBenzMaxim 0:f77ad7f72d04 121 return touchBitSetLevel(sendBit, afterLevel);
IanBenzMaxim 0:f77ad7f72d04 122 }
IanBenzMaxim 0:f77ad7f72d04 123
IanBenzMaxim 0:f77ad7f72d04 124 /// Receive one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 125 /// @param[out] recvBit Received data from the 1-Wire bus.
IanBenzMaxim 0:f77ad7f72d04 126 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 0:f77ad7f72d04 127 error_code readBitSetLevel(bool & recvBit, Level afterLevel) {
IanBenzMaxim 0:f77ad7f72d04 128 recvBit = 1;
IanBenzMaxim 0:f77ad7f72d04 129 return touchBitSetLevel(recvBit, afterLevel);
IanBenzMaxim 0:f77ad7f72d04 130 }
IanBenzMaxim 0:f77ad7f72d04 131
IanBenzMaxim 0:f77ad7f72d04 132 // Alternate forms of the read and write functions.
IanBenzMaxim 6:a8c83a2e6fa4 133
IanBenzMaxim 6:a8c83a2e6fa4 134 error_code touchBit(bool & sendRecvBit) {
IanBenzMaxim 6:a8c83a2e6fa4 135 return touchBitSetLevel(sendRecvBit, NormalLevel);
IanBenzMaxim 6:a8c83a2e6fa4 136 }
IanBenzMaxim 6:a8c83a2e6fa4 137
IanBenzMaxim 0:f77ad7f72d04 138 error_code writeBit(bool sendBit) {
IanBenzMaxim 0:f77ad7f72d04 139 return writeBitSetLevel(sendBit, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 140 }
IanBenzMaxim 6:a8c83a2e6fa4 141
IanBenzMaxim 0:f77ad7f72d04 142 error_code readBit(bool & recvBit) {
IanBenzMaxim 0:f77ad7f72d04 143 return readBitSetLevel(recvBit, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 144 }
IanBenzMaxim 6:a8c83a2e6fa4 145
IanBenzMaxim 0:f77ad7f72d04 146 error_code writeBitPower(bool sendBit) {
IanBenzMaxim 0:f77ad7f72d04 147 return writeBitSetLevel(sendBit, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 148 }
IanBenzMaxim 6:a8c83a2e6fa4 149
IanBenzMaxim 0:f77ad7f72d04 150 error_code readBitPower(bool & recvBit) {
IanBenzMaxim 0:f77ad7f72d04 151 return readBitSetLevel(recvBit, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 152 }
IanBenzMaxim 6:a8c83a2e6fa4 153
IanBenzMaxim 0:f77ad7f72d04 154 error_code writeByte(uint_least8_t sendByte) {
IanBenzMaxim 0:f77ad7f72d04 155 return writeByteSetLevel(sendByte, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 156 }
IanBenzMaxim 6:a8c83a2e6fa4 157
IanBenzMaxim 0:f77ad7f72d04 158 error_code readByte(uint_least8_t & recvByte) {
IanBenzMaxim 0:f77ad7f72d04 159 return readByteSetLevel(recvByte, NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 160 }
IanBenzMaxim 6:a8c83a2e6fa4 161
IanBenzMaxim 0:f77ad7f72d04 162 error_code writeBytePower(uint_least8_t sendByte) {
IanBenzMaxim 0:f77ad7f72d04 163 return writeByteSetLevel(sendByte, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 164 }
IanBenzMaxim 6:a8c83a2e6fa4 165
IanBenzMaxim 0:f77ad7f72d04 166 error_code readBytePower(uint_least8_t & recvByte) {
IanBenzMaxim 0:f77ad7f72d04 167 return readByteSetLevel(recvByte, StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 168 }
IanBenzMaxim 0:f77ad7f72d04 169
IanBenzMaxim 0:f77ad7f72d04 170 MaximInterface_EXPORT static const error_category & errorCategory();
IanBenzMaxim 0:f77ad7f72d04 171 };
IanBenzMaxim 0:f77ad7f72d04 172
IanBenzMaxim 0:f77ad7f72d04 173 inline error_code make_error_code(OneWireMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 174 return error_code(e, OneWireMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 175 }
IanBenzMaxim 6:a8c83a2e6fa4 176
IanBenzMaxim 0:f77ad7f72d04 177 inline error_condition make_error_condition(OneWireMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 178 return error_condition(e, OneWireMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 179 }
IanBenzMaxim 0:f77ad7f72d04 180
IanBenzMaxim 0:f77ad7f72d04 181 } // namespace MaximInterface
IanBenzMaxim 0:f77ad7f72d04 182
IanBenzMaxim 0:f77ad7f72d04 183 #endif