|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef MBED_UARTSERIAL_H |
| 18 | +#define MBED_UARTSERIAL_H |
| 19 | + |
| 20 | +#include "platform/platform.h" |
| 21 | + |
| 22 | +#if DEVICE_SERIAL |
| 23 | + |
| 24 | +#include "FileHandle.h" |
| 25 | +#include "SerialBase.h" |
| 26 | +#include "InterruptIn.h" |
| 27 | +#include "PlatformMutex.h" |
| 28 | +#include "serial_api.h" |
| 29 | +#include "CircularBuffer.h" |
| 30 | + |
| 31 | +#ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE |
| 32 | +#define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256 |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE |
| 36 | +#define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256 |
| 37 | +#endif |
| 38 | + |
| 39 | +namespace mbed { |
| 40 | + |
| 41 | +class UARTSerial : private SerialBase, public FileHandle { |
| 42 | + |
| 43 | +public: |
| 44 | + |
| 45 | + /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. |
| 46 | + * @param tx Transmit pin |
| 47 | + * @param rx Receive pin |
| 48 | + * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) |
| 49 | + */ |
| 50 | + UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); |
| 51 | + virtual ~UARTSerial(); |
| 52 | + |
| 53 | + /** Equivalent to POSIX poll(). Derived from FileHandle. |
| 54 | + * Provides a mechanism to multiplex input/output over a set of file handles. |
| 55 | + */ |
| 56 | + virtual short poll(short events) const; |
| 57 | + |
| 58 | + /** Write the contents of a buffer to a file |
| 59 | + * |
| 60 | + * @param buffer The buffer to write from |
| 61 | + * @param size The number of bytes to write |
| 62 | + * @return The number of bytes written, negative error on failure |
| 63 | + */ |
| 64 | + virtual ssize_t write(const void* buffer, size_t length); |
| 65 | + |
| 66 | + /** Read the contents of a file into a buffer |
| 67 | + * |
| 68 | + * Follows POSIX semantics: |
| 69 | + * |
| 70 | + * * if no data is available, and non-blocking set return -EAGAIN |
| 71 | + * * if no data is available, and blocking set, wait until data is available |
| 72 | + * * If any data is available, call returns immediately |
| 73 | + * |
| 74 | + * @param buffer The buffer to read in to |
| 75 | + * @param size The number of bytes to read |
| 76 | + * @return The number of bytes read, 0 at end of file, negative error on failure |
| 77 | + */ |
| 78 | + virtual ssize_t read(void* buffer, size_t length); |
| 79 | + |
| 80 | + /** Acquire mutex */ |
| 81 | + virtual void lock(void); |
| 82 | + |
| 83 | + /** Release mutex */ |
| 84 | + virtual void unlock(void); |
| 85 | + |
| 86 | + /** Close a file |
| 87 | + * |
| 88 | + * @return 0 on success, negative error code on failure |
| 89 | + */ |
| 90 | + virtual int close(); |
| 91 | + |
| 92 | + /** Check if the file in an interactive terminal device |
| 93 | + * |
| 94 | + * @return True if the file is a terminal |
| 95 | + * @return False if the file is not a terminal |
| 96 | + * @return Negative error code on failure |
| 97 | + */ |
| 98 | + virtual int isatty(); |
| 99 | + |
| 100 | + /** Move the file position to a given offset from from a given location |
| 101 | + * |
| 102 | + * Not valid for a device type FileHandle like UARTSerial. |
| 103 | + * In case of UARTSerial, returns ESPIPE |
| 104 | + * |
| 105 | + * @param offset The offset from whence to move to |
| 106 | + * @param whence The start of where to seek |
| 107 | + * SEEK_SET to start from beginning of file, |
| 108 | + * SEEK_CUR to start from current position in file, |
| 109 | + * SEEK_END to start from end of file |
| 110 | + * @return The new offset of the file, negative error code on failure |
| 111 | + */ |
| 112 | + virtual off_t seek(off_t offset, int whence); |
| 113 | + |
| 114 | + /** Flush any buffers associated with the file |
| 115 | + * |
| 116 | + * @return 0 on success, negative error code on failure |
| 117 | + */ |
| 118 | + virtual int sync(); |
| 119 | + |
| 120 | + /** Set blocking or non-blocking mode |
| 121 | + * The default is blocking. |
| 122 | + * |
| 123 | + * @param blocking true for blocking mode, false for non-blocking mode. |
| 124 | + */ |
| 125 | + virtual int set_blocking(bool blocking) |
| 126 | + { |
| 127 | + _blocking = blocking; |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + /** Register a callback on state change of the file. |
| 132 | + * |
| 133 | + * The specified callback will be called on state changes such as when |
| 134 | + * the file can be written to or read from. |
| 135 | + * |
| 136 | + * The callback may be called in an interrupt context and should not |
| 137 | + * perform expensive operations. |
| 138 | + * |
| 139 | + * Note! This is not intended as an attach-like asynchronous api, but rather |
| 140 | + * as a building block for constructing such functionality. |
| 141 | + * |
| 142 | + * The exact timing of when the registered function |
| 143 | + * is called is not guaranteed and susceptible to change. It should be used |
| 144 | + * as a cue to make read/write/poll calls to find the current state. |
| 145 | + * |
| 146 | + * @param func Function to call on state change |
| 147 | + */ |
| 148 | + virtual void sigio(Callback<void()> func); |
| 149 | + |
| 150 | + /** Setup interrupt handler for DCD line |
| 151 | + * |
| 152 | + * If DCD line is connected, an IRQ handler will be setup. |
| 153 | + * Does nothing if DCD is NC, i.e., not connected. |
| 154 | + * |
| 155 | + * @param dcd_pin Pin-name for DCD |
| 156 | + * @param active_high a boolean set to true if DCD polarity is active low |
| 157 | + */ |
| 158 | + void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); |
| 159 | + |
| 160 | +private: |
| 161 | + |
| 162 | + /** Software serial buffers |
| 163 | + * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json |
| 164 | + */ |
| 165 | + CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; |
| 166 | + CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; |
| 167 | + |
| 168 | + PlatformMutex _mutex; |
| 169 | + |
| 170 | + Callback<void()> _sigio_cb; |
| 171 | + |
| 172 | + bool _blocking; |
| 173 | + bool _tx_irq_enabled; |
| 174 | + InterruptIn *_dcd_irq; |
| 175 | + |
| 176 | + /** Device Hanged up |
| 177 | + * Determines if the device hanged up on us. |
| 178 | + * |
| 179 | + * @return True, if hanged up |
| 180 | + */ |
| 181 | + bool hup() const; |
| 182 | + |
| 183 | + /** ISRs for serial |
| 184 | + * Routines to handle interrupts on serial pins. |
| 185 | + * Copies data into Circular Buffer. |
| 186 | + * Reports the state change to File handle. |
| 187 | + */ |
| 188 | + void tx_irq(void); |
| 189 | + void rx_irq(void); |
| 190 | + |
| 191 | + void wake(void); |
| 192 | + |
| 193 | + void dcd_irq(void); |
| 194 | +}; |
| 195 | +} //namespace mbed |
| 196 | + |
| 197 | +#endif //DEVICE_SERIAL |
| 198 | +#endif //MBED_UARTSERIAL_H |
0 commit comments