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

Committer:
IanBenzMaxim
Date:
Mon Sep 16 11:13:37 2019 -0500
Revision:
8:5ea891c7d1a1
Parent:
7:9cd16581b578
Updated to version 2.0.

Who changed what in which revision?

UserRevisionLine numberNew 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 8:5ea891c7d1a1 33 #ifndef MaximInterfaceCore_Uart_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceCore_Uart_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <stdint.h>
IanBenzMaxim 7:9cd16581b578 37 #include "Config.hpp"
IanBenzMaxim 8:5ea891c7d1a1 38 #include "Result.hpp"
IanBenzMaxim 7:9cd16581b578 39 #include "span.hpp"
IanBenzMaxim 7:9cd16581b578 40 #include "system_error.hpp"
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 7:9cd16581b578 44 /// Universal asynchronous receiver-transmitter interface.
IanBenzMaxim 7:9cd16581b578 45 class Uart {
IanBenzMaxim 7:9cd16581b578 46 public:
IanBenzMaxim 7:9cd16581b578 47 enum ErrorValue {
IanBenzMaxim 7:9cd16581b578 48 TimeoutError = 1, ///< Read operation aborted due to timeout.
IanBenzMaxim 7:9cd16581b578 49 OverrunError ///< Received data lost due to read buffer overrun.
IanBenzMaxim 7:9cd16581b578 50 };
IanBenzMaxim 7:9cd16581b578 51
IanBenzMaxim 7:9cd16581b578 52 virtual ~Uart() {}
IanBenzMaxim 7:9cd16581b578 53
IanBenzMaxim 7:9cd16581b578 54 /// Set the baud rate of the port in Hz.
IanBenzMaxim 8:5ea891c7d1a1 55 virtual Result<void> setBaudRate(int_least32_t baudRate) = 0;
IanBenzMaxim 7:9cd16581b578 56
IanBenzMaxim 7:9cd16581b578 57 /// Generate a break condition on the port for a small amount of time.
IanBenzMaxim 8:5ea891c7d1a1 58 virtual Result<void> sendBreak() = 0;
IanBenzMaxim 7:9cd16581b578 59
IanBenzMaxim 7:9cd16581b578 60 /// Clear all received data that was buffered.
IanBenzMaxim 8:5ea891c7d1a1 61 virtual Result<void> clearReadBuffer() = 0;
IanBenzMaxim 7:9cd16581b578 62
IanBenzMaxim 7:9cd16581b578 63 /// Writes a byte of data to the port.
IanBenzMaxim 8:5ea891c7d1a1 64 virtual Result<void> writeByte(uint_least8_t data) = 0;
IanBenzMaxim 7:9cd16581b578 65
IanBenzMaxim 7:9cd16581b578 66 /// Writes a block of data to the port.
IanBenzMaxim 8:5ea891c7d1a1 67 MaximInterfaceCore_EXPORT virtual Result<void>
IanBenzMaxim 7:9cd16581b578 68 writeBlock(span<const uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 69
IanBenzMaxim 7:9cd16581b578 70 /// @brief
IanBenzMaxim 7:9cd16581b578 71 /// Reads a byte of data from the port. Block until data is received or a
IanBenzMaxim 7:9cd16581b578 72 /// timeout is reached.
IanBenzMaxim 8:5ea891c7d1a1 73 /// @returns Data read from the port if successful.
IanBenzMaxim 8:5ea891c7d1a1 74 virtual Result<uint_least8_t> readByte() = 0;
IanBenzMaxim 7:9cd16581b578 75
IanBenzMaxim 7:9cd16581b578 76 /// @brief
IanBenzMaxim 7:9cd16581b578 77 /// Read a block of data from the port. Block until data is received or a
IanBenzMaxim 7:9cd16581b578 78 /// timeout is reached.
IanBenzMaxim 7:9cd16581b578 79 /// @param[out] data Data read from the port if successful.
IanBenzMaxim 8:5ea891c7d1a1 80 MaximInterfaceCore_EXPORT virtual Result<void>
IanBenzMaxim 7:9cd16581b578 81 readBlock(span<uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 82
IanBenzMaxim 7:9cd16581b578 83 MaximInterfaceCore_EXPORT static const error_category & errorCategory();
IanBenzMaxim 7:9cd16581b578 84 };
IanBenzMaxim 7:9cd16581b578 85
IanBenzMaxim 8:5ea891c7d1a1 86 template <> struct is_error_code_enum<Uart::ErrorValue> : true_type {};
IanBenzMaxim 8:5ea891c7d1a1 87
IanBenzMaxim 7:9cd16581b578 88 inline error_code make_error_code(Uart::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 89 return error_code(e, Uart::errorCategory());
IanBenzMaxim 7:9cd16581b578 90 }
IanBenzMaxim 7:9cd16581b578 91
IanBenzMaxim 7:9cd16581b578 92 inline error_condition make_error_condition(Uart::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 93 return error_condition(e, Uart::errorCategory());
IanBenzMaxim 7:9cd16581b578 94 }
IanBenzMaxim 7:9cd16581b578 95
IanBenzMaxim 7:9cd16581b578 96 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 97
IanBenzMaxim 7:9cd16581b578 98 #endif