Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/DS248x/ds248x.h

Committer:
IanBenzMaxim
Date:
2016-05-09
Revision:
72:6892702709ee
Parent:
71:562f5c702094

File content as of revision 72:6892702709ee:

/******************************************************************//**
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/


#ifndef DS248X_H
#define DS248X_H


#include "mbed.h"
#include "OneWire_Masters/OneWireMaster.h"


class Ds248x: public OneWireMaster
{
public:
    
    enum DS248X_I2C_ADRS
    {
        DS248X_I2C_ADRS0 = 0x18,
        DS248X_I2C_ADRS1,
        DS248X_I2C_ADRS2,
        DS248X_I2C_ADRS3,
        DS248X_I2C_ADRS4,
        DS248X_I2C_ADRS5,
        DS248X_I2C_ADRS6,
        DS248X_I2C_ADRS7
    };
    
    enum DS248X_REG
    {
        REG_CONFIG = 0xC3,
        REG_STATUS = 0xF0,
        REG_READ_DATA = 0xE1,
        REG_PORT_CONFIG = 0xB4,
        REG_CHANNEL_SELECT = 0xD2 //DS2482-800 only
    };
    
    enum DS2484_ADJST_PARAMS
    {
        TRSTL = 0,
        TRSTL_OD,
        TMSP,
        TMSP_OD,
        TW0L,
        TW0L_OD,
        TREC0, //OD NA
        RWPU = 8 //OD NA, see DS2484 datasheet page 13
    };
    
    /// Represents a DS2465 configuration.
    class Config
    {
    public:
        /// @{
        /// 1-Wire Speed
        bool get1WS() const { return m_1WS; }
        void set1WS(bool new1WS) { m_1WS = new1WS; }
        /// @}

        /// @{
        /// Strong Pullup
        bool getSPU() const { return m_SPU; }
        void setSPU(bool newSPU) { m_SPU = newSPU; }
        /// @}

        /// @{
        /// 1-Wire Power Down
        bool getPDN() const { return m_PDN; }
        void setPDN(bool newPDN) { m_PDN = newPDN; }
        /// @}

        /// @{
        /// Active Pullup
        bool getAPU() const { return m_APU; }
        void setAPU(bool newAPU) { m_APU = newAPU; }
        /// @}

        /// Byte representation that is read from the DS2465.
        std::uint8_t readByte() const;
        /// Byte respresentation that is written to the DS2465.
        std::uint8_t writeByte() const;

        /// Reset to the power-on default config.
        void reset();
        Config() { reset(); }

    private:
        bool m_1WS, m_SPU, m_PDN, m_APU;
    };
    
    /**********************************************************//**
    * @brief Ds248x constructor
    * 
    * @details allows user to use existing I2C object
    *
    * On Entry:
    *     @param[in] p_i2c_bus - pointer to existing I2C object
    *
    * On Exit:
    *    @return 
    **************************************************************/
    Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs);
    
    
    /**********************************************************//**
    * @brief Ds248x constructor
    * 
    * @details Object instantiates a new I2C object with no 
    *          public access
    *
    * On Entry:
    *     @param[in] sda - sda pin of I2C bus
    *     @param[in] scl - scl pin of I2C bus
    *
    * On Exit:
    *    @return 
    **************************************************************/
    Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs);
    
    
    /**********************************************************//**
    * @brief Ds248x destructor 
    * 
    * @details deletes I2C object if owner 
    *
    * On Entry:
    *
    * On Exit:
    *    @return 
    **************************************************************/
    virtual ~Ds248x();
    
    
    /**********************************************************//**
    * @brief reset()
    * 
    * @details Perform a device reset on the Ds248x
    *
    * On Entry:
    *
    * On Exit:
    *    @return TRUE if device was reset
    *            FALSE device not detected or failure to perform reset
    **************************************************************/
    OneWireMaster::CmdResult reset(void);
    
    
    /// Write a new configuration to the DS2465.
    /// @param[in] config New configuration to write.
    /// @param verify Verify that the configuration was written successfully.
    OneWireMaster::CmdResult writeConfig(const Config & config, bool verify);

    /// Read the current DS2465 configuration.
    /// @returns The cached current configuration.
    Config currentConfig() const { return m_curConfig; }
    


    OneWireMaster::CmdResult readRegister(DS248X_REG reg, std::uint8_t & buf, bool skipSetPointer = false) const;
    
    
    /**********************************************************//**
    * @brief channel_select()
    * 
    * @details Select the 1-Wire channel on a DS2482-800. 
    *          Min channel = 1
    *
    * On Entry:
    *     @param[in] channel - desired channel of the DS2482
    *
    * On Exit:
    *    @return TRUE if channel selected
    *            FALSE device not detected or failure to perform select
    **************************************************************/
    OneWireMaster::CmdResult channelSelect(uint8_t channel);
    
    
    /**********************************************************//**
    * @brief adjust_timing()
    * 
    * @details adjustable timming available in DS2484 only
    *
    * On Entry:
    *     @param[in] param - 1 of 8 adjustable parameters
    *     @param[in] val - new value for parameter, see datasheet 
    *                      for codes
    *
    * On Exit:
    *    @return TRUE: parameter successfully adjusted
    *            FALSE: failed to adjust parameter
    **************************************************************/
    OneWireMaster::CmdResult adjustTiming(DS2484_ADJST_PARAMS param, uint8_t val);
    
    
    /**********************************************************//**
    * @brief search_triplet()
    * 
    * @details Use the Ds248x help command '1-Wire triplet' to perform 
    *          one bit of a 1-Wire search. This command does two read 
    *          bits and one write bit. The write bit is either the 
    *          default direction (all device have same bit) or in case 
    *          of a discrepancy, the 'search_direction' parameter is 
    *          used. 
    *
    * On Entry:
    *     @param[in] search_direction
    *
    * On Exit:
    *    @return The Ds248x status byte result from the triplet command
    **************************************************************/
    virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb);
    
    
    //Part of OneWireMaster that should be implemented for each master
    //See OneWireMaster.h for documentation
    
    /**********************************************************//**
    * @brief
    * 
    * @details Detect routine that performs a device reset 
    *          followed by writing the configuration byte to default 
    *          values:
    *          1-Wire speed (c1WS) = standard (0)
    *          Strong pull-up (cSPU) = off (0)
    *          Presence pulse masking (cPPM) = off (0)
    *          Active pull-up (cAPU) = on (CONFIG_APU = 0x01)
    *
    * On Entry:
    *
    * On Exit:
    *    @return TRUE if device was detected and written
    *            FALSE device not detected or failure to write 
    *            configuration byte
    **************************************************************/
    virtual OneWireMaster::CmdResult OWInitMaster(void);
    
    virtual OneWireMaster::CmdResult OWReset(void);
    
    virtual OneWireMaster::CmdResult OWTouchBitSetLevel(uint8_t & sendrecvbit, OWLevel after_level);
    
    virtual OneWireMaster::CmdResult OWReadByteSetLevel(uint8_t & recvbyte, OWLevel after_level);
    
    virtual OneWireMaster::CmdResult OWWriteByteSetLevel(uint8_t sendbyte, OWLevel after_level);

    virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed new_speed);

    virtual OneWireMaster::CmdResult OWSetLevel(OWLevel new_level);
    
private:

    enum DS248X_CMDS
    {
        CMD_DRST = 0xF0,
        CMD_WCFG = 0xD2,
        CMD_A1WP = 0xC3, //DS2484 only
        CMD_CHSL = 0xC3, //DS2482-800 only
        CMD_SRP = 0xE1,
        CMD_1WRS = 0xB4,
        CMD_1WWB = 0xA5,
        CMD_1WRB = 0x96,
        CMD_1WSB = 0x87,
        CMD_1WT = 0x78
    };
    
    //private fx for initializing _w_adrs and _r_adrs
    void set_i2c_adrs(DS248X_I2C_ADRS adrs);
    
    I2C *_p_i2c_bus;
    uint8_t _w_adrs, _r_adrs;
    bool _i2c_owner;
    
    // ds248x state
    uint8_t _short_detected;
    
    Config m_curConfig;

    /// Polls the DS2465 status waiting for the 1-Wire Busy bit (1WB) to be cleared.
    /// @param[out] pStatus Optionally retrive the status byte when 1WB cleared.
    /// @returns Success or TimeoutError if poll limit reached.
    OneWireMaster::CmdResult pollBusy(std::uint8_t * pStatus = NULL);

    /// Ensure that the desired 1-Wire level is set in the configuration.
    /// @param level Desired 1-Wire level.
    OneWireMaster::CmdResult configureLevel(OWLevel level);

    /// @note Allow marking const since not public.
    OneWireMaster::CmdResult sendCommand(DS248X_CMDS cmd) const;
    
    /// @note Allow marking const since not public.
    OneWireMaster::CmdResult sendCommand(DS248X_CMDS cmd, std::uint8_t param) const;
};

#endif /*DS248X_H*/