No description
Dependents: ros_lib_kinetic ds
BufferedSerial.h@0:a977d0a3d81e, 2013-05-23 (annotated)
- Committer:
- sam_grove
- Date:
- Thu May 23 19:31:30 2013 +0000
- Revision:
- 0:a977d0a3d81e
- Child:
- 1:57a11fb5d529
TX and RX peripheral functions are IRQ driven. Most stdlib functions are overridden for transmit. Not so much for reading. getc is implemented but not the more formatted scanf.
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 |
sam_grove | 0:a977d0a3d81e | 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 |
sam_grove | 0:a977d0a3d81e | 26 | |
sam_grove | 0:a977d0a3d81e | 27 | #include "mbed.h" |
sam_grove | 0:a977d0a3d81e | 28 | #include "Buffer.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() |
sam_grove | 0:a977d0a3d81e | 43 | * { |
sam_grove | 0:a977d0a3d81e | 44 | * pc.baud(115200); |
sam_grove | 0:a977d0a3d81e | 45 | * |
sam_grove | 0:a977d0a3d81e | 46 | * while(1) |
sam_grove | 0:a977d0a3d81e | 47 | * { |
sam_grove | 0:a977d0a3d81e | 48 | * Timer s; |
sam_grove | 0:a977d0a3d81e | 49 | * |
sam_grove | 0:a977d0a3d81e | 50 | * s.start(); |
sam_grove | 0:a977d0a3d81e | 51 | * pc.printf("Hello World - buff\n"); |
sam_grove | 0:a977d0a3d81e | 52 | * int buffered_time = s.read_us(); |
sam_grove | 0:a977d0a3d81e | 53 | * wait(0.1f); // give time for the buffer to empty |
sam_grove | 0:a977d0a3d81e | 54 | * |
sam_grove | 0:a977d0a3d81e | 55 | * s.reset(); |
sam_grove | 0:a977d0a3d81e | 56 | * printf("Hello World - poll\n"); |
sam_grove | 0:a977d0a3d81e | 57 | * int polled_time = s.read_us(); |
sam_grove | 0:a977d0a3d81e | 58 | * s.stop(); |
sam_grove | 0:a977d0a3d81e | 59 | * wait(0.1f); // give time for the buffer to empty |
sam_grove | 0:a977d0a3d81e | 60 | * |
sam_grove | 0:a977d0a3d81e | 61 | * pc.printf("printf buffered took %d us\n", buffered_time); |
sam_grove | 0:a977d0a3d81e | 62 | * pc.printf("printf polled took %d us\n", polled_time); |
sam_grove | 0:a977d0a3d81e | 63 | * wait(0.5f); |
sam_grove | 0:a977d0a3d81e | 64 | * } |
sam_grove | 0:a977d0a3d81e | 65 | * } |
sam_grove | 0:a977d0a3d81e | 66 | * @endcode |
sam_grove | 0:a977d0a3d81e | 67 | */ |
sam_grove | 0:a977d0a3d81e | 68 | |
sam_grove | 0:a977d0a3d81e | 69 | /** |
sam_grove | 0:a977d0a3d81e | 70 | * @class BufferedSerial |
sam_grove | 0:a977d0a3d81e | 71 | * @brief Software buffers and interrupt driven tx and rx for Serial |
sam_grove | 0:a977d0a3d81e | 72 | */ |
sam_grove | 0:a977d0a3d81e | 73 | class BufferedSerial : public Serial |
sam_grove | 0:a977d0a3d81e | 74 | { |
sam_grove | 0:a977d0a3d81e | 75 | private: |
sam_grove | 0:a977d0a3d81e | 76 | Buffer <char> _rxbuf; |
sam_grove | 0:a977d0a3d81e | 77 | Buffer <char> _txbuf; |
sam_grove | 0:a977d0a3d81e | 78 | |
sam_grove | 0:a977d0a3d81e | 79 | void rxIrq(void); |
sam_grove | 0:a977d0a3d81e | 80 | void txIrq(void); |
sam_grove | 0:a977d0a3d81e | 81 | |
sam_grove | 0:a977d0a3d81e | 82 | public: |
sam_grove | 0:a977d0a3d81e | 83 | /** Create a BufferedSerial port, connected to the specified transmit and receive pins |
sam_grove | 0:a977d0a3d81e | 84 | * @param tx Transmit pin |
sam_grove | 0:a977d0a3d81e | 85 | * @param rx Receive pin |
sam_grove | 0:a977d0a3d81e | 86 | * @note Either tx or rx may be specified as NC if unused |
sam_grove | 0:a977d0a3d81e | 87 | */ |
sam_grove | 0:a977d0a3d81e | 88 | BufferedSerial(PinName tx, PinName rx, const char* name=NULL); |
sam_grove | 0:a977d0a3d81e | 89 | |
sam_grove | 0:a977d0a3d81e | 90 | /** Destroy a BufferedSerial port |
sam_grove | 0:a977d0a3d81e | 91 | */ |
sam_grove | 0:a977d0a3d81e | 92 | virtual ~BufferedSerial(void); |
sam_grove | 0:a977d0a3d81e | 93 | |
sam_grove | 0:a977d0a3d81e | 94 | /** Check on how many bytes are in the rx buffer |
sam_grove | 0:a977d0a3d81e | 95 | * @return 1 if something exists, 0 otherwise |
sam_grove | 0:a977d0a3d81e | 96 | */ |
sam_grove | 0:a977d0a3d81e | 97 | virtual int readable(void); |
sam_grove | 0:a977d0a3d81e | 98 | |
sam_grove | 0:a977d0a3d81e | 99 | /** Check to see if the tx buffer has room |
sam_grove | 0:a977d0a3d81e | 100 | * @return 1 always has room and can overwrite previous content if too small / slow |
sam_grove | 0:a977d0a3d81e | 101 | */ |
sam_grove | 0:a977d0a3d81e | 102 | virtual int writeable(void); |
sam_grove | 0:a977d0a3d81e | 103 | |
sam_grove | 0:a977d0a3d81e | 104 | /** Get a single byte from the BufferedSerial Port. |
sam_grove | 0:a977d0a3d81e | 105 | * Should check readable() before calling this. |
sam_grove | 0:a977d0a3d81e | 106 | * @return A byte that came in on the Serial Port |
sam_grove | 0:a977d0a3d81e | 107 | */ |
sam_grove | 0:a977d0a3d81e | 108 | virtual int getc(void); |
sam_grove | 0:a977d0a3d81e | 109 | |
sam_grove | 0:a977d0a3d81e | 110 | /** Write a single byte to the BufferedSerial Port. |
sam_grove | 0:a977d0a3d81e | 111 | * @param c The byte to write to the Serial Port |
sam_grove | 0:a977d0a3d81e | 112 | * @return The byte that was written to the Serial Port |
sam_grove | 0:a977d0a3d81e | 113 | */ |
sam_grove | 0:a977d0a3d81e | 114 | virtual int putc(int c); |
sam_grove | 0:a977d0a3d81e | 115 | |
sam_grove | 0:a977d0a3d81e | 116 | /** Write a string to the BufferedSerial Port. Must be NULL terminated |
sam_grove | 0:a977d0a3d81e | 117 | * @param s The string to write to the Serial Port |
sam_grove | 0:a977d0a3d81e | 118 | * @return The number of bytes written to the Serial Port |
sam_grove | 0:a977d0a3d81e | 119 | */ |
sam_grove | 0:a977d0a3d81e | 120 | virtual int puts(const char *s); |
sam_grove | 0:a977d0a3d81e | 121 | |
sam_grove | 0:a977d0a3d81e | 122 | /** Write a formatted string to the BufferedSerial Port. |
sam_grove | 0:a977d0a3d81e | 123 | * @param format The string + format specifiers to write to the Serial Port |
sam_grove | 0:a977d0a3d81e | 124 | * @return The number of bytes written to the Serial Port |
sam_grove | 0:a977d0a3d81e | 125 | */ |
sam_grove | 0:a977d0a3d81e | 126 | virtual int printf(const char* format, ...); |
sam_grove | 0:a977d0a3d81e | 127 | |
sam_grove | 0:a977d0a3d81e | 128 | }; |
sam_grove | 0:a977d0a3d81e | 129 | |
sam_grove | 0:a977d0a3d81e | 130 | #endif |