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 DS1920.cpp Source File

DS1920.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 
00034 #include "Slaves/Sensors/DS1920/DS1920.h"
00035 #include "wait_api.h"
00036 
00037 
00038 using namespace OneWire;
00039 using namespace OneWire::crc;
00040 
00041 
00042 enum DS1920_CMDS
00043 {
00044     WRITE_SCRATCHPAD = 0x4E,
00045     READ_SCRATCHPAD = 0xBE,
00046     COPY_SCRATCHPAD = 0x48,
00047     CONV_TEMPERATURE = 0x44,
00048     RECALL = 0xB8  
00049 };
00050 
00051 
00052 /**********************************************************************/
00053 DS1920::DS1920(RandomAccessRomIterator &selector):OneWireSlave(selector)
00054 {
00055 }
00056 
00057 
00058 /**********************************************************************/
00059 DS1920::CmdResult DS1920::writeScratchPad(uint8_t th, uint8_t tl)
00060 {
00061     DS1920::CmdResult deviceResult = DS1920::OpFailure;
00062     
00063     OneWireMaster::CmdResult owmResult = selectDevice();
00064     
00065     if (owmResult == OneWireMaster::Success)
00066     {
00067         uint8_t sendBlock[] = {WRITE_SCRATCHPAD, th, tl};
00068         
00069         owmResult = master().OWWriteBlock(sendBlock, 3);
00070         if (owmResult == OneWireMaster::Success)
00071         {
00072             deviceResult = DS1920::Success;
00073         }
00074         else
00075         {
00076             deviceResult = DS1920::CommsWriteError;
00077         }
00078     }
00079     
00080     return deviceResult;
00081 }
00082 
00083 
00084 /**********************************************************************/
00085 DS1920::CmdResult DS1920::readScratchPad(uint8_t * scratchPadBuff)
00086 {
00087     DS1920::CmdResult deviceResult = DS1920::OpFailure;
00088     
00089     OneWireMaster::CmdResult owmResult = selectDevice();
00090     
00091     if (owmResult == OneWireMaster::Success)
00092     {
00093         uint8_t cmd = READ_SCRATCHPAD;
00094         
00095         owmResult = master().OWWriteBlock(&cmd, 1);
00096         if (owmResult == OneWireMaster::Success)
00097         {
00098             uint8_t rxBlock[9];
00099             owmResult = master().OWReadBlock(rxBlock, 9);
00100             
00101             uint8_t crcCheck = calculateCrc8(rxBlock, 8);
00102             if ((owmResult == OneWireMaster::Success) && (crcCheck == rxBlock[8]))
00103             {
00104                 std::memcpy(scratchPadBuff, rxBlock, 8);
00105                 deviceResult = DS1920::Success;
00106             }
00107             else
00108             {
00109                 deviceResult = DS1920::CommsReadError;
00110             }
00111         }
00112         else
00113         {
00114             deviceResult = DS1920::CommsWriteError;
00115         }
00116     }
00117     
00118     return deviceResult;
00119 }
00120 
00121 /**********************************************************************/
00122 DS1920::CmdResult DS1920::copyScratchPad( void )
00123 {
00124     DS1920::CmdResult deviceResult = DS1920::OpFailure;
00125     
00126     OneWireMaster::CmdResult owmResult = selectDevice();
00127     
00128     if (owmResult == OneWireMaster::Success)
00129     {
00130         owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::StrongLevel); 
00131         if (owmResult == OneWireMaster::Success)
00132         {
00133             wait_ms(10);
00134             
00135             owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);  
00136             if (owmResult == OneWireMaster::Success)
00137             {
00138                 deviceResult = DS1920::Success;
00139             }
00140         }
00141         else
00142         {
00143             deviceResult = DS1920::CommsWriteError;
00144         }
00145     }
00146     
00147     return deviceResult;
00148 }
00149 
00150 /**********************************************************************/
00151 DS1920::CmdResult DS1920::convertTemperature(float & temp)
00152 {
00153     DS1920::CmdResult deviceResult = DS1920::OpFailure;
00154     
00155     OneWireMaster::CmdResult owmResult = selectDevice();
00156     
00157     if (owmResult == OneWireMaster::Success)
00158     {
00159         owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::StrongLevel); 
00160         if (owmResult == OneWireMaster::Success)
00161         {
00162             wait_ms(750);
00163             
00164             owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);  
00165             if (owmResult == OneWireMaster::Success)
00166             {
00167                 uint8_t scratchPadBuff[8];
00168                 deviceResult = this->readScratchPad(scratchPadBuff);
00169                 if(deviceResult == DS1920::Success)
00170                 {
00171                     if(scratchPadBuff[0] & 1)
00172                     {
00173                         temp = (((float) (scratchPadBuff[0] >> 1)) + 0.5F);
00174                     }
00175                     else
00176                     {
00177                         temp = ((float) (scratchPadBuff[0] >> 1));
00178                     }
00179                     
00180                     if(scratchPadBuff[1])
00181                     {
00182                         temp = (temp * -1.0F);
00183                     }
00184                 }
00185             }
00186         }
00187         else
00188         {
00189             deviceResult = DS1920::CommsWriteError;
00190         }
00191     }
00192     
00193     return deviceResult;
00194 }
00195 
00196 
00197 /**********************************************************************/
00198 DS1920::CmdResult DS1920::recallEEPROM( void )
00199 {
00200     DS1920::CmdResult deviceResult = DS1920::OpFailure;
00201     
00202     OneWireMaster::CmdResult owmResult = selectDevice();
00203     
00204     if (owmResult == OneWireMaster::Success)
00205     {
00206         uint8_t cmd = RECALL;
00207         
00208         owmResult = master().OWWriteBlock(&cmd, 1);
00209         if (owmResult == OneWireMaster::Success)
00210         {
00211             deviceResult = DS1920::Success;
00212         }
00213         else
00214         {
00215             deviceResult = DS1920::CommsWriteError;
00216         }
00217     }
00218     
00219     return deviceResult;
00220 }