Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Uart.hpp Source File

Uart.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceCore_Uart_hpp
00034 #define MaximInterfaceCore_Uart_hpp
00035 
00036 #include <stdint.h>
00037 #include "Config.hpp"
00038 #include "Result.hpp"
00039 #include "span.hpp"
00040 #include "system_error.hpp"
00041 
00042 namespace MaximInterfaceCore {
00043 
00044 /// Universal asynchronous receiver-transmitter interface.
00045 class Uart {
00046 public:
00047   enum ErrorValue  {
00048     TimeoutError = 1, ///< Read operation aborted due to timeout.
00049     OverrunError      ///< Received data lost due to read buffer overrun.
00050   };
00051 
00052   virtual ~Uart() {}
00053 
00054   /// Set the baud rate of the port in Hz.
00055   virtual Result<void> setBaudRate(int_least32_t baudRate) = 0;
00056 
00057   /// Generate a break condition on the port for a small amount of time.
00058   virtual Result<void> sendBreak() = 0;
00059 
00060   /// Clear all received data that was buffered.
00061   virtual Result<void> clearReadBuffer() = 0;
00062 
00063   /// Writes a byte of data to the port.
00064   virtual Result<void> writeByte(uint_least8_t data) = 0;
00065 
00066   /// Writes a block of data to the port.
00067   MaximInterfaceCore_EXPORT virtual Result<void>
00068   writeBlock(span<const uint_least8_t> data);
00069 
00070   /// @brief
00071   /// Reads a byte of data from the port. Block until data is received or a
00072   /// timeout is reached.
00073   /// @returns Data read from the port if successful.
00074   virtual Result<uint_least8_t> readByte() = 0;
00075 
00076   /// @brief
00077   /// Read a block of data from the port. Block until data is received or a
00078   /// timeout is reached.
00079   /// @param[out] data Data read from the port if successful.
00080   MaximInterfaceCore_EXPORT virtual Result<void>
00081   readBlock(span<uint_least8_t> data);
00082 
00083   MaximInterfaceCore_EXPORT static const error_category & errorCategory();
00084 };
00085 
00086 template <> struct is_error_code_enum<Uart::ErrorValue> : true_type {};
00087 
00088 inline error_code make_error_code(Uart::ErrorValue  e) {
00089   return error_code(e, Uart::errorCategory());
00090 }
00091 
00092 inline error_condition make_error_condition(Uart::ErrorValue  e) {
00093   return error_condition(e, Uart::errorCategory());
00094 }
00095 
00096 } // namespace MaximInterfaceCore
00097 
00098 #endif