Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Masters/OneWireMaster.h@76:84e6c4994e29, 2016-05-14 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Sat May 14 14:27:56 2016 -0500
- Revision:
- 76:84e6c4994e29
- Parent:
- 75:8b627804927c
- Child:
- 77:529edb329ee0
Move ROM commands outside of OneWireMaster to increase cohesiveness of the class. Do not use subdivide OneWire namespace since it will likely not provide value on this project.
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 | #include "RomId.h" |
IanBenzMaxim | 73:2cecc1372acc | 39 | |
IanBenzMaxim | 73:2cecc1372acc | 40 | namespace OneWire |
IanBenzMaxim | 73:2cecc1372acc | 41 | { |
IanBenzMaxim | 76:84e6c4994e29 | 42 | /// Base class for all 1-Wire Masters. |
IanBenzMaxim | 76:84e6c4994e29 | 43 | class OneWireMaster |
IanBenzMaxim | 32:bce180b544ed | 44 | { |
IanBenzMaxim | 76:84e6c4994e29 | 45 | public: |
IanBenzMaxim | 76:84e6c4994e29 | 46 | /// Speed of the 1-Wire bus |
IanBenzMaxim | 76:84e6c4994e29 | 47 | enum OWSpeed |
IanBenzMaxim | 74:23be10c32fa3 | 48 | { |
IanBenzMaxim | 76:84e6c4994e29 | 49 | StandardSpeed = 0x00, |
IanBenzMaxim | 76:84e6c4994e29 | 50 | OverdriveSpeed = 0x01 |
IanBenzMaxim | 76:84e6c4994e29 | 51 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 52 | |
IanBenzMaxim | 76:84e6c4994e29 | 53 | /// Level of the 1-Wire bus |
IanBenzMaxim | 76:84e6c4994e29 | 54 | enum OWLevel |
IanBenzMaxim | 76:84e6c4994e29 | 55 | { |
IanBenzMaxim | 76:84e6c4994e29 | 56 | NormalLevel = 0x00, |
IanBenzMaxim | 76:84e6c4994e29 | 57 | StrongLevel = 0x02 |
IanBenzMaxim | 76:84e6c4994e29 | 58 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 59 | |
IanBenzMaxim | 76:84e6c4994e29 | 60 | /// Search direction for the Triplet |
IanBenzMaxim | 76:84e6c4994e29 | 61 | enum SearchDirection |
IanBenzMaxim | 76:84e6c4994e29 | 62 | { |
IanBenzMaxim | 76:84e6c4994e29 | 63 | WriteZero = 0, |
IanBenzMaxim | 76:84e6c4994e29 | 64 | WriteOne = 1 |
IanBenzMaxim | 76:84e6c4994e29 | 65 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 66 | |
IanBenzMaxim | 76:84e6c4994e29 | 67 | /// Result of all 1-Wire commands |
IanBenzMaxim | 76:84e6c4994e29 | 68 | enum CmdResult |
IanBenzMaxim | 76:84e6c4994e29 | 69 | { |
IanBenzMaxim | 76:84e6c4994e29 | 70 | Success, |
IanBenzMaxim | 76:84e6c4994e29 | 71 | CommunicationWriteError, |
IanBenzMaxim | 76:84e6c4994e29 | 72 | CommunicationReadError, |
IanBenzMaxim | 76:84e6c4994e29 | 73 | TimeoutError, |
IanBenzMaxim | 76:84e6c4994e29 | 74 | OperationFailure |
IanBenzMaxim | 76:84e6c4994e29 | 75 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 76 | |
IanBenzMaxim | 76:84e6c4994e29 | 77 | /// Perform a CRC16 calculation. |
IanBenzMaxim | 76:84e6c4994e29 | 78 | /// @param crc16 Beginning state of the CRC generator. |
IanBenzMaxim | 76:84e6c4994e29 | 79 | /// @param data Data to pass though the CRC generator. |
IanBenzMaxim | 76:84e6c4994e29 | 80 | /// @returns The calculated CRC16. |
IanBenzMaxim | 76:84e6c4994e29 | 81 | static uint16_t calculateCrc16(uint16_t crc16, uint16_t data); |
IanBenzMaxim | 74:23be10c32fa3 | 82 | |
IanBenzMaxim | 76:84e6c4994e29 | 83 | /// Perform a CRC16 calculation with variable length data. |
IanBenzMaxim | 76:84e6c4994e29 | 84 | /// @param[in] data Data array to pass through the CRC generator. |
IanBenzMaxim | 76:84e6c4994e29 | 85 | /// @param data_offset Offset of the data array to begin processing. |
IanBenzMaxim | 76:84e6c4994e29 | 86 | /// @param data_len Length of the data array to process. |
IanBenzMaxim | 76:84e6c4994e29 | 87 | /// @param crc Beginning state of the CRC generator. |
IanBenzMaxim | 76:84e6c4994e29 | 88 | /// @returns The calculated CRC16. |
IanBenzMaxim | 76:84e6c4994e29 | 89 | static uint16_t calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc = 0); |
IanBenzMaxim | 75:8b627804927c | 90 | |
IanBenzMaxim | 76:84e6c4994e29 | 91 | /// Allow freeing through a base class pointer. |
IanBenzMaxim | 76:84e6c4994e29 | 92 | virtual ~OneWireMaster() { } |
IanBenzMaxim | 75:8b627804927c | 93 | |
IanBenzMaxim | 76:84e6c4994e29 | 94 | /// Initialize a master for use. |
IanBenzMaxim | 76:84e6c4994e29 | 95 | virtual CmdResult OWInitMaster() = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 96 | |
IanBenzMaxim | 76:84e6c4994e29 | 97 | /// Reset all of the devices on the 1-Wire bus and check for a presence pulse. |
IanBenzMaxim | 76:84e6c4994e29 | 98 | /// @returns OperationFailure if reset was performed but no presence pulse was detected. |
IanBenzMaxim | 76:84e6c4994e29 | 99 | virtual CmdResult OWReset() = 0; |
IanBenzMaxim | 75:8b627804927c | 100 | |
IanBenzMaxim | 76:84e6c4994e29 | 101 | /// Send and receive one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 102 | /// @param[in,out] sendRecvBit Buffer containing the bit to send on 1-Wire bus in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 103 | /// Read data from 1-Wire bus will be returned in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 104 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 105 | virtual CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 106 | |
IanBenzMaxim | 76:84e6c4994e29 | 107 | /// Send one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 108 | /// @param sendByte Byte to send on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 109 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 110 | virtual CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel) = 0; |
IanBenzMaxim | 76:84e6c4994e29 | 111 | |
IanBenzMaxim | 76:84e6c4994e29 | 112 | /// Receive one byte of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 113 | /// @param recvByte Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 114 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 115 | virtual CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 116 | |
IanBenzMaxim | 76:84e6c4994e29 | 117 | /// Send a block of communication on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 118 | /// @param[in] sendBuf Buffer to send on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 119 | /// @param sendLen Length of the buffer to send. |
IanBenzMaxim | 76:84e6c4994e29 | 120 | virtual CmdResult OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen); |
IanBenzMaxim | 74:23be10c32fa3 | 121 | |
IanBenzMaxim | 76:84e6c4994e29 | 122 | /// Receive a block of communication on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 123 | /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 124 | /// @param recvLen Length of the buffer to receive. |
IanBenzMaxim | 76:84e6c4994e29 | 125 | virtual CmdResult OWReadBlock(uint8_t *recvBuf, uint8_t recvLen); |
IanBenzMaxim | 74:23be10c32fa3 | 126 | |
IanBenzMaxim | 76:84e6c4994e29 | 127 | /// Set the 1-Wire bus communication speed. |
IanBenzMaxim | 76:84e6c4994e29 | 128 | virtual CmdResult OWSetSpeed(OWSpeed newSpeed) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 129 | |
IanBenzMaxim | 76:84e6c4994e29 | 130 | /// Set the 1-Wire bus level. |
IanBenzMaxim | 76:84e6c4994e29 | 131 | virtual CmdResult OWSetLevel(OWLevel newLevel) = 0; |
IanBenzMaxim | 74:23be10c32fa3 | 132 | |
IanBenzMaxim | 76:84e6c4994e29 | 133 | /**********************************************************//** |
IanBenzMaxim | 76:84e6c4994e29 | 134 | * @brief 1-Wire Triplet operation. |
IanBenzMaxim | 76:84e6c4994e29 | 135 | * |
IanBenzMaxim | 76:84e6c4994e29 | 136 | * @details Perform one bit of a 1-Wire search. This command |
IanBenzMaxim | 76:84e6c4994e29 | 137 | * does two read bits and one write bit. The write bit is either |
IanBenzMaxim | 76:84e6c4994e29 | 138 | * the default direction (all device have same bit) or in case |
IanBenzMaxim | 76:84e6c4994e29 | 139 | * of a discrepancy, the 'search_direction' parameter is used. |
IanBenzMaxim | 76:84e6c4994e29 | 140 | * |
IanBenzMaxim | 76:84e6c4994e29 | 141 | * @param[in,out] search_direction |
IanBenzMaxim | 76:84e6c4994e29 | 142 | * Input with desired direction in case both read bits are zero. |
IanBenzMaxim | 76:84e6c4994e29 | 143 | * Output with direction taken based on read bits. |
IanBenzMaxim | 76:84e6c4994e29 | 144 | * |
IanBenzMaxim | 76:84e6c4994e29 | 145 | * @param[out] sbr Bit result of first read operation. |
IanBenzMaxim | 76:84e6c4994e29 | 146 | * @param[out] tsb Bit result of second read operation. |
IanBenzMaxim | 76:84e6c4994e29 | 147 | **************************************************************/ |
IanBenzMaxim | 76:84e6c4994e29 | 148 | virtual CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb); |
IanBenzMaxim | 75:8b627804927c | 149 | |
IanBenzMaxim | 76:84e6c4994e29 | 150 | /// Send one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 151 | /// @param sendBit Buffer containing the bit to send on 1-Wire bus in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 152 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 153 | CmdResult OWWriteBitSetLevel(uint8_t sendBit, OWLevel afterLevel) { return OWTouchBitSetLevel(sendBit, afterLevel); } |
IanBenzMaxim | 75:8b627804927c | 154 | |
IanBenzMaxim | 76:84e6c4994e29 | 155 | /// Receive one bit of communication and set a new level on the 1-Wire bus. |
IanBenzMaxim | 76:84e6c4994e29 | 156 | /// @param[out] sendRecvBit Read data from 1-Wire bus will be returned in lsb. |
IanBenzMaxim | 76:84e6c4994e29 | 157 | /// @param afterLevel Level to set the 1-Wire bus to after communication. |
IanBenzMaxim | 76:84e6c4994e29 | 158 | CmdResult OWReadBitSetLevel(uint8_t & recvBit, OWLevel afterLevel) { recvBit = 0x01; return OWTouchBitSetLevel(recvBit, afterLevel); } |
IanBenzMaxim | 75:8b627804927c | 159 | |
IanBenzMaxim | 76:84e6c4994e29 | 160 | // Alternate forms of read and write functions |
IanBenzMaxim | 76:84e6c4994e29 | 161 | CmdResult OWWriteBit(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 162 | CmdResult OWReadBit(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 163 | CmdResult OWWriteBitPower(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 164 | CmdResult OWReadBitPower(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 165 | CmdResult OWWriteByte(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 166 | CmdResult OWReadByte(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, NormalLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 167 | CmdResult OWWriteBytePower(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 168 | CmdResult OWReadBytePower(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, StrongLevel); } |
IanBenzMaxim | 76:84e6c4994e29 | 169 | }; |
IanBenzMaxim | 73:2cecc1372acc | 170 | } |
j3 | 15:f6cb0d906fb6 | 171 | |
IanBenzMaxim | 74:23be10c32fa3 | 172 | #endif |