Mike Fruge / OneWire

Dependents:   Max32630_One_Wire_Interface

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Mon Aug 22 22:05:14 2016 +0000
Parent:
114:0be1b9a8eab7
Parent:
118:d184e69051ad
Child:
123:4de2506bf162
Commit message:
added support for DS2431

Changed in this revision

--- a/OneWire.h	Tue Aug 09 19:53:38 2016 -0500
+++ b/OneWire.h	Mon Aug 22 22:05:14 2016 +0000
@@ -35,7 +35,7 @@
 #define MBED_OneWire
 
 #include "Masters/Masters.h"
+#include "RomId/RomCommands.h"
 #include "Slaves/Slaves.h"
-#include "RomId/RomCommands.h"
 
 #endif /* MBED_OneWire */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Slaves/Memory/DS2431/DS2431.cpp	Mon Aug 22 22:05:14 2016 +0000
@@ -0,0 +1,266 @@
+/******************************************************************//**
+* 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 "wait_api.h"
+#include "Slaves/Memory/DS2431/DS2431.h"
+
+using namespace OneWire;
+using namespace OneWire::crc;
+
+enum DS2431_CMDS
+{
+    WRITE_SCRATCHPAD = 0x0F,
+    READ_SCRATCHPAD = 0xAA,
+    COPY_SCRATCHPAD = 0x55,
+    READ_MEMORY = 0xF0
+};
+
+//*********************************************************************
+DS2431::DS2431(RandomAccessRomIterator &selector):OneWireSlave(selector)
+{
+}
+
+//*********************************************************************
+OneWireSlave::CmdResult DS2431::writeMemory(uint16_t targetAddress, const uint8_t *data, uint8_t numBytes)
+{
+    OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
+    
+    if((targetAddress + numBytes) <= 0x88)
+    {
+        result = OneWireSlave::Success;
+        
+        uint8_t startOffset = (targetAddress & 0x0007);
+        uint16_t startRowAddress = (targetAddress & 0xFFF8);
+        uint8_t endOffset = ((targetAddress + numBytes) & 0x0007);
+        uint16_t endRowAddress = ((targetAddress + numBytes) & 0xFFF8);
+        const uint8_t *dataIdx = data;
+        
+        DS2431::Scratchpad scratchpadData;
+        uint8_t esByte;
+        
+        if(startOffset != 0)
+        {
+            result = this->readMemory(startRowAddress, scratchpadData, 8);
+            if(result == OneWireSlave::Success)
+            {
+                std::memcpy((scratchpadData + startOffset), data, (8 - startOffset));
+                result = this->writeScratchpad(startRowAddress, scratchpadData);
+                if(result == OneWireSlave::Success)
+                {
+                    result = this->readScratchpad(scratchpadData , esByte);
+                    if(result == OneWireSlave::Success)
+                    {
+                        result = this->copyScratchpad(startRowAddress, esByte);
+                        startRowAddress += 8;
+                        dataIdx = (data + (8 - startOffset));
+                    }
+                }
+            }
+        }
+        
+        if(result == OneWireSlave::Success)
+        {
+            for(uint16_t row = startRowAddress; row < endRowAddress; row += 8)
+            {
+                std::memcpy(scratchpadData, dataIdx, 8);
+                
+                result = this->writeScratchpad(row, scratchpadData);
+                if(result != OneWireSlave::Success)
+                {
+                    break;
+                }
+                
+                result = this->readScratchpad(scratchpadData, esByte);
+                if(result != OneWireSlave::Success)
+                {
+                    break;
+                }
+                
+                result = this->copyScratchpad(row, esByte);
+                if(result != OneWireSlave::Success)
+                {
+                    break;
+                }
+                
+                dataIdx += 8;
+            }
+        }
+        
+        if(result == OneWireSlave::Success)
+        {
+            if(endOffset != 0)
+            {
+                result = this->readMemory(endRowAddress, scratchpadData, 8);
+                if(result == OneWireSlave::Success)
+                {
+                    std::memcpy(scratchpadData, dataIdx, endOffset);
+                    result = this->writeScratchpad(endRowAddress, scratchpadData);
+                    if(result == OneWireSlave::Success)
+                    {
+                        result = this->readScratchpad(scratchpadData, esByte);
+                        if(result == OneWireSlave::Success)
+                        {
+                            result = this->copyScratchpad(endRowAddress, esByte);
+                        }
+                    }
+                }
+            }
+        }
+    }
+    
+    return result;
+}
+
+//*********************************************************************
+OneWireSlave::CmdResult DS2431::readMemory(uint16_t targetAddress, uint8_t *data, uint8_t numBytes)
+{
+    OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
+    
+    if((targetAddress + numBytes) <= 0x88)
+    {
+        OneWireMaster::CmdResult owmResult = selectDevice();
+        if(owmResult == OneWireMaster::Success)
+        {
+            uint8_t sendBlock[] = {READ_MEMORY, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF)};
+            owmResult = master().OWWriteBlock(sendBlock, 3);
+            if(owmResult == OneWireMaster::Success)
+            {
+                owmResult = master().OWReadBlock(data, numBytes);
+                if(owmResult == OneWireMaster::Success)
+                {
+                    result = OneWireSlave::Success;
+                }
+            }
+        }
+    }
+    
+    return result;
+}
+
+//*********************************************************************
+OneWireSlave::CmdResult DS2431::writeScratchpad(uint16_t targetAddress, const Scratchpad &data)
+{
+    OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    if(owmResult == OneWireMaster::Success)
+    {
+        uint8_t sendBlock[11];
+        sendBlock[0] = WRITE_SCRATCHPAD;
+        sendBlock[1] = (targetAddress &0xFF);
+        sendBlock[2] = ((targetAddress >> 8) &0xFF);
+        std::memcpy((sendBlock + 3), data, 8);
+        
+        owmResult = master().OWWriteBlock(sendBlock, 11);
+        
+        uint16_t invCRC16;
+        uint8_t recvbyte;
+        master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel);
+        invCRC16 = recvbyte;
+        master().OWReadByteSetLevel(recvbyte, OneWireMaster::NormalLevel);
+        invCRC16 |= (recvbyte << 8); 
+        
+        //calc our own inverted CRC16 to compare with one returned
+        uint16_t calculatedInvCRC16 = ~calculateCrc16(sendBlock, 0, 11);
+        
+        if(invCRC16 == calculatedInvCRC16)
+        {
+            result = OneWireSlave::Success;
+        }
+    }
+    
+    return result;
+}
+
+//*********************************************************************
+OneWireSlave::CmdResult DS2431::readScratchpad(Scratchpad &data, uint8_t &esByte)
+{
+    OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    if(owmResult == OneWireMaster::Success)
+    {
+        owmResult = master().OWWriteByteSetLevel(READ_SCRATCHPAD, OneWireMaster::NormalLevel);
+        if(owmResult == OneWireMaster::Success)
+        {
+            uint8_t recvBlock[13];
+            owmResult = master().OWReadBlock(recvBlock, 13);
+            
+            uint16_t invCRC16 = ((recvBlock[12] << 8) | recvBlock[11]);
+            
+            uint8_t idx = 12;
+            while(--idx)
+            {
+                recvBlock[idx] = recvBlock[idx - 1];
+            }
+            recvBlock[0] = READ_SCRATCHPAD;
+            
+            //calc our own inverted CRC16 to compare with one returned
+            uint16_t calculatedInvCRC16 = ~calculateCrc16(recvBlock, 0, 12);
+            
+            if(invCRC16 == calculatedInvCRC16)
+            {
+                esByte = recvBlock[3];
+                std::memcpy(data, (recvBlock + 4), 8);
+                result = OneWireSlave::Success;
+            }
+        }
+    }
+    
+    return result;
+}
+
+//*********************************************************************
+OneWireSlave::CmdResult DS2431::copyScratchpad(uint16_t targetAddress, uint8_t esByte)
+{
+    OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
+    
+    OneWireMaster::CmdResult owmResult = selectDevice();
+    if(owmResult == OneWireMaster::Success)
+    {
+        uint8_t sendBlock[] = {COPY_SCRATCHPAD, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF), esByte};
+        owmResult = master().OWWriteBlock(sendBlock, 4);
+        
+        master().OWSetLevel(OneWireMaster::StrongLevel);
+        wait_ms(10);
+        master().OWSetLevel(OneWireMaster::NormalLevel);
+        
+        uint8_t check;
+        master().OWReadByteSetLevel(check, OneWireMaster::NormalLevel);
+        if(check == 0xAA)
+        {
+            result = OneWireSlave::Success;
+        }
+    }
+    
+    return result;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Slaves/Memory/DS2431/DS2431.h	Mon Aug 22 22:05:14 2016 +0000
@@ -0,0 +1,176 @@
+/******************************************************************//**
+* 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_Slaves_Memory_DS2431
+#define OneWire_Slaves_Memory_DS2431
+
+#include "Slaves/OneWireSlave.h"
+
+namespace OneWire
+{
+    class OneWireMaster;
+    
+    /**
+    * @brief DS2431 1024-Bit 1-Wire EEPROM
+    * @details The DS2431 is a 1024-bit, 1-Wire® EEPROM chip organized 
+    * as four memory pages of 256 bits each. Data is written to an 8-byte 
+    * scratchpad, verified, and then copied to the EEPROM memory. As a 
+    * special feature, the four memory pages can individually be write 
+    * protected or put in EPROM-emulation mode, where bits can only be 
+    * changed from a 1 to a 0 state. The DS2431 communicates over the 
+    * single-conductor 1-Wire bus. The communication follows the standard 
+    * 1-Wire protocol. Each device has its own unalterable and unique 
+    * 64-bit ROM registration number that is factory lasered into the chip. 
+    * The registration number is used to address the device in a multidrop, 
+    * 1-Wire net environment.
+    */
+    class DS2431 : public OneWireSlave
+    {
+    public:
+
+        /**********************************************************//**
+        * @brief DS2431 constructor
+        *
+        * @details Instantiate a DS2431 object that encapsulates the 
+        * 1-Wire master and ROM commands for selecting the device via 
+        * the RandomAccessRomIterator sub-class passed as an argument 
+        * to the constructor.  
+        *
+        * This allows the user to focus on the use of the DS2431 in
+        * their application vs. the low level details of the 1-Wire 
+        * protocol.
+        *
+        * On Entry:
+        * @param[in] selector - reference to RandomAccessRomIterator
+        * sub-class; i.e. SingledropRomIterator, MultidropRomIterator, etc.
+        * See RomId/RomIterator.h
+        *
+        * On Exit:
+        *
+        * @return
+        **************************************************************/
+        DS2431(RandomAccessRomIterator &selector);
+        
+        /**********************************************************//**
+        * @brief writeMemory
+        *
+        * @details Writes data to EEPROM.  Wraps up writeScratchPad,
+        * readScratchPad and copyScratchPad into single function.
+        * 
+        * On Entry:
+        * @param[in] targetAddress - EEPROM memory address to start 
+        * writing at.
+        *
+        * @param[in] data - Pointer to memory holding data.
+        *
+        * @param[in] numBytes - Number of bytes to write.
+        *
+        * On Exit: 
+        *
+        * @return Result of operation
+        **************************************************************/
+        OneWireSlave::CmdResult writeMemory(uint16_t targetAddress, const uint8_t *data, uint8_t numBytes);
+        
+        /**********************************************************//**
+        * @brief readMemory
+        *
+        * @details Reads block of data from EEPROM memory.
+        *
+        * On Entry:
+        * @param[in] targetAddress - EEPROM memory address to start.
+        * reading from
+        *
+        * @param[out] data - Pointer to memory for storing data.
+        *
+        * @param[in] numBytes - Number of bytes to read.
+        *
+        * On Exit:
+        *
+        * @return Result of operation
+        **************************************************************/
+        OneWireSlave::CmdResult readMemory(uint16_t targetAddress, uint8_t *data, uint8_t numBytes);
+        
+    private:
+    
+        typedef array<uint8_t, 8> Scratchpad;
+    
+        /**********************************************************//**
+        * @brief writeScratchpad
+        *
+        * @details Writes 8 bytes to the scratchpad.
+        *
+        * On Entry:
+        * @param[in] targetAddress - EEPROM memory address that this data 
+        * will be copied to.  Must be on row boundary.
+        *
+        * @param[in] data - reference to bounded array type Scratchpad.
+        *
+        * On Exit:
+        *
+        * @return Result of operation
+        **************************************************************/
+        OneWireSlave::CmdResult writeScratchpad(uint16_t targetAddress, const Scratchpad &data);
+        
+        /**********************************************************//**
+        * @brief readScratchpad
+        *
+        * @details Reads contents of scratchpad.
+        *
+        * On Entry:
+        * @param[out] data - reference to bounded array type Scratchpad.
+        *
+        * On Exit:
+        *
+        * @return Result of operation
+        **************************************************************/
+        OneWireSlave::CmdResult readScratchpad(Scratchpad &data, uint8_t &esByte);
+        
+        /**********************************************************//**
+        * @brief copyScratchpad
+        *
+        * @details Copies contents of sractshpad to EEPROM.
+        *
+        * On Entry:
+        * @param[in] targetAddress - EEPROM memory address that this data 
+        * will be copied to.  Must be on row boundary.
+        *
+        * @param[in] esByte - Returned from reading scratchpad.
+        *
+        * On Exit:
+        *
+        * @return Result of operation
+        **************************************************************/
+        OneWireSlave::CmdResult copyScratchpad(uint16_t targetAddress, uint8_t esByte);
+    };
+}
+
+#endif /*OneWire_Slaves_Memory_DS2431*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Slaves/Memory/Memory.h	Mon Aug 22 22:05:14 2016 +0000
@@ -0,0 +1,41 @@
+/******************************************************************//**
+* 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_Slaves_Memory
+#define OneWire_Slaves_Memory
+
+
+#include "Slaves/Memory/DS2431/DS2431.h"
+
+
+#endif /*OneWire_Slaves_Memory*/
\ No newline at end of file
--- a/Slaves/Slaves.h	Tue Aug 09 19:53:38 2016 -0500
+++ b/Slaves/Slaves.h	Mon Aug 22 22:05:14 2016 +0000
@@ -36,6 +36,7 @@
 
 #include "Slaves/Authenticators/Authenticators.h"
 #include "Slaves/Bridges/Bridges.h"
+#include "Slaves/Memory/Memory.h"
 #include "Slaves/Sensors/Sensors.h"
 #include "Slaves/Switches/Switches.h"