RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Revision:
20:5db9b77b38a6
Parent:
19:e53cdee9a33c
Child:
21:66459cb32ce0
--- a/main.cpp	Mon Jan 02 06:14:27 2017 +0000
+++ b/main.cpp	Mon Jan 02 17:32:47 2017 +0000
@@ -20,22 +20,22 @@
 Serial xx(PC_10, PC_11); // debug((((( port - use an additional USB serial port to monitor this
 
 // the second #define below gets rid of all the debug printfs
-#define debug(x) xx.prinf x
+#define debug(x) xx.printf x
 //#define debug(x) {}
 
 DigitalOut led1(LED1);
 
 #define FRAME_7E (0x7e)
 #define BUFLEN (1<<14)
-volatile char rxbuf[BUFLEN];
+char rxbuf[BUFLEN];
 char frbuf[6000]; // buffer for ppp frame
 
 struct {
     int online; 
     struct {
-        volatile char * buf;
+        char * buf;
         volatile int head; 
-        int tail; 
+        volatile int tail; 
         int total;
     } rx; // serial port buffer
     struct {
@@ -56,13 +56,10 @@
 void rxHandler() // serial port receive interrupt handler
 {
     while ( pc.readable() ) {
+        int hd = (ppp.rx.head+1)&(BUFLEN-1); // increment/wrap
+        if ( hd == ppp.rx.tail ) break; // watch for buffer full
         ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in rx buffer
-        int hd = (ppp.rx.head+1)&(BUFLEN-1); // increment/wrap
-        if ( hd != ppp.rx.tail ) { // watch for buffer full
-            __disable_irq();
-                ppp.rx.head = hd; // update head pointer
-            __enable_irq();
-        }
+        ppp.rx.head = hd; // update head pointer
     }
 }
 
@@ -74,17 +71,21 @@
 
 int pc_readable() // check if buffer has data
 {
-    return (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ;
+    __disable_irq(); // critical section start
+        int readable = (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ; 
+    __enable_irq(); // critical section end
+    return readable;
 }
 
 int pc_getBuf() // get one character from the buffer
 {
-    if (ppp.rx.head!=ppp.rx.tail) {
+    if (pc_readable) {
         int x = ppp.rx.buf[ ppp.rx.tail ];
-        ppp.rx.tail=(ppp.rx.tail+1)&(BUFLEN-1);
+        __disable_irq(); // critical section start
+            ppp.rx.tail=(ppp.rx.tail+1)&(BUFLEN-1);
+        __enable_irq(); // critical section end
         return x;
-    }
-    return -1;
+    } else return -1;
 }
 
 void scanForConnectString(); // scan for connect attempts from pc