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