Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/OneWireMaster.cpp

Committer:
IanBenzMaxim
Date:
2016-03-21
Revision:
21:00c94aeb533e
Parent:
17:b646b1e3970b
Child:
23:e8e403d61359

File content as of revision 21:00c94aeb533e:

/******************************************************************//**
* 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 "OneWireMaster.h"


const uint16_t OneWireMaster::_oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWWriteBit(uint8_t sendbit)
{
    return OWTouchBit(sendbit);
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWReadBit(uint8_t & recvbit)
{
    recvbit = 0x01;
    return(OWTouchBit(recvbit)); 
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWTouchByte(uint8_t & sendrecvbyte)
{
    OneWireInterface::CmdResult result = OWWriteByte(sendrecvbyte);

    if (result == OneWireInterface::Success)
    {
        OWReadByte(sendrecvbyte);
    }
    
    return result;
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWBlock(uint8_t *tran_buf, uint8_t tran_len)
{
    OneWireInterface::CmdResult result;
    
    for (uint8_t i = 0; i < tran_len; i++) 
    {
        result = OWTouchByte(tran_buf[i]);
        
        if (result != OneWireInterface::Success)
        {
            break;
        }
    }
    
    return result;
}


OneWireInterface::CmdResult OneWireMaster::OWFirst(RomId & romId)
{
    // reset the search state
    _last_discrepancy = 0;
    _last_device_flag = 0;
    _last_family_discrepancy = 0;

   return OWSearch(romId);  
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWNext(RomId & romId)
{
    // leave the search state alone
    return OWSearch(romId);
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWVerify(const RomId & romId)
{
    OneWireInterface::CmdResult result;
    
    RomId romIdCopy(romId);
    
    int ld_backup, ldf_backup, lfd_backup;

    // keep a backup copy of the current state
    ld_backup = _last_discrepancy;
    ldf_backup = _last_device_flag;
    lfd_backup = _last_family_discrepancy;

    // set search to find the same device
    _last_discrepancy = 64;
    _last_device_flag = false;

    result = OWSearch(romIdCopy);
    if (result == OneWireInterface::Success) 
    {
        // check if same device found
        if (romId != romIdCopy) 
        {
            result = OneWireInterface::OperationFailure;
        }
    }

    // restore the search state
    _last_discrepancy = ld_backup;
    _last_device_flag = ldf_backup;
    _last_family_discrepancy = lfd_backup;

    // return the result of the verify
    return result;
}


//*********************************************************************
void OneWireMaster::OWTargetSetup(RomId & romId)
{
    // set the search state to find SearchFamily type devices
    for (int i = 1; i < 8; i++) 
    {
        romId[i] = 0;
    }
    
    _last_discrepancy = 64;
    _last_family_discrepancy = 0;
    _last_device_flag = false;
}


//*********************************************************************
void OneWireMaster::OWFamilySkipSetup(void)
{
    // set the Last discrepancy to last family discrepancy
    _last_discrepancy = _last_family_discrepancy;

    // clear the last family discrpepancy
    _last_family_discrepancy = 0;

    // check for end of list
    if (_last_discrepancy == 0)
    {
        _last_device_flag = true;
    }
}


//*********************************************************************
OneWireInterface::CmdResult OneWireMaster::OWReadROM(RomId & romId)
{
    OneWireInterface::CmdResult result;
    
    uint8_t buf[2 + RomId::byteLen];

    result = OWReset();
    if (result == OneWireInterface::Success)
    {
        result = OWWriteByte(0x33); // READ ROM
    }

    // read the ROM
    if (result == OneWireInterface::Success)
    {
        result = OWReadBlock(buf, RomId::byteLen);
    }
    
    // verify CRC8
    if ((result == OneWireInterface::Success) && romId.crc8Valid() && (buf[1] != 0))
    {
        romId = RomId(reinterpret_cast<std::uint8_t (&)[RomId::byteLen]>(buf[0]));
    }

    return result;
}
    

//*********************************************************************    
OneWireInterface::CmdResult OneWireMaster::OWSkipROM(void)
{
    OneWireInterface::CmdResult result;
    
    result = OWReset();
    if (result == OneWireInterface::Success) 
    {
        result = OWWriteByte(0xCC);
    }
    
    return result;
}
    

//*********************************************************************    
OneWireInterface::CmdResult OneWireMaster::OWMatchROM(const RomId & romId)
{
    OneWireInterface::CmdResult result;
    
    uint8_t buf[1 + RomId::byteLen];

    // use MatchROM
    result = OWReset();
    if (result == OneWireInterface::Success) 
    {
        buf[0] = 0x55;
        std::memcpy(&buf[1], romId, RomId::byteLen);
        // send command and rom
        result = OWWriteBlock(buf, 1 + RomId::byteLen);
    }

    return result;
}
    

//*********************************************************************    
OneWireInterface::CmdResult OneWireMaster::OWOverdriveSkipROM(void)
{
    OneWireInterface::CmdResult result = OWSpeed(SPEED_STANDARD);
    
    if (result == OneWireInterface::Success)
    {
        result = OWReset();
    }
    
    if (result == OneWireInterface::Success)
    {
        result = OWWriteByte(0x3C);
    }
    
    if (result == OneWireInterface::Success)
    {
        result = OWSpeed(SPEED_OVERDRIVE);
    }
    
    return result;
}
    

//*********************************************************************    
OneWireInterface::CmdResult OneWireMaster::OWOverdriveMatchROM(const RomId & romId)
{
    OneWireInterface::CmdResult result;
    
    // use overdrive MatchROM
    OWSpeed(SPEED_STANDARD);
    
    result = OWReset();
    if (result == OneWireInterface::Success) 
    {
        result = OWWriteByte(0x69);
        if (result == OneWireInterface::Success) 
        {
            OWSpeed(SPEED_OVERDRIVE);
            // send ROM
            result = OWWriteBlock(romId, RomId::byteLen);
        }
    }
    return result;
}
    

//*********************************************************************    
OneWireInterface::CmdResult OneWireMaster::OWResume(void)
{
    OneWireInterface::CmdResult result;
    
    result = OWReset();
    if (result == OneWireInterface::Success) 
    {
        result = OWWriteByte(0xA5);
    }
    
    return result;
}


//--------------------------------------------------------------------------
// Calculate a new CRC16 from the input data shorteger.  Return the current
// CRC16 and also update the global variable CRC16.
//
uint16_t OneWireMaster::calculateCRC16(uint16_t CRC16, uint16_t data)
{
   data = (data ^ (CRC16 & 0xff)) & 0xff;
   CRC16 >>= 8;

   if (_oddparity[data & 0xf] ^ _oddparity[data >> 4])
     CRC16 ^= 0xc001;

   data <<= 6;
   CRC16  ^= data;
   data <<= 1;
   CRC16   ^= data;

   return CRC16;
}

uint16_t OneWireMaster::calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc)
{  
  for (size_t i = data_offset; i < (data_len + data_offset); i++)
    crc = calculateCRC16(crc, data[i]);
  return crc;
}