Serial Interrupt Library with mbed RTOS for multi-threading
Dependencies: buffered-serial1 mbed-rtos mbed
Fork of Serial_interrupts by
Revision 8:df59d668cab1, committed 2012-12-10
- Comitter:
- tylerjw
- Date:
- Mon Dec 10 23:42:58 2012 +0000
- Parent:
- 7:180678808740
- Child:
- 9:dbcc3caeba88
- Commit message:
- moved to library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buffered-serial1.lib Mon Dec 10 23:42:58 2012 +0000 @@ -0,0 +1,1 @@ +buffered-serial1#707b9f3904dd
--- a/buffered_serial.cpp Mon Dec 10 23:27:28 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-#include "buffered_serial.h"
-
-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;
- rx_in=0;
- rx_out=0;
-
- switch( _serial.index ) {
- case 0:
- device_irqn = UART0_IRQn;
- break;
- case 1:
- device_irqn = UART1_IRQn;
- break;
- case 2:
- device_irqn = UART2_IRQn;
- break;
- case 3:
- device_irqn = UART3_IRQn;
- break;
- }
-
- // attach the interrupts
- Serial::attach(this, &BufferedSerial::Rx_interrupt, Serial::RxIrq);
- Serial::attach(this, &BufferedSerial::Tx_interrupt, Serial::TxIrq);
-}
-
-// Copy tx line buffer to large tx buffer for tx interrupt routine
-void BufferedSerial::send_line(char *c)
-{
- int i;
- char temp_char;
- bool empty;
- i = 0;
- strncpy(tx_line,c,LINE_SIZE);
- // Start Critical Section - don't interrupt while changing global buffer variables
- NVIC_DisableIRQ(device_irqn);
- empty = (tx_in == tx_out);
- while ((i==0) || (tx_line[i-1] != '\n')) {
- // Wait if buffer full
- if (IS_TX_FULL) {
- // End Critical Section - need to let interrupt routine empty buffer by sending
- NVIC_EnableIRQ(device_irqn);
- //while (IS_TX_FULL) ; // buffer is full
- tx_sem.wait();
- // 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 = NEXT(tx_in);
- }
- if (Serial::writeable() && (empty)) {
- temp_char = tx_buffer[tx_out];
- tx_out = NEXT(tx_out);
- // Send first character to start tx interrupts, if stopped
- LPC_UART1->THR = temp_char;
- }
- // End Critical Section
- NVIC_EnableIRQ(device_irqn);
-}
-
-// Read a line from the large rx buffer from rx interrupt routine
-void BufferedSerial::read_line(char *c)
-{
- int i;
- i = 0;
- // Start Critical Section - don't interrupt while changing global buffer variables
- NVIC_DisableIRQ(device_irqn);
- // Loop reading rx buffer characters until end of line character
- while ((i==0) || (rx_line[i-1] != '\r')) {
- // Wait if buffer empty
- if (IS_RX_EMPTY) { // buffer empty
- // End Critical Section - need to allow rx interrupt to get new characters for buffer
- NVIC_EnableIRQ(device_irqn);
- //while (rx_in == rx_out) ; // buffer empty
- rx_sem.wait();
- // Start Critical Section - don't interrupt while changing global buffer variables
- NVIC_DisableIRQ(device_irqn);
- } else {
- rx_sem.wait();
- }
- rx_line[i] = rx_buffer[rx_out];
- i++;
- rx_out = NEXT(rx_out);
-
- }
- rx_line[i-1] = 0;
- // End Critical Section
- NVIC_EnableIRQ(device_irqn);
- strncpy(c,rx_line,LINE_SIZE);
-}
-
-// Interupt Routine to read in data from serial port
-void BufferedSerial::Rx_interrupt()
-{
- uint32_t IRR1 = LPC_UART1->IIR;
- led1=1;
- while (readable() && !(IS_RX_FULL)) {
- rx_buffer[rx_in] = LPC_UART1->RBR;
- rx_in = NEXT(rx_in);
- rx_sem.release();
- }
- led1=0;
-}
-
-// Interupt Routine to write out data to serial port
-void BufferedSerial::Tx_interrupt()
-{
- uint32_t IRR = LPC_UART1->IIR;
- led2=1;
- while ((writeable()) && (tx_in != tx_out)) { // while serial is writeable and there are still characters in the buffer
- LPC_UART1->THR = tx_buffer[tx_out]; // send the character
- tx_out = NEXT(tx_out);
- }
- if(!IS_TX_FULL) // if not full
- tx_sem.release();
- led2=0;
-}
\ No newline at end of file
--- a/buffered_serial.h Mon Dec 10 23:27:28 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-#ifndef BUFFERED_SERIAL_H
-#define BUFFERED_SERIAL_H
-
-#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)
-#define IS_RX_EMPTY (rx_in == rx_out)
-
-#include "mbed.h"
-#include "rtos.h"
-
-class BufferedSerial : public Serial
-{
-public:
- BufferedSerial(PinName tx, PinName rx);
-
- void send_line(char*);
- void read_line(char*);
-
-private:
- void Tx_interrupt();
- void Rx_interrupt();
-
-// for disabling the irq
- IRQn device_irqn;
-// Circular buffers for serial TX and RX data - used by interrupt routines
-// 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;
- volatile int tx_out;
- volatile int rx_in;
- volatile int rx_out;
-// Line buffers for sprintf and sscanf
- char tx_line[LINE_SIZE];
- char rx_line[LINE_SIZE];
-
- DigitalOut led1;
- DigitalOut led2;
-
- Semaphore rx_sem;
- Semaphore tx_sem;
-};
-
-#endif
\ No newline at end of file
--- a/main.cpp Mon Dec 10 23:27:28 2012 +0000 +++ b/main.cpp Mon Dec 10 23:42:58 2012 +0000 @@ -6,7 +6,7 @@ // 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); // UART1 - p13, p14 DigitalOut led3(LED3); DigitalOut led4(LED4);
