Maxim Integrated / OneWire

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS2413.cpp Source File

DS2413.cpp

00001 /******************************************************************//**
00002 * Copyright (C) 2016 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 "Slaves/Switches/DS2413/DS2413.h"
00034 
00035 using OneWire::DS2413;
00036 using OneWire::OneWireMaster;
00037 
00038 enum DS2413_CMDS
00039 {
00040     PIO_ACCESS_READ = 0xF5,
00041     PIO_ACCESS_WRITE = 0x5A
00042 };
00043 
00044 enum DS2413_PIO
00045 {
00046     PIOA,
00047     PIOB,
00048     PIOAB
00049 };
00050 
00051 DS2413::DS2413(RandomAccessRomIterator &selector) : OneWireSlave(selector)
00052 {
00053 }
00054 
00055 DS2413::CmdResult DS2413::pioAccessReadChA(uint8_t & val)
00056 {
00057     DS2413::CmdResult result = OpFailure;
00058 
00059     result = pioAccessRead(val);
00060 
00061     val = (val & 0x01);
00062 
00063     return result;
00064 }
00065 
00066 DS2413::CmdResult DS2413::pioAccessReadChB(uint8_t & val)
00067 {
00068     DS2413::CmdResult result = OpFailure;
00069 
00070     result = pioAccessRead(val);
00071 
00072     val = ((val >> 2) & 0x01);
00073 
00074     return result;
00075 }
00076 
00077 DS2413::CmdResult DS2413::pioAccessWriteChA(uint8_t val)
00078 {
00079     DS2413::CmdResult result = OpFailure;
00080 
00081     uint8_t local_val = 0;
00082 
00083     //read current state of PIO
00084     result = pioAccessRead(local_val);
00085 
00086     if (result == DS2413::Success)
00087     {
00088         //modify
00089         //current state of pioB OR
00090         //desired state of pioA OR
00091         //bits[7:2] should all be 1, per datasheet
00092         val = (0xFC | (((local_val >> 1) & 0x02) | (0x01 & val)));
00093 
00094         //write, bit[1:0] new state of pio
00095         result = pioAccessWrite(val);
00096     }
00097 
00098     return result;
00099 }
00100 
00101 DS2413::CmdResult DS2413::pioAccessWriteChB(uint8_t val)
00102 {
00103     DS2413::CmdResult result = OpFailure;
00104 
00105     uint8_t local_val = 0;
00106 
00107     //read current state of PIO
00108     result = pioAccessRead(local_val);
00109 
00110     if (result == DS2413::Success)
00111     {
00112         //modify
00113         //current state of pioA OR
00114         //desired state of pioB OR
00115         //bits[7:2] should all be 1, per datasheet
00116         val = (0xFC | ((local_val & 0x01) | (0x02 & (val << 1))));
00117 
00118         //write, bit[1:0] new state of pio
00119         result = pioAccessWrite(val);
00120     }
00121 
00122     return result;
00123 }
00124 
00125 DS2413::CmdResult DS2413::pioAccessWriteChAB(uint8_t val)
00126 {
00127     return pioAccessWrite((0x03 & val) | 0xFC);
00128 }
00129 
00130 DS2413::CmdResult DS2413::pioAccessRead(uint8_t & val)
00131 {
00132     DS2413::CmdResult result = DS2413::OpFailure;
00133 
00134     OneWireMaster::CmdResult ow_result = selectDevice();
00135 
00136     if (ow_result == OneWireMaster::Success)
00137     {
00138         ow_result = master().OWWriteByte(PIO_ACCESS_READ);
00139 
00140         if (ow_result == OneWireMaster::Success)
00141         {
00142             ow_result = master().OWReadByte(val);
00143 
00144             if (ow_result == OneWireMaster::Success)
00145             {
00146                 result = DS2413::Success;
00147             }
00148             else
00149             {
00150                 result = DS2413::CommsReadError;
00151             }
00152         }
00153         else
00154         {
00155             result = DS2413::CommsWriteError;
00156         }
00157     }
00158 
00159     return result;
00160 }
00161 
00162 DS2413::CmdResult DS2413::pioAccessWrite(uint8_t val)
00163 {
00164     DS2413::CmdResult result = DS2413::OpFailure;
00165 
00166     OneWireMaster::CmdResult ow_result = selectDevice();
00167 
00168     if (ow_result == OneWireMaster::Success)
00169     {
00170         uint8_t send_block[] = { PIO_ACCESS_WRITE, val, static_cast<uint8_t>(~val) };
00171 
00172         ow_result = master().OWWriteBlock(send_block, 3);
00173         if (ow_result == OneWireMaster::Success)
00174         {
00175             uint8_t expected_status = ((0x01 & val) | ((0x01 & val) << 1) |
00176                 ((0x02 & val) << 1) | ((0x02 & val) << 2));
00177 
00178             uint8_t rcv_block[2];
00179             ow_result = master().OWReadBlock(rcv_block, 2);
00180 
00181             if (ow_result == OneWireMaster::Success)
00182             {
00183                 if ((rcv_block[0] == 0xAA) && ((rcv_block[1] & 0x0F) == expected_status))
00184                 {
00185                     result = DS2413::Success;
00186                 }
00187             }
00188             else
00189             {
00190                 result = DS2413::CommsReadError;
00191             }
00192         }
00193         else
00194         {
00195             result = DS2413::CommsWriteError;
00196         }
00197     }
00198 
00199     return result;
00200 }