Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
RomId.hpp@21:00c94aeb533e, 2016-03-21 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Mar 21 14:12:28 2016 -0500
- Revision:
- 21:00c94aeb533e
- Parent:
- OneWire_Masters/RomId.hpp@17:b646b1e3970b
- Child:
- 24:8942d8478d68
Added class for DS2465. Added a ReadBytePower operation to OneWireMaster since this is required by the authenticators including the DS28E15. Tweaked member data of OneWireMaster. Added path to header file includes to reduce compiler setup required.
Who changed what in which revision?
User | Revision | Line number | New 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> |
j3 | 17:b646b1e3970b | 38 | #include <cstring> |
j3 | 17:b646b1e3970b | 39 | |
j3 | 17:b646b1e3970b | 40 | class RomId |
j3 | 17:b646b1e3970b | 41 | { |
j3 | 17:b646b1e3970b | 42 | public: |
j3 | 17:b646b1e3970b | 43 | static const std::size_t byteLen = 8; |
j3 | 17:b646b1e3970b | 44 | typedef std::uint8_t byteBuffer[byteLen]; |
j3 | 17:b646b1e3970b | 45 | |
IanBenzMaxim | 21:00c94aeb533e | 46 | static std::uint8_t calculateCRC8(std::uint8_t crc8, std::uint8_t data); |
IanBenzMaxim | 21:00c94aeb533e | 47 | static std::uint8_t calculateCRC8(const std::uint8_t * data, std::size_t data_len, std::uint8_t crc = 0); |
j3 | 17:b646b1e3970b | 48 | |
j3 | 17:b646b1e3970b | 49 | private: |
j3 | 17:b646b1e3970b | 50 | static const std::size_t familyCodeIdx = 0; |
j3 | 17:b646b1e3970b | 51 | static const std::size_t crc8Idx = 7; |
j3 | 17:b646b1e3970b | 52 | static const std::uint8_t defaultByteVal = 0x00; |
j3 | 17:b646b1e3970b | 53 | |
j3 | 17:b646b1e3970b | 54 | byteBuffer m_romId; |
j3 | 17:b646b1e3970b | 55 | |
j3 | 17:b646b1e3970b | 56 | public: |
j3 | 17:b646b1e3970b | 57 | const RomId & operator=(const RomId & rhs) { |
j3 | 17:b646b1e3970b | 58 | std::memcpy(this->m_romId, rhs.m_romId, byteLen); |
j3 | 17:b646b1e3970b | 59 | return rhs; |
j3 | 17:b646b1e3970b | 60 | } |
j3 | 17:b646b1e3970b | 61 | bool operator==(const RomId & rhs) const { |
j3 | 17:b646b1e3970b | 62 | return (std::memcmp(this->m_romId, rhs.m_romId, byteLen) == 0); |
j3 | 17:b646b1e3970b | 63 | } |
j3 | 17:b646b1e3970b | 64 | bool operator!=(const RomId & rhs) const { |
j3 | 17:b646b1e3970b | 65 | return !operator==(rhs); |
j3 | 17:b646b1e3970b | 66 | } |
j3 | 17:b646b1e3970b | 67 | operator byteBuffer &() { |
j3 | 17:b646b1e3970b | 68 | return m_romId; // Conversion to array reference |
j3 | 17:b646b1e3970b | 69 | } |
j3 | 17:b646b1e3970b | 70 | operator const byteBuffer &() const { |
j3 | 17:b646b1e3970b | 71 | return m_romId; // Conversion to const array reference |
j3 | 17:b646b1e3970b | 72 | } |
j3 | 17:b646b1e3970b | 73 | |
j3 | 17:b646b1e3970b | 74 | void reset() { |
j3 | 17:b646b1e3970b | 75 | std::memset(m_romId, defaultByteVal, byteLen); |
j3 | 17:b646b1e3970b | 76 | } |
j3 | 17:b646b1e3970b | 77 | |
j3 | 17:b646b1e3970b | 78 | RomId() { |
j3 | 17:b646b1e3970b | 79 | reset(); |
j3 | 17:b646b1e3970b | 80 | } |
j3 | 17:b646b1e3970b | 81 | RomId(const RomId & romId) { |
j3 | 17:b646b1e3970b | 82 | operator=(romId); |
j3 | 17:b646b1e3970b | 83 | } |
j3 | 17:b646b1e3970b | 84 | RomId(const byteBuffer & romIdBytes) { |
j3 | 17:b646b1e3970b | 85 | std::memcpy(m_romId, romIdBytes, byteLen); |
j3 | 17:b646b1e3970b | 86 | } |
j3 | 17:b646b1e3970b | 87 | |
j3 | 17:b646b1e3970b | 88 | std::uint8_t familyCode() const { |
j3 | 17:b646b1e3970b | 89 | return m_romId[familyCodeIdx]; |
j3 | 17:b646b1e3970b | 90 | } |
j3 | 17:b646b1e3970b | 91 | void setFamilyCode(std::uint8_t familyCode) { |
j3 | 17:b646b1e3970b | 92 | m_romId[familyCodeIdx] = familyCode; |
j3 | 17:b646b1e3970b | 93 | } |
j3 | 17:b646b1e3970b | 94 | |
j3 | 17:b646b1e3970b | 95 | std::uint8_t crc8() const { |
j3 | 17:b646b1e3970b | 96 | return m_romId[crc8Idx]; |
j3 | 17:b646b1e3970b | 97 | } |
j3 | 17:b646b1e3970b | 98 | void setCrc8(std::uint8_t crc8) { |
j3 | 17:b646b1e3970b | 99 | m_romId[crc8Idx] = crc8; |
j3 | 17:b646b1e3970b | 100 | } |
j3 | 17:b646b1e3970b | 101 | bool crc8Valid() const { |
j3 | 17:b646b1e3970b | 102 | return (calculateCRC8(m_romId, (byteLen - 1), 0x00) == crc8()); |
j3 | 17:b646b1e3970b | 103 | } |
j3 | 17:b646b1e3970b | 104 | void setValidCrc8() { |
j3 | 17:b646b1e3970b | 105 | setCrc8(calculateCRC8(m_romId, (byteLen - 1), 0x00)); |
j3 | 17:b646b1e3970b | 106 | } |
j3 | 17:b646b1e3970b | 107 | |
j3 | 17:b646b1e3970b | 108 | bool valid() const { |
j3 | 17:b646b1e3970b | 109 | return (crc8Valid() && (familyCode() != defaultByteVal)); |
j3 | 17:b646b1e3970b | 110 | } |
j3 | 17:b646b1e3970b | 111 | }; |
j3 | 17:b646b1e3970b | 112 | |
j3 | 17:b646b1e3970b | 113 | #endif |