CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

COM_POWER_ON_TX.h

Committer:
ee12b079
Date:
2016-07-24
Revision:
275:a2f1d544ab8b
Parent:
269:72529b247333

File content as of revision 275:a2f1d544ab8b:

#define STANDBY_PRCS(tm_ptr){\
    Base_tc *stdby_tc = new Long_tc;\
    stdby_tc->next_TC = NULL;\
    PUTshort_or_long(stdby_tc,LONG_TC_CODE);\
    PUTcrc_pass(stdby_tc,0x1);\
    PUTexec_status(stdby_tc,0);\
    stdby_tc->TC_string[0] = 0x01;\
    stdby_tc->TC_string[1] = 0x41;\
    stdby_tc->TC_string[2] = 0x81;\
    stdby_tc->TC_string[3] = 0x40;\
    stdby_tc->TC_string[4] = 0;\
    stdby_tc->TC_string[5] = 0;\
    stdby_tc->TC_string[6] = 0;\
    stdby_tc->TC_string[7] = 0;\
    stdby_tc->TC_string[8] = 0;\
    uint16_t crc16 = crc16_gen(stdby_tc->TC_string, 9);\
    stdby_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
    stdby_tc->TC_string[10] = (crc16 & 0x00FF);\
    for(int i = 11; i < 135; i++){\
        stdby_tc->TC_string[i] = 0;\
    }\
    tm_ptr =  FCTN_CDMS_RLY_TMTC(stdby_tc);\
    delete stdby_tc;\
}

#define SET_BCN_STANDBY(tm_ptr){\
    Base_tc *beacon_tc = new Long_tc;\
    beacon_tc->next_TC = NULL;\
    PUTshort_or_long(beacon_tc,LONG_TC_CODE);\
    PUTcrc_pass(beacon_tc,0x1);\
    PUTexec_status(beacon_tc,0);\
    beacon_tc->TC_string[0] = 0x01;\
    beacon_tc->TC_string[1] = BCN_APID_SOURCE;\
    beacon_tc->TC_string[2] = BCN_SERVICE;\
    beacon_tc->TC_string[3] = 0xE2;\
    beacon_tc->TC_string[4] = 0x01;\
    beacon_tc->TC_string[5] = 0;\
    beacon_tc->TC_string[6] = 0;\
    beacon_tc->TC_string[7] = 0;\
    beacon_tc->TC_string[8] = 0;\
    uint16_t crc16 = crc16_gen(beacon_tc->TC_string, 9);\
    beacon_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
    beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
    for(int i = 11; i < 135; i++){\
        beacon_tc->TC_string[i] = 0;\
    }\
    tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
    delete beacon_tc;\
}

#define P_CDMS_HK_MAIN {\
    Base_tc *hk_main_ptr = new Short_tc;\
    hk_main_ptr->next_TC = NULL;\
    PUTshort_or_long(hk_main_ptr,SHORT_TC_CODE);\
    PUTcrc_pass(hk_main_ptr,0x1);\
    PUTexec_status(hk_main_ptr,0);\
    hk_main_ptr->TC_string[0] = 0x01;\
    hk_main_ptr->TC_string[1] = 0x81;\
    hk_main_ptr->TC_string[2] = 0x81;\
    hk_main_ptr->TC_string[3] = 0x04;\
    hk_main_ptr->TC_string[4] = 0;\
    hk_main_ptr->TC_string[5] = 0;\
    hk_main_ptr->TC_string[6] = 0;\
    hk_main_ptr->TC_string[7] = 0;\
    hk_main_ptr->TC_string[8] = 0;\
    uint16_t crc16 = crc16_gen(hk_main_ptr->TC_string, 9);\
    hk_main_ptr->TC_string[9]  = (crc16 & 0xFF00)>>8;\
    hk_main_ptr->TC_string[10] = (crc16 & 0x00FF);\
    Base_tm *tm_ptr = NULL;\
    tm_ptr = FCTN_CDMS_RLY_TMTC(hk_main_ptr);\
    delete hk_main_ptr;\
    /*DELETE THE TM AFTER USE*/\
    Base_tm *del_tm = tm_ptr;\
    int overCount = 0;\
    while( del_tm != NULL ){\
        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
            Base_tm *temp = del_tm->next_TM;\
            delete del_tm;\
            del_tm = temp;\
            ++overCount;\
        }\
        else{\
            RESET_CDMS;\
            break;\
        }\
    }\
}

#define P_BCN_TX_MAIN(tm_ptr) {\
    Base_tc *bcn_tx_main_ptr = new Long_tc;\
    bcn_tx_main_ptr->next_TC = NULL;\
    PUTshort_or_long(bcn_tx_main_ptr,LONG_TC_CODE);\
    PUTcrc_pass(bcn_tx_main_ptr,0x1);\
    PUTexec_status(bcn_tx_main_ptr,0);\
    bcn_tx_main_ptr->TC_string[0] = 0x01;\
    bcn_tx_main_ptr->TC_string[1] = 0x41;\
    bcn_tx_main_ptr->TC_string[2] = 0x81;\
    bcn_tx_main_ptr->TC_string[3] = 0x07;\
    bcn_tx_main_ptr->TC_string[4] = 0;\
    bcn_tx_main_ptr->TC_string[5] = 0;\
    bcn_tx_main_ptr->TC_string[6] = 0;\
    bcn_tx_main_ptr->TC_string[7] = 0;\
    bcn_tx_main_ptr->TC_string[8] = 0;\
    uint16_t crc16 = crc16_gen(bcn_tx_main_ptr->TC_string, 9);\
    bcn_tx_main_ptr->TC_string[9]  = (crc16 & 0xFF00)>>8;\
    bcn_tx_main_ptr->TC_string[10] = (crc16 & 0x00FF);\
    for(int i = 11; i < 135; i++){\
        bcn_tx_main_ptr->TC_string[i] = 0;\
    }\
    tm_ptr =  FCTN_CDMS_RLY_TMTC(bcn_tx_main_ptr);\
    delete bcn_tx_main_ptr;\
}

void COM_POWER_ON_TX() {
    gPC.puts("Inside COM_POWER_ON_TX\r\n");
    if( !(gFLAGS & COM_TX_FLAG) ){
        gFLAGS = gFLAGS | COM_TX_FLAG;
        gPC.puts("Inside COM_TX_FLAG\r\n");
        gFLAGS = gFLAGS | BAE_SW_EN_FLAG ;
        if( gFLAGS & BAE_SW_EN_FLAG ){
            /*WARNING: INFINITE WHILE LOOP POSSIBLE: if standby ack received and bcn tx main status = 0*/
            bool retryFlag = true;
            while( retryFlag == true ){
                Base_tm *tm_ptr = NULL;
                gPC.puts("setting Bcn off\r\n");
                wait(1);
                SET_BCN_STANDBY(tm_ptr);
                gPC.puts("set Bcn off\r\n");
                uint8_t bcn_main_status = (tm_ptr->TM_string[2] & 0xEF);
                gPC.printf("bcn_main_status = 0x%02X\r\n",bcn_main_status);
                if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
                    /*ACK RECCEIVED*/
                    if( bcn_main_status == 0xA0 ){
                        gPC.puts("A0\r\n");
                        retryFlag = false;
                        RF_SW_CNTRL_TX = 1;
                        Thread::wait(25);
                        RF_SW_CNTRL_TX = 0;
                        gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
                    }
                    else{
                        gPC.puts("diff\r\n");
                        Thread::wait(5000);
                    }
                }
                else{
                    gPC.puts("not A0\r\n");
                    SW_RST_BAE();
                    Base_tm *tm_ptr2 = NULL;
                    gPC.puts("set bcn off\r\n");
                    SET_BCN_STANDBY(tm_ptr2);
                    gPC.puts("bcn off\r\n");
                    uint8_t standbyAck2 = 0;
                    uint8_t bcn_main_status2 = (tm_ptr2->TM_string[2] & 0xEF);
                    if( ( bcn_main_status2 == 0xC0 )||( bcn_main_status2 == 0xA0 ) ){
                        if( bcn_main_status2 == 0xA0 ){
                            gPC.puts("ack = A0\r\n");
                            retryFlag = false;
                            RF_SW_CNTRL_TX = 1;
                            Thread::wait(25);
                            RF_SW_CNTRL_TX = 0;
                            gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);
                        }
                        else{
                            gPC.puts("ack = C0\r\n");
                            Thread::wait(5000);
                        }                           
                    }
                    else{
                        gPC.puts("sw off BAE\r\n");
                        retryFlag = false;
                        SW_OFF_BAE();
                        gPC.puts("swd off BAE\r\n");
                        gFLAGS = gFLAGS | COM_AUTO_POWER_OFF_BAE_FLAG;
                        RF_SW_CNTRL_TX = 1;
                        Thread::wait(25);
                        RF_SW_CNTRL_TX = 0;
                        gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
                    }
                    /*DELETE THE TM AFTER USE*/
                    Base_tm *del_tm = tm_ptr2;
                    int overCount = 0;
                    while( del_tm != NULL ){
                        if( (overCount < TM_OVERFLOW_CONSTANT) ){
                            Base_tm *temp = del_tm->next_TM;
                            delete del_tm;
                            del_tm = temp;
                            ++overCount;
                        }
                        else{
                            RESET_CDMS;
                            break;
                        }
                    }
                }
                /*DELETE THE TM AFTER USE*/
                Base_tm *del_tm = tm_ptr;
                int overCount = 0;
                while( del_tm != NULL ){
                    if( (overCount < TM_OVERFLOW_CONSTANT) ){
                        Base_tm *temp = del_tm->next_TM;
                        delete del_tm;
                        del_tm = temp;
                        ++overCount;
                    }
                    else{
                        RESET_CDMS;
                        break;
                    }
                }
            }
        }
        else{
            gPC.puts("RF_SW_CNTRL_TX\r\n");
            RF_SW_CNTRL_TX = 1;
            Thread::wait(25);
            RF_SW_CNTRL_TX = 0;
            gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
        }
    }
    gPC.puts("swd off BAE\r\n");
    COM_TX_CNTRL = 1;
    uint8_t adfConfigPass = 0xFF;
    for( int i = 0 ; i < COM_TX_CONFIG_LIMIT ; ++i ){
        /*PENDING: CONFIGURE ADF and set adfConfigPass*/
        if( adfConfigPass == 0xFF ){
            break;
        }
    }
    if( adfConfigPass == 0xFF ){
        /*adf successfully configured*/
        gFLAGS = gFLAGS | COM_TX_STATUS_FLAG;
        /*CALL SIGN*/
        Base_tm *power_on_tm = NULL;
        Base_tm *power_on_tm_head = NULL;
        get_call_sign( power_on_tm_head );
        power_on_tm = power_on_tm_head;
        /*ACK L1*/
        get_ack_l1(power_on_tm->next_TM);
        int overFlowCountADF = 0;
        while( power_on_tm->next_TM != NULL ){
            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){
                power_on_tm = power_on_tm->next_TM;
                ++overFlowCountADF;
            }
            else{
                RESET_CDMS;
                break;
            }
        }
        get_tc_list(power_on_tm->next_TM, 0x00);
        /*Call Sign, ACK_L1, TC_LIST*/
        /*snd_tm.head_pointer(power_on_tm_head);*/
        /*transmit_adf;*/
        uint8_t transmissionPass = 0xFF;
        /*PENDING: get acknowledgement of transmission*/
        /*deleting the telemetry*/
        power_on_tm = power_on_tm_head;
        overFlowCountADF = 0;
        while( power_on_tm != NULL ){
            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){
                Base_tm *temp = power_on_tm->next_TM;
                delete power_on_tm;
                power_on_tm = temp;
                ++overFlowCountADF;
            }
            else{
                RESET_CDMS;
                break;
            }
        }
        if( transmissionPass == 0 ){
            /*NOT Transmitted succesfully*/
            COM_POWER_OFF_TX;
            P_CDMS_HK_MAIN;
            Base_tm *ptr_tm = NULL;
            STANDBY_PRCS(ptr_tm);
            uint8_t standbyACK = 0x00;
            uint8_t bcn_main_status = (ptr_tm->TM_string[2] & 0xEF);
            if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
                standbyACK = 1;
            }
            if(standbyACK == 1){
                uint8_t ackReceived = 0x00;
                Base_tm *tm_ptr = NULL;
                P_BCN_TX_MAIN(tm_ptr);
                uint8_t ackcode = tm_ptr->TM_string[2] & 0xEF;
                if( (ackcode == 0xA0) || (ackcode == 0xC0))
                    ackReceived = 0xFF;
                /*DELETE THE TM AFTER USE*/
                Base_tm *del_tm = tm_ptr;
                int overCount = 0;
                while( del_tm != NULL ){
                    if( (overCount < TM_OVERFLOW_CONSTANT) ){
                        Base_tm *temp = del_tm->next_TM;
                        delete del_tm;
                        del_tm = temp;
                        ++overCount;
                    }
                    else{
                        RESET_CDMS;
                        break;
                    }
                }
                if( ackReceived == 0xFF ){
                    Thread::wait(5000);
                }
            }
            /*DELETE THE TM AFTER USE*/
            Base_tm *del_tm = ptr_tm;
            int overCount = 0;
            while( del_tm != NULL ){
                if( (overCount < TM_OVERFLOW_CONSTANT) ){
                    Base_tm *temp = del_tm->next_TM;
                    delete del_tm;
                    del_tm = temp;
                    ++overCount;
                }
                else{
                    RESET_CDMS;
                    break;
                }
            }
            RESET_CDMS_TC;            
        }
    }
    else{
        /*ADF not configured*/
        gFLAGS = gFLAGS & (~COM_TX_STATUS_FLAG);
        COM_POWER_OFF_TX;
        P_CDMS_HK_MAIN;
        Base_tm *ptr_tm = NULL;
        STANDBY_PRCS(ptr_tm);
        uint8_t standbyACK = 0x00;
        uint8_t bcn_main_status = (ptr_tm->TM_string[2] & 0xEF);
        if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
            standbyACK = 1;
        }
        if(standbyACK == 1){
            uint8_t ackReceived = 0x00;
            Base_tm *tm_ptr = NULL;
            P_BCN_TX_MAIN(tm_ptr);
            uint8_t ackcode = tm_ptr->TM_string[2] & 0xEF;
            if( (ackcode == 0xA0) || (ackcode == 0xC0))
                ackReceived = 0xFF;
            /*DELETE THE TM AFTER USE*/
            Base_tm *del_tm = tm_ptr;
            int overCount = 0;
            while( del_tm != NULL ){
                if( (overCount < TM_OVERFLOW_CONSTANT) ){
                    Base_tm *temp = del_tm->next_TM;
                    delete del_tm;
                    del_tm = temp;
                    ++overCount;
                }
                else{
                    RESET_CDMS;
                    break;
                }
            }
            if( ackReceived == 0xFF ){
                Thread::wait(5000);
            }
        }
        /*DELETE THE TM AFTER USE*/
        Base_tm *del_tm = ptr_tm;
        int overCount = 0;
        while( del_tm != NULL ){
            if( (overCount < TM_OVERFLOW_CONSTANT) ){
                Base_tm *temp = del_tm->next_TM;
                delete del_tm;
                del_tm = temp;
                ++overCount;
            }
            else{
                RESET_CDMS;
                break;
            }
        }
        RESET_CDMS_TC;
    }
    gPC.puts("COMPLETED COM_POWER_ON_TX\r\n");
}