Serial Interrupt Library with mbed RTOS for multi-threading
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
Revision 5:52705b8ccb32, committed 2012-12-10
- Comitter:
- tylerjw
- Date:
- Mon Dec 10 22:55:39 2012 +0000
- Parent:
- 4:b6e538868312
- Child:
- 6:ddab1e541812
- Commit message:
- added defines for next and is_full sections
Changed in this revision
| buffered_serial.cpp | Show annotated file Show diff for this revision Revisions of this file |
| buffered_serial.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
--- a/buffered_serial.h Mon Dec 10 22:43:18 2012 +0000
+++ b/buffered_serial.h Mon Dec 10 22:55:39 2012 +0000
@@ -4,8 +4,11 @@
#define BUFFER_SIZE 255
#define LINE_SIZE 80
#define NEXT(x) ((x+1)&BUFFER_SIZE)
+#define IS_TX_FULL (((tx_in + 1) & BUFFER_SIZE) == tx_out)
+#define IS_RX_FULL (((rx_in + 1) & BUFFER_SIZE) == rx_out)
#include "mbed.h"
+#include "rtos.h"
class BufferedSerial : public Serial
{
@@ -37,6 +40,9 @@
DigitalOut led1;
DigitalOut led2;
+
+ Semaphore rx_sem;
+ Semaphore tx_sem;
};
#endif
\ No newline at end of file
