UART1 buffered serial driver, requires RTOS

Dependents:   Serial_interrupts_buffered HARP2 HARP3

Buffered serial UART1 - Setup to work with UART1 (p13,p14)

Uses RTOS to block current thread.

Reference: http://mbed.org/users/tylerjw/notebook/buffered-serial-with-rtos/

Files at this revision

API Documentation at this revision

Comitter:
tylerjw
Date:
Mon Dec 17 22:35:51 2012 +0000
Parent:
3:a4a21e18acd1
Commit message:
naming conventions and documentation

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
diff -r a4a21e18acd1 -r d3122119f92b buffered_serial.cpp
--- a/buffered_serial.cpp	Thu Dec 13 06:53:43 2012 +0000
+++ b/buffered_serial.cpp	Mon Dec 17 22:35:51 2012 +0000
@@ -1,11 +1,38 @@
+/*
+ * @file buffered_serial.cpp
+ * @author Tyler Weaver
+ *
+ * @section LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Buffered serial UART1 - Setup to work with UART1 (p13,p14)
+ *
+ * Uses RTOS to block current thread.
+ */
 #include "buffered_serial.h"
 
-BufferedSerial::BufferedSerial(PinName tx, PinName rx) : Serial(tx,rx), /*led1(LED1), led2(LED2),*/ rx_sem(0), tx_sem(0)
+BufferedSerial::BufferedSerial() : Serial(p13,p14), /*led1(LED1), led2(LED2),*/ rx_sem_(0), tx_sem_(0)
 {
-    tx_in=0;
-    tx_out=0;
-    rx_in=0;
-    rx_out=0;
+    tx_in_=0;
+    tx_out_=0;
+    rx_in_=0;
+    rx_out_=0;
 
     device_irqn = UART1_IRQn;
 
@@ -15,33 +42,33 @@
 }
 
 // Copy tx line buffer to large tx buffer for tx interrupt routine
-void BufferedSerial::send_line(char *c)
+void BufferedSerial::put_line(char *c)
 {
     int i;
     char temp_char;
     bool empty;
     i = 0;
-    strncpy(tx_line,c,LINE_SIZE);
+    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')) {
+    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();
+            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];
+        tx_buffer_[tx_in_] = tx_line_[i];
         i++;
-        tx_in = NEXT(tx_in);
+        tx_in_ = NEXT(tx_in_);
     }
     if (Serial::writeable() && (empty)) {
-        temp_char = tx_buffer[tx_out];
-        tx_out = NEXT(tx_out);
+        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;
     }
@@ -50,40 +77,39 @@
 }
 
 // Read a line from the large rx buffer from rx interrupt routine
-void BufferedSerial::read_line(char *c)
+void BufferedSerial::get_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] != '\n')) {
+    while ((i==0) || (rx_line_[i-1] != '\n')) {
         // 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();
+            
+            rx_sem_.wait();
             // Start Critical Section - don't interrupt while changing global buffer variables
             NVIC_DisableIRQ(device_irqn);
         } else {
-            rx_sem.wait();
+            rx_sem_.wait();
         }
-        rx_line[i] = rx_buffer[rx_out];
+        rx_line_[i] = rx_buffer_[rx_out_];
         i++;
-        rx_out = NEXT(rx_out);
-        
+        rx_out_ = NEXT(rx_out_);
+
         // prevent overflow on rx_line
-        if(i == LINE_SIZE) 
-        {
+        if(i == LINE_SIZE) {
             i--;
             break;
         }
     }
-    rx_line[i++] = 0;
+    rx_line_[i++] = 0;
     // End Critical Section
     NVIC_EnableIRQ(device_irqn);
-    strncpy(c,rx_line,i);
+    strncpy(c,rx_line_,i);
 }
 
 // Interupt Routine to read in data from serial port
@@ -92,9 +118,9 @@
     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();
+        rx_buffer_[rx_in_] = LPC_UART1->RBR;
+        rx_in_ = NEXT(rx_in_);
+        rx_sem_.release();
     }
     // led1=0;
 }
@@ -104,11 +130,11 @@
 {
     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);
+    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();
+        tx_sem_.release();
     // led2=0;
 }
\ No newline at end of file
diff -r a4a21e18acd1 -r d3122119f92b buffered_serial.h
--- a/buffered_serial.h	Thu Dec 13 06:53:43 2012 +0000
+++ b/buffered_serial.h	Mon Dec 17 22:35:51 2012 +0000
@@ -1,6 +1,30 @@
 /*
-    Buffered serial 1 - Setup to work with UART1 (p13,p14)
-*/
+ * @file buffered_serial.h
+ * @author Tyler Weaver
+ *
+ * @section LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Buffered serial UART1 - Setup to work with UART1 (p13,p14)
+ *
+ * Uses RTOS to block current thread.
+ */
 
 #ifndef BUFFERED_SERIAL_H
 #define BUFFERED_SERIAL_H
@@ -8,20 +32,37 @@
 #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)
+#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"
-
+/** Buffered serial UART1 - Setup to work with UART1 (p13,p14)
+ *
+ * Uses RTOS to block current thread.
+ */
 class BufferedSerial : public Serial
 {
 public:
-    BufferedSerial(PinName tx, PinName rx);
+    /** Default Constructor
+    * Initialize UART1 - Serial(p13,p14)
+    * Initialize Semaphores
+    * Attach Serial Interrupts
+    */
+    BufferedSerial();
 
-    void send_line(char*);
-    void read_line(char*);
+    /** Put cstring in buffer/output
+    *
+    * @param cstring to put in buffer for printing (max length = 80 characters)
+    */
+    void put_line(char*);
+    
+    /** Gets a cstring from the buffer/input 
+    *
+    * @param buffer cstring to put line from buffer in (ends at '\n' or 80 characters)
+    */
+    void get_line(char*);
 
 private:
     void Tx_interrupt();
@@ -32,23 +73,23 @@
     
 // 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];
+    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;
+    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];
+    char tx_line_[LINE_SIZE];
+    char rx_line_[LINE_SIZE];
 
     //DigitalOut led1; // debug
     //DigitalOut led2;
     
-    Semaphore rx_sem;
-    Semaphore tx_sem;
+    Semaphore rx_sem_;
+    Semaphore tx_sem_;
 };
 
 #endif
\ No newline at end of file