Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Links/OneWireMaster.cpp@10:de4b8812877d, 2020-01-06 (annotated)
- Committer:
- reARMnimator
- Date:
- Mon Jan 06 15:54:55 2020 +0000
- Revision:
- 10:de4b8812877d
- Parent:
- 6:a8c83a2e6fa4
Fixed inappropriate include path.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 0:f77ad7f72d04 | 1 | /******************************************************************************* |
IanBenzMaxim | 0:f77ad7f72d04 | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 0:f77ad7f72d04 | 3 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 0:f77ad7f72d04 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 0:f77ad7f72d04 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 0:f77ad7f72d04 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 0:f77ad7f72d04 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 0:f77ad7f72d04 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 0:f77ad7f72d04 | 10 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 0:f77ad7f72d04 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 0:f77ad7f72d04 | 13 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 0:f77ad7f72d04 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 0:f77ad7f72d04 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 0:f77ad7f72d04 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 0:f77ad7f72d04 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 0:f77ad7f72d04 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 0:f77ad7f72d04 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 0:f77ad7f72d04 | 21 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 0:f77ad7f72d04 | 25 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 0:f77ad7f72d04 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 0:f77ad7f72d04 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 0:f77ad7f72d04 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 0:f77ad7f72d04 | 30 | * ownership rights. |
IanBenzMaxim | 0:f77ad7f72d04 | 31 | *******************************************************************************/ |
IanBenzMaxim | 0:f77ad7f72d04 | 32 | |
IanBenzMaxim | 0:f77ad7f72d04 | 33 | #include <MaximInterface/Utilities/Error.hpp> |
IanBenzMaxim | 0:f77ad7f72d04 | 34 | #include "OneWireMaster.hpp" |
IanBenzMaxim | 0:f77ad7f72d04 | 35 | |
IanBenzMaxim | 0:f77ad7f72d04 | 36 | namespace MaximInterface { |
IanBenzMaxim | 0:f77ad7f72d04 | 37 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 38 | static const int maxBitNum = 7; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 39 | |
IanBenzMaxim | 0:f77ad7f72d04 | 40 | error_code OneWireMaster::writeByteSetLevel(uint_least8_t sendByte, |
IanBenzMaxim | 0:f77ad7f72d04 | 41 | Level afterLevel) { |
IanBenzMaxim | 0:f77ad7f72d04 | 42 | error_code result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 43 | for (int bitNum = 0; (bitNum <= maxBitNum) && !result; ++bitNum) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 44 | result = writeBitSetLevel(((sendByte >> bitNum) & 1) == 1, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 45 | (bitNum == maxBitNum) ? afterLevel : NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 46 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 47 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 48 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 49 | |
IanBenzMaxim | 0:f77ad7f72d04 | 50 | error_code OneWireMaster::readByteSetLevel(uint_least8_t & recvByte, |
IanBenzMaxim | 0:f77ad7f72d04 | 51 | Level afterLevel) { |
IanBenzMaxim | 0:f77ad7f72d04 | 52 | recvByte = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 53 | error_code result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 54 | for (int bitNum = 0; bitNum <= maxBitNum; ++bitNum) { |
IanBenzMaxim | 0:f77ad7f72d04 | 55 | bool recvBit; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 56 | result = readBitSetLevel(recvBit, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 57 | (bitNum == maxBitNum) ? afterLevel : NormalLevel); |
IanBenzMaxim | 0:f77ad7f72d04 | 58 | if (result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 59 | break; |
IanBenzMaxim | 0:f77ad7f72d04 | 60 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 61 | if (recvBit) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 62 | recvByte |= (1 << bitNum); |
IanBenzMaxim | 0:f77ad7f72d04 | 63 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 64 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 65 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 66 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 67 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 68 | error_code OneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) { |
IanBenzMaxim | 0:f77ad7f72d04 | 69 | error_code result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 70 | for (span<const uint_least8_t>::index_type i = 0; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 71 | i < sendBuf.size() && !result; ++i) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 72 | result = writeByte(sendBuf[i]); |
IanBenzMaxim | 0:f77ad7f72d04 | 73 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 74 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 75 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 76 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 77 | error_code OneWireMaster::readBlock(span<uint_least8_t> recvBuf) { |
IanBenzMaxim | 0:f77ad7f72d04 | 78 | error_code result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 79 | for (span<uint_least8_t>::index_type i = 0; i < recvBuf.size() && !result; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 80 | ++i) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 81 | result = readByte(recvBuf[i]); |
IanBenzMaxim | 0:f77ad7f72d04 | 82 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 83 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 84 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 85 | |
IanBenzMaxim | 0:f77ad7f72d04 | 86 | error_code OneWireMaster::triplet(TripletData & data) { |
IanBenzMaxim | 0:f77ad7f72d04 | 87 | error_code result = readBit(data.readBit); |
IanBenzMaxim | 0:f77ad7f72d04 | 88 | if (!result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 89 | result = readBit(data.readBitComplement); |
IanBenzMaxim | 0:f77ad7f72d04 | 90 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 91 | if (!result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 92 | if (data.readBit) { |
IanBenzMaxim | 0:f77ad7f72d04 | 93 | data.writeBit = 1; |
IanBenzMaxim | 0:f77ad7f72d04 | 94 | } else if (data.readBitComplement) { |
IanBenzMaxim | 0:f77ad7f72d04 | 95 | data.writeBit = 0; |
IanBenzMaxim | 0:f77ad7f72d04 | 96 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 97 | // else: use data.writeBit parameter |
IanBenzMaxim | 0:f77ad7f72d04 | 98 | result = writeBit(data.writeBit); |
IanBenzMaxim | 0:f77ad7f72d04 | 99 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 100 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 101 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 102 | |
IanBenzMaxim | 0:f77ad7f72d04 | 103 | const error_category & OneWireMaster::errorCategory() { |
IanBenzMaxim | 0:f77ad7f72d04 | 104 | static class : public error_category { |
IanBenzMaxim | 0:f77ad7f72d04 | 105 | public: |
IanBenzMaxim | 0:f77ad7f72d04 | 106 | virtual const char * name() const { return "OneWireMaster"; } |
IanBenzMaxim | 0:f77ad7f72d04 | 107 | |
IanBenzMaxim | 0:f77ad7f72d04 | 108 | virtual std::string message(int condition) const { |
IanBenzMaxim | 0:f77ad7f72d04 | 109 | switch (condition) { |
IanBenzMaxim | 0:f77ad7f72d04 | 110 | case NoSlaveError: |
IanBenzMaxim | 0:f77ad7f72d04 | 111 | return "No Slave Error"; |
IanBenzMaxim | 0:f77ad7f72d04 | 112 | |
IanBenzMaxim | 0:f77ad7f72d04 | 113 | case ShortDetectedError: |
IanBenzMaxim | 0:f77ad7f72d04 | 114 | return "Short Detected Error"; |
IanBenzMaxim | 0:f77ad7f72d04 | 115 | |
IanBenzMaxim | 0:f77ad7f72d04 | 116 | case InvalidSpeedError: |
IanBenzMaxim | 0:f77ad7f72d04 | 117 | return "Invalid Speed Error"; |
IanBenzMaxim | 0:f77ad7f72d04 | 118 | |
IanBenzMaxim | 0:f77ad7f72d04 | 119 | case InvalidLevelError: |
IanBenzMaxim | 0:f77ad7f72d04 | 120 | return "Invalid Level Error"; |
IanBenzMaxim | 0:f77ad7f72d04 | 121 | |
IanBenzMaxim | 0:f77ad7f72d04 | 122 | default: |
IanBenzMaxim | 0:f77ad7f72d04 | 123 | return defaultErrorMessage(condition); |
IanBenzMaxim | 0:f77ad7f72d04 | 124 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 125 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 126 | } instance; |
IanBenzMaxim | 0:f77ad7f72d04 | 127 | return instance; |
IanBenzMaxim | 0:f77ad7f72d04 | 128 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 129 | |
IanBenzMaxim | 0:f77ad7f72d04 | 130 | } // namespace MaximInterface |