Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Links/LoggingI2CMaster.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 <utility> |
IanBenzMaxim | 0:f77ad7f72d04 | 34 | #include <MaximInterface/Utilities/HexConversions.hpp> |
IanBenzMaxim | 0:f77ad7f72d04 | 35 | #include "LoggingI2CMaster.hpp" |
IanBenzMaxim | 0:f77ad7f72d04 | 36 | |
IanBenzMaxim | 0:f77ad7f72d04 | 37 | using std::string; |
IanBenzMaxim | 0:f77ad7f72d04 | 38 | |
IanBenzMaxim | 0:f77ad7f72d04 | 39 | namespace MaximInterface { |
IanBenzMaxim | 0:f77ad7f72d04 | 40 | |
IanBenzMaxim | 0:f77ad7f72d04 | 41 | static const char startString[] = "S "; |
IanBenzMaxim | 0:f77ad7f72d04 | 42 | static const char stopString[] = "P"; |
IanBenzMaxim | 0:f77ad7f72d04 | 43 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 44 | static string formatDataString(span<const uint_least8_t> data, bool read) { |
IanBenzMaxim | 0:f77ad7f72d04 | 45 | string dataBuilder; |
IanBenzMaxim | 6:a8c83a2e6fa4 | 46 | for (span<const uint_least8_t>::index_type i = 0; i < data.size(); ++i) { |
IanBenzMaxim | 0:f77ad7f72d04 | 47 | if (read) { |
IanBenzMaxim | 0:f77ad7f72d04 | 48 | dataBuilder.append(1, '['); |
IanBenzMaxim | 0:f77ad7f72d04 | 49 | } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 50 | dataBuilder.append(byteArrayToHexString(data.subspan(i, 1))); |
IanBenzMaxim | 0:f77ad7f72d04 | 51 | if (read) { |
IanBenzMaxim | 0:f77ad7f72d04 | 52 | dataBuilder.append(1, ']'); |
IanBenzMaxim | 0:f77ad7f72d04 | 53 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 54 | dataBuilder.append(1, ' '); |
IanBenzMaxim | 0:f77ad7f72d04 | 55 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 56 | return dataBuilder; |
IanBenzMaxim | 0:f77ad7f72d04 | 57 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 58 | |
IanBenzMaxim | 0:f77ad7f72d04 | 59 | void LoggingI2CMaster::tryWriteMessage() { |
IanBenzMaxim | 0:f77ad7f72d04 | 60 | if (writeMessage) { |
IanBenzMaxim | 0:f77ad7f72d04 | 61 | writeMessage(messageBuilder); |
IanBenzMaxim | 0:f77ad7f72d04 | 62 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 63 | messageBuilder.clear(); |
IanBenzMaxim | 0:f77ad7f72d04 | 64 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 65 | |
IanBenzMaxim | 0:f77ad7f72d04 | 66 | error_code LoggingI2CMaster::start(uint_least8_t address) { |
IanBenzMaxim | 0:f77ad7f72d04 | 67 | messageBuilder.append(startString); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 68 | messageBuilder.append(formatDataString(make_span(&address, 1), false)); |
IanBenzMaxim | 0:f77ad7f72d04 | 69 | return I2CMasterDecorator::start(address); |
IanBenzMaxim | 0:f77ad7f72d04 | 70 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 71 | |
IanBenzMaxim | 0:f77ad7f72d04 | 72 | error_code LoggingI2CMaster::stop() { |
IanBenzMaxim | 0:f77ad7f72d04 | 73 | messageBuilder.append(stopString); |
IanBenzMaxim | 0:f77ad7f72d04 | 74 | tryWriteMessage(); |
IanBenzMaxim | 0:f77ad7f72d04 | 75 | return I2CMasterDecorator::stop(); |
IanBenzMaxim | 0:f77ad7f72d04 | 76 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 77 | |
IanBenzMaxim | 0:f77ad7f72d04 | 78 | error_code LoggingI2CMaster::writeByte(uint_least8_t data) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 79 | messageBuilder.append(formatDataString(make_span(&data, 1), false)); |
IanBenzMaxim | 0:f77ad7f72d04 | 80 | return I2CMasterDecorator::writeByte(data); |
IanBenzMaxim | 0:f77ad7f72d04 | 81 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 82 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 83 | error_code LoggingI2CMaster::writeBlock(span<const uint_least8_t> data) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 84 | messageBuilder.append(formatDataString(data, false)); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 85 | return I2CMasterDecorator::writeBlock(data); |
IanBenzMaxim | 0:f77ad7f72d04 | 86 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 87 | |
IanBenzMaxim | 3:f818ea5172ed | 88 | error_code LoggingI2CMaster::writePacketImpl(uint_least8_t address, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 89 | span<const uint_least8_t> data, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 90 | bool sendStop) { |
IanBenzMaxim | 0:f77ad7f72d04 | 91 | messageBuilder.append(startString); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 92 | messageBuilder.append(formatDataString(make_span(&address, 1), false)); |
IanBenzMaxim | 0:f77ad7f72d04 | 93 | error_code result = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 94 | I2CMasterDecorator::writePacketImpl(address, data, sendStop); |
IanBenzMaxim | 0:f77ad7f72d04 | 95 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 96 | messageBuilder.append(formatDataString(data, false)); |
IanBenzMaxim | 0:f77ad7f72d04 | 97 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 98 | if (sendStop || result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 99 | messageBuilder.append(stopString); |
IanBenzMaxim | 0:f77ad7f72d04 | 100 | tryWriteMessage(); |
IanBenzMaxim | 0:f77ad7f72d04 | 101 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 102 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 103 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 104 | |
IanBenzMaxim | 0:f77ad7f72d04 | 105 | error_code LoggingI2CMaster::readByte(AckStatus status, uint_least8_t & data) { |
IanBenzMaxim | 0:f77ad7f72d04 | 106 | error_code result = I2CMasterDecorator::readByte(status, data); |
IanBenzMaxim | 0:f77ad7f72d04 | 107 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 108 | messageBuilder.append(formatDataString(make_span(&data, 1), true)); |
IanBenzMaxim | 0:f77ad7f72d04 | 109 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 110 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 111 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 112 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 113 | error_code LoggingI2CMaster::readBlock(AckStatus status, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 114 | span<uint_least8_t> data) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 115 | error_code result = I2CMasterDecorator::readBlock(status, data); |
IanBenzMaxim | 0:f77ad7f72d04 | 116 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 117 | messageBuilder.append(formatDataString(data, true)); |
IanBenzMaxim | 0:f77ad7f72d04 | 118 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 119 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 120 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 121 | |
IanBenzMaxim | 3:f818ea5172ed | 122 | error_code LoggingI2CMaster::readPacketImpl(uint_least8_t address, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 123 | span<uint_least8_t> data, |
IanBenzMaxim | 6:a8c83a2e6fa4 | 124 | bool sendStop) { |
IanBenzMaxim | 0:f77ad7f72d04 | 125 | messageBuilder.append(startString); |
IanBenzMaxim | 6:a8c83a2e6fa4 | 126 | messageBuilder.append(formatDataString(make_span(&address, 1), false)); |
IanBenzMaxim | 0:f77ad7f72d04 | 127 | error_code result = |
IanBenzMaxim | 6:a8c83a2e6fa4 | 128 | I2CMasterDecorator::readPacketImpl(address, data, sendStop); |
IanBenzMaxim | 0:f77ad7f72d04 | 129 | if (!result) { |
IanBenzMaxim | 6:a8c83a2e6fa4 | 130 | messageBuilder.append(formatDataString(data, true)); |
IanBenzMaxim | 0:f77ad7f72d04 | 131 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 132 | if (sendStop || result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 133 | messageBuilder.append(stopString); |
IanBenzMaxim | 0:f77ad7f72d04 | 134 | tryWriteMessage(); |
IanBenzMaxim | 0:f77ad7f72d04 | 135 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 136 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 137 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 138 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 139 | } // namespace MaximInterface |