Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Fri Apr 08 16:11:16 2016 -0500
Revision:
49:36954b62f503
Parent:
33:a4c015046956
Child:
62:43039aeca2ab
Added a Read Scratchpad operation for DS28E15/22/25. Added comments for various classes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 17:b646b1e3970b 1 /******************************************************************//**
j3 17:b646b1e3970b 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 17:b646b1e3970b 3 *
j3 17:b646b1e3970b 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 17:b646b1e3970b 5 * copy of this software and associated documentation files (the "Software"),
j3 17:b646b1e3970b 6 * to deal in the Software without restriction, including without limitation
j3 17:b646b1e3970b 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 17:b646b1e3970b 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 17:b646b1e3970b 9 * Software is furnished to do so, subject to the following conditions:
j3 17:b646b1e3970b 10 *
j3 17:b646b1e3970b 11 * The above copyright notice and this permission notice shall be included
j3 17:b646b1e3970b 12 * in all copies or substantial portions of the Software.
j3 17:b646b1e3970b 13 *
j3 17:b646b1e3970b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 17:b646b1e3970b 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 17:b646b1e3970b 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 17:b646b1e3970b 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 17:b646b1e3970b 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 17:b646b1e3970b 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 17:b646b1e3970b 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 17:b646b1e3970b 21 *
j3 17:b646b1e3970b 22 * Except as contained in this notice, the name of Maxim Integrated
j3 17:b646b1e3970b 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 17:b646b1e3970b 24 * Products, Inc. Branding Policy.
j3 17:b646b1e3970b 25 *
j3 17:b646b1e3970b 26 * The mere transfer of this software does not imply any licenses
j3 17:b646b1e3970b 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 17:b646b1e3970b 28 * trademarks, maskwork rights, or any other form of intellectual
j3 17:b646b1e3970b 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 17:b646b1e3970b 30 * ownership rights.
j3 17:b646b1e3970b 31 **********************************************************************/
j3 17:b646b1e3970b 32
j3 17:b646b1e3970b 33
j3 17:b646b1e3970b 34 #ifndef _ROMID_HPP
j3 17:b646b1e3970b 35 #define _ROMID_HPP
j3 17:b646b1e3970b 36
j3 17:b646b1e3970b 37 #include <cstdint>
IanBenzMaxim 33:a4c015046956 38
IanBenzMaxim 33:a4c015046956 39 #include "array.hpp"
j3 17:b646b1e3970b 40
IanBenzMaxim 49:36954b62f503 41 /// Standard container for a 1-Wire ROM ID.
j3 17:b646b1e3970b 42 class RomId
j3 17:b646b1e3970b 43 {
j3 17:b646b1e3970b 44 public:
IanBenzMaxim 49:36954b62f503 45 /// Length of the buffer in bytes.
j3 17:b646b1e3970b 46 static const std::size_t byteLen = 8;
IanBenzMaxim 49:36954b62f503 47
IanBenzMaxim 49:36954b62f503 48 /// Built-in array representation.
IanBenzMaxim 33:a4c015046956 49 typedef array<std::uint8_t, byteLen>::Buffer ByteBuffer;
j3 17:b646b1e3970b 50
IanBenzMaxim 49:36954b62f503 51 /// Perform a CRC8 calculation.
IanBenzMaxim 49:36954b62f503 52 /// @param crc8 Beginning state of the CRC generator.
IanBenzMaxim 49:36954b62f503 53 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 49:36954b62f503 54 /// @returns The calculated CRC8.
IanBenzMaxim 21:00c94aeb533e 55 static std::uint8_t calculateCRC8(std::uint8_t crc8, std::uint8_t data);
IanBenzMaxim 49:36954b62f503 56
IanBenzMaxim 49:36954b62f503 57 /// Perform a CRC8 calculation with variable length data.
IanBenzMaxim 49:36954b62f503 58 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 49:36954b62f503 59 /// @param data_len Length of the data array to process.
IanBenzMaxim 49:36954b62f503 60 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 49:36954b62f503 61 /// @returns The calculated CRC8.
IanBenzMaxim 21:00c94aeb533e 62 static std::uint8_t calculateCRC8(const std::uint8_t * data, std::size_t data_len, std::uint8_t crc = 0);
j3 17:b646b1e3970b 63
j3 17:b646b1e3970b 64 private:
j3 17:b646b1e3970b 65 static const std::size_t familyCodeIdx = 0;
j3 17:b646b1e3970b 66 static const std::size_t crc8Idx = 7;
IanBenzMaxim 49:36954b62f503 67
IanBenzMaxim 49:36954b62f503 68 /// Default starting value is all bytes 0x00.
j3 17:b646b1e3970b 69 static const std::uint8_t defaultByteVal = 0x00;
j3 17:b646b1e3970b 70
IanBenzMaxim 33:a4c015046956 71 array<std::uint8_t, byteLen> m_romId;
j3 17:b646b1e3970b 72
j3 17:b646b1e3970b 73 public:
IanBenzMaxim 49:36954b62f503 74 const RomId & operator=(const RomId & rhs)
IanBenzMaxim 49:36954b62f503 75 {
IanBenzMaxim 33:a4c015046956 76 this->m_romId = rhs.m_romId;
j3 17:b646b1e3970b 77 return rhs;
j3 17:b646b1e3970b 78 }
IanBenzMaxim 49:36954b62f503 79
IanBenzMaxim 49:36954b62f503 80 bool operator==(const RomId & rhs) const
IanBenzMaxim 49:36954b62f503 81 {
IanBenzMaxim 33:a4c015046956 82 return (this->m_romId == rhs.m_romId);
j3 17:b646b1e3970b 83 }
IanBenzMaxim 49:36954b62f503 84
IanBenzMaxim 49:36954b62f503 85 bool operator!=(const RomId & rhs) const
IanBenzMaxim 49:36954b62f503 86 {
j3 17:b646b1e3970b 87 return !operator==(rhs);
j3 17:b646b1e3970b 88 }
IanBenzMaxim 49:36954b62f503 89
IanBenzMaxim 49:36954b62f503 90 /// Conversion to array reference.
IanBenzMaxim 49:36954b62f503 91 operator ByteBuffer &()
IanBenzMaxim 49:36954b62f503 92 {
IanBenzMaxim 49:36954b62f503 93 return m_romId;
j3 17:b646b1e3970b 94 }
IanBenzMaxim 49:36954b62f503 95
IanBenzMaxim 49:36954b62f503 96 /// Conversion to const array reference.
IanBenzMaxim 49:36954b62f503 97 operator const ByteBuffer &() const
IanBenzMaxim 49:36954b62f503 98 {
IanBenzMaxim 49:36954b62f503 99 return m_romId;
j3 17:b646b1e3970b 100 }
j3 17:b646b1e3970b 101
IanBenzMaxim 49:36954b62f503 102 /// Reset to the default starting value.
IanBenzMaxim 49:36954b62f503 103 void reset()
IanBenzMaxim 49:36954b62f503 104 {
j3 17:b646b1e3970b 105 std::memset(m_romId, defaultByteVal, byteLen);
j3 17:b646b1e3970b 106 }
j3 17:b646b1e3970b 107
IanBenzMaxim 33:a4c015046956 108 RomId() { reset(); }
IanBenzMaxim 33:a4c015046956 109 RomId(const RomId & romId) : m_romId(romId.m_romId) { }
IanBenzMaxim 33:a4c015046956 110 RomId(const ByteBuffer & romIdBytes) : m_romId(romIdBytes) { }
j3 17:b646b1e3970b 111
IanBenzMaxim 49:36954b62f503 112 /// Read the Family Code byte.
IanBenzMaxim 49:36954b62f503 113 std::uint8_t familyCode() const
IanBenzMaxim 49:36954b62f503 114 {
j3 17:b646b1e3970b 115 return m_romId[familyCodeIdx];
j3 17:b646b1e3970b 116 }
IanBenzMaxim 49:36954b62f503 117
IanBenzMaxim 49:36954b62f503 118 /// Set the family code byte.
IanBenzMaxim 49:36954b62f503 119 void setFamilyCode(std::uint8_t familyCode)
IanBenzMaxim 49:36954b62f503 120 {
j3 17:b646b1e3970b 121 m_romId[familyCodeIdx] = familyCode;
j3 17:b646b1e3970b 122 }
j3 17:b646b1e3970b 123
IanBenzMaxim 49:36954b62f503 124 /// Read the CRC8 byte.
IanBenzMaxim 49:36954b62f503 125 std::uint8_t crc8() const
IanBenzMaxim 49:36954b62f503 126 {
j3 17:b646b1e3970b 127 return m_romId[crc8Idx];
j3 17:b646b1e3970b 128 }
IanBenzMaxim 49:36954b62f503 129
IanBenzMaxim 49:36954b62f503 130 /// Set the CRC8 byte.
IanBenzMaxim 49:36954b62f503 131 void setCrc8(std::uint8_t crc8)
IanBenzMaxim 49:36954b62f503 132 {
j3 17:b646b1e3970b 133 m_romId[crc8Idx] = crc8;
j3 17:b646b1e3970b 134 }
IanBenzMaxim 49:36954b62f503 135
IanBenzMaxim 49:36954b62f503 136 /// Check if the CRC8 is valid for the ROM ID.
IanBenzMaxim 49:36954b62f503 137 /// @returns True if the CRC8 is valid.
IanBenzMaxim 49:36954b62f503 138 bool crc8Valid() const
IanBenzMaxim 49:36954b62f503 139 {
j3 17:b646b1e3970b 140 return (calculateCRC8(m_romId, (byteLen - 1), 0x00) == crc8());
j3 17:b646b1e3970b 141 }
IanBenzMaxim 49:36954b62f503 142
IanBenzMaxim 49:36954b62f503 143 /// Calculate and set the CRC8 for the ROM ID.
IanBenzMaxim 49:36954b62f503 144 void setValidCrc8()
IanBenzMaxim 49:36954b62f503 145 {
j3 17:b646b1e3970b 146 setCrc8(calculateCRC8(m_romId, (byteLen - 1), 0x00));
j3 17:b646b1e3970b 147 }
j3 17:b646b1e3970b 148
IanBenzMaxim 49:36954b62f503 149 /// Check if the ROM ID is valid (Family Code and CRC8 are both valid).
IanBenzMaxim 49:36954b62f503 150 /// @returns True if the ROM ID is valid.
IanBenzMaxim 49:36954b62f503 151 bool valid() const
IanBenzMaxim 49:36954b62f503 152 {
j3 17:b646b1e3970b 153 return (crc8Valid() && (familyCode() != defaultByteVal));
j3 17:b646b1e3970b 154 }
j3 17:b646b1e3970b 155 };
j3 17:b646b1e3970b 156
IanBenzMaxim 49:36954b62f503 157 #endif