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:27:28 2012 +0000
Parent:
6:ddab1e541812
Child:
8:df59d668cab1
Commit message:
tx semaphore

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 23:07:39 2012 +0000
+++ b/buffered_serial.cpp	Mon Dec 10 23:27:28 2012 +0000
@@ -43,8 +43,8 @@
         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
-
+            //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);
         }
@@ -72,22 +72,20 @@
     // 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) { // 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);
-            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
@@ -117,5 +115,7 @@
         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:07:39 2012 +0000
+++ b/buffered_serial.h	Mon Dec 10 23:27:28 2012 +0000
@@ -6,6 +6,7 @@
 #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"