Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Links/OneWireMaster.hpp@7:471901a04573, 2019-03-04 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Mar 04 08:10:00 2019 -0600
- Revision:
- 7:471901a04573
- Parent:
- 6:a8c83a2e6fa4
Updated to version 1.7.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:471901a04573 | 68 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 69 | /// Reset all of the devices on the 1-Wire bus and check for a presence pulse. |
IanBenzMaxim | 7:471901a04573 | 70 | /// @returns |
IanBenzMaxim | 7:471901a04573 | 71 | /// NoSlaveError if reset was performed but no presence pulse was detected. |
IanBenzMaxim | 0:f77ad7f72d04 | 72 | virtual error_code reset() = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 73 | |
IanBenzMaxim | 7:471901a04573 | 74 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 75 | /// Send and receive one bit of communication and set a new level on the |
IanBenzMaxim | 0:f77ad7f72d04 | 76 | /// 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 77 | /// @param[in,out] sendRecvBit |
IanBenzMaxim | 0:f77ad7f72d04 | 78 | /// Input containing the bit to send and output containing the received bit. |
IanBenzMaxim | 0:f77ad7f72d04 | 79 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 0:f77ad7f72d04 | 80 | virtual error_code touchBitSetLevel(bool & sendRecvBit, Level afterLevel) = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 81 | |
IanBenzMaxim | 7:471901a04573 | 82 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 83 | /// Send one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 84 | /// @param sendByte Byte to send on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 85 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 0:f77ad7f72d04 | 86 | MaximInterface_EXPORT virtual error_code |
IanBenzMaxim | 0:f77ad7f72d04 | 87 | writeByteSetLevel(uint_least8_t sendByte, Level afterLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 88 | |
IanBenzMaxim | 7:471901a04573 | 89 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 90 | /// Receive one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 91 | /// @param recvByte Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 92 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 0:f77ad7f72d04 | 93 | MaximInterface_EXPORT virtual error_code |
IanBenzMaxim | 0:f77ad7f72d04 | 94 | readByteSetLevel(uint_least8_t & recvByte, Level afterLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 95 | |
IanBenzMaxim | 7:471901a04573 | 96 | /// @brief Send a block of communication on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 97 | /// @param[in] sendBuf Buffer to send on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 98 | MaximInterface_EXPORT virtual error_code |
IanBenzMaxim | 6:a8c83a2e6fa4 | 99 | writeBlock(span<const uint_least8_t> sendBuf); |
IanBenzMaxim | 0:f77ad7f72d04 | 100 | |
IanBenzMaxim | 7:471901a04573 | 101 | /// @brief Receive a block of communication on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 102 | /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 103 | MaximInterface_EXPORT virtual error_code |
IanBenzMaxim | 6:a8c83a2e6fa4 | 104 | readBlock(span<uint_least8_t> recvBuf); |
IanBenzMaxim | 0:f77ad7f72d04 | 105 | |
IanBenzMaxim | 0:f77ad7f72d04 | 106 | /// Set the 1-Wire bus communication speed. |
IanBenzMaxim | 0:f77ad7f72d04 | 107 | virtual error_code setSpeed(Speed newSpeed) = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 108 | |
IanBenzMaxim | 0:f77ad7f72d04 | 109 | /// Set the 1-Wire bus level. |
IanBenzMaxim | 0:f77ad7f72d04 | 110 | virtual error_code setLevel(Level newLevel) = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 111 | |
IanBenzMaxim | 7:471901a04573 | 112 | /// @brief 1-Wire Triplet operation. |
IanBenzMaxim | 0:f77ad7f72d04 | 113 | /// @details Perform one bit of a 1-Wire search. This command |
IanBenzMaxim | 0:f77ad7f72d04 | 114 | /// does two read bits and one write bit. The write bit is either |
IanBenzMaxim | 0:f77ad7f72d04 | 115 | /// the default direction (all devices have same bit) or in case |
IanBenzMaxim | 0:f77ad7f72d04 | 116 | /// of a discrepancy, the data.writeBit parameter is used. |
IanBenzMaxim | 7:471901a04573 | 117 | /// @param[in,out] data |
IanBenzMaxim | 0:f77ad7f72d04 | 118 | /// Input with desired writeBit in case both read bits are zero. |
IanBenzMaxim | 0:f77ad7f72d04 | 119 | /// Output with all data fields set. |
IanBenzMaxim | 0:f77ad7f72d04 | 120 | MaximInterface_EXPORT virtual error_code triplet(TripletData & data); |
IanBenzMaxim | 0:f77ad7f72d04 | 121 | |
IanBenzMaxim | 7:471901a04573 | 122 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 123 | /// Send one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 124 | /// @param sendBit Bit to send on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 125 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 0:f77ad7f72d04 | 126 | error_code writeBitSetLevel(bool sendBit, Level afterLevel) { |
IanBenzMaxim | 0:f77ad7f72d04 | 127 | return touchBitSetLevel(sendBit, afterLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 128 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 129 | |
IanBenzMaxim | 7:471901a04573 | 130 | /// @brief |
IanBenzMaxim | 0:f77ad7f72d04 | 131 | /// Receive one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 132 | /// @param[out] recvBit Received data from the 1-Wire bus. |
IanBenzMaxim | 0:f77ad7f72d04 | 133 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 0:f77ad7f72d04 | 134 | error_code readBitSetLevel(bool & recvBit, Level afterLevel) { |
IanBenzMaxim | 0:f77ad7f72d04 | 135 | recvBit = 1; |
IanBenzMaxim | 0:f77ad7f72d04 | 136 | return touchBitSetLevel(recvBit, afterLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 137 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 138 | |
IanBenzMaxim | 0:f77ad7f72d04 | 139 | // Alternate forms of the read and write functions. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 140 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 141 | error_code touchBit(bool & sendRecvBit) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 142 | return touchBitSetLevel(sendRecvBit, NormalLevel); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 143 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 144 | |
IanBenzMaxim | 0:f77ad7f72d04 | 145 | error_code writeBit(bool sendBit) { |
IanBenzMaxim | 0:f77ad7f72d04 | 146 | return writeBitSetLevel(sendBit, NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 147 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 148 | |
IanBenzMaxim | 0:f77ad7f72d04 | 149 | error_code readBit(bool & recvBit) { |
IanBenzMaxim | 0:f77ad7f72d04 | 150 | return readBitSetLevel(recvBit, NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 151 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 152 | |
IanBenzMaxim | 0:f77ad7f72d04 | 153 | error_code writeBitPower(bool sendBit) { |
IanBenzMaxim | 0:f77ad7f72d04 | 154 | return writeBitSetLevel(sendBit, StrongLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 155 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 156 | |
IanBenzMaxim | 0:f77ad7f72d04 | 157 | error_code readBitPower(bool & recvBit) { |
IanBenzMaxim | 0:f77ad7f72d04 | 158 | return readBitSetLevel(recvBit, StrongLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 159 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 160 | |
IanBenzMaxim | 0:f77ad7f72d04 | 161 | error_code writeByte(uint_least8_t sendByte) { |
IanBenzMaxim | 0:f77ad7f72d04 | 162 | return writeByteSetLevel(sendByte, NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 163 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 164 | |
IanBenzMaxim | 0:f77ad7f72d04 | 165 | error_code readByte(uint_least8_t & recvByte) { |
IanBenzMaxim | 0:f77ad7f72d04 | 166 | return readByteSetLevel(recvByte, NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 167 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 168 | |
IanBenzMaxim | 0:f77ad7f72d04 | 169 | error_code writeBytePower(uint_least8_t sendByte) { |
IanBenzMaxim | 0:f77ad7f72d04 | 170 | return writeByteSetLevel(sendByte, StrongLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 171 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 172 | |
IanBenzMaxim | 0:f77ad7f72d04 | 173 | error_code readBytePower(uint_least8_t & recvByte) { |
IanBenzMaxim | 0:f77ad7f72d04 | 174 | return readByteSetLevel(recvByte, StrongLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 175 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 176 | |
IanBenzMaxim | 0:f77ad7f72d04 | 177 | MaximInterface_EXPORT static const error_category & errorCategory(); |
IanBenzMaxim | 0:f77ad7f72d04 | 178 | }; |
IanBenzMaxim | 0:f77ad7f72d04 | 179 | |
IanBenzMaxim | 0:f77ad7f72d04 | 180 | inline error_code make_error_code(OneWireMaster::ErrorValue e) { |
IanBenzMaxim | 0:f77ad7f72d04 | 181 | return error_code(e, OneWireMaster::errorCategory()); |
IanBenzMaxim | 0:f77ad7f72d04 | 182 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 183 | |
IanBenzMaxim | 0:f77ad7f72d04 | 184 | inline error_condition make_error_condition(OneWireMaster::ErrorValue e) { |
IanBenzMaxim | 0:f77ad7f72d04 | 185 | return error_condition(e, OneWireMaster::errorCategory()); |
IanBenzMaxim | 0:f77ad7f72d04 | 186 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 187 | |
IanBenzMaxim | 0:f77ad7f72d04 | 188 | } // namespace MaximInterface |
IanBenzMaxim | 0:f77ad7f72d04 | 189 | |
IanBenzMaxim | 0:f77ad7f72d04 | 190 | #endif |