Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

MaximInterfaceDevices/DS9481P_300.cpp

Committer:
IanBenzMaxim
Date:
2020-05-29
Revision:
12:7eb41621ba22
Parent:
8:5ea891c7d1a1

File content as of revision 12:7eb41621ba22:

/*******************************************************************************
* Copyright (C) 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 <MaximInterfaceCore/Error.hpp>
#include "DS9481P_300.hpp"

#define TRY MaximInterfaceCore_TRY

namespace MaximInterfaceDevices {

using namespace Core;

DS9481P_300::DS9481P_300(Sleep & sleep, SerialPort & serialPort)
    : serialPort(&serialPort), currentBus(OneWire), ds2480b(sleep, serialPort),
      oneWireMaster_(*this), ds9400(serialPort), i2cMaster_(*this) {}

Result<void> DS9481P_300::connect(const std::string & portName) {
  Result<void> result = serialPort->connect(portName);
  if (result) {
    result = selectOneWire();
    if (result) {
      currentBus = OneWire;
    } else {
      serialPort->disconnect();
    }
  }
  return result;
}

Result<void> DS9481P_300::disconnect() { return serialPort->disconnect(); }

Result<void> DS9481P_300::selectOneWire() {
  // Escape DS9400 mode.
  TRY(ds9400.escape());
  TRY(ds2480b.initialize());
  return none;
}

Result<void> DS9481P_300::selectBus(Bus newBus) {
  if (currentBus != newBus) {
    switch (currentBus) {
    case OneWire: // Next bus I2C.
      // Escape DS2480 Mode.
      TRY(ds2480b.escape());
      // Wait for awake notification.
      TRY(ds9400.waitAwake());
      break;

    case I2C: // Next bus OneWire.
      TRY(selectOneWire());
      break;
    }
    currentBus = newBus;
  }
  return none;
}

Result<void> DS9481P_300::OneWireMaster::reset() {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::reset();
}

Result<bool> DS9481P_300::OneWireMaster::touchBitSetLevel(bool sendBit,
                                                          Level afterLevel) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::touchBitSetLevel(sendBit, afterLevel);
}

Result<void>
DS9481P_300::OneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
                                              Level afterLevel) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
}

Result<uint_least8_t>
DS9481P_300::OneWireMaster::readByteSetLevel(Level afterLevel) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::readByteSetLevel(afterLevel);
}

Result<void>
DS9481P_300::OneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::writeBlock(sendBuf);
}

Result<void>
DS9481P_300::OneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::readBlock(recvBuf);
}

Result<void> DS9481P_300::OneWireMaster::setSpeed(Speed newSpeed) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::setSpeed(newSpeed);
}

Result<void> DS9481P_300::OneWireMaster::setLevel(Level newLevel) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::setLevel(newLevel);
}

Result<OneWireMaster::TripletData>
DS9481P_300::OneWireMaster::triplet(bool sendBit) {
  TRY(parent->selectBus(OneWire));
  return OneWireMasterDecorator::triplet(sendBit);
}

Result<void> DS9481P_300::I2CMaster::start(uint_least8_t address) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::start(address);
}

Result<void> DS9481P_300::I2CMaster::stop() {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::stop();
}

Result<void> DS9481P_300::I2CMaster::writeByte(uint_least8_t data) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::writeByte(data);
}

Result<void>
DS9481P_300::I2CMaster::writeBlock(span<const uint_least8_t> data) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::writeBlock(data);
}

Result<void> DS9481P_300::I2CMaster::writePacket(uint_least8_t address,
                                                 span<const uint_least8_t> data,
                                                 DoStop doStop) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::writePacket(address, data, doStop);
}

Result<uint_least8_t> DS9481P_300::I2CMaster::readByte(DoAck doAck) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::readByte(doAck);
}

Result<void> DS9481P_300::I2CMaster::readBlock(span<uint_least8_t> data,
                                               DoAck doAck) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::readBlock(data, doAck);
}

Result<void> DS9481P_300::I2CMaster::readPacket(uint_least8_t address,
                                                span<uint_least8_t> data,
                                                DoStop doStop) {
  TRY(parent->selectBus(I2C));
  return I2CMasterDecorator::readPacket(address, data, doStop);
}

Result<void> DS9481P_300::DS2480BWithEscape::escape() {
  return sendCommand(0xE5);
}

Result<void> DS9481P_300::DS9400WithEscape::escape() { return configure('O'); }

} // namespace MaximInterfaceDevices