the do / gr-peach-opencv-project

Fork of gr-peach-opencv-project by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UARTSerial.h Source File

UARTSerial.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef MBED_UARTSERIAL_H
00018 #define MBED_UARTSERIAL_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_SERIAL
00023 
00024 #include "FileHandle.h"
00025 #include "SerialBase.h"
00026 #include "InterruptIn.h"
00027 #include "PlatformMutex.h"
00028 #include "serial_api.h"
00029 #include "CircularBuffer.h"
00030 
00031 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE
00032 #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE  256
00033 #endif
00034 
00035 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE
00036 #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE  256
00037 #endif
00038 
00039 namespace mbed {
00040 
00041 class UARTSerial : private SerialBase, public FileHandle {
00042 
00043 public:
00044 
00045     /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
00046      *  @param tx Transmit pin
00047      *  @param rx Receive pin
00048      *  @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
00049      */
00050     UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
00051     virtual ~UARTSerial();
00052 
00053     /** Equivalent to POSIX poll(). Derived from FileHandle.
00054      *  Provides a mechanism to multiplex input/output over a set of file handles.
00055      */
00056     virtual short poll(short events) const;
00057 
00058     /** Write the contents of a buffer to a file
00059      *
00060      *  @param buffer   The buffer to write from
00061      *  @param size     The number of bytes to write
00062      *  @return         The number of bytes written, negative error on failure
00063      */
00064     virtual ssize_t write(const void* buffer, size_t length);
00065 
00066     /** Read the contents of a file into a buffer
00067      *
00068      *  Follows POSIX semantics:
00069      *
00070      *  * if no data is available, and non-blocking set return -EAGAIN
00071      *  * if no data is available, and blocking set, wait until data is available
00072      *  * If any data is available, call returns immediately
00073      *
00074      *  @param buffer   The buffer to read in to
00075      *  @param size     The number of bytes to read
00076      *  @return         The number of bytes read, 0 at end of file, negative error on failure
00077      */
00078     virtual ssize_t read(void* buffer, size_t length);
00079 
00080     /** Acquire mutex */
00081     virtual void lock(void);
00082 
00083     /** Release mutex */
00084     virtual void unlock(void);
00085 
00086     /** Close a file
00087      *
00088      *  @return         0 on success, negative error code on failure
00089      */
00090     virtual int close();
00091 
00092     /** Check if the file in an interactive terminal device
00093      *
00094      *  @return         True if the file is a terminal
00095      *  @return         False if the file is not a terminal
00096      *  @return         Negative error code on failure
00097      */
00098     virtual int isatty();
00099 
00100     /** Move the file position to a given offset from from a given location
00101      *
00102      * Not valid for a device type FileHandle like UARTSerial.
00103      * In case of UARTSerial, returns ESPIPE
00104      *
00105      *  @param offset   The offset from whence to move to
00106      *  @param whence   The start of where to seek
00107      *      SEEK_SET to start from beginning of file,
00108      *      SEEK_CUR to start from current position in file,
00109      *      SEEK_END to start from end of file
00110      *  @return         The new offset of the file, negative error code on failure
00111      */
00112     virtual off_t seek(off_t offset, int whence);
00113 
00114     /** Flush any buffers associated with the file
00115      *
00116      *  @return         0 on success, negative error code on failure
00117      */
00118     virtual int sync();
00119 
00120     /** Set blocking or non-blocking mode
00121      *  The default is blocking.
00122      *
00123      *  @param blocking true for blocking mode, false for non-blocking mode.
00124      */
00125     virtual int set_blocking(bool blocking)
00126     {
00127         _blocking = blocking;
00128         return 0;
00129     }
00130 
00131     /** Register a callback on state change of the file.
00132      *
00133      *  The specified callback will be called on state changes such as when
00134      *  the file can be written to or read from.
00135      *
00136      *  The callback may be called in an interrupt context and should not
00137      *  perform expensive operations.
00138      *
00139      *  Note! This is not intended as an attach-like asynchronous api, but rather
00140      *  as a building block for constructing  such functionality.
00141      *
00142      *  The exact timing of when the registered function
00143      *  is called is not guaranteed and susceptible to change. It should be used
00144      *  as a cue to make read/write/poll calls to find the current state.
00145      *
00146      *  @param func     Function to call on state change
00147      */
00148     virtual void sigio(Callback<void()> func);
00149 
00150     /** Setup interrupt handler for DCD line
00151      *
00152      *  If DCD line is connected, an IRQ handler will be setup.
00153      *  Does nothing if DCD is NC, i.e., not connected.
00154      *
00155      *  @param dcd_pin         Pin-name for DCD
00156      *  @param active_high     a boolean set to true if DCD polarity is active low
00157      */
00158     void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
00159 
00160 private:
00161 
00162     /** Software serial buffers
00163      *  By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
00164      */
00165     CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf;
00166     CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf;
00167 
00168     PlatformMutex _mutex;
00169 
00170     Callback<void()> _sigio_cb;
00171 
00172     bool _blocking;
00173     bool _tx_irq_enabled;
00174     InterruptIn *_dcd_irq;
00175 
00176     /** Device Hanged up
00177      *  Determines if the device hanged up on us.
00178      *
00179      *  @return True, if hanged up
00180      */
00181     bool hup() const;
00182 
00183     /** ISRs for serial
00184      *  Routines to handle interrupts on serial pins.
00185      *  Copies data into Circular Buffer.
00186      *  Reports the state change to File handle.
00187      */
00188     void tx_irq(void);
00189     void rx_irq(void);
00190 
00191     void wake(void);
00192 
00193     void dcd_irq(void);
00194 };
00195 } //namespace mbed
00196 
00197 #endif //DEVICE_SERIAL
00198 #endif //MBED_UARTSERIAL_H
00199