Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Thu May 12 14:38:16 2016 -0500
Revision:
73:2cecc1372acc
Parent:
RomId.hpp@62:43039aeca2ab
Child:
75:8b627804927c
Added namespaces. Renamed files and directories for consistency. Use <stdint.h> instead of <cstdint> since it is not supported by C++98.

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
IanBenzMaxim 73:2cecc1372acc 33 #ifndef OneWire_RomId
IanBenzMaxim 73:2cecc1372acc 34 #define OneWire_RomId
j3 17:b646b1e3970b 35
IanBenzMaxim 73:2cecc1372acc 36 #include "array.h"
j3 17:b646b1e3970b 37
IanBenzMaxim 73:2cecc1372acc 38 namespace OneWire
j3 17:b646b1e3970b 39 {
IanBenzMaxim 73:2cecc1372acc 40 /// Standard container for a 1-Wire ROM ID.
IanBenzMaxim 73:2cecc1372acc 41 class RomId
IanBenzMaxim 73:2cecc1372acc 42 {
IanBenzMaxim 73:2cecc1372acc 43 public:
IanBenzMaxim 73:2cecc1372acc 44 /// Length of the buffer in bytes.
IanBenzMaxim 73:2cecc1372acc 45 static const size_t byteLen = 8;
IanBenzMaxim 73:2cecc1372acc 46
IanBenzMaxim 73:2cecc1372acc 47 /// Built-in array representation.
IanBenzMaxim 73:2cecc1372acc 48 typedef array<uint8_t, byteLen>::Buffer ByteBuffer;
j3 17:b646b1e3970b 49
IanBenzMaxim 73:2cecc1372acc 50 /// Perform a CRC8 calculation.
IanBenzMaxim 73:2cecc1372acc 51 /// @param crc8 Beginning state of the CRC generator.
IanBenzMaxim 73:2cecc1372acc 52 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 73:2cecc1372acc 53 /// @returns The calculated CRC8.
IanBenzMaxim 73:2cecc1372acc 54 static uint8_t calculateCRC8(uint8_t crc8, uint8_t data);
IanBenzMaxim 73:2cecc1372acc 55
IanBenzMaxim 73:2cecc1372acc 56 /// Perform a CRC8 calculation with variable length data.
IanBenzMaxim 73:2cecc1372acc 57 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 73:2cecc1372acc 58 /// @param data_len Length of the data array to process.
IanBenzMaxim 73:2cecc1372acc 59 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 73:2cecc1372acc 60 /// @returns The calculated CRC8.
IanBenzMaxim 73:2cecc1372acc 61 static uint8_t calculateCRC8(const uint8_t * data, size_t data_len, uint8_t crc = 0);
j3 17:b646b1e3970b 62
IanBenzMaxim 73:2cecc1372acc 63 private:
IanBenzMaxim 73:2cecc1372acc 64 static const size_t familyCodeIdx = 0;
IanBenzMaxim 73:2cecc1372acc 65 static const size_t crc8Idx = 7;
IanBenzMaxim 73:2cecc1372acc 66
IanBenzMaxim 73:2cecc1372acc 67 /// Default starting value is all bytes 0x00.
IanBenzMaxim 73:2cecc1372acc 68 static const uint8_t defaultByteVal = 0x00;
j3 17:b646b1e3970b 69
IanBenzMaxim 73:2cecc1372acc 70 array<uint8_t, byteLen> m_romId;
j3 17:b646b1e3970b 71
IanBenzMaxim 73:2cecc1372acc 72 public:
IanBenzMaxim 73:2cecc1372acc 73 RomId() { reset(); }
IanBenzMaxim 73:2cecc1372acc 74 RomId(const RomId & romId) : m_romId(romId.m_romId) { }
IanBenzMaxim 73:2cecc1372acc 75 RomId(const ByteBuffer & romIdBytes) : m_romId(romIdBytes) { }
IanBenzMaxim 62:43039aeca2ab 76
IanBenzMaxim 73:2cecc1372acc 77 const RomId & operator=(const RomId & rhs)
IanBenzMaxim 73:2cecc1372acc 78 {
IanBenzMaxim 73:2cecc1372acc 79 this->m_romId = rhs.m_romId;
IanBenzMaxim 73:2cecc1372acc 80 return rhs;
IanBenzMaxim 73:2cecc1372acc 81 }
IanBenzMaxim 73:2cecc1372acc 82
IanBenzMaxim 73:2cecc1372acc 83 bool operator==(const RomId & rhs) const
IanBenzMaxim 73:2cecc1372acc 84 {
IanBenzMaxim 73:2cecc1372acc 85 return (this->m_romId == rhs.m_romId);
IanBenzMaxim 73:2cecc1372acc 86 }
IanBenzMaxim 73:2cecc1372acc 87
IanBenzMaxim 73:2cecc1372acc 88 bool operator!=(const RomId & rhs) const
IanBenzMaxim 73:2cecc1372acc 89 {
IanBenzMaxim 73:2cecc1372acc 90 return !operator==(rhs);
IanBenzMaxim 73:2cecc1372acc 91 }
IanBenzMaxim 73:2cecc1372acc 92
IanBenzMaxim 73:2cecc1372acc 93 /// Conversion to array reference.
IanBenzMaxim 73:2cecc1372acc 94 operator ByteBuffer &()
IanBenzMaxim 73:2cecc1372acc 95 {
IanBenzMaxim 73:2cecc1372acc 96 return m_romId;
IanBenzMaxim 73:2cecc1372acc 97 }
IanBenzMaxim 73:2cecc1372acc 98
IanBenzMaxim 73:2cecc1372acc 99 /// Conversion to const array reference.
IanBenzMaxim 73:2cecc1372acc 100 operator const ByteBuffer &() const
IanBenzMaxim 73:2cecc1372acc 101 {
IanBenzMaxim 73:2cecc1372acc 102 return m_romId;
IanBenzMaxim 73:2cecc1372acc 103 }
j3 17:b646b1e3970b 104
IanBenzMaxim 73:2cecc1372acc 105 /// Reset to the default starting value.
IanBenzMaxim 73:2cecc1372acc 106 void reset()
IanBenzMaxim 73:2cecc1372acc 107 {
IanBenzMaxim 73:2cecc1372acc 108 std::memset(m_romId, defaultByteVal, byteLen);
IanBenzMaxim 73:2cecc1372acc 109 }
j3 17:b646b1e3970b 110
IanBenzMaxim 73:2cecc1372acc 111 /// Read the Family Code byte.
IanBenzMaxim 73:2cecc1372acc 112 uint8_t familyCode() const
IanBenzMaxim 73:2cecc1372acc 113 {
IanBenzMaxim 73:2cecc1372acc 114 return m_romId[familyCodeIdx];
IanBenzMaxim 73:2cecc1372acc 115 }
IanBenzMaxim 73:2cecc1372acc 116
IanBenzMaxim 73:2cecc1372acc 117 /// Set the family code byte.
IanBenzMaxim 73:2cecc1372acc 118 void setFamilyCode(uint8_t familyCode)
IanBenzMaxim 73:2cecc1372acc 119 {
IanBenzMaxim 73:2cecc1372acc 120 m_romId[familyCodeIdx] = familyCode;
IanBenzMaxim 73:2cecc1372acc 121 }
j3 17:b646b1e3970b 122
IanBenzMaxim 73:2cecc1372acc 123 /// Read the CRC8 byte.
IanBenzMaxim 73:2cecc1372acc 124 uint8_t crc8() const
IanBenzMaxim 73:2cecc1372acc 125 {
IanBenzMaxim 73:2cecc1372acc 126 return m_romId[crc8Idx];
IanBenzMaxim 73:2cecc1372acc 127 }
IanBenzMaxim 73:2cecc1372acc 128
IanBenzMaxim 73:2cecc1372acc 129 /// Set the CRC8 byte.
IanBenzMaxim 73:2cecc1372acc 130 void setCrc8(uint8_t crc8)
IanBenzMaxim 73:2cecc1372acc 131 {
IanBenzMaxim 73:2cecc1372acc 132 m_romId[crc8Idx] = crc8;
IanBenzMaxim 73:2cecc1372acc 133 }
IanBenzMaxim 73:2cecc1372acc 134
IanBenzMaxim 73:2cecc1372acc 135 /// Check if the CRC8 is valid for the ROM ID.
IanBenzMaxim 73:2cecc1372acc 136 /// @returns True if the CRC8 is valid.
IanBenzMaxim 73:2cecc1372acc 137 bool crc8Valid() const
IanBenzMaxim 73:2cecc1372acc 138 {
IanBenzMaxim 73:2cecc1372acc 139 return (calculateCRC8(m_romId, (byteLen - 1), 0x00) == crc8());
IanBenzMaxim 73:2cecc1372acc 140 }
IanBenzMaxim 73:2cecc1372acc 141
IanBenzMaxim 73:2cecc1372acc 142 /// Calculate and set the CRC8 for the ROM ID.
IanBenzMaxim 73:2cecc1372acc 143 void setValidCrc8()
IanBenzMaxim 73:2cecc1372acc 144 {
IanBenzMaxim 73:2cecc1372acc 145 setCrc8(calculateCRC8(m_romId, (byteLen - 1), 0x00));
IanBenzMaxim 73:2cecc1372acc 146 }
j3 17:b646b1e3970b 147
IanBenzMaxim 73:2cecc1372acc 148 /// Check if the ROM ID is valid (Family Code and CRC8 are both valid).
IanBenzMaxim 73:2cecc1372acc 149 /// @returns True if the ROM ID is valid.
IanBenzMaxim 73:2cecc1372acc 150 bool valid() const
IanBenzMaxim 73:2cecc1372acc 151 {
IanBenzMaxim 73:2cecc1372acc 152 return (crc8Valid() && (familyCode() != defaultByteVal));
IanBenzMaxim 73:2cecc1372acc 153 }
IanBenzMaxim 73:2cecc1372acc 154 };
IanBenzMaxim 73:2cecc1372acc 155 }
j3 17:b646b1e3970b 156
IanBenzMaxim 49:36954b62f503 157 #endif