Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/OneWireMaster.cpp

Committer:
IanBenzMaxim
Date:
2016-03-22
Revision:
24:8942d8478d68
Parent:
23:e8e403d61359
Child:
26:a361e3f42ba5

File content as of revision 24:8942d8478d68:

/******************************************************************//**
* 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 };


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


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


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

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


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


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

   return OWSearch(romId);  
}


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


//*********************************************************************
OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId)
{
    OneWireMaster::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 == OneWireMaster::Success) 
    {
        // check if same device found
        if (romId != romIdCopy) 
        {
            result = OneWireMaster::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;
    }
}


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

    result = OWReset();
    if (result == OneWireMaster::Success)
    {
        result = OWWriteByte(READ_ROM);
    }

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

    return result;
}
    

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

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

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

    return result;
}
    

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

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

//*********************************************************************    
OneWireMaster::CmdResult OneWireMaster::OWResume(void)
{
    OneWireMaster::CmdResult result;
    
    result = OWReset();
    if (result == OneWireMaster::Success) 
    {
        result = OWWriteByte(RESUME);
    }
    
    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;
}