Mike Fruge / OneWire

Dependents:   Max32630_One_Wire_Interface

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Thu Jun 02 21:56:16 2016 +0000
Parent:
79:7f22823a5a2d
Child:
81:e2a3ad98874e
Commit message:
added support for DS1920 temperature i-button

Changed in this revision

Temperature/DS1920/DS1920.cpp Show annotated file Show diff for this revision Revisions of this file
Temperature/DS1920/DS1920.h Show annotated file Show diff for this revision Revisions of this file
Temperature/OneWireTemperature.h Show annotated file Show diff for this revision Revisions of this file
crc.cpp Show annotated file Show diff for this revision Revisions of this file
crc.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature/DS1920/DS1920.cpp	Thu Jun 02 21:56:16 2016 +0000
@@ -0,0 +1,202 @@
+/******************************************************************//**
+* Copyright (C) 2016 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 "DS1920.h"
+
+
+using namespace OneWire;
+using namespace OneWire::crc;
+
+
+enum DS1920_CMDS
+{
+    WRITE_SCRATCHPAD = 0x4E,
+    READ_SCRATCHPAD = 0xBE,
+    COPY_SCRATCHPAD = 0x48,
+    CONV_TEMPERATURE = 0x44,
+    RECALL = 0xB8  
+};
+
+
+/**********************************************************************/
+DS1920::DS1920(RandomAccessRomIterator &selector) : OneWireSlave(selector)
+{
+}
+
+
+/**********************************************************************/
+DS1920::CmdResult DS1920::writeScratchPad(uint8_t th, uint8_t tl)
+{
+    DS1920::CmdResult deviceResult = DS1920::OpFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    
+    if (owmResult == OneWireMaster::Success)
+    {
+        uint8_t sendBlock[] = {WRITE_SCRATCHPAD, th, tl};
+        
+        owmResult = master().OWWriteBlock(sendBlock, 3);
+        if (owmResult == OneWireMaster::Success)
+        {
+            deviceResult = DS1920::Success;
+        }
+        else
+        {
+            deviceResult = DS1920::CommsWriteError;
+        }
+    }
+    
+    return deviceResult;
+}
+
+
+/**********************************************************************/
+DS1920::CmdResult DS1920::readScratchPad(uint8_t * scratchPadBuff)
+{
+    DS1920::CmdResult deviceResult = DS1920::OpFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    
+    if (owmResult == OneWireMaster::Success)
+    {
+        uint8_t cmd = READ_SCRATCHPAD;
+        
+        owmResult = master().OWWriteBlock(&cmd, 1);
+        if (owmResult == OneWireMaster::Success)
+        {
+            uint8_t rxBlock[9];
+            owmResult = master().OWReadBlock(rxBlock, 9);
+            
+            uint8_t crcCheck = calculateCrc8(rxBlock, 8);
+            if ((owmResult == OneWireMaster::Success) && (crcCheck == rxBlock[8]))
+            {
+                std::memcpy(scratchPadBuff, rxBlock, 8);
+                deviceResult = DS1920::Success;
+            }
+            else
+            {
+                deviceResult = DS1920::CommsReadError;
+            }
+        }
+        else
+        {
+            deviceResult = DS1920::CommsWriteError;
+        }
+    }
+    
+    return deviceResult;
+}
+
+/**********************************************************************/
+DS1920::CmdResult DS1920::copyScratchPad( void )
+{
+    DS1920::CmdResult deviceResult = DS1920::OpFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    
+    if (owmResult == OneWireMaster::Success)
+    {
+        owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::StrongLevel); 
+        if (owmResult == OneWireMaster::Success)
+        {
+            wait_ms(10);
+            
+            owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);  
+            if (owmResult == OneWireMaster::Success)
+            {
+                deviceResult = DS1920::Success;
+            }
+        }
+        else
+        {
+            deviceResult = DS1920::CommsWriteError;
+        }
+    }
+    
+    return deviceResult;
+}
+
+/**********************************************************************/
+DS1920::CmdResult DS1920::convertTemperature(uint16_t temp)
+{
+    DS1920::CmdResult deviceResult = DS1920::OpFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    
+    if (owmResult == OneWireMaster::Success)
+    {
+        owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::StrongLevel); 
+        if (owmResult == OneWireMaster::Success)
+        {
+            wait_ms(750);
+            
+            owmResult = master().OWSetLevel(OneWireMaster::NormalLevel);  
+            if (owmResult == OneWireMaster::Success)
+            {
+                deviceResult = DS1920::Success;
+            }
+        }
+        else
+        {
+            deviceResult = DS1920::CommsWriteError;
+        }
+    }
+    
+    return deviceResult;
+}
+
+
+/**********************************************************************/
+DS1920::CmdResult DS1920::recallEEPROM( void )
+{
+    DS1920::CmdResult deviceResult = DS1920::OpFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    
+    if (owmResult == OneWireMaster::Success)
+    {
+        uint8_t cmd = RECALL;
+        
+        owmResult = master().OWWriteBlock(&cmd, 1);
+        if (owmResult == OneWireMaster::Success)
+        {
+            deviceResult = DS1920::Success;
+        }
+        else
+        {
+            deviceResult = DS1920::CommsWriteError;
+        }
+    }
+    
+    return deviceResult;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature/DS1920/DS1920.h	Thu Jun 02 21:56:16 2016 +0000
@@ -0,0 +1,182 @@
+/******************************************************************//**
+* Copyright (C) 2016 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.
+**********************************************************************/
+
+#ifndef OneWire_Temperature_DS1920
+#define OneWire_Temperature_DS1920
+
+#include "mbed.h"
+#include "Masters/OneWireMasters.h"
+#include "OneWireSlave.h"
+#include "crc.h"
+
+namespace OneWire
+{
+    class OneWireMaster;
+    
+    /**
+    * @brief DS1920 1-wire temperature i-button
+    *
+    * @details The iButton® temperature logger (DS1920) provides 
+    * direct-to-digital 9-bit temperature readings over a range of 
+    * -55°C to +100°C in 0.5° increments. The iButton communicates with
+    * a processor using the 1-Wire® protocol through a hardware port 
+    * interface. The port interface provides both the physical link and 
+    * handles the communication protocols that enable the processor to
+    * access iButton resources with simple commands. Two bytes of 
+    * EEPROM can be used either to set alarm triggers or for storing 
+    * user data.
+    *
+    * @code
+    * @endcode
+    */
+    class DS1920 : public OneWireSlave
+    {
+    public:
+        enum CmdResult
+        {
+            Success,
+            CommsReadError,
+            CommsWriteError,
+            OpFailure
+        };
+
+        /**********************************************************//**
+        * @brief DS1920 constructor
+        *
+        * @details
+        *
+        * On Entry:
+        * @param[in] selector - Reference to RandomAccessRomiteraor 
+        * object that encapsulates owm master that has access to this 
+        * device and ROM function commands used to a select device
+        *
+        * On Exit:
+        *
+        * @return
+        **************************************************************/
+        DS1920(RandomAccessRomIterator &selector);
+        
+        
+        /**********************************************************//**
+        * @brief Write Scratchpad Command
+        *
+        * @details If the result of a temperature measurement is higher 
+        * than TH or lower than TL, an alarm flag inside the device is 
+        * set. This flag is updated with every temperature measurement. 
+        * As long as the alarm flag is set, the DS1920 will respond to 
+        * the alarm search command.
+        *
+        * On Entry:
+        * @param[in] th - 8-bit upper temperature threshold, MSB 
+        * indicates sign 
+        * @param[in] tl - 8-bit lower temperature threshold, MSB 
+        * indicates sign 
+        *
+        * On Exit:
+        * @param[out]
+        *
+        * @return CmdResult - result of operation
+        **************************************************************/
+        CmdResult writeScratchPad(uint8_t th, uint8_t tl);
+        
+        
+        /**********************************************************//**
+        * @brief Read Scratchpad Command
+        *
+        * @details This command reads the complete scratchpad.
+        *
+        * On Entry:
+        * @param[in] scratchPadBuff - array for receiving contents of 
+        * scratchpad, this buffer will be over written
+        *
+        * On Exit:
+        * @param[out] scratchPadBuff - contents of scratchpad
+        *
+        * @return CmdResult - result of operation
+        **************************************************************/
+        CmdResult readScratchPad(uint8_t * scratchPadBuff);
+        
+        /**********************************************************//**
+        * @brief Copy Scratchpad Command
+        *
+        * @details This command copies from the scratchpad into the 
+        * EEPROM of the DS1920, storing the temperature trigger bytes 
+        * in nonvolatile memory.
+        *
+        * On Entry:
+        * @param[in] 
+        *
+        * On Exit:
+        * @param[out]
+        *
+        * @return CmdResult - result of operation
+        **************************************************************/
+        CmdResult copyScratchPad( void );
+        
+        /**********************************************************//**
+        * @brief Convert Temperature Command
+        *
+        * @details This command begins a temperature conversion. 
+        *
+        * On Entry:
+        * @param[in]
+        *
+        * On Exit:
+        * @param[out] temp - temperature conversion results
+        *
+        * @return CmdResult - result of operation
+        **************************************************************/
+        CmdResult convertTemperature(uint16_t temp);
+        
+        
+        /**********************************************************//**
+        * @brief Recall Command
+        *
+        * @details This command recalls the temperature trigger values 
+        * stored in EEPROM to the scratchpad
+        *
+        * On Entry:
+        * @param[in]
+        *
+        * On Exit:
+        * @param[out] 
+        *
+        * @return CmdResult - result of operation
+        **************************************************************/
+        CmdResult recallEEPROM( void );
+        
+    private:
+
+    };
+}
+
+#endif /* OneWire_Temperature_DS1920 */
\ No newline at end of file
--- a/Temperature/OneWireTemperature.h	Mon May 23 18:57:12 2016 -0500
+++ b/Temperature/OneWireTemperature.h	Thu Jun 02 21:56:16 2016 +0000
@@ -35,4 +35,7 @@
 #define ONEWIRETEMPERATURE_H
 
 
+#include "DS1920/DS1920.h"
+
+
 #endif /*ONEWIRETEMPERATURE_H*/
--- a/crc.cpp	Mon May 23 18:57:12 2016 -0500
+++ b/crc.cpp	Thu Jun 02 21:56:16 2016 +0000
@@ -1,5 +1,36 @@
+
+
 #include "crc.h"
 
+
+uint8_t OneWire::crc::calculateCrc8(uint8_t crc8, uint8_t data)
+{
+    // See Application Note 27
+    crc8 = crc8 ^ data;
+    for (int i = 0; i < 8; i++)
+    {
+        if (crc8 & 1)
+        {
+            crc8 = (crc8 >> 1) ^ 0x8c;
+        }
+        else
+        {
+            crc8 = (crc8 >> 1);
+        }
+    }
+ 
+    return crc8;
+}
+ 
+uint8_t OneWire::crc::calculateCrc8(const uint8_t * data, size_t dataLen, uint8_t crc)
+{
+    for (size_t i = 0; i < dataLen; i++)
+    {
+        crc = calculateCrc8(crc, data[i]);
+    }
+    return crc;
+}
+ 
 uint16_t OneWire::crc::calculateCrc16(uint16_t crc16, uint16_t data)
 {
     const uint16_t oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
--- a/crc.h	Mon May 23 18:57:12 2016 -0500
+++ b/crc.h	Thu Jun 02 21:56:16 2016 +0000
@@ -8,6 +8,19 @@
 {
     namespace crc
     {
+        /// Perform a CRC8 calculation.
+        /// @param crc8 Beginning state of the CRC generator.
+        /// @param data Data to pass though the CRC generator.
+        /// @returns The calculated CRC8.
+        uint8_t calculateCrc8(uint8_t crc8, uint8_t data);
+        
+        /// Perform a CRC8 calculation with variable length data.
+        /// @param[in] data Data array to pass through the CRC generator.
+        /// @param dataLen Length of the data array to process.
+        /// @param crc Beginning state of the CRC generator.
+        /// @returns The calculated CRC8.
+        uint8_t calculateCrc8(const uint8_t * data, size_t dataLen, uint8_t crc = 0);
+        
         /// Perform a CRC16 calculation.
         /// @param crc16 Beginning state of the CRC generator.
         /// @param data Data to pass though the CRC generator.