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.
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
Diff: buffered_serial.cpp
- Revision:
- 5:52705b8ccb32
- Parent:
- 2:3d959c9fc9d7
- Child:
- 6:ddab1e541812
--- a/buffered_serial.cpp Mon Dec 10 22:43:18 2012 +0000
+++ b/buffered_serial.cpp Mon Dec 10 22:55:39 2012 +0000
@@ -1,6 +1,6 @@
#include "buffered_serial.h"
-BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx), led1(LED1), led2(LED2)
+BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx), led1(LED1), led2(LED2), rx_sem(0), tx_sem(0)
{
tx_in=0;
tx_out=0;
@@ -40,21 +40,21 @@
empty = (tx_in == tx_out);
while ((i==0) || (tx_line[i-1] != '\n')) {
// Wait if buffer full
- if (((tx_in + 1) & BUFFER_SIZE) == tx_out) {
+ if (IS_TX_FULL) {
// End Critical Section - need to let interrupt routine empty buffer by sending
NVIC_EnableIRQ(device_irqn);
- while (((tx_in + 1) & BUFFER_SIZE) == tx_out) {
+ while (IS_TX_FULL) { // buffer is full
}
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(device_irqn);
}
tx_buffer[tx_in] = tx_line[i];
i++;
- tx_in = (tx_in + 1) & BUFFER_SIZE;
+ tx_in = NEXT(tx_in);
}
if (Serial::writeable() && (empty)) {
temp_char = tx_buffer[tx_out];
- tx_out = (tx_out + 1) & BUFFER_SIZE;
+ tx_out = NEXT(tx_out);
// Send first character to start tx interrupts, if stopped
//Serial::putc(temp_char);
LPC_UART1->THR = temp_char;
@@ -84,7 +84,7 @@
}
rx_line[i] = rx_buffer[rx_out];
i++;
- rx_out = (rx_out + 1) & BUFFER_SIZE;
+ rx_out = NEXT(rx_out);
}
rx_line[i-1] = 0;
// End Critical Section
@@ -101,10 +101,10 @@
led1=1;
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
- while ((readable()) || (((rx_in + 1) & BUFFER_SIZE) == rx_out)) {
+ while (readable() || IS_RX_FULL) {
rx_buffer[rx_in] = LPC_UART1->RBR;
//rx_buffer[rx_in] = Serial::getc();
- rx_in = (rx_in + 1) & BUFFER_SIZE;
+ rx_in = NEXT(rx_in);
}
led1=0;
return;
@@ -121,7 +121,7 @@
while ((writeable()) && (tx_in != tx_out)) { // while serial is writeable and there are still characters in the buffer
//Serial::putc(tx_buffer[tx_out]);
LPC_UART1->THR = tx_buffer[tx_out]; // send the character
- tx_out = (tx_out + 1) & BUFFER_SIZE;
+ tx_out = NEXT(tx_out);
}
led2=0;
return;
