Serial Interrupt Library with mbed RTOS for multi-threading
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
Revision 2:3d959c9fc9d7, committed 2012-12-10
- Comitter:
- tylerjw
- Date:
- Mon Dec 10 22:33:57 2012 +0000
- Parent:
- 1:2f1e54d137c7
- Child:
- 3:2f8f50256c2e
- Commit message:
- working with rtos included
Changed in this revision
--- a/buffered_serial.cpp Mon Dec 10 00:32:22 2012 +0000
+++ b/buffered_serial.cpp Mon Dec 10 22:33:57 2012 +0000
@@ -1,6 +1,6 @@
#include "buffered_serial.h"
-BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx)
+BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx), led1(LED1), led2(LED2)
{
tx_in=0;
tx_out=0;
@@ -21,7 +21,7 @@
device_irqn = UART3_IRQn;
break;
}
- //device_irqn = UART1_IRQn;
+
// attach the interrupts
Serial::attach(this, &BufferedSerial::Rx_interrupt, Serial::RxIrq);
Serial::attach(this, &BufferedSerial::Tx_interrupt, Serial::TxIrq);
@@ -56,7 +56,8 @@
temp_char = tx_buffer[tx_out];
tx_out = (tx_out + 1) & BUFFER_SIZE;
// Send first character to start tx interrupts, if stopped
- Serial::putc(temp_char);
+ //Serial::putc(temp_char);
+ LPC_UART1->THR = temp_char;
}
// End Critical Section
NVIC_EnableIRQ(device_irqn);
@@ -95,29 +96,33 @@
// Interupt Routine to read in data from serial port
void BufferedSerial::Rx_interrupt()
{
- //led1=1;
+ //uint32_t RBR = LPC_UART1->RBR;
+ uint32_t IRR1 = LPC_UART1->IIR;
+ led1=1;
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
- while ((Serial::readable()) || (((rx_in + 1) & BUFFER_SIZE) == rx_out)) {
- rx_buffer[rx_in] = Serial::getc();
-// Uncomment to Echo to USB serial to watch data flow
-// monitor_Serial::putc(rx_buffer[rx_in]);
+ while ((readable()) || (((rx_in + 1) & BUFFER_SIZE) == rx_out)) {
+ rx_buffer[rx_in] = LPC_UART1->RBR;
+ //rx_buffer[rx_in] = Serial::getc();
rx_in = (rx_in + 1) & BUFFER_SIZE;
}
- //led1=0;
+ led1=0;
return;
}
// Interupt Routine to write out data to serial port
void BufferedSerial::Tx_interrupt()
{
- //led2=1;
+ //uint32_t RBR = LPC_UART1->RBR;
+ uint32_t IRR = LPC_UART1->IIR;
+ led2=1;
// Loop to fill more than one character in UART's transmit FIFO buffer
// Stop if buffer empty
- while ((Serial::writeable()) && (tx_in != tx_out)) {
- Serial::putc(tx_buffer[tx_out]);
+ 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;
}
- //led2=0;
+ led2=0;
return;
}
\ No newline at end of file
--- a/buffered_serial.h Mon Dec 10 00:32:22 2012 +0000
+++ b/buffered_serial.h Mon Dec 10 22:33:57 2012 +0000
@@ -3,18 +3,18 @@
#define BUFFER_SIZE 255
#define LINE_SIZE 80
+#define NEXT(x) ((x+1)&BUFFER_SIZE)
#include "mbed.h"
class BufferedSerial : public Serial
{
public:
- BufferedSerial::BufferedSerial(PinName tx, PinName rx);
+ BufferedSerial(PinName tx, PinName rx);
void send_line(char*);
void read_line(char*);
-
private:
void Tx_interrupt();
void Rx_interrupt();
@@ -34,6 +34,9 @@
// Line buffers for sprintf and sscanf
char tx_line[LINE_SIZE];
char rx_line[LINE_SIZE];
+
+ DigitalOut led1;
+ DigitalOut led2;
};
#endif
\ No newline at end of file
--- a/main.cpp Mon Dec 10 00:32:22 2012 +0000
+++ b/main.cpp Mon Dec 10 22:33:57 2012 +0000
@@ -8,35 +8,11 @@
// LED1 and LED2 indicate RX and TX interrupt routine activity
// LED3 changing indicate main loop running
+BufferedSerial device(p13,p14); // tx, rx
-BufferedSerial device(p13,p14); // tx, rx
-//IRQn device_irqn = UART1_IRQn;
-// Can also use USB and type back in the number printed out in a terminal window
-// Serial monitor_device(USBTX, USBRX);
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
-/*
-void Tx_interrupt();
-void Rx_interrupt();
-void send_line();
-void read_line();
-
-
-// Circular buffers for serial TX and RX data - used by interrupt routines
-const int buffer_size = 255;
-// might need to increase buffer size for high baud rates
-char tx_buffer[buffer_size];
-char rx_buffer[buffer_size];
-// Circular buffer pointers
-// volatile makes read-modify-write atomic
-volatile int tx_in=0;
-volatile int tx_out=0;
-volatile int rx_in=0;
-volatile int rx_out=0;
-*/
// Line buffers for sprintf and sscanf
char tx_line[80];
char rx_line[80];
@@ -47,12 +23,7 @@
int i=0;
int rx_i=0;
device.baud(9600);
-/*
-// Setup a serial interrupt function to receive data
- device.attach(&Rx_interrupt, Serial::RxIrq);
-// Setup a serial interrupt function to transmit data
- device.attach(&Tx_interrupt, Serial::TxIrq);
-*/
+
// Formatted IO test using send and receive serial interrupts
// with sprintf and sscanf
while (1) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Dec 10 22:33:57 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/carlos_nascimento08/code/mbed-rtos/#2736675d00b1
