Multi COM Nucleo board test

Dependencies:   mbed

main.cpp

Committer:
CANGuru
Date:
2018-12-20
Revision:
0:c206e1eca99c

File content as of revision 0:c206e1eca99c:

#include "mbed.h"

DigitalOut myled(LED1);
DigitalOut tx2led(PG_0);
DigitalOut tx5led(PG_1);

DigitalOut pd0(PD_0);
DigitalOut pd1(PD_1);
DigitalOut ph0(PH_0);
DigitalOut ph1(PH_1);

Serial pc(USBTX, USBRX); // tx, rx

Ticker second;

RawSerial U8(PE_1,PE_0); // 460K Fast
RawSerial U6(PG_14,PG_9); // 460K Fast
RawSerial U5(PC_12,PD_2); // 460K Fast
RawSerial U3(PB_10,PB_11); // 115K Slow
RawSerial U2(PD_5,PD_6); // 460K Fast

char mess2[100]={"***T2_UART2______*_1____012345________$!"};
char mess5[100]={"***T5_UART5______*__2___012345________$!"};
char mess6[100]={"***T6_UART6______*___3__012345________$!"};
char mess8[100]={"***T8_UART8______*____4_012345________$!"};
char mess3[200]={"***T3_UART3_115k_*_DGPS_0123456789ABCDEF***T3_UART3_115k_*_DGPS_0123456789ABCDEF***T3_UART3_115k_*_DGPS_0123456789ABCDEF***T3_UART3_115k_*_DGPS_0123456789ABCDEF***T3_UART3_115k_*_DGPS_0123456789ABC\r\n"};

unsigned char rx2[100];
unsigned char rx5[100];

unsigned char rcv2[100];
unsigned char rcv5[100];


int u8count;
int u5count;
int u6count;
int u3count;
int u2count;
int uscount;

int u2delay;
int u5delay;

uint16_t csum2;
uint16_t csum5;

uint16_t drop2;
uint16_t drop5;

bool tick8=0;
bool tick5=0;
bool tick6=0;
bool tick3=0;
bool tick2=0;

char *u8pointer;
char *u5pointer;
char *u6pointer;
char *u3pointer;
char *u2pointer;

bool u2ready;
bool u5ready;

bool u2txready;
bool u5txready;

uint32_t basetime; // Base timer in milliseconds

uint32_t lastrx2acttime,lastrx5acttime; // Base timer in milliseconds
uint32_t lastrx2time,lastrx5time; // Base timer in milliseconds

uint32_t tx2time,tx5time; // Base timer in milliseconds

char tempstr[20];

uint8_t rogue2,rogue5;

#define TXRATE 20

void flip(void) {

    if (uscount < 999) 
        {
        uscount++;
        }
        else 
        {
        uscount=0;

        //PPS=1; // 1 for PPS ON
        }
        
    basetime++; //Increment base timer at 1ms rate.

        if (uscount % TXRATE == 0) {
            tx2time=basetime;
            u2txready=1;}

        if (uscount % TXRATE == 10) {
                tx5time=basetime;
                u5txready=1;
                }
                
        if (uscount % 500 ==0){
            //rogue2++;
            //rogue5++;
            }
    
    pd0=!pd0;
}

// UART2 character receive interrupt
void rx2int(void){
static unsigned char *rxpointer;
static unsigned char count;
unsigned char temp;
unsigned char *source,*dest;
    __disable_irq();
    pd1=1;
    
        temp=U2.getc();
        *rxpointer++=temp;
        
        if (temp=='!') {
            *rxpointer=0;
            source=rx2;
            dest=rcv2;
            while(*source)
                *dest++=*source++;
            //strcpy(rcv2,rx2);
            u2ready=1;
            rxpointer=rx2;
            count=0;
            }

    pd1=0;

    __enable_irq();

    }

// UART5 character receive interrupt
void rx5int(void){
static unsigned char *rxpointer;
static unsigned char count;
unsigned char temp;
unsigned char *source,*dest;
    __disable_irq();

    pd1=1;
        temp=U5.getc();
        *rxpointer++=temp;
        if (temp=='!') {
            *rxpointer=0;
            source=rx5;
            dest=rcv5;
            while(*source)
                *dest++=*source++;
//            strcpy(rcv5,rx5);
            u5ready=1;
            rxpointer=rx5;
            }
        pd1=0;
    __enable_irq();

}






int main()
{
char tempstring[20];
uint32_t txtime;  
uint32_t timediff;  
uint32_t tmpValue;
  
    U2.baud(115200);
    U5.baud(115200);    
    pc.baud(460800);

    second.attach(&flip, 0.001); // 1ms ticks

    u2count=40; 
    u2pointer=mess2;
    u5count=40; 
    u5pointer=mess5;
            
    U2.attach(&rx2int,Serial::RxIrq);
    U5.attach(&rx5int,Serial::RxIrq);

    pc.printf("Start\r\n");
    
    rogue2=0;
    rogue5=128;

    while(1) {
        myled = !myled;

// Transmit U2 Message on UART2
        if  (u2txready){
                        u2txready=0;
                        u2count=40;

                        sprintf(tempstr,"%06d",tx2time);

                        mess2[24]=tempstr[0];
                        mess2[25]=tempstr[1];
                        mess2[26]=tempstr[2];
                        mess2[27]=tempstr[3];
                        mess2[28]=tempstr[4];
                        mess2[29]=tempstr[5];
                        
                        if (rogue2==0xff) rogue2=0x40;
                        mess2[14]=rogue2++;
                        //if (mess2[14]=='!')mess2[14]='*';
                                                
                        u2pointer=mess2;

                        tx2led=1;
                        /*
                        while (u2count--)
                            {
                                U2.putc(*u2pointer++);
                            }
                            */
                        U2.printf("%s",mess2);
                        tx2led=0;
                        }

// Transmit U5 Message on UART5
        if  (u5txready){
                        u5txready=0;
                        u5count=40;

                        sprintf(tempstr,"%06d",tx5time);

                        mess5[24]=tempstr[0];
                        mess5[25]=tempstr[1];
                        mess5[26]=tempstr[2];
                        mess5[27]=tempstr[3];
                        mess5[28]=tempstr[4];
                        mess5[29]=tempstr[5];

                        if (rogue5==0xff) rogue5=0x40;
                        mess5[14]=rogue5++;
                        //if (mess5[14]=='!')mess5[14]='*';
                        
                        u5pointer=mess5;
                        tx5led=1;                        
/*
                        while (u5count--)
                            {
                                U5.putc(*u5pointer++);
                            }
*/
                        U5.printf("%s",mess5);
                        tx5led=0;
                    }

//Receive U5 message on U2        
    if (u2ready){
            ph0=1;
            tempstring[0]=rcv2[24];
            tempstring[1]=rcv2[25];
            tempstring[2]=rcv2[26];
            tempstring[3]=rcv2[27];
            tempstring[4]=rcv2[28];
            tempstring[5]=rcv2[29];
            tempstring[6]=NULL;
        
            if (sscanf(tempstring,"%u",&tmpValue) == 1) {
                    txtime = tmpValue;
                    timediff=txtime-lastrx2time;
                    lastrx2acttime=basetime-txtime;
                }
            if (timediff>TXRATE)drop2++;
            pc.printf("_1__TR:%06d Tm:%06d Fl:%06d TD:%06d %03d\t\t%02X\r\n", basetime, txtime, lastrx2acttime, timediff, drop2,rogue2);
            //pc.printf("%s TR:%06d Tm:%06d Fl:%06d TD:%06d %03d\r\n",rcv2, basetime, txtime, lastrx2acttime, timediff, drop2);
                                
            lastrx2time=txtime;        

            u2ready=0;
            ph0=0;
            };

//Receive U2 message on U5
    if (u5ready){
          ph1=1;
            tempstring[0]=rcv5[24];
            tempstring[1]=rcv5[25];
            tempstring[2]=rcv5[26];
            tempstring[3]=rcv5[27];
            tempstring[4]=rcv5[28];
            tempstring[5]=rcv5[29];
            tempstring[6]=NULL;

            if (sscanf(tempstring,"%u",&tmpValue) == 1) {
                    txtime = tmpValue;
                    timediff=txtime-lastrx5time;
                    lastrx5acttime=basetime-txtime;
                }

            if (timediff>TXRATE)drop5++;
            pc.printf("__2_TR:%06d Tm:%06d Fl:%06d TD:%06d    %03d\t%02X\r\n", basetime, txtime, lastrx5acttime, timediff, drop5,rogue5);
            //pc.printf("%s TR:%06d Tm:%06d Fl:%06d TD:%06d %03d\r\n",rcv5, basetime, txtime, lastrx5acttime, timediff, drop5);

            lastrx5time=txtime;        

            u5ready=0;
            ph1=0;
            };
        
    }
}