NuMaker Transfer data UART to Ethernet
Fork of Serial-to-Ethernet by
BufferSerial/BufferSerial.h@6:014b1a469aed, 2021-03-02 (annotated)
- Committer:
- SHLIU1@OANBE02333.nuvoton.com
- Date:
- Tue Mar 02 10:09:10 2021 +0800
- Revision:
- 6:014b1a469aed
Support the both V5.X and V6.X for mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 1 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 2 | /** |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 3 | * @file BufferSerial.h |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 4 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 5 | * @author sam grove |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 6 | * @version 1.0 |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 7 | * @see |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 8 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 9 | * Copyright (c) 2013 |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 10 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 12 | * you may not use this file except in compliance with the License. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 13 | * You may obtain a copy of the License at |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 14 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 16 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 17 | * Unless required by applicable law or agreed to in writing, software |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 20 | * See the License for the specific language governing permissions and |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 21 | * limitations under the License. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 22 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 23 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 24 | #ifndef BUFFEREDSERIAL_H |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 25 | #define BUFFEREDSERIAL_H |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 26 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 27 | #include "mbed.h" |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 28 | #include "MyBuffer.h" |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 29 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 30 | #if (MBED_MAJOR_VERSION == 5) && (MBED_MINOR_VERSION >= 2) |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 31 | #elif (MBED_MAJOR_VERSION == 2) && (MBED_PATCH_VERSION > 130) |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 32 | #else |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 33 | //#error "BufferSerial version 13 and newer requires use of Mbed OS 5.2.0 and newer or Mbed 2 version 130 and newer. Use BufferSerial version 12 and older or upgrade the Mbed version. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 34 | #endif |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 35 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 36 | /** A serial port (UART) for communication with other serial devices |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 37 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 38 | * Can be used for Full Duplex communication, or Simplex by specifying |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 39 | * one pin as NC (Not Connected) |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 40 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 41 | * Example: |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 42 | * @code |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 43 | * #include "mbed.h" |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 44 | * #include "BufferSerial.h" |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 45 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 46 | * BufferSerial pc(USBTX, USBRX); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 47 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 48 | * int main() |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 49 | * { |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 50 | * while(1) |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 51 | * { |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 52 | * Timer s; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 53 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 54 | * s.start(); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 55 | * pc.printf("Hello World - buffer\n"); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 56 | * int buffer_time = s.read_us(); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 57 | * wait(0.1f); // give time for the buffer to empty |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 58 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 59 | * s.reset(); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 60 | * printf("Hello World - blocking\n"); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 61 | * int polled_time = s.read_us(); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 62 | * s.stop(); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 63 | * wait(0.1f); // give time for the buffer to empty |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 64 | * |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 65 | * pc.printf("printf buffer took %d us\n", buffer_time); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 66 | * pc.printf("printf blocking took %d us\n", polled_time); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 67 | * wait(0.5f); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 68 | * } |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 69 | * } |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 70 | * @endcode |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 71 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 72 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 73 | /** |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 74 | * @class BufferSerial |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 75 | * @brief Software buffers and interrupt driven tx and rx for Serial |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 76 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 77 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 78 | #if MBED_MAJOR_VERSION >= 6 |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 79 | #include "MyUnbufferedSerial.h" |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 80 | class BufferSerial : public MyUnbufferedSerial |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 81 | { |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 82 | #else |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 83 | class BufferSerial : public RawSerial |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 84 | { |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 85 | #endif |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 86 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 87 | private: |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 88 | MyBuffer <char> _rxbuf; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 89 | MyBuffer <char> _txbuf; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 90 | uint32_t _buf_size; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 91 | uint32_t _tx_multiple; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 92 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 93 | void rxIrq(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 94 | void txIrq(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 95 | void prime(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 96 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 97 | public: |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 98 | /** Create a BufferSerial port, connected to the specified transmit and receive pins |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 99 | * @param tx Transmit pin |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 100 | * @param rx Receive pin |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 101 | * @param buf_size printf() buffer size |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 102 | * @param tx_multiple amount of max printf() present in the internal ring buffer at one time |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 103 | * @param name optional name |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 104 | * @note Either tx or rx may be specified as NC if unused |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 105 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 106 | BufferSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 107 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 108 | /** Destroy a BufferSerial port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 109 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 110 | virtual ~BufferSerial(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 111 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 112 | /** Check on how many bytes are in the rx buffer |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 113 | * @return 1 if something exists, 0 otherwise |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 114 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 115 | virtual int readable(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 116 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 117 | /** Check to see if the tx buffer has room |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 118 | * @return 1 always has room and can overwrite previous content if too small / slow |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 119 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 120 | virtual int writeable(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 121 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 122 | /** Get a single byte from the BufferSerial Port. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 123 | * Should check readable() before calling this. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 124 | * @return A byte that came in on the Serial Port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 125 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 126 | virtual int getc(void); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 127 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 128 | /** Write a single byte to the BufferSerial Port. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 129 | * @param c The byte to write to the Serial Port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 130 | * @return The byte that was written to the Serial Port Buffer |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 131 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 132 | virtual int putc(int c); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 133 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 134 | /** Write a string to the BufferSerial Port. Must be NULL terminated |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 135 | * @param s The string to write to the Serial Port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 136 | * @return The number of bytes written to the Serial Port Buffer |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 137 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 138 | virtual int puts(const char *s); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 139 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 140 | /** Write a formatted string to the BufferSerial Port. |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 141 | * @param format The string + format specifiers to write to the Serial Port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 142 | * @return The number of bytes written to the Serial Port Buffer |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 143 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 144 | virtual int printf(const char* format, ...); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 145 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 146 | /** Write data to the Buffer Serial Port |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 147 | * @param s A pointer to data to send |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 148 | * @param length The amount of data being pointed to |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 149 | * @return The number of bytes written to the Serial Port Buffer |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 150 | */ |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 151 | virtual ssize_t write(const void *s, std::size_t length); |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 152 | }; |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 153 | |
SHLIU1@OANBE02333.nuvoton.com | 6:014b1a469aed | 154 | #endif |