Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
mfruge
Date:
Tue Aug 13 14:42:37 2019 +0000
Revision:
142:85b71cfd617e
Parent:
139:f0e0a7976846
Added functions to ROMCommands to add Alarm Search functionality

Who changed what in which revision?

UserRevisionLine numberNew 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"
j3 1:91e52f8ab8bf 37
IanBenzMaxim 73:2cecc1372acc 38 namespace mbed { class I2C; }
j3 1:91e52f8ab8bf 39
IanBenzMaxim 73:2cecc1372acc 40 namespace OneWire
j3 1:91e52f8ab8bf 41 {
IanBenzMaxim 76:84e6c4994e29 42 /// Interface to the DS2484, DS2482-100, DS2482-101, DS2482-800 1-Wire masters.
IanBenzMaxim 76:84e6c4994e29 43 class DS248x : public OneWireMaster
j3 5:ce108eeb878d 44 {
IanBenzMaxim 76:84e6c4994e29 45 public:
IanBenzMaxim 76:84e6c4994e29 46 /// Device register pointers.
IanBenzMaxim 76:84e6c4994e29 47 enum Register
IanBenzMaxim 76:84e6c4994e29 48 {
IanBenzMaxim 76:84e6c4994e29 49 ConfigReg = 0xC3,
IanBenzMaxim 76:84e6c4994e29 50 StatusReg = 0xF0,
IanBenzMaxim 76:84e6c4994e29 51 ReadDataReg = 0xE1,
IanBenzMaxim 76:84e6c4994e29 52 PortConfigReg = 0xB4,
IanBenzMaxim 76:84e6c4994e29 53 ChannelSelectReg = 0xD2 // DS2482-800 only
IanBenzMaxim 76:84e6c4994e29 54 };
IanBenzMaxim 76:84e6c4994e29 55
IanBenzMaxim 76:84e6c4994e29 56 /// Represents a DS248x configuration.
IanBenzMaxim 76:84e6c4994e29 57 class Config
IanBenzMaxim 73:2cecc1372acc 58 {
IanBenzMaxim 73:2cecc1372acc 59 public:
IanBenzMaxim 76:84e6c4994e29 60 /// @{
IanBenzMaxim 76:84e6c4994e29 61 /// 1-Wire Speed
IanBenzMaxim 76:84e6c4994e29 62 bool get1WS() const { return m_1WS; }
IanBenzMaxim 76:84e6c4994e29 63 void set1WS(bool new1WS) { m_1WS = new1WS; }
IanBenzMaxim 76:84e6c4994e29 64 /// @}
IanBenzMaxim 74:23be10c32fa3 65
IanBenzMaxim 76:84e6c4994e29 66 /// @{
IanBenzMaxim 76:84e6c4994e29 67 /// Strong Pullup
IanBenzMaxim 76:84e6c4994e29 68 bool getSPU() const { return m_SPU; }
IanBenzMaxim 76:84e6c4994e29 69 void setSPU(bool newSPU) { m_SPU = newSPU; }
IanBenzMaxim 76:84e6c4994e29 70 /// @}
IanBenzMaxim 69:f915c4c59a69 71
IanBenzMaxim 76:84e6c4994e29 72 /// @{
IanBenzMaxim 76:84e6c4994e29 73 /// 1-Wire Power Down
IanBenzMaxim 76:84e6c4994e29 74 bool getPDN() const { return m_PDN; }
IanBenzMaxim 76:84e6c4994e29 75 void setPDN(bool newPDN) { m_PDN = newPDN; }
IanBenzMaxim 76:84e6c4994e29 76 /// @}
IanBenzMaxim 74:23be10c32fa3 77
IanBenzMaxim 76:84e6c4994e29 78 /// @{
IanBenzMaxim 76:84e6c4994e29 79 /// Active Pullup
IanBenzMaxim 76:84e6c4994e29 80 bool getAPU() const { return m_APU; }
IanBenzMaxim 76:84e6c4994e29 81 void setAPU(bool newAPU) { m_APU = newAPU; }
IanBenzMaxim 76:84e6c4994e29 82 /// @}
IanBenzMaxim 74:23be10c32fa3 83
j3 91:e80108bc870a 84 /// Byte representation that is read from the DS248x.
IanBenzMaxim 76:84e6c4994e29 85 uint8_t readByte() const;
j3 91:e80108bc870a 86 /// Byte respresentation that is written to the DS248x.
IanBenzMaxim 76:84e6c4994e29 87 uint8_t writeByte() const;
IanBenzMaxim 74:23be10c32fa3 88
IanBenzMaxim 76:84e6c4994e29 89 /// Reset to the power-on default config.
IanBenzMaxim 76:84e6c4994e29 90 void reset();
IanBenzMaxim 76:84e6c4994e29 91 Config() { reset(); }
IanBenzMaxim 74:23be10c32fa3 92
IanBenzMaxim 73:2cecc1372acc 93 private:
IanBenzMaxim 76:84e6c4994e29 94 bool m_1WS, m_SPU, m_PDN, m_APU;
IanBenzMaxim 76:84e6c4994e29 95 };
j3 91:e80108bc870a 96
IanBenzMaxim 76:84e6c4994e29 97 /// Performs a soft reset on the DS248x.
IanBenzMaxim 76:84e6c4994e29 98 /// @note This is note a 1-Wire Reset.
IanBenzMaxim 76:84e6c4994e29 99 OneWireMaster::CmdResult reset(void);
IanBenzMaxim 76:84e6c4994e29 100
IanBenzMaxim 76:84e6c4994e29 101 /// Write a new configuration to the DS248x.
IanBenzMaxim 76:84e6c4994e29 102 /// @param[in] config New configuration to write.
IanBenzMaxim 76:84e6c4994e29 103 /// @param verify Verify that the configuration was written successfully.
IanBenzMaxim 76:84e6c4994e29 104 OneWireMaster::CmdResult writeConfig(const Config & config, bool verify);
IanBenzMaxim 76:84e6c4994e29 105
IanBenzMaxim 76:84e6c4994e29 106 /// Read the current DS248x configuration.
IanBenzMaxim 76:84e6c4994e29 107 /// @returns The cached current configuration.
IanBenzMaxim 76:84e6c4994e29 108 Config currentConfig() const { return m_curConfig; }
IanBenzMaxim 76:84e6c4994e29 109
IanBenzMaxim 76:84e6c4994e29 110 /// Reads a register from the DS248x.
IanBenzMaxim 76:84e6c4994e29 111 /// @param reg Register to read from.
IanBenzMaxim 76:84e6c4994e29 112 /// @param[out] buf Buffer to hold read data.
IanBenzMaxim 76:84e6c4994e29 113 /// @param skipSetPointer Assume that the read pointer is already set to the correct register.
IanBenzMaxim 76:84e6c4994e29 114 OneWireMaster::CmdResult readRegister(Register reg, uint8_t & buf, bool skipSetPointer = false) const;
IanBenzMaxim 76:84e6c4994e29 115
IanBenzMaxim 76:84e6c4994e29 116 /// @details Performs a device reset followed by writing the configuration byte to default values:
IanBenzMaxim 76:84e6c4994e29 117 /// 1-Wire Speed Standard
IanBenzMaxim 76:84e6c4994e29 118 /// Strong Pullup Off
IanBenzMaxim 76:84e6c4994e29 119 /// 1-Wire Powerdown Off
IanBenzMaxim 76:84e6c4994e29 120 /// Active Pullup On
IanBenzMaxim 76:84e6c4994e29 121 virtual OneWireMaster::CmdResult OWInitMaster();
j3 91:e80108bc870a 122
IanBenzMaxim 76:84e6c4994e29 123 /// @note Perform a 1-Wire triplet using the DS248x command.
IanBenzMaxim 76:84e6c4994e29 124 virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb);
j3 91:e80108bc870a 125
IanBenzMaxim 76:84e6c4994e29 126 virtual OneWireMaster::CmdResult OWReset();
IanBenzMaxim 76:84e6c4994e29 127 virtual OneWireMaster::CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel);
IanBenzMaxim 76:84e6c4994e29 128 virtual OneWireMaster::CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel);
IanBenzMaxim 76:84e6c4994e29 129 virtual OneWireMaster::CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel);
IanBenzMaxim 76:84e6c4994e29 130 virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed newSpeed);
IanBenzMaxim 76:84e6c4994e29 131 virtual OneWireMaster::CmdResult OWSetLevel(OWLevel newLevel);
IanBenzMaxim 73:2cecc1372acc 132
j3 91:e80108bc870a 133 protected:
IanBenzMaxim 76:84e6c4994e29 134 enum Command
IanBenzMaxim 76:84e6c4994e29 135 {
IanBenzMaxim 76:84e6c4994e29 136 DeviceResetCmd = 0xF0,
IanBenzMaxim 76:84e6c4994e29 137 WriteDeviceConfigCmd = 0xD2,
IanBenzMaxim 76:84e6c4994e29 138 AdjustOwPortCmd = 0xC3, // DS2484 only
IanBenzMaxim 76:84e6c4994e29 139 ChannelSelectCmd = 0xC3, // DS2482-800 only
IanBenzMaxim 76:84e6c4994e29 140 SetReadPointerCmd = 0xE1,
IanBenzMaxim 76:84e6c4994e29 141 OwResetCmd = 0xB4,
IanBenzMaxim 76:84e6c4994e29 142 OwWriteByteCmd = 0xA5,
IanBenzMaxim 76:84e6c4994e29 143 OwReadByteCmd = 0x96,
IanBenzMaxim 76:84e6c4994e29 144 OwSingleBitCmd = 0x87,
IanBenzMaxim 76:84e6c4994e29 145 OwTripletCmd = 0x78
IanBenzMaxim 76:84e6c4994e29 146 };
j3 91:e80108bc870a 147
j3 139:f0e0a7976846 148 /// Construct to use an existing I2C interface.
j3 139:f0e0a7976846 149 /// @param i2c_bus Configured I2C communication interface for DS248x.
j3 139:f0e0a7976846 150 /// @param adrs I2C bus address of the DS248x in mbed format.
j3 139:f0e0a7976846 151 DS248x(mbed::I2C & i2c_bus, uint8_t adrs);
j3 139:f0e0a7976846 152
j3 139:f0e0a7976846 153 /// @note Allow marking const since not public.
j3 139:f0e0a7976846 154 OneWireMaster::CmdResult sendCommand(Command cmd) const;
IanBenzMaxim 76:84e6c4994e29 155
j3 139:f0e0a7976846 156 /// @note Allow marking const since not public.
j3 139:f0e0a7976846 157 OneWireMaster::CmdResult sendCommand(Command cmd, uint8_t param) const;
j3 139:f0e0a7976846 158
j3 139:f0e0a7976846 159 private:
j3 91:e80108bc870a 160 /// Polls the DS248x status waiting for the 1-Wire Busy bit (1WB) to be cleared.
j3 139:f0e0a7976846 161 /// @param[out] pStatus Optionally retrieve the status byte when 1WB cleared.
IanBenzMaxim 76:84e6c4994e29 162 /// @returns Success or TimeoutError if poll limit reached.
IanBenzMaxim 76:84e6c4994e29 163 OneWireMaster::CmdResult pollBusy(uint8_t * pStatus = NULL);
IanBenzMaxim 74:23be10c32fa3 164
IanBenzMaxim 76:84e6c4994e29 165 /// Ensure that the desired 1-Wire level is set in the configuration.
IanBenzMaxim 76:84e6c4994e29 166 /// @param level Desired 1-Wire level.
IanBenzMaxim 76:84e6c4994e29 167 OneWireMaster::CmdResult configureLevel(OWLevel level);
j3 139:f0e0a7976846 168
j3 139:f0e0a7976846 169 mbed::I2C & m_i2c_bus;
j3 139:f0e0a7976846 170 uint8_t m_adrs;
j3 139:f0e0a7976846 171 Config m_curConfig;
IanBenzMaxim 76:84e6c4994e29 172 };
IanBenzMaxim 73:2cecc1372acc 173 }
j3 1:91e52f8ab8bf 174
IanBenzMaxim 75:8b627804927c 175 #endif