Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS9481P_300.cpp Source File

DS9481P_300.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 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 <MaximInterface/Utilities/Error.hpp>
00034 #include "DS9481P_300.hpp"
00035 
00036 namespace MaximInterface {
00037 
00038 DS9481P_300::DS9481P_300(Sleep & sleep, SerialPort & serialPort)
00039     : serialPort(&serialPort), currentBus(OneWire), ds2480b(sleep, serialPort),
00040       oneWireMaster_(*this), ds9400(serialPort), i2cMaster_(*this) {}
00041 
00042 error_code DS9481P_300::connect(const std::string & portName) {
00043   error_code result = serialPort->connect(portName);
00044   if (!result) {
00045     result = selectOneWire();
00046     if (result) {
00047       serialPort->disconnect();
00048     } else {
00049       currentBus = OneWire;
00050     }
00051   }
00052   return result;
00053 }
00054 
00055 error_code DS9481P_300::disconnect() {
00056   return serialPort->disconnect();
00057 }
00058 
00059 bool DS9481P_300::connected() const {
00060   return serialPort->connected();
00061 }
00062 
00063 std::string DS9481P_300::portName() const {
00064   return serialPort->portName();
00065 }
00066 
00067 error_code DS9481P_300::selectOneWire() {
00068   // Escape DS9400 mode.
00069   error_code result = ds9400.escape();
00070   if (!result) {
00071     result = ds2480b.initialize();
00072   }
00073   return result;
00074 }
00075 
00076 error_code DS9481P_300::selectBus(Bus newBus) {
00077   error_code result;
00078   if (currentBus != newBus) {
00079     switch (currentBus) {
00080     case OneWire: // Next bus I2C.
00081       // Escape DS2480 Mode.
00082       result = ds2480b.escape();
00083       if (!result) {
00084         // Wait for awake notification.
00085         result = ds9400.waitAwake();
00086       }
00087       break;
00088 
00089     case I2C: // Next bus OneWire.
00090       result = selectOneWire();
00091       break;
00092     }
00093     if (!result) {
00094       currentBus = newBus;
00095     }
00096   }
00097   return result;
00098 }
00099 
00100 error_code DS9481P_300::OneWireMasterImpl::reset() {
00101   error_code result = parent->selectBus(OneWire);
00102   if (!result) {
00103     result = OneWireMasterDecorator::reset();
00104   }
00105   return result;
00106 }
00107 
00108 error_code DS9481P_300::OneWireMasterImpl::touchBitSetLevel(bool & sendRecvBit,
00109                                                             Level afterLevel) {
00110   error_code result = parent->selectBus(OneWire);
00111   if (!result) {
00112     result = OneWireMasterDecorator::touchBitSetLevel(sendRecvBit, afterLevel);
00113   }
00114   return result;
00115 }
00116 
00117 error_code
00118 DS9481P_300::OneWireMasterImpl::writeByteSetLevel(uint_least8_t sendByte,
00119                                                   Level afterLevel) {
00120   error_code result = parent->selectBus(OneWire);
00121   if (!result) {
00122     result = OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
00123   }
00124   return result;
00125 }
00126 
00127 error_code
00128 DS9481P_300::OneWireMasterImpl::readByteSetLevel(uint_least8_t & recvByte,
00129                                                  Level afterLevel) {
00130   error_code result = parent->selectBus(OneWire);
00131   if (!result) {
00132     result = OneWireMasterDecorator::readByteSetLevel(recvByte, afterLevel);
00133   }
00134   return result;
00135 }
00136 
00137 error_code
00138 DS9481P_300::OneWireMasterImpl::writeBlock(span<const uint_least8_t> sendBuf) {
00139   error_code result = parent->selectBus(OneWire);
00140   if (!result) {
00141     result = OneWireMasterDecorator::writeBlock(sendBuf);
00142   }
00143   return result;
00144 }
00145 
00146 error_code
00147 DS9481P_300::OneWireMasterImpl::readBlock(span<uint_least8_t> recvBuf) {
00148   error_code result = parent->selectBus(OneWire);
00149   if (!result) {
00150     result = OneWireMasterDecorator::readBlock(recvBuf);
00151   }
00152   return result;
00153 }
00154 
00155 error_code DS9481P_300::OneWireMasterImpl::setSpeed(Speed newSpeed) {
00156   error_code result = parent->selectBus(OneWire);
00157   if (!result) {
00158     result = OneWireMasterDecorator::setSpeed(newSpeed);
00159   }
00160   return result;
00161 }
00162 
00163 error_code DS9481P_300::OneWireMasterImpl::setLevel(Level newLevel) {
00164   error_code result = parent->selectBus(OneWire);
00165   if (!result) {
00166     result = OneWireMasterDecorator::setLevel(newLevel);
00167   }
00168   return result;
00169 }
00170 
00171 error_code DS9481P_300::OneWireMasterImpl::triplet(TripletData & data) {
00172   error_code result = parent->selectBus(OneWire);
00173   if (!result) {
00174     result = OneWireMasterDecorator::triplet(data);
00175   }
00176   return result;
00177 }
00178 
00179 error_code DS9481P_300::I2CMasterImpl::start(uint_least8_t address) {
00180   error_code result = parent->selectBus(I2C);
00181   if (!result) {
00182     result = I2CMasterDecorator::start(address);
00183   }
00184   return result;
00185 }
00186 
00187 error_code DS9481P_300::I2CMasterImpl::stop() {
00188   error_code result = parent->selectBus(I2C);
00189   if (!result) {
00190     result = I2CMasterDecorator::stop();
00191   }
00192   return result;
00193 }
00194 
00195 error_code DS9481P_300::I2CMasterImpl::writeByte(uint_least8_t data) {
00196   error_code result = parent->selectBus(I2C);
00197   if (!result) {
00198     result = I2CMasterDecorator::writeByte(data);
00199   }
00200   return result;
00201 }
00202 
00203 error_code
00204 DS9481P_300::I2CMasterImpl::writeBlock(span<const uint_least8_t> data) {
00205   error_code result = parent->selectBus(I2C);
00206   if (!result) {
00207     result = I2CMasterDecorator::writeBlock(data);
00208   }
00209   return result;
00210 }
00211 
00212 error_code DS9481P_300::I2CMasterImpl::writePacketImpl(
00213     uint_least8_t address, span<const uint_least8_t> data, bool sendStop) {
00214   error_code result = parent->selectBus(I2C);
00215   if (!result) {
00216     result = I2CMasterDecorator::writePacketImpl(address, data, sendStop);
00217   }
00218   return result;
00219 }
00220 
00221 error_code DS9481P_300::I2CMasterImpl::readByte(AckStatus status,
00222                                                 uint_least8_t & data) {
00223   error_code result = parent->selectBus(I2C);
00224   if (!result) {
00225     result = I2CMasterDecorator::readByte(status, data);
00226   }
00227   return result;
00228 }
00229 
00230 error_code DS9481P_300::I2CMasterImpl::readBlock(AckStatus status,
00231                                                  span<uint_least8_t> data) {
00232   error_code result = parent->selectBus(I2C);
00233   if (!result) {
00234     result = I2CMasterDecorator::readBlock(status, data);
00235   }
00236   return result;
00237 }
00238 
00239 error_code DS9481P_300::I2CMasterImpl::readPacketImpl(uint_least8_t address,
00240                                                       span<uint_least8_t> data,
00241                                                       bool sendStop) {
00242   error_code result = parent->selectBus(I2C);
00243   if (!result) {
00244     result = I2CMasterDecorator::readPacketImpl(address, data, sendStop);
00245   }
00246   return result;
00247 }
00248 
00249 error_code DS9481P_300::DS2480BWithEscape::escape() {
00250   return sendCommand(0xE5);
00251 }
00252 
00253 error_code DS9481P_300::DS9400WithEscape::escape() {
00254   return configure('O');
00255 }
00256 
00257 } // namespace MaximInterface