Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Masters/DS248x/DS248x.h@75:8b627804927c, 2016-05-13 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 13 14:52:50 2016 -0500
- Revision:
- 75:8b627804927c
- Parent:
- 74:23be10c32fa3
- Child:
- 76:84e6c4994e29
Code cleanup.
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 | 73:2cecc1372acc | 43 | namespace Masters |
j3 | 5:ce108eeb878d | 44 | { |
IanBenzMaxim | 75:8b627804927c | 45 | /// Interface to the DS2484, DS2482-100, DS2482-101, DS2482-800 1-Wire masters. |
IanBenzMaxim | 74:23be10c32fa3 | 46 | class DS248x : public OneWireMaster |
IanBenzMaxim | 73:2cecc1372acc | 47 | { |
IanBenzMaxim | 73:2cecc1372acc | 48 | public: |
IanBenzMaxim | 75:8b627804927c | 49 | /// Device register pointers. |
IanBenzMaxim | 73:2cecc1372acc | 50 | enum Register |
IanBenzMaxim | 73:2cecc1372acc | 51 | { |
IanBenzMaxim | 73:2cecc1372acc | 52 | ConfigReg = 0xC3, |
IanBenzMaxim | 73:2cecc1372acc | 53 | StatusReg = 0xF0, |
IanBenzMaxim | 73:2cecc1372acc | 54 | ReadDataReg = 0xE1, |
IanBenzMaxim | 73:2cecc1372acc | 55 | PortConfigReg = 0xB4, |
IanBenzMaxim | 73:2cecc1372acc | 56 | ChannelSelectReg = 0xD2 // DS2482-800 only |
IanBenzMaxim | 73:2cecc1372acc | 57 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 58 | |
IanBenzMaxim | 75:8b627804927c | 59 | /// 1-Wire port adjustment parameters. |
IanBenzMaxim | 74:23be10c32fa3 | 60 | /// @note DS2484 only |
IanBenzMaxim | 73:2cecc1372acc | 61 | enum OwAdjustParam |
IanBenzMaxim | 73:2cecc1372acc | 62 | { |
IanBenzMaxim | 73:2cecc1372acc | 63 | tRSTL = 0, |
IanBenzMaxim | 73:2cecc1372acc | 64 | tRSTL_OD, |
IanBenzMaxim | 73:2cecc1372acc | 65 | tMSP, |
IanBenzMaxim | 73:2cecc1372acc | 66 | tMSP_OD, |
IanBenzMaxim | 73:2cecc1372acc | 67 | tW0L, |
IanBenzMaxim | 73:2cecc1372acc | 68 | tW0L_OD, |
IanBenzMaxim | 75:8b627804927c | 69 | tREC0, // OD NA |
IanBenzMaxim | 75:8b627804927c | 70 | RWPU = 8 // OD NA, see DS2484 datasheet page 13 |
IanBenzMaxim | 73:2cecc1372acc | 71 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 72 | |
IanBenzMaxim | 75:8b627804927c | 73 | /// Represents a DS248x configuration. |
IanBenzMaxim | 73:2cecc1372acc | 74 | class Config |
IanBenzMaxim | 73:2cecc1372acc | 75 | { |
IanBenzMaxim | 73:2cecc1372acc | 76 | public: |
IanBenzMaxim | 73:2cecc1372acc | 77 | /// @{ |
IanBenzMaxim | 73:2cecc1372acc | 78 | /// 1-Wire Speed |
IanBenzMaxim | 73:2cecc1372acc | 79 | bool get1WS() const { return m_1WS; } |
IanBenzMaxim | 73:2cecc1372acc | 80 | void set1WS(bool new1WS) { m_1WS = new1WS; } |
IanBenzMaxim | 73:2cecc1372acc | 81 | /// @} |
j3 | 5:ce108eeb878d | 82 | |
IanBenzMaxim | 73:2cecc1372acc | 83 | /// @{ |
IanBenzMaxim | 73:2cecc1372acc | 84 | /// Strong Pullup |
IanBenzMaxim | 73:2cecc1372acc | 85 | bool getSPU() const { return m_SPU; } |
IanBenzMaxim | 73:2cecc1372acc | 86 | void setSPU(bool newSPU) { m_SPU = newSPU; } |
IanBenzMaxim | 73:2cecc1372acc | 87 | /// @} |
IanBenzMaxim | 69:f915c4c59a69 | 88 | |
IanBenzMaxim | 73:2cecc1372acc | 89 | /// @{ |
IanBenzMaxim | 73:2cecc1372acc | 90 | /// 1-Wire Power Down |
IanBenzMaxim | 73:2cecc1372acc | 91 | bool getPDN() const { return m_PDN; } |
IanBenzMaxim | 73:2cecc1372acc | 92 | void setPDN(bool newPDN) { m_PDN = newPDN; } |
IanBenzMaxim | 73:2cecc1372acc | 93 | /// @} |
IanBenzMaxim | 69:f915c4c59a69 | 94 | |
IanBenzMaxim | 73:2cecc1372acc | 95 | /// @{ |
IanBenzMaxim | 73:2cecc1372acc | 96 | /// Active Pullup |
IanBenzMaxim | 73:2cecc1372acc | 97 | bool getAPU() const { return m_APU; } |
IanBenzMaxim | 73:2cecc1372acc | 98 | void setAPU(bool newAPU) { m_APU = newAPU; } |
IanBenzMaxim | 73:2cecc1372acc | 99 | /// @} |
IanBenzMaxim | 69:f915c4c59a69 | 100 | |
IanBenzMaxim | 73:2cecc1372acc | 101 | /// Byte representation that is read from the DS2465. |
IanBenzMaxim | 73:2cecc1372acc | 102 | uint8_t readByte() const; |
IanBenzMaxim | 73:2cecc1372acc | 103 | /// Byte respresentation that is written to the DS2465. |
IanBenzMaxim | 73:2cecc1372acc | 104 | uint8_t writeByte() const; |
IanBenzMaxim | 69:f915c4c59a69 | 105 | |
IanBenzMaxim | 73:2cecc1372acc | 106 | /// Reset to the power-on default config. |
IanBenzMaxim | 73:2cecc1372acc | 107 | void reset(); |
IanBenzMaxim | 73:2cecc1372acc | 108 | Config() { reset(); } |
IanBenzMaxim | 69:f915c4c59a69 | 109 | |
IanBenzMaxim | 73:2cecc1372acc | 110 | private: |
IanBenzMaxim | 73:2cecc1372acc | 111 | bool m_1WS, m_SPU, m_PDN, m_APU; |
IanBenzMaxim | 73:2cecc1372acc | 112 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 113 | |
IanBenzMaxim | 75:8b627804927c | 114 | /// Construct to use an existing I2C interface. |
IanBenzMaxim | 75:8b627804927c | 115 | /// @param i2c_bus Configured I2C communication interface for DS248x. |
IanBenzMaxim | 75:8b627804927c | 116 | /// @param adrs I2C bus address of the DS248x in mbed format. |
IanBenzMaxim | 73:2cecc1372acc | 117 | DS248x(mbed::I2C & i2c_bus, uint8_t adrs); |
IanBenzMaxim | 74:23be10c32fa3 | 118 | |
IanBenzMaxim | 75:8b627804927c | 119 | /// Construct with a new I2C interface. |
IanBenzMaxim | 75:8b627804927c | 120 | /// @param sda SDA pin of the I2C bus. |
IanBenzMaxim | 75:8b627804927c | 121 | /// @param scl SCL pin of the I2C bus. |
IanBenzMaxim | 75:8b627804927c | 122 | /// @param adrs I2C bus address of the DS248x in mbed format. |
IanBenzMaxim | 73:2cecc1372acc | 123 | DS248x(PinName sda, PinName scl, uint8_t adrs); |
IanBenzMaxim | 74:23be10c32fa3 | 124 | |
IanBenzMaxim | 75:8b627804927c | 125 | /// Destroys I2C interface if owner. |
IanBenzMaxim | 73:2cecc1372acc | 126 | virtual ~DS248x(); |
IanBenzMaxim | 74:23be10c32fa3 | 127 | |
IanBenzMaxim | 75:8b627804927c | 128 | /// Performs a soft reset on the DS248x. |
IanBenzMaxim | 75:8b627804927c | 129 | /// @note This is note a 1-Wire Reset. |
IanBenzMaxim | 73:2cecc1372acc | 130 | OneWireMaster::CmdResult reset(void); |
IanBenzMaxim | 74:23be10c32fa3 | 131 | |
IanBenzMaxim | 75:8b627804927c | 132 | /// Write a new configuration to the DS248x. |
IanBenzMaxim | 73:2cecc1372acc | 133 | /// @param[in] config New configuration to write. |
IanBenzMaxim | 73:2cecc1372acc | 134 | /// @param verify Verify that the configuration was written successfully. |
IanBenzMaxim | 73:2cecc1372acc | 135 | OneWireMaster::CmdResult writeConfig(const Config & config, bool verify); |
IanBenzMaxim | 69:f915c4c59a69 | 136 | |
IanBenzMaxim | 75:8b627804927c | 137 | /// Read the current DS248x configuration. |
IanBenzMaxim | 73:2cecc1372acc | 138 | /// @returns The cached current configuration. |
IanBenzMaxim | 73:2cecc1372acc | 139 | Config currentConfig() const { return m_curConfig; } |
IanBenzMaxim | 74:23be10c32fa3 | 140 | |
IanBenzMaxim | 75:8b627804927c | 141 | /// Reads a register from the DS248x. |
IanBenzMaxim | 75:8b627804927c | 142 | /// @param reg Register to read from. |
IanBenzMaxim | 75:8b627804927c | 143 | /// @param[out] buf Buffer to hold read data. |
IanBenzMaxim | 75:8b627804927c | 144 | /// @param skipSetPointer Assume that the read pointer is already set to the correct register. |
IanBenzMaxim | 73:2cecc1372acc | 145 | OneWireMaster::CmdResult readRegister(Register reg, uint8_t & buf, bool skipSetPointer = false) const; |
IanBenzMaxim | 74:23be10c32fa3 | 146 | |
IanBenzMaxim | 75:8b627804927c | 147 | /// Select the 1-Wire channel on a DS2482-800. |
IanBenzMaxim | 75:8b627804927c | 148 | /// @note DS2482-800 only |
IanBenzMaxim | 75:8b627804927c | 149 | /// @param channel Channel number to select beginning at 1. |
IanBenzMaxim | 73:2cecc1372acc | 150 | OneWireMaster::CmdResult selectChannel(uint8_t channel); |
IanBenzMaxim | 74:23be10c32fa3 | 151 | |
IanBenzMaxim | 75:8b627804927c | 152 | /// Adjust 1-Wire port paramaters. |
IanBenzMaxim | 75:8b627804927c | 153 | /// @note DS2484 only |
IanBenzMaxim | 75:8b627804927c | 154 | /// @param param Parameter to adjust. |
IanBenzMaxim | 75:8b627804927c | 155 | /// @param val New parameter value to set. Consult datasheet for value mappings. |
IanBenzMaxim | 73:2cecc1372acc | 156 | OneWireMaster::CmdResult adjustOwPort(OwAdjustParam param, uint8_t val); |
IanBenzMaxim | 74:23be10c32fa3 | 157 | |
IanBenzMaxim | 75:8b627804927c | 158 | /// @details Performs a device reset followed by writing the configuration byte to default values: |
IanBenzMaxim | 75:8b627804927c | 159 | /// 1-Wire Speed Standard |
IanBenzMaxim | 75:8b627804927c | 160 | /// Strong Pullup Off |
IanBenzMaxim | 75:8b627804927c | 161 | /// 1-Wire Powerdown Off |
IanBenzMaxim | 75:8b627804927c | 162 | /// Active Pullup On |
IanBenzMaxim | 75:8b627804927c | 163 | virtual OneWireMaster::CmdResult OWInitMaster(); |
IanBenzMaxim | 75:8b627804927c | 164 | /// @note Perform a 1-Wire triplet using the DS248x command. |
IanBenzMaxim | 75:8b627804927c | 165 | virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb); |
IanBenzMaxim | 75:8b627804927c | 166 | virtual OneWireMaster::CmdResult OWReset(); |
IanBenzMaxim | 75:8b627804927c | 167 | virtual OneWireMaster::CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel); |
IanBenzMaxim | 75:8b627804927c | 168 | virtual OneWireMaster::CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel); |
IanBenzMaxim | 75:8b627804927c | 169 | virtual OneWireMaster::CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel); |
IanBenzMaxim | 75:8b627804927c | 170 | virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed newSpeed); |
IanBenzMaxim | 75:8b627804927c | 171 | virtual OneWireMaster::CmdResult OWSetLevel(OWLevel newLevel); |
IanBenzMaxim | 74:23be10c32fa3 | 172 | |
IanBenzMaxim | 73:2cecc1372acc | 173 | private: |
IanBenzMaxim | 75:8b627804927c | 174 | enum Command |
IanBenzMaxim | 73:2cecc1372acc | 175 | { |
IanBenzMaxim | 75:8b627804927c | 176 | DeviceResetCmd = 0xF0, |
IanBenzMaxim | 75:8b627804927c | 177 | WriteDeviceConfigCmd = 0xD2, |
IanBenzMaxim | 75:8b627804927c | 178 | AdjustOwPortCmd = 0xC3, // DS2484 only |
IanBenzMaxim | 75:8b627804927c | 179 | ChannelSelectCmd = 0xC3, // DS2482-800 only |
IanBenzMaxim | 75:8b627804927c | 180 | SetReadPointerCmd = 0xE1, |
IanBenzMaxim | 75:8b627804927c | 181 | OwResetCmd = 0xB4, |
IanBenzMaxim | 75:8b627804927c | 182 | OwWriteByteCmd = 0xA5, |
IanBenzMaxim | 75:8b627804927c | 183 | OwReadByteCmd = 0x96, |
IanBenzMaxim | 75:8b627804927c | 184 | OwSingleBitCmd = 0x87, |
IanBenzMaxim | 75:8b627804927c | 185 | OwTripletCmd = 0x78 |
IanBenzMaxim | 73:2cecc1372acc | 186 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 187 | |
IanBenzMaxim | 75:8b627804927c | 188 | mbed::I2C *m_p_i2c_bus; |
IanBenzMaxim | 75:8b627804927c | 189 | uint8_t m_adrs; |
IanBenzMaxim | 75:8b627804927c | 190 | bool m_i2c_owner; |
IanBenzMaxim | 73:2cecc1372acc | 191 | Config m_curConfig; |
IanBenzMaxim | 69:f915c4c59a69 | 192 | |
IanBenzMaxim | 73:2cecc1372acc | 193 | /// Polls the DS2465 status waiting for the 1-Wire Busy bit (1WB) to be cleared. |
IanBenzMaxim | 73:2cecc1372acc | 194 | /// @param[out] pStatus Optionally retrive the status byte when 1WB cleared. |
IanBenzMaxim | 73:2cecc1372acc | 195 | /// @returns Success or TimeoutError if poll limit reached. |
IanBenzMaxim | 73:2cecc1372acc | 196 | OneWireMaster::CmdResult pollBusy(uint8_t * pStatus = NULL); |
IanBenzMaxim | 73:2cecc1372acc | 197 | |
IanBenzMaxim | 73:2cecc1372acc | 198 | /// Ensure that the desired 1-Wire level is set in the configuration. |
IanBenzMaxim | 73:2cecc1372acc | 199 | /// @param level Desired 1-Wire level. |
IanBenzMaxim | 73:2cecc1372acc | 200 | OneWireMaster::CmdResult configureLevel(OWLevel level); |
IanBenzMaxim | 69:f915c4c59a69 | 201 | |
IanBenzMaxim | 73:2cecc1372acc | 202 | /// @note Allow marking const since not public. |
IanBenzMaxim | 75:8b627804927c | 203 | OneWireMaster::CmdResult sendCommand(Command cmd) const; |
IanBenzMaxim | 74:23be10c32fa3 | 204 | |
IanBenzMaxim | 73:2cecc1372acc | 205 | /// @note Allow marking const since not public. |
IanBenzMaxim | 75:8b627804927c | 206 | OneWireMaster::CmdResult sendCommand(Command cmd, uint8_t param) const; |
IanBenzMaxim | 73:2cecc1372acc | 207 | }; |
IanBenzMaxim | 73:2cecc1372acc | 208 | } |
IanBenzMaxim | 73:2cecc1372acc | 209 | } |
j3 | 1:91e52f8ab8bf | 210 | |
IanBenzMaxim | 75:8b627804927c | 211 | #endif |