CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

ThreadsAndFunctions.h

Committer:
shreeshas95
Date:
2015-12-30
Revision:
5:ab276a17ca07
Parent:
4:104dd82c99b8
Child:
6:79d422d1ed42

File content as of revision 5:ab276a17ca07:

// 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_FLAG);
    gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
}

#define reset_all {\
    Base_tc *tcp = gHEAD_NODE_TCL;\
    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*/\
    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
                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 ){
            gPC.puts("NEW TC RECEIVED\r\n");
            gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
            gFLAGS = gFLAGS & (~COM_RX_FLAG);
            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();

            if( gTOTAL_VALID_TC > 0 ){
                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
                    gPC.puts("checking for tc list complete\r\n");
                    uint8_t tempContinue = 0xFF;
                    continueToExecute(tempContinue);
                    if(tempContinue == 0x00){
                        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( true ){
                            gPC.puts("GC 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 ){
                                gPC.puts("EXECUTING TELECOMMANDS\r\n");
                                EXECUTE_OBOSC_ONLY;
                                EXECUTE_TC;
                                gPC.puts("COMPLETED EXECUTION\r\n");
                            }
                            else{
                                gFLAGS = gFLAGS | COM_PA_HOT_FLAG;
                                COM_POWER_OFF_TX;
                                RX1M.attach(&rx_read, Serial::RxIrq);
                                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                            }
                        }
                        else{
                            gPC.puts("GS code mismatch !!\r\n");
                            reset_all;
                            gFLAGS = gFLAGS & (~COM_SESSION_VALIDITY);
                            // PENDING : ENABLE THREADS
                            gSESSION_TIMEOUT.detach();
                            gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
                            // WARNING: clear COM_MNG_TMTC ?
                        }
                    }
                    else{
                        gPC.puts("tc list is not complete\r\n");
                        if(tempContinue & 0x01){
                            gPC.puts("last packet bit not found\r\n");
                        }
                        if( tempContinue & 0x02 ){
                            gPC.puts("missing psc found\r\n");
                        }
                        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);
                        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 ){
                                gPC.printf("%x ", test_tc->TC_string[i]);
                            }
                            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
                    // WARNING: reset_all ? clear com_session ?
                    RX1M.attach(&rx_read, Serial::RxIrq);
                    gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                }
            }
            else{
                gPC.puts("No valid TC recweived\r\n");
                RX1M.attach(&rx_read, Serial::RxIrq);
                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);

                if( !(gFLAGS & COM_TX_FLAG) ){
                    reset_all;
                    // PENDING : ENABLE THREADS
                    gSESSION_TIMEOUT.detach();
                    gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
                }
                // else wait
            }
        }
        else if( gFLAGS & COM_PA_HOT_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();
                COM_POWER_ON_TX;
                EXECUTE_OBOSC_ONLY;
                EXECUTE_TC;
            }
        }
        else{
            // SOME INVALID SIGNAL RECEIVED
            COM_POWER_OFF_TX;
            reset_all;
            // PENDING : ENABLE THREADS
            gSESSION_TIMEOUT.detach();
            gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
        }
    }
}

//void SCIENCE_FUN(void const *args){
////    SPIpayload.format(8,0);
////    SPIpayload.frequency(1000000);
//    for ( int i = 0 ; i < PAYLOAD_BUFFER_LENGTH ; i++ ){
//        gPAYLOAD_BUFFER[i] = 1;
//    }
//
//    // initialise the buffer for dma
//    // SPIpayload.bulkRead_init(VAR_SPACE::payload_dma_buffer, PAYLOAD_DMA_SIZE, &payload_interrupt_fun);
//    // start dma read
//    // SPIpayload.bulkRead_start();
//
//    // attach DMA interrupt
//    while(true){
//         gSCIENCE_THREAD->signal_wait(0x01);
//
////*********************************************************THE TEST
//        
//        //read rtc time later
//        uint64_t RTC_time = 0;
//        Science_Data_Compression::complete_compression( gPAYLOAD_BUFFER , RTC_time);
////        SPI_mutex.lock();
////        disk_write(SDcard_lastWritten ,  5);
////        SPI_mutex.unlock();
//
////**********************************************THE TEST
//
////        counter_for_payload++;
////        if(counter_for_payload == 10){
////         payload_ticker.detach();
////         ledg = 0;
////         PC.puts("its over\r\n");
////        }
//        
////        SPIpayload.bulkRead_start();
//
//    }
//}