Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UnbufferedSerial.h Source File

UnbufferedSerial.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_UNBUFFERED_SERIAL_H
00018 #define MBED_UNBUFFERED_SERIAL_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
00023 
00024 #include <cstdarg>
00025 
00026 #include "drivers/SerialBase.h"
00027 #include "platform/FileHandle.h"
00028 #include "platform/mbed_toolchain.h"
00029 #include "platform/NonCopyable.h"
00030 
00031 
00032 namespace mbed {
00033 
00034 /**
00035  * \defgroup drivers_UnbufferedSerial UnbufferedSerial class
00036  * \ingroup drivers-public-api-uart
00037  * @{
00038  */
00039 
00040 /**
00041  * Class implementation for unbuffered I/O for an interrupt driven application
00042  * or one that needs to have more control.
00043  */
00044 class UnbufferedSerial:
00045     private SerialBase,
00046     public FileHandle,
00047     private NonCopyable<UnbufferedSerial> {
00048 public:
00049     /**
00050      * Create a serial port instance connected to the specified transmit and
00051      * receive pins, with the specified baud rate.
00052      *
00053      *  @param tx Transmit pin
00054      *  @param rx Receive pin
00055      *  @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
00056      *
00057      *  @note
00058      *    Either tx or rx may be specified as NC if unused
00059      */
00060     UnbufferedSerial(
00061         PinName tx,
00062         PinName rx,
00063         int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
00064     );
00065 
00066     /** Create a UnbufferedSerial port, connected to the specified transmit and
00067      *  receive pins, with a particular baud rate.
00068      *  @param static_pinmap reference to structure which holds static pinmap
00069      *  @param baud The baud rate of the serial port (optional, defaults to
00070      *              MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
00071      */
00072     UnbufferedSerial(
00073         const serial_pinmap_t &static_pinmap,
00074         int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
00075     );
00076 
00077     /** Write the contents of a buffer to a file
00078      *
00079      * Blocks until all data is written
00080      *
00081      *  @param buffer   The buffer to write from
00082      *  @param size     The number of bytes to write
00083      *  @return         The number of bytes written
00084      */
00085     virtual ssize_t write(const void *buffer, size_t size);
00086 
00087     /** Read the contents of a file into a buffer
00088      *
00089      *  Blocks and reads exactly one character
00090      *
00091      *  @param buffer   The buffer to read in to
00092      *  @param size     The number of bytes to read
00093      *  @return         The number of bytes read
00094      */
00095     virtual ssize_t read(void *buffer, size_t size);
00096 
00097     /** Move the file position to a given offset from from a given location
00098      *
00099      * Not valid for a device type FileHandle like UnbufferedSerial.
00100      * In case of UnbufferedSerial, returns ESPIPE
00101      *
00102      *  @param offset   The offset from whence to move to
00103      *  @param whence   The start of where to seek
00104      *      SEEK_SET to start from beginning of file,
00105      *      SEEK_CUR to start from current position in file,
00106      *      SEEK_END to start from end of file
00107      *  @return         The new offset of the file, negative error code on failure
00108      */
00109     virtual off_t seek(off_t offset, int whence = SEEK_SET)
00110     {
00111         return -ESPIPE;
00112     }
00113 
00114     /** Get the size of the file
00115      *
00116      *  @return         Size of the file in bytes
00117      */
00118     virtual off_t size()
00119     {
00120         return -EINVAL;
00121     }
00122 
00123     /** Check if the file in an interactive terminal device
00124      *
00125      *  @return         True if the file is a terminal
00126      *  @return         False if the file is not a terminal
00127      *  @return         Negative error code on failure
00128      */
00129     virtual int isatty()
00130     {
00131         return true;
00132     }
00133 
00134     /** Close a file
00135      *
00136      *  @return         0 on success, negative error code on failure
00137      */
00138     virtual int close()
00139     {
00140         return 0;
00141     }
00142 
00143 
00144     /** Check for poll event flags
00145      * Check the events listed in events to see if data can be read or written
00146      * without blocking.
00147      * Call is nonblocking - returns state of events.
00148      *
00149      * @param events        bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
00150      *
00151      * @returns             bitmask of poll events that have occurred.
00152      */
00153     virtual short poll(short events) const;
00154 
00155 #if DEVICE_SERIAL_FC
00156     // For now use the base enum - but in future we may have extra options
00157     // such as XON/XOFF or manual GPIO RTSCTS.
00158     using SerialBase::Flow;
00159     // In C++11, we wouldn't need to also have using directives for each value
00160     using SerialBase::Disabled;
00161     using SerialBase::RTS;
00162     using SerialBase::CTS;
00163     using SerialBase::RTSCTS;
00164 
00165     /** Set the flow control type on the serial port
00166      *
00167      *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
00168      *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
00169      *  @param flow2 the second flow control pin (CTS for RTSCTS)
00170      */
00171     void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
00172 #endif // DEVICE_SERIAL_FC
00173 };
00174 
00175 } // namespace mbed
00176 
00177 #endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)
00178 
00179 #endif // MBED_UNBUFFERED_SERIAL_H