Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Masters/OneWireMaster.h@142:85b71cfd617e, 2019-08-13 (annotated)
- Committer:
- mfruge
- Date:
- Tue Aug 13 14:42:37 2019 +0000
- Revision:
- 142:85b71cfd617e
- Parent:
- 141:cf38f48a2a49
Added functions to ROMCommands to add Alarm Search functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 5:ce108eeb878d | 1 | /******************************************************************//** |
j3 | 5:ce108eeb878d | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 5:ce108eeb878d | 3 | * |
j3 | 5:ce108eeb878d | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 5:ce108eeb878d | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 5:ce108eeb878d | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 5:ce108eeb878d | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 5:ce108eeb878d | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 5:ce108eeb878d | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 5:ce108eeb878d | 10 | * |
j3 | 5:ce108eeb878d | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 5:ce108eeb878d | 12 | * in all copies or substantial portions of the Software. |
j3 | 5:ce108eeb878d | 13 | * |
j3 | 5:ce108eeb878d | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 5:ce108eeb878d | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 5:ce108eeb878d | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 5:ce108eeb878d | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 5:ce108eeb878d | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 5:ce108eeb878d | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 5:ce108eeb878d | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 5:ce108eeb878d | 21 | * |
j3 | 5:ce108eeb878d | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 5:ce108eeb878d | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 5:ce108eeb878d | 24 | * Products, Inc. Branding Policy. |
j3 | 5:ce108eeb878d | 25 | * |
j3 | 5:ce108eeb878d | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 5:ce108eeb878d | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 5:ce108eeb878d | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 5:ce108eeb878d | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 5:ce108eeb878d | 30 | * ownership rights. |
j3 | 5:ce108eeb878d | 31 | **********************************************************************/ |
j3 | 5:ce108eeb878d | 32 | |
IanBenzMaxim | 73:2cecc1372acc | 33 | #ifndef OneWire_Masters_OneWireMaster |
IanBenzMaxim | 73:2cecc1372acc | 34 | #define OneWire_Masters_OneWireMaster |
j3 | 5:ce108eeb878d | 35 | |
IanBenzMaxim | 73:2cecc1372acc | 36 | #include <stdint.h> |
IanBenzMaxim | 73:2cecc1372acc | 37 | #include <stddef.h> |
IanBenzMaxim | 73:2cecc1372acc | 38 | |
IanBenzMaxim | 73:2cecc1372acc | 39 | namespace OneWire |
IanBenzMaxim | 73:2cecc1372acc | 40 | { |
IanBenzMaxim | 76:84e6c4994e29 | 41 | /// Base class for all 1-Wire Masters. |
IanBenzMaxim | 76:84e6c4994e29 | 42 | class OneWireMaster |
IanBenzMaxim | 32:bce180b544ed | 43 | { |
IanBenzMaxim | 76:84e6c4994e29 | 44 | public: |
IanBenzMaxim | 79:7f22823a5a2d | 45 | /// Speed of the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 46 | enum OWSpeed |
IanBenzMaxim | 74:23be10c32fa3 | 47 | { |
IanBenzMaxim | 76:84e6c4994e29 | 48 | StandardSpeed = 0x00, |
IanBenzMaxim | 76:84e6c4994e29 | 49 | OverdriveSpeed = 0x01 |
IanBenzMaxim | 76:84e6c4994e29 | 50 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 51 | |
IanBenzMaxim | 79:7f22823a5a2d | 52 | /// Level of the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 53 | enum OWLevel |
IanBenzMaxim | 76:84e6c4994e29 | 54 | { |
IanBenzMaxim | 76:84e6c4994e29 | 55 | NormalLevel = 0x00, |
IanBenzMaxim | 76:84e6c4994e29 | 56 | StrongLevel = 0x02 |
IanBenzMaxim | 76:84e6c4994e29 | 57 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 58 | |
IanBenzMaxim | 79:7f22823a5a2d | 59 | /// Search direction for the triplet operation. |
IanBenzMaxim | 76:84e6c4994e29 | 60 | enum SearchDirection |
IanBenzMaxim | 76:84e6c4994e29 | 61 | { |
IanBenzMaxim | 76:84e6c4994e29 | 62 | WriteZero = 0, |
IanBenzMaxim | 76:84e6c4994e29 | 63 | WriteOne = 1 |
IanBenzMaxim | 76:84e6c4994e29 | 64 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 65 | |
IanBenzMaxim | 79:7f22823a5a2d | 66 | /// Result of all 1-Wire commands. |
IanBenzMaxim | 76:84e6c4994e29 | 67 | enum CmdResult |
IanBenzMaxim | 76:84e6c4994e29 | 68 | { |
IanBenzMaxim | 76:84e6c4994e29 | 69 | Success, |
IanBenzMaxim | 76:84e6c4994e29 | 70 | CommunicationWriteError, |
IanBenzMaxim | 76:84e6c4994e29 | 71 | CommunicationReadError, |
IanBenzMaxim | 76:84e6c4994e29 | 72 | TimeoutError, |
IanBenzMaxim | 76:84e6c4994e29 | 73 | OperationFailure |
IanBenzMaxim | 76:84e6c4994e29 | 74 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 75 | |
IanBenzMaxim | 76:84e6c4994e29 | 76 | /// Allow freeing through a base class pointer. |
IanBenzMaxim | 76:84e6c4994e29 | 77 | virtual ~OneWireMaster() { } |
IanBenzMaxim | 75:8b627804927c | 78 | |
IanBenzMaxim | 76:84e6c4994e29 | 79 | /// Initialize a master for use. |
IanBenzMaxim | 76:84e6c4994e29 | 80 | virtual CmdResult OWInitMaster() = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 81 | |
IanBenzMaxim | 76:84e6c4994e29 | 82 | /// Reset all of the devices on the 1-Wire bus and check for a presence pulse. |
IanBenzMaxim | 76:84e6c4994e29 | 83 | /// @returns OperationFailure if reset was performed but no presence pulse was detected. |
IanBenzMaxim | 76:84e6c4994e29 | 84 | virtual CmdResult OWReset() = 0; |
IanBenzMaxim | 75:8b627804927c | 85 | |
IanBenzMaxim | 76:84e6c4994e29 | 86 | /// Send and receive one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 87 | /// @param[in,out] sendRecvBit Buffer containing the bit to send on 1-Wire bus in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 88 | /// Read data from 1-Wire bus will be returned in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 89 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 90 | virtual CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 91 | |
IanBenzMaxim | 76:84e6c4994e29 | 92 | /// Send one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 93 | /// @param sendByte Byte to send on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 94 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 95 | virtual CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel) = 0; |
IanBenzMaxim | 76:84e6c4994e29 | 96 | |
IanBenzMaxim | 76:84e6c4994e29 | 97 | /// Receive one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 98 | /// @param recvByte Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 99 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 100 | virtual CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 101 | |
IanBenzMaxim | 76:84e6c4994e29 | 102 | /// Send a block of communication on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 103 | /// @param[in] sendBuf Buffer to send on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 104 | /// @param sendLen Length of the buffer to send. |
j3 | 141:cf38f48a2a49 | 105 | virtual CmdResult OWWriteBlock(const uint8_t *sendBuf, size_t sendLen); |
IanBenzMaxim | 74:23be10c32fa3 | 106 | |
IanBenzMaxim | 76:84e6c4994e29 | 107 | /// Receive a block of communication on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 108 | /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 109 | /// @param recvLen Length of the buffer to receive. |
j3 | 141:cf38f48a2a49 | 110 | virtual CmdResult OWReadBlock(uint8_t *recvBuf, size_t recvLen); |
IanBenzMaxim | 74:23be10c32fa3 | 111 | |
IanBenzMaxim | 76:84e6c4994e29 | 112 | /// Set the 1-Wire bus communication speed. |
IanBenzMaxim | 76:84e6c4994e29 | 113 | virtual CmdResult OWSetSpeed(OWSpeed newSpeed) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 114 | |
IanBenzMaxim | 76:84e6c4994e29 | 115 | /// Set the 1-Wire bus level. |
IanBenzMaxim | 76:84e6c4994e29 | 116 | virtual CmdResult OWSetLevel(OWLevel newLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 117 | |
IanBenzMaxim | 76:84e6c4994e29 | 118 | /**********************************************************//** |
IanBenzMaxim | 76:84e6c4994e29 | 119 | * @brief 1-Wire Triplet operation. |
IanBenzMaxim | 76:84e6c4994e29 | 120 | * |
IanBenzMaxim | 76:84e6c4994e29 | 121 | * @details Perform one bit of a 1-Wire search. This command |
IanBenzMaxim | 76:84e6c4994e29 | 122 | * does two read bits and one write bit. The write bit is either |
IanBenzMaxim | 76:84e6c4994e29 | 123 | * the default direction (all device have same bit) or in case |
IanBenzMaxim | 79:7f22823a5a2d | 124 | * of a discrepancy, the 'searchDirection' parameter is used. |
IanBenzMaxim | 76:84e6c4994e29 | 125 | * |
IanBenzMaxim | 79:7f22823a5a2d | 126 | * @param[in,out] searchDirection |
IanBenzMaxim | 76:84e6c4994e29 | 127 | * Input with desired direction in case both read bits are zero. |
IanBenzMaxim | 76:84e6c4994e29 | 128 | * Output with direction taken based on read bits. |
IanBenzMaxim | 76:84e6c4994e29 | 129 | * |
IanBenzMaxim | 76:84e6c4994e29 | 130 | * @param[out] sbr Bit result of first read operation. |
IanBenzMaxim | 76:84e6c4994e29 | 131 | * @param[out] tsb Bit result of second read operation. |
IanBenzMaxim | 76:84e6c4994e29 | 132 | **************************************************************/ |
IanBenzMaxim | 76:84e6c4994e29 | 133 | virtual CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb); |
IanBenzMaxim | 75:8b627804927c | 134 | |
IanBenzMaxim | 76:84e6c4994e29 | 135 | /// Send one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 136 | /// @param sendBit Buffer containing the bit to send on 1-Wire bus in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 137 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 138 | CmdResult OWWriteBitSetLevel(uint8_t sendBit, OWLevel afterLevel) { return OWTouchBitSetLevel(sendBit, afterLevel); } |
IanBenzMaxim | 75:8b627804927c | 139 | |
IanBenzMaxim | 76:84e6c4994e29 | 140 | /// Receive one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 141 | /// @param[out] sendRecvBit Read data from 1-Wire bus will be returned in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 142 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 143 | CmdResult OWReadBitSetLevel(uint8_t & recvBit, OWLevel afterLevel) { recvBit = 0x01; return OWTouchBitSetLevel(recvBit, afterLevel); } |
IanBenzMaxim | 75:8b627804927c | 144 | |
IanBenzMaxim | 76:84e6c4994e29 | 145 | // Alternate forms of read and write functions |
IanBenzMaxim | 76:84e6c4994e29 | 146 | CmdResult OWWriteBit(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 147 | CmdResult OWReadBit(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 148 | CmdResult OWWriteBitPower(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 149 | CmdResult OWReadBitPower(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 150 | CmdResult OWWriteByte(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 151 | CmdResult OWReadByte(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 152 | CmdResult OWWriteBytePower(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 153 | CmdResult OWReadBytePower(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 154 | }; |
IanBenzMaxim | 73:2cecc1372acc | 155 | } |
j3 | 15:f6cb0d906fb6 | 156 | |
IanBenzMaxim | 74:23be10c32fa3 | 157 | #endif |