CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

ThreadsAndFunctions.h

Committer:
ee12b079
Date:
2016-03-02
Revision:
135:522f7d73d04f
Parent:
134:4694fc291ce1
Child:
144:4c20fcc105ce

File content as of revision 135:522f7d73d04f:

// UART ISR
void rx_read(){
    gRX_CURRENT_DATA_NODE->values[gRX_COUNT] = RX1M.getc();
    gFLAGS = gFLAGS | UART_INT_FLAG;
    gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
}

// RX_TIMEOUT ISR
void after_receive(){
    gRX_TIMEOUT.detach();
    gFLAGS = gFLAGS | NEW_TC_RECEIVED;
    gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
}

// SESSION_TIMOUT ISR
void after_session(){
    gSESSION_TIMEOUT.detach();
    gFLAGS = gFLAGS | COM_SESSION_TIMEOUT_FLAG;
    if( gMASTER_STATE != TCL_STATE_EXECUTING ){
        gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
    }
}

#define reset_all {\
    Base_tc *tcp = gHEAD_NODE_TCL;\
    if (DEBUG)\
        gPC.puts("inside reset\r\n");\
    gFLAGS = 0x0000;\
    while(tcp != NULL){\
        Base_tc *temp = tcp->next_TC;\
        delete tcp;\
        tcp = temp;\
    }\
    gHEAD_NODE_TCL = NULL;\
    gLAST_NODE_TCL = NULL;\
    gMASTER_STATE = TCL_STATE_INCOMPLETE;\
    gTOTAL_INCORRECT_SIZE_TC = 0;\
    gTOTAL_CRC_FAIL_TC = 0;\
    gTOTAL_REPEATED_TC = 0;\
    gTOTAL_VALID_TC = 0;\
    for(int i = 0 ; i < TM_LONG_SIZE ; ++i){\
        gLAST_TM[i] = 0x00;\
    }\
    gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
    COM_RX_DATA_NODE *dataptr = gRX_HEAD_DATA_NODE;\
    while( dataptr != NULL ){\
        COM_RX_DATA_NODE *temp = dataptr->next_node;\
        delete dataptr;\
        dataptr = temp;\
    }\
    gRX_HEAD_DATA_NODE = new COM_RX_DATA_NODE;\
    gRX_HEAD_DATA_NODE->next_node = NULL;\
    gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;\
    gRX_COUNT = 0;\
    /*PENDING : ALL GLOBAL VAARIABLES AND FLAGS*/\
    if (DEBUG)\
        gPC.puts("finished reset all\r\n");\
}

#define PUT_RAW_BYTE {\
    if( gRX_COUNT < (RX_BUFFER_LENGTH-1) ){\
        ++gRX_COUNT;\
    }\
    else{\
        gRX_COUNT = 0;\
        gRX_CURRENT_DATA_NODE->next_node = new COM_RX_DATA_NODE;\
        gRX_CURRENT_DATA_NODE = gRX_CURRENT_DATA_NODE->next_node;\
        gRX_CURRENT_DATA_NODE->next_node = NULL;\
    }\
    gRX_TIMEOUT.attach(&after_receive, RX_TIMEOUT_LIMIT);\
}

void COM_MNG_TMTC_FUN(void const *args){
    if( !(gFLAGS & COM_SESSION_FLAG) ){
        //gLEDR = 1;
        //gLEDG = 1;
    }
    while(true){
        Thread::signal_wait( COM_MNG_TMTC_SIGNAL_UART_INT );
        if( gFLAGS & UART_INT_FLAG ){
            gFLAGS = gFLAGS & (~UART_INT_FLAG);
            if( !(gFLAGS & COM_SESSION_FLAG) ){
                // PENDING : DISABLE THREADS
                gPAY_SPI.bulkRead_end();
                gFLAGS = gFLAGS | COM_SESSION_FLAG;
                gSESSION_TIMEOUT.attach(&after_session, SESSION_TIME_LIMIT);
                gFLAGS = gFLAGS | COM_RX_FLAG;
                PUT_RAW_BYTE;
                // PENDING : MEASURE RSSI
                if( gFLAGS & COM_SESSION_FLAG ){
                    //gLEDR = 0;
                    //gLEDG = 0;
                }
            }
            else if( gFLAGS & COM_RX_FLAG ){
                //gLEDR = 0;
                //gLEDG = 1;
                PUT_RAW_BYTE;
            }
            else{
                //gLEDR = 1;
                //gLEDG = 0;
                gFLAGS = gFLAGS | COM_RX_FLAG;
                PUT_RAW_BYTE;
            }
        }
        else if( gFLAGS & NEW_TC_RECEIVED ){
            if (DEBUG)
                gPC.puts("NEW TC RECEIVED\r\n");
//          DEBUF_PRINT("NEW TC RECEIVED\r\n");
            Thread::wait(2000);    /*To be removed in flight model*/
            gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
            gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;

            // DISABLE THE RX1M INTERRUPT
            RX1M.attach(NULL);
            
            // VERIFY CRC, REPEATED PSC AND UPDATE TOTAL_VALID_TC, INCORRECT SIZE TC, CRC FAIL TC
            while(gRX_COUNT < (RX_BUFFER_LENGTH)){
                gRX_CURRENT_DATA_NODE->values[gRX_COUNT] = 0x00;
                ++gRX_COUNT;
            }
            gRX_COUNT = 0;
            raw_data_to_tc();
            gFLAGS = gFLAGS & (~COM_RX_FLAG);
            
            if( gTOTAL_VALID_TC > 0 ){
                if (DEBUG)
                    gPC.printf("valid TC rx: %u\r\n", gTOTAL_VALID_TC);
                if( gTOTAL_VALID_TC < COM_MAX_TC_LIMIT ){
                    // CHECK WEATHER TC LIST HAS MISSING TC OR WEATHER LAST FRAME BIT IS HIGH IN THE LAST PSC-TC
                    if (DEBUG)
                        gPC.puts("checking for tc list complete\r\n");
                    uint8_t tempContinue = 0xFF;
                    continueToExecute(tempContinue);
                    if(tempContinue == 0x00){
                        if (DEBUG)
                            gPC.puts("tc list is complete\r\n");
                        // CHECK WEATHER GS VERIFICATION CODE MATCHES
                        uint8_t tempGSver = 0x00;
                        GScodeVerification(tempGSver);
                        /*PENDING: INCLUDE GS VERIFICATION CODE*/
                        if( tempGSver == 0xFF ){
                            if (DEBUG)
                                gPC.puts("GS code match !!\r\n");
                            gFLAGS = gFLAGS | COM_SESSION_VALIDITY;
                            COM_POWER_ON_TX;
                            // PENDING: PA HOT HANDLED IN EXECUTE_XXX FUNCTIONS
                            uint8_t tempPAHot = 0x00;
                            isPAhot(tempPAHot);
                            if( tempPAHot == 0xFF ){
                                gFLAGS = gFLAGS & ~(COM_PA_HOT_FLAG);
                                gCOM_PA_COOLING_TIMEOUT.detach();
                                if (DEBUG)
                                    gPC.puts("EXECUTING TELECOMMANDS\r\n");
//                              DEBUF_PRINT("EXECUTING TELECOMMANDS\r\n");
                                EXECUTE_OBOSC_ONLY;
                                EXECUTE_TC;
                                if (DEBUG)
                                    gPC.puts("COMPLETED EXECUTION\r\n");
                            }
                            else{
                                gFLAGS = gFLAGS | COM_PA_HOT_FLAG;
                                // PENDING: power off transmitter only
                                gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
                                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                                RX1M.attach(&rx_read, Serial::RxIrq);
                            }
                        }
                        else{
                            if(DEBUG)
                                gPC.puts("GS code mismatch !!\r\n");
                            RX1M.attach(&rx_read, Serial::RxIrq);
                            reset_all;
                            gFLAGS = gFLAGS & (~COM_SESSION_VALIDITY);
                            // PENDING : ENABLE THREADS
                            gPAY_SPI.bulkRead_start();
                            gSESSION_TIMEOUT.detach();
                            gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
                            // WARNING: clear COM_MNG_TMTC ?
                        }
                    }
                    else{
                        if (DEBUG)
                            gPC.puts("tc list is not complete\r\n");
                        if(tempContinue & 0x01){
                            if (DEBUG)
                                gPC.puts("last packet bit not found\r\n");
                        }
                        if( tempContinue & 0x02 ){
                            if (DEBUG)
                                gPC.puts("missing psc found\r\n");
                        }
                        if (DEBUG){
                            gPC.printf("crc fail tc = %u\r\n", gTOTAL_CRC_FAIL_TC);
                            gPC.printf("incorrect size tc = %u\r\n", gTOTAL_INCORRECT_SIZE_TC);
                            gPC.printf("repeated psc = %u\r\n", gTOTAL_REPEATED_TC);    
                            gPC.puts("\r\n");
                        }
                        Base_tc *test_tc = gHEAD_NODE_TCL;
                        while( test_tc != NULL ){
                            int length = 0;
                            if( GETshort_or_long_tc(test_tc) == SHORT_TC_CODE ){
                                length = TC_SHORT_SIZE;
                            }
                            else{
                                length = TC_LONG_SIZE;
                            }
                            for(int i = 0 ; i < length ; ++i ){
                                int x = 128;
                                for( int j = 7 ; j >= 0 ; --j ){
                                    gPC.printf("%d", (test_tc->TC_string[i] & x)>>j);
                                    x = x/2;
                                }
                            }
                            gPC.puts("\r\n");
                            test_tc = test_tc->next_TC;
                        }
                        //gPC.puts("\r\n");
                        COM_POWER_ON_TX;
                        // PENDING : POWER OFF TX
                        RX1M.attach(&rx_read, Serial::RxIrq);
                        gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                    }
                }
                else{
                    COM_POWER_ON_TX;
                    // POWER OFF TX transmitter only
                    // WARNING: reset_all ? clear com_session ?
                    RX1M.attach(&rx_read, Serial::RxIrq);
                    gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                }
            }
            else{
                if (DEBUG)
                    gPC.puts("No valid TC received\r\n");
                RX1M.attach(&rx_read, Serial::RxIrq);
                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                reset_all;
                // PENDING : ENABLE THREADS
                gPAY_SPI.bulkRead_start();
                gSESSION_TIMEOUT.detach();
                gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
            }
        }
        else if( gFLAGS & COM_PA_HOT_FLAG ){
            if (DEBUG)
                gPC.puts("checking for PA hot in main\r\n");
            if( gFLAGS & COM_RX_FLAG ){
                uint8_t tempPA = 0xFF;
                isPAhot(tempPA);
                if( tempPA == 0x00 ){
                    gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
                }
                else{
                    gFLAGS = gFLAGS & ~(COM_PA_HOT_FLAG);
                    gCOM_PA_COOLING_TIMEOUT.detach();
                }
            }
            else{
                uint8_t tempPA = 0xFF;
                isPAhot(tempPA);
                if( tempPA == 0x00 ){
                    gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
                }
                else{
                    /*gPC.puts("PA IS COOLED DOWN\r\n");*/
                    gFLAGS = gFLAGS & (~COM_PA_HOT_FLAG);
                    gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
                    RX1M.attach(NULL);
                    gCOM_PA_COOLING_TIMEOUT.detach();
                    COM_POWER_ON_TX;
                    EXECUTE_OBOSC_ONLY;
                    EXECUTE_TC;
                }
            }
        }
        else if( gFLAGS & COM_SESSION_TIMEOUT_FLAG ){
            if (DEBUG)
                gPC.puts("session timeout: resetting in main\r\n");
            COM_POWER_OFF_TX;
            /*PENDING : ENABLE THREADS*/
            gPAY_SPI.bulkRead_start();
            gSESSION_TIMEOUT.detach();
            gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
            gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
            reset_all;
        }
        else{
            if (DEBUG)
                gPC.puts("INVALID SIGNAL RECEIVED IN MAIN\r\n");
            // SOME INVALID SIGNAL RECEIVED
            COM_POWER_OFF_TX;
            reset_all;
            // PENDING : ENABLE THREADS
            gPAY_SPI.bulkRead_start();
            gSESSION_TIMEOUT.detach();
            gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
        }
    }
}

void payload_isr_fun(){
    gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
}

void SCIENCE_FUN(void const *args){
    
    // initialisation of payload spi
    gPAY_SPI.frequency(1000000);
    gPAY_SPI.format(8,0);
    gPAY_SPI.bulkRead_init(gPAYLOAD_BUFFER, PAYLOAD_BUFFER_LENGTH, &payload_isr_fun);
    gPAY_SPI.bulkRead_start();
     
    while(true){
        gPC.puts("entering science thread\r\n");
        gSCIENCE_THREAD->signal_wait(SCIENCE_SIGNAL);
        gPC.puts("sig_set\r\n");
        srp(gPAYLOAD_BUFFER);
        gPC.puts("exit_srp\r\n");
        gPAY_SPI.bulkRead_start();
    }
}