Chris BAYLEY / Mbed 2 deprecated UARTSnoop

Dependencies:   MODSERIAL Terminal mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Relay bytes received as SPI slave to PC-USB-Serial
00002 
00003 #include <string>
00004 #include "mbed.h"
00005 //#include "Terminal.h"
00006 #include "MODSERIAL.h"
00007 #include "stripe.h"
00008 #include "TinyQueue.h"
00009 
00010  
00011 MODSERIAL   pc(USBTX,NC,4096);
00012 Serial      wt(NC,p10);
00013 Serial      r8(NC,p14);
00014 
00015 DigitalOut  ledCTS(LED1);
00016 DigitalOut  ledRTS(LED2);
00017 //DigitalOut  ledOV(LED4);
00018 Timeout     toCTSLed;
00019 Timeout     toRTSLed;
00020 Timeout     icto;
00021 /*InterruptIn  nCTS(p11);
00022 InterruptIn  nRTS(p12);
00023 */
00024 DigitalIn   nCTS(p18);
00025 DigitalIn   nRTS(p19);
00026 tinyQ_t     outQ;
00027 
00028 #define BAUD (115200)
00029 
00030 
00031 Stripe * pStripe;
00032 
00033 Stripe* newStripe()
00034 {
00035     static char alt;
00036     return new Stripe(249+(alt++%2)*4,124,19);
00037 }
00038 
00039 void offCTSLed(void)
00040 {
00041     ledCTS = 0;
00042 }
00043 
00044 void onCTSLed(void)
00045 {
00046     ledCTS = 1;
00047     toCTSLed.attach(offCTSLed,0.1);
00048 }
00049 
00050 void offRTSLed(void)
00051 {
00052     ledRTS = 0;
00053 }
00054 
00055 void onRTSLed(void)
00056 {
00057     ledRTS = 1;
00058     toRTSLed.attach(offRTSLed,0.1);
00059 }
00060 
00061 void ctsFall(void)
00062 {
00063     onCTSLed();
00064 }
00065 
00066 void rtsFall(void)
00067 {
00068     onRTSLed();
00069 }
00070 
00071 /* flush Stripes after no activity */
00072 void flush( void )
00073 {
00074     Qpush(&outQ, (void*)pStripe);
00075     pStripe = newStripe();
00076 }
00077 
00078 // This function is called when a character goes into the RX buffer.
00079 void rxCallbackWT(void) {
00080     if ( ! pStripe->inbyte(wt.getc()) )
00081     {
00082         Qpush(&outQ, (void*)pStripe);
00083         pStripe = newStripe();
00084     }
00085     icto.attach(flush,0.5);
00086 }
00087 
00088 void rxCallbackR8(void) {
00089     if ( ! pStripe->outbyte(r8.getc()) )
00090     {
00091         Qpush(&outQ, (void*)pStripe);
00092         pStripe = newStripe();
00093     }
00094     //icto.attach(flush,0.5);
00095 }
00096   
00097 int main()
00098 {
00099     char c=0;
00100     
00101     pc.baud(BAUD);
00102     wt.baud(BAUD);
00103     r8.baud(BAUD);
00104            
00105     //nCTS.mode(PullUp);
00106     //nRTS.mode(PullUp);
00107     
00108     
00109     wt.attach(&rxCallbackWT, Serial::RxIrq);
00110     r8.attach(&rxCallbackR8, Serial::RxIrq);
00111     
00112     pc.printf("==== Snoop dog ready... ===\n");
00113     
00114     pStripe = newStripe();
00115 
00116     while(1)
00117     {
00118         Stripe * pStripe;
00119     
00120         if ( Qpop(&outQ,(void**)&pStripe) )
00121         {
00122             while ( c = pStripe->getc() )
00123                 pc.putc(c);
00124             
00125             delete pStripe;
00126         }
00127     }
00128 }