Serial Interrupt Library with mbed RTOS for multi-threading

Dependencies:   buffered-serial1 mbed-rtos mbed

Fork of Serial_interrupts by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
tylerjw
Date:
Mon Dec 10 23:07:39 2012 +0000
Parent:
5:52705b8ccb32
Child:
7:180678808740
Commit message:
rx semaphore

Changed in this revision

buffered_serial.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/buffered_serial.cpp	Mon Dec 10 22:55:39 2012 +0000
+++ b/buffered_serial.cpp	Mon Dec 10 23:07:39 2012 +0000
@@ -6,7 +6,7 @@
     tx_out=0;
     rx_in=0;
     rx_out=0;
-    
+
     switch( _serial.index ) {
         case 0:
             device_irqn = UART0_IRQn;
@@ -21,7 +21,7 @@
             device_irqn = UART3_IRQn;
             break;
     }
-    
+
     // attach the interrupts
     Serial::attach(this, &BufferedSerial::Rx_interrupt, Serial::RxIrq);
     Serial::attach(this, &BufferedSerial::Tx_interrupt, Serial::TxIrq);
@@ -35,17 +35,17 @@
     bool empty;
     i = 0;
     strncpy(tx_line,c,LINE_SIZE);
-// Start Critical Section - don't interrupt while changing global buffer variables
+    // 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
+        // Wait if buffer full
         if (IS_TX_FULL) {
-// End Critical Section - need to let interrupt routine empty buffer by sending
+            // End Critical Section - need to let interrupt routine empty buffer by sending
             NVIC_EnableIRQ(device_irqn);
-            while (IS_TX_FULL) { // buffer is full
-            }
-// Start Critical Section - don't interrupt while changing global buffer variables
+            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];
@@ -55,13 +55,11 @@
     if (Serial::writeable() && (empty)) {
         temp_char = tx_buffer[tx_out];
         tx_out = NEXT(tx_out);
-// Send first character to start tx interrupts, if stopped
-        //Serial::putc(temp_char);
+        // Send first character to start tx interrupts, if stopped
         LPC_UART1->THR = temp_char;
     }
-// End Critical Section
+    // End Critical Section
     NVIC_EnableIRQ(device_irqn);
-    return;
 }
 
 // Read a line from the large rx buffer from rx interrupt routine
@@ -69,60 +67,55 @@
 {
     int i;
     i = 0;
-// Start Critical Section - don't interrupt while changing global buffer variables
+    // 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
+    // Loop reading rx buffer characters until end of line character
     while ((i==0) || (rx_line[i-1] != '\r')) {
-// Wait if buffer empty
-        if (rx_in == rx_out) {
-// End Critical Section - need to allow rx interrupt to get new characters for buffer
+        // Wait if buffer empty
+        if (rx_in == rx_out) { // 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) {
-            }
-// Start Critical Section - don't interrupt while changing global buffer variables
+            //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);
+            rx_line[i] = rx_buffer[rx_out];
+            i++;
+            rx_out = NEXT(rx_out);
+        } else {
+            rx_sem.wait();
+            rx_line[i] = rx_buffer[rx_out];
+            i++;
+            rx_out = NEXT(rx_out);
         }
-        rx_line[i] = rx_buffer[rx_out];
-        i++;
-        rx_out = NEXT(rx_out);
     }
     rx_line[i-1] = 0;
-// End Critical Section
+    // End Critical Section
     NVIC_EnableIRQ(device_irqn);
     strncpy(c,rx_line,LINE_SIZE);
-    return;
 }
 
 // Interupt Routine to read in data from serial port
 void BufferedSerial::Rx_interrupt()
 {
-    //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 (readable() || IS_RX_FULL) { 
+    while (readable() && !(IS_RX_FULL)) {
         rx_buffer[rx_in] = LPC_UART1->RBR;
-        //rx_buffer[rx_in] = Serial::getc();
         rx_in = NEXT(rx_in);
+        rx_sem.release();
     }
     led1=0;
-    return;
 }
 
 // Interupt Routine to write out data to serial port
 void BufferedSerial::Tx_interrupt()
 {
-    //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 ((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 = NEXT(tx_out);
     }
     led2=0;
-    return;
 }
\ No newline at end of file