1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

OneWire_Masters/DS248x/ds248x.cpp

Committer:
IanBenzMaxim
Date:
2016-03-30
Revision:
32:bce180b544ed
Parent:
27:d5aaefa252f1
Child:
69:f915c4c59a69

File content as of revision 32:bce180b544ed:

/******************************************************************//**
* 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.
**********************************************************************/


#include "ds248x.h"
#include "RomId.hpp"


//*********************************************************************
Ds248x::Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs)
:_p_i2c_bus(&i2c_bus), _i2c_owner(false)
{
    set_i2c_adrs(adrs);
}


//*********************************************************************
Ds248x::Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs)
:_p_i2c_bus(new I2C(sda, scl)), _i2c_owner(true)
{
    set_i2c_adrs(adrs);
}


//*********************************************************************
Ds248x::~Ds248x()
{
    if(_i2c_owner)
    {
        delete _p_i2c_bus;
    }
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWInitMaster(void)
{
    return(detect());
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::detect(void)
{
    OneWireMaster::CmdResult result;
    
    // reset the ds2484 ON selected address
    result = reset();
    if(result == OneWireMaster::Success)
    {
        // default configuration
        _c1WS = 0;
        _cSPU = 0;
        _cPDN = 0;
        _cAPU = 0;
        
        result = write_config(_c1WS | _cSPU | _cPDN | _cAPU);
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::reset(void)
{
    OneWireMaster::CmdResult result;
    
    char status;
    char packet[] = {CMD_DRST};
    
    // Device Reset
    //   S AD,0 [A] DRST [A] Sr AD,1 [A] [SS] A\ P
    //  [] indicates from slave
    //  SS status byte to read to verify state
    
    if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
        {
            result = OneWireMaster::CommunicationReadError;
        }
        else
        {
            if((status & 0xF7) == 0x10)
            {
                result = OneWireMaster::Success;
            }
            else
            {
                result = OneWireMaster::OperationFailure;
            }
        }
    }
    
    return result;   
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::write_config(uint8_t config)
{
    OneWireMaster::CmdResult result;
    
    char read_config;
    char packet [] = {CMD_WCFG, (config | (~config << 4))};
    
    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        if(_p_i2c_bus->read(_r_adrs, &read_config, 1) != I2C_READ_OK)
        {
            result = OneWireMaster::CommunicationReadError;
        }
        else
        {
            // check for failure due to incorrect read back
            if (config != read_config)
            {
                reset();
                result = OneWireMaster::OperationFailure;
            }
            else
            {
                result = OneWireMaster::Success;
            }
        }
    }
    
    return result;   
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::channel_select(uint8_t channel)
{
    OneWireMaster::CmdResult result;
    
    char ch, ch_read, check;
    char packet [2];
    
    packet[0] = CMD_CHSL;
    
    // Channel Select (Case A)
    //   S AD,0 [A] CHSL [A] CC [A] Sr AD,1 [A] [RR] A\ P
    //  [] indicates from slave
    //  CC channel value
    //  RR channel read back
    
    switch (channel)
    {
      default: case 0: ch = 0xF0; ch_read = 0xB8; break;
      case 1: ch = 0xE1; ch_read = 0xB1; break;
      case 2: ch = 0xD2; ch_read = 0xAA; break;
      case 3: ch = 0xC3; ch_read = 0xA3; break;
      case 4: ch = 0xB4; ch_read = 0x9C; break;
      case 5: ch = 0xA5; ch_read = 0x95; break;
      case 6: ch = 0x96; ch_read = 0x8E; break;
      case 7: ch = 0x87; ch_read = 0x87; break;
    };
    
    packet[1] = ch;
    
    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        if(_p_i2c_bus->read(_r_adrs, &check, 1) != I2C_READ_OK)
        {
            result = OneWireMaster::CommunicationReadError;
        }
        else
        {
            // check for failure due to incorrect read back of channel
            if (check == ch_read)
            {
                result = OneWireMaster::Success;
            }
            else
            {
                result = OneWireMaster::OperationFailure;
            }
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::adjust_timing(uint8_t param, uint8_t val)
{
    OneWireMaster::CmdResult result;
    
    char read_port_config;
    char control_byte;

    control_byte = (((param & 0x0F) << 4) | (val & 0x0F));

    char packet [] = {CMD_A1WP, control_byte};

    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        if(_p_i2c_bus->read(_r_adrs, &read_port_config, 1) != I2C_READ_OK)
        {
            result = OneWireMaster::CommunicationReadError;
        }
        else
        {
            // check for failure due to incorrect read back
            if ((control_byte & 0x0F) != read_port_config) 
            {
                result = OneWireMaster::OperationFailure;
                reset();
            }
            else
            {
                result = OneWireMaster::Success;
            }
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb)
{
    OneWireMaster::CmdResult result;
    
    uint8_t poll_count = 0;
    char packet [] = {CMD_1WT, search_direction ? 0x80 : 0x00};
    char read_data;

    // 1-Wire Triplet (Case B)
    //   S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P
    //                                         \--------/
    //                           Repeat until 1WB bit has changed to 0
    //  [] indicates from slave
    //  SS indicates byte containing search direction bit value in msbit

    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        // loop checking 1WB bit for completion of 1-Wire operation
        // abort if poll limit reached
        
        //dummy write for loop 
        result = OneWireMaster::Success; //so far
        do 
        {
            if(_p_i2c_bus->read(_r_adrs, &read_data, 1) != I2C_READ_OK)
            {
                result = OneWireMaster::CommunicationReadError;
            }
        } 
        while ((read_data & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
        
        if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
        {
            // check for failure due to poll limit reached
            if(poll_count >= POLL_LIMIT)  
            {
                // handle error
                // ...
                reset();
                result = OneWireMaster::TimeoutError;
            }
        }
        else
        {
            // check bit results in status byte
            sbr = ((read_data & STATUS_SBR) == STATUS_SBR);
            tsb = ((read_data & STATUS_TSB) == STATUS_TSB);
            search_direction = ((read_data & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO;
            
            result = OneWireMaster::Success;
        }
    }

    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWReset(void)
{
    OneWireMaster::CmdResult result;
    
    uint8_t poll_count = 0;
    char status;
    char packet [] = {CMD_1WRS};

    // 1-Wire reset (Case B)
    //   S AD,0 [A] 1WRS [A] Sr AD,1 [A] [Status] A [Status] A\ P
    //                                   \--------/
    //                       Repeat until 1WB bit has changed to 0
    //  [] indicates from slave

    if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        // loop checking 1WB bit for completion of 1-Wire operation
        // abort if poll limit reached
        //dummy write for loop 
        result = OneWireMaster::Success; //so far
        do 
        {
            if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
            {
                result = OneWireMaster::CommunicationReadError;
            }
        } 
        while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
        
        if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
        {
            // check for failure due to poll limit reached
            if(poll_count >= POLL_LIMIT)  
            {
                // handle error
                // ...
                reset();
                result = OneWireMaster::TimeoutError;
            }
        }
        else
        {
            // check for short condition
            if (status & STATUS_SD)
            {
                _short_detected = true;
            }
            else
            {
                _short_detected = false;
            }
        
            // check for presence detect
            if (status & STATUS_PPD)
            {
                result = OneWireMaster::Success;
            }
            else
            {
                result = OneWireMaster::OperationFailure;
            }
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit, OWLevel after_level)
{
    OneWireMaster::CmdResult result;
    
    result = ConfigureSPU(after_level == LEVEL_STRONG);
    if (result != OneWireMaster::Success)
        return result;
    
    uint8_t poll_count = 0;
    char status;
    char packet[] = {CMD_1WSB, sendrecvbit ? 0x80 : 0x00};

    // 1-Wire bit (Case B)
    //   S AD,0 [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P
    //                                          \--------/
    //                           Repeat until 1WB bit has changed to 0
    //  [] indicates from slave
    //  BB indicates byte containing bit value in msbit

    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        // loop checking 1WB bit for completion of 1-Wire operation
        // abort if poll limit reached
        //dummy write for loop 
        result = OneWireMaster::Success; //so far
        do 
        {
            if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
            {
                result = OneWireMaster::CommunicationReadError;
            }
        } 
        while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
        
        if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
        {
            // check for failure due to poll limit reached
            if (poll_count >= POLL_LIMIT) 
            {
                // handle error
                // ...
                reset();
                result = OneWireMaster::TimeoutError;
            }
        }
        else
        {
            // return bit state through out param
            if (status & STATUS_SBR)
            {
                sendrecvbit = 1;
                
            }
            else
            {
                sendrecvbit = 0;
            }
            result = OneWireMaster::Success;
        }
    }

    return result; 
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte, OWLevel after_level)
{
    OneWireMaster::CmdResult result;
    
    result = ConfigureSPU(after_level == LEVEL_STRONG);
    if (result != OneWireMaster::Success)
        return result;
    
    uint8_t poll_count = 0;
    char status;
    char packet [] = {CMD_1WWB, sendbyte};

    // 1-Wire Write Byte (Case B)
    //   S AD,0 [A] 1WWB [A] DD [A] Sr AD,1 [A] [Status] A [Status] A\ P
    //                                          \--------/
    //                             Repeat until 1WB bit has changed to 0
    //  [] indicates from slave
    //  DD data to write

    if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
    {
        result = OneWireMaster::CommunicationWriteError;
    }
    else
    {
        // loop checking 1WB bit for completion of 1-Wire operation
        // abort if poll limit reached
        //dummy write for loop 
        result = OneWireMaster::Success; //so far
        do 
        {
            if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
            {
                result = OneWireMaster::CommunicationReadError;
            }
        } 
        while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
        
        if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
        {
            // check for failure due to poll limit reached
            if (poll_count >= POLL_LIMIT) 
            {
                // handle error
                // ...
                reset();
                result = OneWireMaster::TimeoutError;
            }
        }
        else
        {
            result = OneWireMaster::Success;
        }
    }

    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte, OWLevel after_level)
{
    OneWireMaster::CmdResult result;
    
    if (after_level == LEVEL_STRONG) // Enabling strong pull-up after a Read Byte command is not supported natively by the DS248x
    {
        uint8_t recvbit;
        recvbyte = 0;
        
        for (unsigned int i = 1; i <= 8; i++)
        {
            // Set strong pull-up on last bit
            result = OWReadBit(recvbit, (i == 8 ? LEVEL_STRONG : LEVEL_NORMAL));
            if (result != Success)
                break;
            recvbyte = (recvbyte << 1) | recvbit;
        }
    }
    else
    {
        uint8_t poll_count = 0;
        char data, status;
        char packet[2] = {CMD_1WRB, 0};

        // 1-Wire Read Bytes (Case C)
        //   S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\
        //                                   \--------/
        //                     Repeat until 1WB bit has changed to 0
        //   Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P
        //
        //  [] indicates from slave
        //  DD data read

        if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK)
        {
            result = OneWireMaster::CommunicationWriteError;
        }
        else
        {
            // loop checking 1WB bit for completion of 1-Wire operation
            // abort if poll limit reached
            //dummy write for loop 
            result = OneWireMaster::Success; //so far
            do 
            {
                if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK)
                {
                    result = OneWireMaster::CommunicationReadError;
                }
            } 
            while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError));
            
            if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT))
            {
                // check for failure due to poll limit reached
                if (poll_count >= POLL_LIMIT) 
                {
                    // handle error
                    // ...
                    reset();
                    result = OneWireMaster::TimeoutError;
                }
            }
            else
            {
                packet[0] = CMD_SRP;
                packet[1] = 0xE1;
            
                if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK)
                {
                    result = OneWireMaster::CommunicationWriteError;
                }
                else
                {
                    if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK)
                    {
                        result = OneWireMaster::CommunicationReadError;
                    }
                    else
                    {
                        recvbyte = data;
                        result = OneWireMaster::Success;
                    }
                }
            }
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len)
{
    OneWireMaster::CmdResult result;
    
    for(uint8_t idx = 0; idx < tran_len; idx++)
    {
        result = OneWireMaster::OWWriteByte(tran_buf[idx]);
        if(result != OneWireMaster::Success)
        {
            break;
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len)
{
    OneWireMaster::CmdResult result;
    
    for(uint8_t idx = 0; idx < rx_len; idx++)
    {
        //OwReadByte() uses pass by reference
        result = OneWireMaster::OWReadByte(rx_buf[idx]);
        if(result != OneWireMaster::Success)
        {
            break;
        }
    }
    
    return result;
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWSetSpeed(OWSpeed new_speed)
{
    // set the speed
    if (new_speed == SPEED_OVERDRIVE)
    {
        _c1WS = CONFIG_1WS;
    }
    else
    {
        _c1WS = 0;
    }

    // write the new config, and return result of op
    return write_config(_c1WS | _cSPU | _cPDN | _cAPU);
}


//*********************************************************************
OneWireMaster::CmdResult Ds248x::OWSetLevel(OWLevel new_level)
{
    OneWireMaster::CmdResult result;
    
    // function only will turn back to non-strong pull-up
    if (new_level != LEVEL_NORMAL)
    {
        result = OneWireMaster::OperationFailure;
    }
    else
    {
        // clear the strong pull-up bit in the global config state
        _cSPU = 0;
        
        result = write_config(_c1WS | _cSPU | _cPDN | _cAPU);
    }

    return result;
}


OneWireMaster::CmdResult Ds248x::ConfigureSPU(bool spu_enable)
{
  OneWireMaster::CmdResult result;
  if ((_cSPU == CONFIG_SPU) != spu_enable)
  {
    _cSPU = spu_enable;
    result =  write_config(_c1WS | _cSPU | _cPDN | _cAPU);
  }
  else
  {
    result = OneWireMaster::Success;
  }
  return result;
}


//*********************************************************************
void Ds248x::set_i2c_adrs(DS248X_I2C_ADRS adrs)
{
    _w_adrs = (adrs << 1);
    _r_adrs = (_w_adrs | 1);
}