Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS9481P_300.cpp Source File

DS9481P_300.cpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #include <MaximInterfaceCore/Error.hpp>
00034 #include "DS9481P_300.hpp"
00035 
00036 #define TRY MaximInterfaceCore_TRY
00037 
00038 namespace MaximInterfaceDevices {
00039 
00040 using namespace Core;
00041 
00042 DS9481P_300::DS9481P_300(Sleep & sleep, SerialPort & serialPort)
00043     : serialPort(&serialPort), currentBus(OneWire), ds2480b(sleep, serialPort),
00044       oneWireMaster_(*this), ds9400(serialPort), i2cMaster_(*this) {}
00045 
00046 Result<void> DS9481P_300::connect(const std::string & portName) {
00047   Result<void> result = serialPort->connect(portName);
00048   if (result) {
00049     result = selectOneWire();
00050     if (result) {
00051       currentBus = OneWire;
00052     } else {
00053       serialPort->disconnect();
00054     }
00055   }
00056   return result;
00057 }
00058 
00059 Result<void> DS9481P_300::disconnect() { return serialPort->disconnect(); }
00060 
00061 Result<void> DS9481P_300::selectOneWire() {
00062   // Escape DS9400 mode.
00063   TRY(ds9400.escape());
00064   TRY(ds2480b.initialize());
00065   return none;
00066 }
00067 
00068 Result<void> DS9481P_300::selectBus(Bus newBus) {
00069   if (currentBus != newBus) {
00070     switch (currentBus) {
00071     case OneWire: // Next bus I2C.
00072       // Escape DS2480 Mode.
00073       TRY(ds2480b.escape());
00074       // Wait for awake notification.
00075       TRY(ds9400.waitAwake());
00076       break;
00077 
00078     case I2C: // Next bus OneWire.
00079       TRY(selectOneWire());
00080       break;
00081     }
00082     currentBus = newBus;
00083   }
00084   return none;
00085 }
00086 
00087 Result<void> DS9481P_300::OneWireMaster::reset() {
00088   TRY(parent->selectBus(OneWire));
00089   return OneWireMasterDecorator::reset();
00090 }
00091 
00092 Result<bool> DS9481P_300::OneWireMaster::touchBitSetLevel(bool sendBit,
00093                                                           Level afterLevel) {
00094   TRY(parent->selectBus(OneWire));
00095   return OneWireMasterDecorator::touchBitSetLevel(sendBit, afterLevel);
00096 }
00097 
00098 Result<void>
00099 DS9481P_300::OneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
00100                                               Level afterLevel) {
00101   TRY(parent->selectBus(OneWire));
00102   return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
00103 }
00104 
00105 Result<uint_least8_t>
00106 DS9481P_300::OneWireMaster::readByteSetLevel(Level afterLevel) {
00107   TRY(parent->selectBus(OneWire));
00108   return OneWireMasterDecorator::readByteSetLevel(afterLevel);
00109 }
00110 
00111 Result<void>
00112 DS9481P_300::OneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
00113   TRY(parent->selectBus(OneWire));
00114   return OneWireMasterDecorator::writeBlock(sendBuf);
00115 }
00116 
00117 Result<void>
00118 DS9481P_300::OneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
00119   TRY(parent->selectBus(OneWire));
00120   return OneWireMasterDecorator::readBlock(recvBuf);
00121 }
00122 
00123 Result<void> DS9481P_300::OneWireMaster::setSpeed(Speed newSpeed) {
00124   TRY(parent->selectBus(OneWire));
00125   return OneWireMasterDecorator::setSpeed(newSpeed);
00126 }
00127 
00128 Result<void> DS9481P_300::OneWireMaster::setLevel(Level newLevel) {
00129   TRY(parent->selectBus(OneWire));
00130   return OneWireMasterDecorator::setLevel(newLevel);
00131 }
00132 
00133 Result<OneWireMaster::TripletData>
00134 DS9481P_300::OneWireMaster::triplet(bool sendBit) {
00135   TRY(parent->selectBus(OneWire));
00136   return OneWireMasterDecorator::triplet(sendBit);
00137 }
00138 
00139 Result<void> DS9481P_300::I2CMaster::start(uint_least8_t address) {
00140   TRY(parent->selectBus(I2C));
00141   return I2CMasterDecorator::start(address);
00142 }
00143 
00144 Result<void> DS9481P_300::I2CMaster::stop() {
00145   TRY(parent->selectBus(I2C));
00146   return I2CMasterDecorator::stop();
00147 }
00148 
00149 Result<void> DS9481P_300::I2CMaster::writeByte(uint_least8_t data) {
00150   TRY(parent->selectBus(I2C));
00151   return I2CMasterDecorator::writeByte(data);
00152 }
00153 
00154 Result<void>
00155 DS9481P_300::I2CMaster::writeBlock(span<const uint_least8_t> data) {
00156   TRY(parent->selectBus(I2C));
00157   return I2CMasterDecorator::writeBlock(data);
00158 }
00159 
00160 Result<void> DS9481P_300::I2CMaster::writePacket(uint_least8_t address,
00161                                                  span<const uint_least8_t> data,
00162                                                  DoStop doStop) {
00163   TRY(parent->selectBus(I2C));
00164   return I2CMasterDecorator::writePacket(address, data, doStop);
00165 }
00166 
00167 Result<uint_least8_t> DS9481P_300::I2CMaster::readByte(DoAck doAck) {
00168   TRY(parent->selectBus(I2C));
00169   return I2CMasterDecorator::readByte(doAck);
00170 }
00171 
00172 Result<void> DS9481P_300::I2CMaster::readBlock(span<uint_least8_t> data,
00173                                                DoAck doAck) {
00174   TRY(parent->selectBus(I2C));
00175   return I2CMasterDecorator::readBlock(data, doAck);
00176 }
00177 
00178 Result<void> DS9481P_300::I2CMaster::readPacket(uint_least8_t address,
00179                                                 span<uint_least8_t> data,
00180                                                 DoStop doStop) {
00181   TRY(parent->selectBus(I2C));
00182   return I2CMasterDecorator::readPacket(address, data, doStop);
00183 }
00184 
00185 Result<void> DS9481P_300::DS2480BWithEscape::escape() {
00186   return sendCommand(0xE5);
00187 }
00188 
00189 Result<void> DS9481P_300::DS9400WithEscape::escape() { return configure('O'); }
00190 
00191 } // namespace MaximInterfaceDevices