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