Device interface library for multiple platforms including Mbed.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.
The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface
MaximInterfaceCore/I2CMaster.cpp@8:5ea891c7d1a1, 2019-09-16 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Sep 16 11:13:37 2019 -0500
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
- Child:
- 11:3f3bf6bf5e6c
Updated to version 2.0.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 7:9cd16581b578 | 1 | /******************************************************************************* |
IanBenzMaxim | 8:5ea891c7d1a1 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 7:9cd16581b578 | 3 | * |
IanBenzMaxim | 7:9cd16581b578 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 7:9cd16581b578 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 7:9cd16581b578 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 7:9cd16581b578 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 7:9cd16581b578 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 7:9cd16581b578 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 7:9cd16581b578 | 10 | * |
IanBenzMaxim | 7:9cd16581b578 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 7:9cd16581b578 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 7:9cd16581b578 | 13 | * |
IanBenzMaxim | 7:9cd16581b578 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 7:9cd16581b578 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 7:9cd16581b578 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 7:9cd16581b578 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 7:9cd16581b578 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 7:9cd16581b578 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 7:9cd16581b578 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 7:9cd16581b578 | 21 | * |
IanBenzMaxim | 7:9cd16581b578 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 7:9cd16581b578 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 7:9cd16581b578 | 25 | * |
IanBenzMaxim | 7:9cd16581b578 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 7:9cd16581b578 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 7:9cd16581b578 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 7:9cd16581b578 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 7:9cd16581b578 | 30 | * ownership rights. |
IanBenzMaxim | 7:9cd16581b578 | 31 | *******************************************************************************/ |
IanBenzMaxim | 7:9cd16581b578 | 32 | |
IanBenzMaxim | 7:9cd16581b578 | 33 | #include "Error.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 34 | #include "I2CMaster.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 35 | |
IanBenzMaxim | 7:9cd16581b578 | 36 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 37 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 38 | Result<void> I2CMaster::writeBlock(span<const uint_least8_t> data) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 39 | for (span<const uint_least8_t>::index_type i = 0; i < data.size(); ++i) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 40 | MaximInterfaceCore_TRY(writeByte(data[i])); |
IanBenzMaxim | 7:9cd16581b578 | 41 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 42 | return none; |
IanBenzMaxim | 7:9cd16581b578 | 43 | } |
IanBenzMaxim | 7:9cd16581b578 | 44 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 45 | Result<void> I2CMaster::writePacket(uint_least8_t address, |
IanBenzMaxim | 7:9cd16581b578 | 46 | span<const uint_least8_t> data, |
IanBenzMaxim | 8:5ea891c7d1a1 | 47 | DoStop doStop) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 48 | Result<void> result = start(address & 0xFE); |
IanBenzMaxim | 8:5ea891c7d1a1 | 49 | if (result) { |
IanBenzMaxim | 7:9cd16581b578 | 50 | result = writeBlock(data); |
IanBenzMaxim | 7:9cd16581b578 | 51 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 52 | if (doStop == Stop || (doStop == StopOnError && !result)) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 53 | const Result<void> stopResult = stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 54 | if (result) { |
IanBenzMaxim | 7:9cd16581b578 | 55 | result = stopResult; |
IanBenzMaxim | 7:9cd16581b578 | 56 | } |
IanBenzMaxim | 7:9cd16581b578 | 57 | } |
IanBenzMaxim | 7:9cd16581b578 | 58 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 59 | } |
IanBenzMaxim | 7:9cd16581b578 | 60 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 61 | Result<void> I2CMaster::readBlock(span<uint_least8_t> data, DoAck doAck) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 62 | for (span<uint_least8_t>::index_type i = 0; i < data.size(); ++i) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 63 | MaximInterfaceCore_TRY_VALUE( |
IanBenzMaxim | 8:5ea891c7d1a1 | 64 | data[i], readByte(i == (data.size() - 1) ? doAck : Ack)); |
IanBenzMaxim | 7:9cd16581b578 | 65 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 66 | return none; |
IanBenzMaxim | 7:9cd16581b578 | 67 | } |
IanBenzMaxim | 7:9cd16581b578 | 68 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 69 | Result<void> I2CMaster::readPacket(uint_least8_t address, |
IanBenzMaxim | 8:5ea891c7d1a1 | 70 | span<uint_least8_t> data, DoStop doStop) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 71 | Result<void> result = start(address | 0x01); |
IanBenzMaxim | 8:5ea891c7d1a1 | 72 | if (result) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 73 | result = readBlock(data, Nack); |
IanBenzMaxim | 7:9cd16581b578 | 74 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 75 | if (doStop == Stop || (doStop == StopOnError && !result)) { |
IanBenzMaxim | 8:5ea891c7d1a1 | 76 | const Result<void> stopResult = stop(); |
IanBenzMaxim | 8:5ea891c7d1a1 | 77 | if (result) { |
IanBenzMaxim | 7:9cd16581b578 | 78 | result = stopResult; |
IanBenzMaxim | 7:9cd16581b578 | 79 | } |
IanBenzMaxim | 7:9cd16581b578 | 80 | } |
IanBenzMaxim | 7:9cd16581b578 | 81 | return result; |
IanBenzMaxim | 7:9cd16581b578 | 82 | } |
IanBenzMaxim | 7:9cd16581b578 | 83 | |
IanBenzMaxim | 7:9cd16581b578 | 84 | const error_category & I2CMaster::errorCategory() { |
IanBenzMaxim | 7:9cd16581b578 | 85 | static class : public error_category { |
IanBenzMaxim | 7:9cd16581b578 | 86 | public: |
IanBenzMaxim | 7:9cd16581b578 | 87 | virtual const char * name() const { return "I2CMaster"; } |
IanBenzMaxim | 7:9cd16581b578 | 88 | |
IanBenzMaxim | 7:9cd16581b578 | 89 | virtual std::string message(int condition) const { |
IanBenzMaxim | 7:9cd16581b578 | 90 | switch (condition) { |
IanBenzMaxim | 7:9cd16581b578 | 91 | case NackError: |
IanBenzMaxim | 7:9cd16581b578 | 92 | return "Nack Error"; |
IanBenzMaxim | 7:9cd16581b578 | 93 | } |
IanBenzMaxim | 8:5ea891c7d1a1 | 94 | return defaultErrorMessage(condition); |
IanBenzMaxim | 7:9cd16581b578 | 95 | } |
IanBenzMaxim | 7:9cd16581b578 | 96 | } instance; |
IanBenzMaxim | 7:9cd16581b578 | 97 | return instance; |
IanBenzMaxim | 7:9cd16581b578 | 98 | } |
IanBenzMaxim | 7:9cd16581b578 | 99 | |
IanBenzMaxim | 7:9cd16581b578 | 100 | } // namespace MaximInterfaceCore |