Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did
Dependents: buffered_serial_test BLE_Police_HRM evena_BLE_Police_HRM df-2014-workshop-rfid-case-generator-k64f ... more
Example
#include "mbed.h"
#include "BufferedSerial.h"
BufferedSerial pc(USBTX, USBRX);
int main()
{
pc.baud(115200);
while(1)
{
Timer s;
s.start();
pc.printf("Hello World - buff\n");
int buffered_time = s.read_us();
wait(0.1f); // give time for the buffer to empty
s.reset();
printf("Hello World - poll\n");
int polled_time = s.read_us();
s.stop();
wait(0.1f); // give time for the buffer to empty
pc.printf("printf buffered took %d us\n", buffered_time);
pc.printf("printf polled took %d us\n", polled_time);
wait(0.5f);
}
}
Diff: BufferedSerial.h
- Revision:
- 7:6fa214b41d73
- Parent:
- 6:8287e83943f0
- Child:
- 8:506247a040bc
diff -r 8287e83943f0 -r 6fa214b41d73 BufferedSerial.h
--- a/BufferedSerial.h Tue Sep 09 20:20:38 2014 +0000
+++ b/BufferedSerial.h Fri Jan 02 03:47:26 2015 +0000
@@ -27,8 +27,12 @@
#include "mbed.h"
#include "Buffer.h"
+// Base Class
#define SERIAL_BASE RawSerial
+// Internal buffer size
+#define BUFFEREDSERIAL_MAX_BUFFER_SIZE 512
+
/** A serial port (UART) for communication with other serial devices
*
* Can be used for Full Duplex communication, or Simplex by specifying
@@ -75,6 +79,7 @@
private:
Buffer <char> _rxbuf;
Buffer <char> _txbuf;
+ char _buffer[BUFFEREDSERIAL_MAX_BUFFER_SIZE+1];
void rxIrq(void);
void txIrq(void);
@@ -84,6 +89,7 @@
/** Create a BufferedSerial port, connected to the specified transmit and receive pins
* @param tx Transmit pin
* @param rx Receive pin
+ * @param name optional name
* @note Either tx or rx may be specified as NC if unused
*/
BufferedSerial(PinName tx, PinName rx, const char* name=NULL);
Sam Grove

