Serial UART snooper. Connect RX and TX of the UUT to 2 x RX pins on mbed to inspect the traffic in both directions

Dependencies:   MODSERIAL Terminal mbed

Files at this revision

API Documentation at this revision

Comitter:
cbayley
Date:
Thu Aug 02 21:23:04 2012 +0000
Parent:
2:60e6df8211f2
Child:
4:15064c0a5b00
Commit message:
Version 2.6; Got buffering right - none on input and plenty on output. bytes are fed into 'Stripes' on input interrupts and Stripes are queued for output.

Changed in this revision

STQ_TinyQueue.c Show diff for this revision Revisions of this file
STQ_TinyQueue.h Show diff for this revision Revisions of this file
TinyQueue.cpp Show annotated file Show diff for this revision Revisions of this file
TinyQueue.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
stripe.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/STQ_TinyQueue.c	Wed Aug 01 02:17:00 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-#include "STQ_TinyQueue.h"
-
-
-
-
-/**
- * \brief Push an Accessibility Event onto our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c the Event to send to the iphone
- * \return null if Q full,  1 on success
- * \sa Qpop(), Qjump()
- * \ingroup tinyQ
- */
-int Qpush( tinyQ_t* q, void* p )
-{
-   if (q->fill >= Q_SIZE)
-      return 0;
-
-   q->fill++;
-   q->data[q->head++] = p;
-
-   return 1;
-}
-
-/**
- * \brief Push an Accessibility Event onto front of our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c the Event to send to the iphone
- * \return null if Q full, 1 on success
- * \sa Qpush(), Qpop()
- * \ingroup tinyQ
- */
-int Qjump( tinyQ_t* q, void* p)
-{
-   if (q->fill >= Q_SIZE)
-      return 0;
-
-   q->fill++;
-   q->data[--q->tail] = p;
-
-   return 1;
-}
-
-/**
- * \brief Pop an Accessibility Event off our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c pointer to receive the Event
- * \return null if Q empty, 1 on success
- * \sa Qpop(), Qjump()
- * \ingroup tinyQ
- */
-int Qpop( tinyQ_t* q, void **p)
-{
-   if (! q->fill )
-      return 0;
-
-   q->fill--;
-   *p = q->data[q->tail++];
-
-   return 1;
-}
-
-/* @} */
\ No newline at end of file
--- a/STQ_TinyQueue.h	Wed Aug 01 02:17:00 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-#ifndef TINYQ_H
-#define TINYQ_H
-
-/** \defgroup tinyQ Tiny Queue
- * Tiny Queues implements a 4 byte queue for iPhone events with just 1 byte of management overhead.
- * Head and Tail pointers both inherently wrap at the end of the buffer and no bounds checking is 
- * necesary IFF the buffer size is set as a power of 2 equal to the width of the pointers !!
- * i.e head is 2 bits wide so the buffer must be 2^2 = 4 bytes deep.
- * @{
- */
-/** NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head ) */
-#define Q_SIZE 8
-
-/** a tinyQ tracks a 8 * queue with just 1 byte overhead */
-typedef struct
-{
-    unsigned char head:3;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
-    unsigned char tail:3;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
-    unsigned char fill:4;  ///< Must be 1 bit bigger than the head and tail pointers
-    void* data[Q_SIZE];     ///< NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head )
-}tinyQ_t;
-
-
-/**
- * \brief Push an Accessibility Event onto our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c the Event to send to the iphone
- * \return null if Q full,  1 on success
- * \sa Qpop(), Qjump()
- * \ingroup tinyQ
- */
-int Qpush( tinyQ_t* q, void* p );
-
-/**
- * \brief Push an Accessibility Event onto front of our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c the Event to send to the iphone
- * \return null if Q full, 1 on success
- * \sa Qpush(), Qpop()
- * \ingroup tinyQ
- */
-int Qjump( tinyQ_t* q, void* p );
-
-/**
- * \brief Pop an Accessibility Event off our queue for sending to the iphone
- * \param q pointer to our tinyQ_t
- * \param c pointer to receive the Event
- * \return null if Q empty, 1 on success
- * \sa Qpop(), Qjump()
- * \ingroup tinyQ
- */
-int Qpop( tinyQ_t* q, void ** p);
-
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TinyQueue.cpp	Thu Aug 02 21:23:04 2012 +0000
@@ -0,0 +1,63 @@
+#include "TinyQueue.h"
+
+
+
+
+/**
+ * \brief Push an Accessibility Event onto our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full,  1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpush( tinyQ_t* q, void* p )
+{
+   if (q->fill >= Q_SIZE)
+      return 0;
+
+   q->fill++;
+   q->data[q->head++] = p;
+
+   return 1;
+}
+
+/**
+ * \brief Push an Accessibility Event onto front of our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full, 1 on success
+ * \sa Qpush(), Qpop()
+ * \ingroup tinyQ
+ */
+int Qjump( tinyQ_t* q, void* p)
+{
+   if (q->fill >= Q_SIZE)
+      return 0;
+
+   q->fill++;
+   q->data[--q->tail] = p;
+
+   return 1;
+}
+
+/**
+ * \brief Pop an Accessibility Event off our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c pointer to receive the Event
+ * \return null if Q empty, 1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpop( tinyQ_t* q, void **p)
+{
+   if (! q->fill )
+      return 0;
+
+   q->fill--;
+   *p = q->data[q->tail++];
+
+   return 1;
+}
+
+/* @} */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TinyQueue.h	Thu Aug 02 21:23:04 2012 +0000
@@ -0,0 +1,54 @@
+#ifndef TINYQ_H
+#define TINYQ_H
+
+/** \defgroup tinyQ Tiny Queue
+ * Tiny Queues implements a 4 byte queue for iPhone events with just 1 byte of management overhead.
+ * Head and Tail pointers both inherently wrap at the end of the buffer and no bounds checking is 
+ * necesary IFF the buffer size is set as a power of 2 equal to the width of the pointers !!
+ * i.e head is 2 bits wide so the buffer must be 2^2 = 4 bytes deep.
+ * @{
+ */
+/** NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head ) */
+#define Q_SIZE 256
+
+/** a tinyQ tracks a 8 * queue with just 1 byte overhead */
+typedef struct
+{
+    unsigned char head:8;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
+    unsigned char tail:8;  ///< the bit depth MUST be that power of 2 that is the Q_SIZE
+    unsigned char fill:9;  ///< Must be 1 bit bigger than the head and tail pointers
+    void* data[Q_SIZE];     ///< NOTE: Q_SIZE MUST be == 2 ^ widthof( tinyQ_t.head )
+}tinyQ_t;
+
+
+/**
+ * \brief Push an Accessibility Event onto our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full,  1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpush( tinyQ_t* q, void* p );
+
+/**
+ * \brief Push an Accessibility Event onto front of our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c the Event to send to the iphone
+ * \return null if Q full, 1 on success
+ * \sa Qpush(), Qpop()
+ * \ingroup tinyQ
+ */
+int Qjump( tinyQ_t* q, void* p );
+
+/**
+ * \brief Pop an Accessibility Event off our queue for sending to the iphone
+ * \param q pointer to our tinyQ_t
+ * \param c pointer to receive the Event
+ * \return null if Q empty, 1 on success
+ * \sa Qpop(), Qjump()
+ * \ingroup tinyQ
+ */
+int Qpop( tinyQ_t* q, void ** p);
+
+#endif
\ No newline at end of file
--- a/main.cpp	Wed Aug 01 02:17:00 2012 +0000
+++ b/main.cpp	Thu Aug 02 21:23:04 2012 +0000
@@ -2,24 +2,22 @@
 
 #include <string>
 #include "mbed.h"
-#include "Terminal.h"
+//#include "Terminal.h"
 #include "MODSERIAL.h"
 #include "stripe.h"
-#include "STQ_TinyQueue.h"
+#include "TinyQueue.h"
 
-#define SHOW_HEX 1
  
-MODSERIAL   pc(USBTX,NC,2048);
+MODSERIAL   pc(USBTX,NC,4096);
 Serial   wt(NC,p10);
 Serial   r8(NC,p14);
-//MODSERIAL   wt(NC,p10,2048);
-//MODSERIAL   r8(NC,p14,2048);
-//Serial    pc(USBTX,NC);
+
 DigitalOut  ledCTS(LED1);
 DigitalOut  ledRTS(LED2);
 //DigitalOut  ledOV(LED4);
 Timeout     toCTSLed;
 Timeout     toRTSLed;
+Timeout     icto;
 /*InterruptIn  nCTS(p11);
 InterruptIn  nRTS(p12);
 */
@@ -29,21 +27,14 @@
 
 #define BAUD (115200)
 
-#define RED     (0xff0000)
-#define LT_RED  (0xffa0a0)
-#define BLUE    (0x0000ff)
-#define LT_BLUE (0xa0a0ff)
-#define GREEN   (0x00ff00)
-#define YELLOW  (0xffff00)
-#define WHITE   (0xffffff)
+
+Stripe * pStripe;
 
-
-#define COLOR_IN_ASC    BLUE
-#define COLOR_IN_HEX    BLUE
-#define COLOR_OUT_ASC   RED
-#define COLOR_OUT_HEX   RED
-
-
+Stripe* newStripe()
+{
+    static char alt;
+    return new Stripe(249+(alt++%2)*4,124,19);
+}
 
 void offCTSLed(void)
 {
@@ -77,71 +68,60 @@
     onRTSLed();
 }
 
+/* flush Stripes after no activity */
+void flush( void )
+{
+        Qpush(&outQ, (void*)pStripe);
+        pStripe = newStripe();
+}
+
 // This function is called when a character goes into the RX buffer.
-void rxCallbackWT(MODSERIAL_IRQ_INFO *q) {
-    //led3 = !led3;
-//    pc.foreground(COLOR_IN_ASC);
-    pc.putc(wt.getc());
+void rxCallbackWT(void) {
+    if ( ! pStripe->inbyte(wt.getc()) )
+    {
+        Qpush(&outQ, (void*)pStripe);
+        pStripe = newStripe();
+    }
+    icto.attach(flush,0.5);
 }
-void rxCallbackR8(MODSERIAL_IRQ_INFO *q) {
-    //led3 = !led3;
-    //pc.foreground(COLOR_OUT_ASC);
-    pc.putc(r8.getc());
+
+void rxCallbackR8(void) {
+    if ( ! pStripe->outbyte(r8.getc()) )
+    {
+        Qpush(&outQ, (void*)pStripe);
+        pStripe = newStripe();
+    }
+    icto.attach(flush,0.5);
 }
   
 int main()
 {
     char c=0;
-    static int alt = 0;
-
-
+    
     pc.baud(BAUD);
     wt.baud(BAUD);
     r8.baud(BAUD);
            
     nCTS.mode(PullUp);
     nRTS.mode(PullUp);
-    /*nCTS.fall(ctsFall);
-    nRTS.fall(rtsFall);
-*/
-
-   //wt.attach(&rxCallbackWT, MODSERIAL::RxIrq);
-   //r8.attach(&rxCallbackR8, MODSERIAL::RxIrq);
+    
+    wt.attach(&rxCallbackWT, Serial::RxIrq);
+    r8.attach(&rxCallbackR8, Serial::RxIrq);
     
     pc.printf("==== Snoop dog ready... ===\n");
     
-    Stripe * pStripe = new Stripe(249,124,19);
+    pStripe = newStripe();
 
     while(1)
     {
-        
-        ledCTS = !nCTS;
-        ledRTS = !nRTS;
+        Stripe * pStripe;
     
-        if (r8.readable() || wt.readable() )
+        if ( Qpop(&outQ,(void**)&pStripe) )
         {
-            if ( r8.readable()  )
-            {
-                c = r8.getc();
-                pStripe->outbyte(c);
-            }
-                 
-            if ( wt.readable()  )
-            {
-                c = wt.getc();
-                pStripe->inbyte(c);
-            }
+            while ( c = pStripe->getc() )
+                pc.putc(c);
             
-            if (!pStripe->advance())
-            {
-                while ( c = pStripe->getc() )
-                    pc.putc(c);
-                
-                delete pStripe;
-                pStripe = new Stripe(249+(alt++%2)*4,124,19);
-            }
-            
-           
+            delete pStripe;
         }
     }
 }
--- a/stripe.cpp	Wed Aug 01 02:17:00 2012 +0000
+++ b/stripe.cpp	Thu Aug 02 21:23:04 2012 +0000
@@ -36,7 +36,7 @@
 
 int Stripe::advance( void )
 {
-    if ( ++ this->wp >= 16 )
+    if ( ++ wp >= 16 )
         return 0;
     else
         return 1;
@@ -44,38 +44,37 @@
 
 int Stripe::inbyte( uint8_t b )
 {
-    sprintf(&this->strip.row[1].hex[this->wp*3], "%02X ", b );
+    sprintf(&strip.row[1].hex[wp*3], "%02X ", b );
     
     if (b >=0x20 && b<=0x80)
     {
-         this->strip.row[1].asc[this->wp] = b;
+         strip.row[1].asc[wp] = b;
     }else{
-         this->strip.row[1].asc[this->wp] = '.';
+         strip.row[1].asc[wp] = '.';
     }
-    return 0;
+    return advance();
 }
 
 int Stripe::outbyte( uint8_t b )
 {
-    sprintf(&this->strip.row[0].hex[this->wp*3], "%02X ", b );
+    sprintf(&strip.row[0].hex[wp*3], "%02X ", b );
     
     if (b >=0x20 && b<=0x80)
     {
-         this->strip.row[0].asc[this->wp] = b;
+         strip.row[0].asc[wp] = b;
     }else{
-         this->strip.row[0].asc[this->wp] = '.';
+         strip.row[0].asc[wp] = '.';
     }
-    return 0;
+    return advance();
 }
 
-uint8_t
-Stripe::getc( void )
+uint8_t Stripe::getc( void )
 {
     char c;
     
-    while ( this->rp < sizeof(this->strip) )
+    while ( rp < sizeof(strip) )
     {
-        c = ((char*)(&this->strip))[this->rp++];
+        c = ((char*)(&strip))[rp++];
         if (c)
             return c;
     }