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