Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Masters/DS248x/DS248x.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:
- 78:0cbbac7f2016
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 | 1:91e52f8ab8bf | 1 | /******************************************************************//** |
j3 | 1:91e52f8ab8bf | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 1:91e52f8ab8bf | 3 | * |
j3 | 1:91e52f8ab8bf | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 1:91e52f8ab8bf | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 1:91e52f8ab8bf | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 1:91e52f8ab8bf | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 1:91e52f8ab8bf | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 1:91e52f8ab8bf | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 1:91e52f8ab8bf | 10 | * |
j3 | 1:91e52f8ab8bf | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 1:91e52f8ab8bf | 12 | * in all copies or substantial portions of the Software. |
j3 | 1:91e52f8ab8bf | 13 | * |
j3 | 1:91e52f8ab8bf | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 1:91e52f8ab8bf | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 1:91e52f8ab8bf | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 1:91e52f8ab8bf | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 1:91e52f8ab8bf | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 1:91e52f8ab8bf | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 1:91e52f8ab8bf | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 1:91e52f8ab8bf | 21 | * |
j3 | 1:91e52f8ab8bf | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 1:91e52f8ab8bf | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 1:91e52f8ab8bf | 24 | * Products, Inc. Branding Policy. |
j3 | 1:91e52f8ab8bf | 25 | * |
j3 | 1:91e52f8ab8bf | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 1:91e52f8ab8bf | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 1:91e52f8ab8bf | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 1:91e52f8ab8bf | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 1:91e52f8ab8bf | 30 | * ownership rights. |
j3 | 1:91e52f8ab8bf | 31 | **********************************************************************/ |
j3 | 1:91e52f8ab8bf | 32 | |
IanBenzMaxim | 73:2cecc1372acc | 33 | #ifndef OneWire_Masters_DS248x |
IanBenzMaxim | 73:2cecc1372acc | 34 | #define OneWire_Masters_DS248x |
j3 | 1:91e52f8ab8bf | 35 | |
IanBenzMaxim | 73:2cecc1372acc | 36 | #include "Masters/OneWireMaster.h" |
IanBenzMaxim | 73:2cecc1372acc | 37 | #include "PinNames.h" |
j3 | 1:91e52f8ab8bf | 38 | |
IanBenzMaxim | 73:2cecc1372acc | 39 | namespace mbed { class I2C; } |
j3 | 1:91e52f8ab8bf | 40 | |
IanBenzMaxim | 73:2cecc1372acc | 41 | namespace OneWire |
j3 | 1:91e52f8ab8bf | 42 | { |
IanBenzMaxim | 76:84e6c4994e29 | 43 | /// Interface to the DS2484, DS2482-100, DS2482-101, DS2482-800 1-Wire masters. |
IanBenzMaxim | 76:84e6c4994e29 | 44 | class DS248x : public OneWireMaster |
j3 | 5:ce108eeb878d | 45 | { |
IanBenzMaxim | 76:84e6c4994e29 | 46 | public: |
IanBenzMaxim | 76:84e6c4994e29 | 47 | /// Device register pointers. |
IanBenzMaxim | 76:84e6c4994e29 | 48 | enum Register |
IanBenzMaxim | 76:84e6c4994e29 | 49 | { |
IanBenzMaxim | 76:84e6c4994e29 | 50 | ConfigReg = 0xC3, |
IanBenzMaxim | 76:84e6c4994e29 | 51 | StatusReg = 0xF0, |
IanBenzMaxim | 76:84e6c4994e29 | 52 | ReadDataReg = 0xE1, |
IanBenzMaxim | 76:84e6c4994e29 | 53 | PortConfigReg = 0xB4, |
IanBenzMaxim | 76:84e6c4994e29 | 54 | ChannelSelectReg = 0xD2 // DS2482-800 only |
IanBenzMaxim | 76:84e6c4994e29 | 55 | }; |
IanBenzMaxim | 76:84e6c4994e29 | 56 | |
IanBenzMaxim | 76:84e6c4994e29 | 57 | /// 1-Wire port adjustment parameters. |
IanBenzMaxim | 76:84e6c4994e29 | 58 | /// @note DS2484 only |
IanBenzMaxim | 76:84e6c4994e29 | 59 | enum OwAdjustParam |
IanBenzMaxim | 76:84e6c4994e29 | 60 | { |
IanBenzMaxim | 76:84e6c4994e29 | 61 | tRSTL = 0, |
IanBenzMaxim | 76:84e6c4994e29 | 62 | tRSTL_OD, |
IanBenzMaxim | 76:84e6c4994e29 | 63 | tMSP, |
IanBenzMaxim | 76:84e6c4994e29 | 64 | tMSP_OD, |
IanBenzMaxim | 76:84e6c4994e29 | 65 | tW0L, |
IanBenzMaxim | 76:84e6c4994e29 | 66 | tW0L_OD, |
IanBenzMaxim | 76:84e6c4994e29 | 67 | tREC0, // OD NA |
IanBenzMaxim | 76:84e6c4994e29 | 68 | RWPU = 8 // OD NA, see DS2484 datasheet page 13 |
IanBenzMaxim | 76:84e6c4994e29 | 69 | }; |
IanBenzMaxim | 76:84e6c4994e29 | 70 | |
IanBenzMaxim | 76:84e6c4994e29 | 71 | /// Represents a DS248x configuration. |
IanBenzMaxim | 76:84e6c4994e29 | 72 | class Config |
IanBenzMaxim | 73:2cecc1372acc | 73 | { |
IanBenzMaxim | 73:2cecc1372acc | 74 | public: |
IanBenzMaxim | 76:84e6c4994e29 | 75 | /// @{ |
IanBenzMaxim | 76:84e6c4994e29 | 76 | /// 1-Wire Speed |
IanBenzMaxim | 76:84e6c4994e29 | 77 | bool get1WS() const { return m_1WS; } |
IanBenzMaxim | 76:84e6c4994e29 | 78 | void set1WS(bool new1WS) { m_1WS = new1WS; } |
IanBenzMaxim | 76:84e6c4994e29 | 79 | /// @} |
IanBenzMaxim | 74:23be10c32fa3 | 80 | |
IanBenzMaxim | 76:84e6c4994e29 | 81 | /// @{ |
IanBenzMaxim | 76:84e6c4994e29 | 82 | /// Strong Pullup |
IanBenzMaxim | 76:84e6c4994e29 | 83 | bool getSPU() const { return m_SPU; } |
IanBenzMaxim | 76:84e6c4994e29 | 84 | void setSPU(bool newSPU) { m_SPU = newSPU; } |
IanBenzMaxim | 76:84e6c4994e29 | 85 | /// @} |
IanBenzMaxim | 69:f915c4c59a69 | 86 | |
IanBenzMaxim | 76:84e6c4994e29 | 87 | /// @{ |
IanBenzMaxim | 76:84e6c4994e29 | 88 | /// 1-Wire Power Down |
IanBenzMaxim | 76:84e6c4994e29 | 89 | bool getPDN() const { return m_PDN; } |
IanBenzMaxim | 76:84e6c4994e29 | 90 | void setPDN(bool newPDN) { m_PDN = newPDN; } |
IanBenzMaxim | 76:84e6c4994e29 | 91 | /// @} |
IanBenzMaxim | 74:23be10c32fa3 | 92 | |
IanBenzMaxim | 76:84e6c4994e29 | 93 | /// @{ |
IanBenzMaxim | 76:84e6c4994e29 | 94 | /// Active Pullup |
IanBenzMaxim | 76:84e6c4994e29 | 95 | bool getAPU() const { return m_APU; } |
IanBenzMaxim | 76:84e6c4994e29 | 96 | void setAPU(bool newAPU) { m_APU = newAPU; } |
IanBenzMaxim | 76:84e6c4994e29 | 97 | /// @} |
IanBenzMaxim | 74:23be10c32fa3 | 98 | |
IanBenzMaxim | 76:84e6c4994e29 | 99 | /// Byte representation that is read from the DS2465. |
IanBenzMaxim | 76:84e6c4994e29 | 100 | uint8_t readByte() const; |
IanBenzMaxim | 76:84e6c4994e29 | 101 | /// Byte respresentation that is written to the DS2465. |
IanBenzMaxim | 76:84e6c4994e29 | 102 | uint8_t writeByte() const; |
IanBenzMaxim | 74:23be10c32fa3 | 103 | |
IanBenzMaxim | 76:84e6c4994e29 | 104 | /// Reset to the power-on default config. |
IanBenzMaxim | 76:84e6c4994e29 | 105 | void reset(); |
IanBenzMaxim | 76:84e6c4994e29 | 106 | Config() { reset(); } |
IanBenzMaxim | 74:23be10c32fa3 | 107 | |
IanBenzMaxim | 73:2cecc1372acc | 108 | private: |
IanBenzMaxim | 76:84e6c4994e29 | 109 | bool m_1WS, m_SPU, m_PDN, m_APU; |
IanBenzMaxim | 76:84e6c4994e29 | 110 | }; |
IanBenzMaxim | 76:84e6c4994e29 | 111 | |
IanBenzMaxim | 76:84e6c4994e29 | 112 | /// Construct to use an existing I2C interface. |
IanBenzMaxim | 76:84e6c4994e29 | 113 | /// @param i2c_bus Configured I2C communication interface for DS248x. |
IanBenzMaxim | 76:84e6c4994e29 | 114 | /// @param adrs I2C bus address of the DS248x in mbed format. |
IanBenzMaxim | 76:84e6c4994e29 | 115 | DS248x(mbed::I2C & i2c_bus, uint8_t adrs); |
IanBenzMaxim | 76:84e6c4994e29 | 116 | |
IanBenzMaxim | 76:84e6c4994e29 | 117 | /// Construct with a new I2C interface. |
IanBenzMaxim | 76:84e6c4994e29 | 118 | /// @param sda SDA pin of the I2C bus. |
IanBenzMaxim | 76:84e6c4994e29 | 119 | /// @param scl SCL pin of the I2C bus. |
IanBenzMaxim | 76:84e6c4994e29 | 120 | /// @param adrs I2C bus address of the DS248x in mbed format. |
IanBenzMaxim | 76:84e6c4994e29 | 121 | DS248x(PinName sda, PinName scl, uint8_t adrs); |
IanBenzMaxim | 76:84e6c4994e29 | 122 | |
IanBenzMaxim | 76:84e6c4994e29 | 123 | /// Destroys I2C interface if owner. |
IanBenzMaxim | 76:84e6c4994e29 | 124 | virtual ~DS248x(); |
IanBenzMaxim | 76:84e6c4994e29 | 125 | |
IanBenzMaxim | 76:84e6c4994e29 | 126 | /// Performs a soft reset on the DS248x. |
IanBenzMaxim | 76:84e6c4994e29 | 127 | /// @note This is note a 1-Wire Reset. |
IanBenzMaxim | 76:84e6c4994e29 | 128 | OneWireMaster::CmdResult reset(void); |
IanBenzMaxim | 76:84e6c4994e29 | 129 | |
IanBenzMaxim | 76:84e6c4994e29 | 130 | /// Write a new configuration to the DS248x. |
IanBenzMaxim | 76:84e6c4994e29 | 131 | /// @param[in] config New configuration to write. |
IanBenzMaxim | 76:84e6c4994e29 | 132 | /// @param verify Verify that the configuration was written successfully. |
IanBenzMaxim | 76:84e6c4994e29 | 133 | OneWireMaster::CmdResult writeConfig(const Config & config, bool verify); |
IanBenzMaxim | 76:84e6c4994e29 | 134 | |
IanBenzMaxim | 76:84e6c4994e29 | 135 | /// Read the current DS248x configuration. |
IanBenzMaxim | 76:84e6c4994e29 | 136 | /// @returns The cached current configuration. |
IanBenzMaxim | 76:84e6c4994e29 | 137 | Config currentConfig() const { return m_curConfig; } |
IanBenzMaxim | 76:84e6c4994e29 | 138 | |
IanBenzMaxim | 76:84e6c4994e29 | 139 | /// Reads a register from the DS248x. |
IanBenzMaxim | 76:84e6c4994e29 | 140 | /// @param reg Register to read from. |
IanBenzMaxim | 76:84e6c4994e29 | 141 | /// @param[out] buf Buffer to hold read data. |
IanBenzMaxim | 76:84e6c4994e29 | 142 | /// @param skipSetPointer Assume that the read pointer is already set to the correct register. |
IanBenzMaxim | 76:84e6c4994e29 | 143 | OneWireMaster::CmdResult readRegister(Register reg, uint8_t & buf, bool skipSetPointer = false) const; |
IanBenzMaxim | 76:84e6c4994e29 | 144 | |
IanBenzMaxim | 76:84e6c4994e29 | 145 | /// Select the 1-Wire channel on a DS2482-800. |
IanBenzMaxim | 76:84e6c4994e29 | 146 | /// @note DS2482-800 only |
IanBenzMaxim | 76:84e6c4994e29 | 147 | /// @param channel Channel number to select beginning at 1. |
IanBenzMaxim | 76:84e6c4994e29 | 148 | OneWireMaster::CmdResult selectChannel(uint8_t channel); |
IanBenzMaxim | 76:84e6c4994e29 | 149 | |
IanBenzMaxim | 76:84e6c4994e29 | 150 | /// Adjust 1-Wire port paramaters. |
IanBenzMaxim | 76:84e6c4994e29 | 151 | /// @note DS2484 only |
IanBenzMaxim | 76:84e6c4994e29 | 152 | /// @param param Parameter to adjust. |
IanBenzMaxim | 76:84e6c4994e29 | 153 | /// @param val New parameter value to set. Consult datasheet for value mappings. |
IanBenzMaxim | 76:84e6c4994e29 | 154 | OneWireMaster::CmdResult adjustOwPort(OwAdjustParam param, uint8_t val); |
IanBenzMaxim | 74:23be10c32fa3 | 155 | |
IanBenzMaxim | 76:84e6c4994e29 | 156 | /// @details Performs a device reset followed by writing the configuration byte to default values: |
IanBenzMaxim | 76:84e6c4994e29 | 157 | /// 1-Wire Speed Standard |
IanBenzMaxim | 76:84e6c4994e29 | 158 | /// Strong Pullup Off |
IanBenzMaxim | 76:84e6c4994e29 | 159 | /// 1-Wire Powerdown Off |
IanBenzMaxim | 76:84e6c4994e29 | 160 | /// Active Pullup On |
IanBenzMaxim | 76:84e6c4994e29 | 161 | virtual OneWireMaster::CmdResult OWInitMaster(); |
IanBenzMaxim | 76:84e6c4994e29 | 162 | /// @note Perform a 1-Wire triplet using the DS248x command. |
IanBenzMaxim | 76:84e6c4994e29 | 163 | virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb); |
IanBenzMaxim | 76:84e6c4994e29 | 164 | virtual OneWireMaster::CmdResult OWReset(); |
IanBenzMaxim | 76:84e6c4994e29 | 165 | virtual OneWireMaster::CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel); |
IanBenzMaxim | 76:84e6c4994e29 | 166 | virtual OneWireMaster::CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel); |
IanBenzMaxim | 76:84e6c4994e29 | 167 | virtual OneWireMaster::CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel); |
IanBenzMaxim | 76:84e6c4994e29 | 168 | virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed newSpeed); |
IanBenzMaxim | 76:84e6c4994e29 | 169 | virtual OneWireMaster::CmdResult OWSetLevel(OWLevel newLevel); |
IanBenzMaxim | 73:2cecc1372acc | 170 | |
IanBenzMaxim | 76:84e6c4994e29 | 171 | private: |
IanBenzMaxim | 76:84e6c4994e29 | 172 | enum Command |
IanBenzMaxim | 76:84e6c4994e29 | 173 | { |
IanBenzMaxim | 76:84e6c4994e29 | 174 | DeviceResetCmd = 0xF0, |
IanBenzMaxim | 76:84e6c4994e29 | 175 | WriteDeviceConfigCmd = 0xD2, |
IanBenzMaxim | 76:84e6c4994e29 | 176 | AdjustOwPortCmd = 0xC3, // DS2484 only |
IanBenzMaxim | 76:84e6c4994e29 | 177 | ChannelSelectCmd = 0xC3, // DS2482-800 only |
IanBenzMaxim | 76:84e6c4994e29 | 178 | SetReadPointerCmd = 0xE1, |
IanBenzMaxim | 76:84e6c4994e29 | 179 | OwResetCmd = 0xB4, |
IanBenzMaxim | 76:84e6c4994e29 | 180 | OwWriteByteCmd = 0xA5, |
IanBenzMaxim | 76:84e6c4994e29 | 181 | OwReadByteCmd = 0x96, |
IanBenzMaxim | 76:84e6c4994e29 | 182 | OwSingleBitCmd = 0x87, |
IanBenzMaxim | 76:84e6c4994e29 | 183 | OwTripletCmd = 0x78 |
IanBenzMaxim | 76:84e6c4994e29 | 184 | }; |
IanBenzMaxim | 69:f915c4c59a69 | 185 | |
IanBenzMaxim | 76:84e6c4994e29 | 186 | mbed::I2C *m_p_i2c_bus; |
IanBenzMaxim | 76:84e6c4994e29 | 187 | uint8_t m_adrs; |
IanBenzMaxim | 76:84e6c4994e29 | 188 | bool m_i2c_owner; |
IanBenzMaxim | 76:84e6c4994e29 | 189 | Config m_curConfig; |
IanBenzMaxim | 76:84e6c4994e29 | 190 | |
IanBenzMaxim | 76:84e6c4994e29 | 191 | /// Polls the DS2465 status waiting for the 1-Wire Busy bit (1WB) to be cleared. |
IanBenzMaxim | 76:84e6c4994e29 | 192 | /// @param[out] pStatus Optionally retrive the status byte when 1WB cleared. |
IanBenzMaxim | 76:84e6c4994e29 | 193 | /// @returns Success or TimeoutError if poll limit reached. |
IanBenzMaxim | 76:84e6c4994e29 | 194 | OneWireMaster::CmdResult pollBusy(uint8_t * pStatus = NULL); |
IanBenzMaxim | 74:23be10c32fa3 | 195 | |
IanBenzMaxim | 76:84e6c4994e29 | 196 | /// Ensure that the desired 1-Wire level is set in the configuration. |
IanBenzMaxim | 76:84e6c4994e29 | 197 | /// @param level Desired 1-Wire level. |
IanBenzMaxim | 76:84e6c4994e29 | 198 | OneWireMaster::CmdResult configureLevel(OWLevel level); |
IanBenzMaxim | 76:84e6c4994e29 | 199 | |
IanBenzMaxim | 76:84e6c4994e29 | 200 | /// @note Allow marking const since not public. |
IanBenzMaxim | 76:84e6c4994e29 | 201 | OneWireMaster::CmdResult sendCommand(Command cmd) const; |
IanBenzMaxim | 76:84e6c4994e29 | 202 | |
IanBenzMaxim | 76:84e6c4994e29 | 203 | /// @note Allow marking const since not public. |
IanBenzMaxim | 76:84e6c4994e29 | 204 | OneWireMaster::CmdResult sendCommand(Command cmd, uint8_t param) const; |
IanBenzMaxim | 76:84e6c4994e29 | 205 | }; |
IanBenzMaxim | 73:2cecc1372acc | 206 | } |
j3 | 1:91e52f8ab8bf | 207 | |
IanBenzMaxim | 75:8b627804927c | 208 | #endif |