Tyler Weaver / Mbed 2 deprecated Serial_interrupts_buffered

Dependencies:   buffered-serial1 mbed-rtos mbed

Fork of Serial_interrupts by jim hamblen

Revision:
1:2f1e54d137c7
Child:
2:3d959c9fc9d7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffered_serial.cpp	Mon Dec 10 00:32:22 2012 +0000
@@ -0,0 +1,123 @@
+#include "buffered_serial.h"
+
+BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx)
+{
+    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;
+    }
+    //device_irqn = UART1_IRQn;
+    // 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 (((tx_in + 1) & BUFFER_SIZE) == tx_out) {
+// End Critical Section - need to let interrupt routine empty buffer by sending
+            NVIC_EnableIRQ(device_irqn);
+            while (((tx_in + 1) & BUFFER_SIZE) == tx_out) {
+            }
+// 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;
+    }
+    if (Serial::writeable() && (empty)) {
+        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);
+    }
+// End Critical Section
+    NVIC_EnableIRQ(device_irqn);
+    return;
+}
+
+// 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 (rx_in == rx_out) {
+// 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
+            NVIC_DisableIRQ(device_irqn);
+        }
+        rx_line[i] = rx_buffer[rx_out];
+        i++;
+        rx_out = (rx_out + 1) & BUFFER_SIZE;
+    }
+    rx_line[i-1] = 0;
+// 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()
+{
+    //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]);
+        rx_in = (rx_in + 1) & BUFFER_SIZE;
+    }
+    //led1=0;
+    return;
+}
+
+// Interupt Routine to write out data to serial port
+void BufferedSerial::Tx_interrupt()
+{
+    //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]);
+        tx_out = (tx_out + 1) & BUFFER_SIZE;
+    }
+    //led2=0;
+    return;
+}
\ No newline at end of file