cdms_i2c_hardware_test

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of standaloneworkingi2c_cdms by Team Fox

main.cpp

Committer:
prasanthbj05
Date:
2016-07-06
Revision:
160:25a01d8da5d4
Parent:
159:390ca9b41d0e

File content as of revision 160:25a01d8da5d4:

// TESTING PUSH PULL IN MAIN CPP

#include "mbed.h"

uint8_t rcv_isr = 0; // flag for interrupt
extern uint8_t temp_flag =0;

#define DEBUG 1
#define SDCARD 0
#define I2C_PL 0
#define PRINT 1


#include "SimpleDMA.h"
#include "dmaSPIslave.h"
#include "rtos.h"
#include "mbed_debug.h"
#include "cassert"

#include "Structures.h"
#include "pinconfig.h"
#include "DefinitionsAndGlobals.h"
#include "crc.h"
#include "i2c.h"
#include "COM_SND_TM_functions.h"
#include "COM_SND_TM.h"
#include "cdms_sd.h"
#include "CDMS_HK.h"
#include "OBSRS.h"
#include "adf.h"
#include "COM_RCV_TC.h"
#include "COM_MNG_TMTC.h"
#include "COM_POWER_ON_TX.h"
#include "COM_POWER_OFF_TX.h"
#include "Compression.h"
#include "ThreadsAndFunctions.h"
//#include <InterruptIn.h>

InterruptIn temp_irpt(PTC6);
Mutex i2c_mutex;

uint8_t TM[134];
 //uint8_t TC[11] = {0x01, 0x60, 0x81, 0x34, 0x01, 0x00, 0x00, 0x00, 0x00, 0xee, 0x0d};
uint8_t TC[250];


#define PL_TC(tm_ptr){\
    Base_tc *beacon_tc = new Long_tc;\
    beacon_tc->next_TC = NULL;\
    PUTshort_or_long(beacon_tc,SHORT_TC_CODE);\
    PUTcrc_pass(beacon_tc,0x1);\
    PUTexec_status(beacon_tc,0);\
    beacon_tc->TC_string[0] = 0x01;\
    beacon_tc->TC_string[1] = 0x60;\
    beacon_tc->TC_string[2] = 0x81;\
    beacon_tc->TC_string[3] = 0x21;\
    beacon_tc->TC_string[4] = 0x00;\
    beacon_tc->TC_string[5] = 0;\
    beacon_tc->TC_string[6] = 0;\
    beacon_tc->TC_string[7] = 0;\
    beacon_tc->TC_string[8] = 0;\
    beacon_tc->TC_string[9] = 0x1d;\
    beacon_tc->TC_string[10] = 0xd9;\
    for(uint8_t z=11;z<135;z++){beacon_tc->TC_string[z] = 0;}\
    tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
}


//void set_sig(){gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);}

/*void verifyHK_DATA()
{
    /*hk_verify[0]=TM[26];
    hk_verify[1]=TM[27];
    hk_verify[2]=TM[28];
    hk_verify[3]=TM[29];
   
   //float input=5.4332;
        uint8_t output[4];
    uint8_t output1[4];
   // assert(sizeof(float) == sizeof(uint32_t));
   // uint32_t* temp = reinterpret_cast<uint32_t*>(&input);
 
    //float* output1 = reinterpret_cast<float*>(temp);

   // printf("\n\r %f  ", input);
   // std::cout << "\n\r uint32 \t"<<*temp << std::endl;
 
  /*  output[0] =(uint8_t )(((*temp)>>24)&0xFF);
    output[1] =(uint8_t ) (((*temp)>>16)&0xFF);
    output[2] =(uint8_t ) (((*temp)>>8)&0xFF); 
    output[3] =(uint8_t ) ((*temp) & 0xFF);           // verify the logic 
    */
    //printf("\n\r inside %d %d %d %d", output[3],output[2],output[1],output[0]);
    //std:: cout << "\n\r uint8  inside " << output[3] << '\t' << output[2] << '\t' << output[1] << '\t' << output[0] <<std::endl; 
    
    /*convertion back or recheck code
    
    output[0] =TM[42];
    output[1] =TM[43];
    output[2] =TM[44]; 
    output[3] =TM[45];           // verify the logic 
  
   //printf("\n\rthe value is 26\t %x\n",TM[38]);
     //                       printf("\n\rthe value is  27\t%x\n",TM[39]);
       //                      printf("\n\rthe value is 28\t%x\n",TM[40]);
         //                     printf("\n\rthe value is 29\t%x\n",TM[41]); 

  
    
    uint32_t input_stage1=0x00000000;
    output1[0]=(uint32_t)(output[0]);
    output1[1]=(uint32_t)(output[1]);
    output1[2]=(uint32_t)(output[2]);
    output1[3]=(uint32_t)(output[3]);
    
    //input_stage1=output[3]+(output[2]*(0x100))+(output[1]*(0x10000))+(output[0]*(0x1000000));
    input_stage1=(output1[0]<<24) | (output1[1]<<16) | (output1[2]<<8) | (output1[3]);
    
    
    assert(sizeof(float) == sizeof(uint32_t));
    float* temp1 = reinterpret_cast<float*>(&input_stage1);    
    printf("\n\r the value is: %f \n",*temp1);
   
   
//___________________________________________________-   
   /*
    float input =5.4367;
    //uint32_t tem;
    uint8_t Tmm[4];
        assert(sizeof(float) == sizeof(uint32_t));
    uint32_t* tem = reinterpret_cast<uint32_t*>(&input);

    Tmm[0] =(uint8_t )(((*tem)>>24)&0xFF);
    Tmm[2] =(uint8_t ) (((*tem)>>16)&0xFF);
    Tmm[1] =(uint8_t ) (((*tem)>>8)&0xFF);
    Tmm[3] =(uint8_t ) ((*tem) & 0xFF); 
    
    
    uint8_t output1[4];    
    //uint32_t input_stage1=0x00000000;
    output1[0]=(uint32_t)(Tmm[0]);
    output1[1]=(uint32_t)(Tmm[1]);
    output1[2]=(uint32_t)(Tmm[2]);
    output1[3]=(uint32_t)(Tmm[3]);
    
    
    //input_stage1=output[3]+(output[2]*(0x100))+(output[1]*(0x10000))+(output[0]*(0x1000000));
    uint32_t input_stage1=(output1[0]<<24) | (output1[1]<<16) | (output1[2]<<8) | (output1[3]);
    
    
    //assert(sizeof(float) == sizeof(uint32_t));
    float* value = reinterpret_cast<float*>(&input_stage1);
    printf("\n\rthe actual value: %f\n",input);
    printf("\n\r the value is: %f \n",&value);
    
}*/

uint8_t test[10] = {0};
Thread *ptr_t_i2c;
void test_thread(void const * args)
{
    while(1)
    {
        //pc.printf("\n\n\rWaiting");
        Thread::signal_wait(0x4);   //signalled by ISR when CDMS sends an interrupt
        gPC.printf("\n\rThread started");
        for(uint8_t z=0;z<10;z++)
            test[z] = 0x55;
        //while(i2c_mutex.trylock()==false)
        //{
            gPC.printf("\n\rThread 1 waiting for i2c_mutex");
            i2c_mutex.lock();
        //}
        gPC.printf("\n\rMutex locked by Thread 1");
        write_ack = FCTN_I2C_WRITE((char*)TC,930);
        wait(1); //should poll instead of wait
        if(write_ack==0) 
        {
            if(BAE_I2C_GPIO == 1)
            {              
                read_ack = FCTN_I2C_READ((char*)TM,135);
                //gPC.printf("\n\rExited  read");
                //pdir2
                //if(read_ack==1)
            }
            else
            {
                #if PRINT
                gPC.printf("\nbae_INTR NOT HIGH");
                gPC.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                #endif 
                I2C_busreset();
                #if PRINT
                gPC.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                #endif
                //I2C_reInit();                
                //if(q==2)
                //debug();
                //else 
                //debug3();
            }
            write_ack=1;
        }
        i2c_mutex.unlock();
        gPC.printf("\n\rMutex Unlocked by Thread 1");
    }
}
//uint8_t buffer[512];
void temp_isr()
{
    //wait_us(300);
    if(temp_flag ==1)
    {
        temp_flag |= 0x08;
        temp_flag |= 0x04;
        temp_flag |= 0x02;
        temp_flag |= 0x01;
    }
    FCTN_CDMS_INIT_RTC();
    //for(int i=100;i<1000;i++){temp_flag ++;}
    ptr_t_i2c->signal_set(0x4);
}

void blink2(float delay)
{    
    for(uint8_t i=0;i<20;i++)
    {
        gLED2 = !gLED2;
        wait(delay);
    }
    gLED2 = 0;
}
void blink3(float delay)
{    
    for(uint8_t i=0;i<20;i++)
    {
        gLED3 = !gLED3;
        wait(delay);
    }
    gLED3 = 0;
}
void blink4(float delay)
{    
    for(uint8_t i=0;i<20;i++)
    {
        gLED4 = !gLED4;
        wait(delay);
    }
    gLED4 = 0;
}
int main()
{
    CDMS_I2C_GPIO = 0;   
    
    
    //gLEDR = 1;
    
    // ******************INITIALISATIONS START******************
    // COM RX
    RX1M.baud(1200);
    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;
//    gRX_CURRENT_PTR = gRX_CURRENT_DATA_NODE->values;
    RX1M.attach(&rx_read, Serial::RxIrq);
    
    //#if I2c
    //I2C to Payload    (depends on pl interrupt design)
    //PYLD_I2C_Int.rise(&isr_pyldtm);
    
    //#endif
    master.frequency(100000);
    // DEBUG
    
    gPC.baud(9600);
    gPC.puts("\n\rwelcome to mng_tm_tc\r\n");
    // COMMON SPI
   // spi.format(8,0);
//    spi.frequency(1000000);
//
//    // SD CARD
    cs_sd = 1;
    gCS_RTC = 1;
    gCS_ADF = 1;
//    
//    //FCTN_CDMS_INIT_RTC();/* rtc initialization*/
//    #if SDCARD
    
//    #endif
//    
//    #if DEBUG
//    gPC.puts("welcome to mng_tmtc\r\n");
//    #endif
//    
//    // COM_MNG_TMTC THREAD
//    gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);
//    gCOM_MNG_TMTC_THREAD->set_priority(osPriorityAboveNormal);
//    #if DEBUG
//    gPC.puts("allocating threads\r\n");
//    #endif
//    #if SDCARD
//    gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
//   // gPC.puts("step one complete\r\n");
//    gSCIENCE_THREAD->set_priority(osPriorityBelowNormal);
//    #endif
//    
//    #if DEBUG
//    gPC.puts("competed allocating threads\r\n");
//    #endif
//      
    // *******************INITIALISATIONS END********************
    
    //RtosTimer gCDMS_HK_TIMER(FCTN_CDMS_HK_MAIN, osTimerPeriodic);
    //gCDMS_HK_TIMER.start(5000);
    
    ptr_t_i2c = new Thread(test_thread);
    ptr_t_i2c->set_priority(osPriorityRealtime);
    
    temp_irpt.enable_irq();
    temp_irpt.mode(PullUp);
    temp_irpt.fall(&temp_isr);
    uint8_t q=0;
    
    uint8_t TC[135];
    for(uint8_t z=0;z<135;z++)
        TC[z] = 0x55;
    
    /*int arr2[135][3];
    for(uint8_t z=0;z<135;z++)
    {
        arr2[z][0]= 135;
        arr2[z][1]=1;
        arr2[z][2]=135 ;
    }*/
    //uint8_t hk_verify[4];
    int arr1[44][3]={
                            {135,5,134},
                            {135  ,1,134},//11,137
                            {135  ,1,134},//14,200
                            {135  ,1,134},//1,142
                            {20  ,1,134},//1,135
                            {20  ,1,135},//11,135
                            {1  ,1,135},//1,135
                            {14  ,1,200},//14,200
                            {1   ,1,200},//1,200
                            {135  ,1,134},//0,135
                            {1   ,1,128},//1,128
                            {51  ,1,135},//51,135
                            {11  ,1,134},//11,135
                            {0  ,1,215},//0,215
                            {1   ,1,135},//1,135
                            {1   ,1,35},//1,35
                            {11   ,1,134},
                            {14  ,1,55},//14,55
                            {11  ,1,134},
                            {11  ,1,134},
                            {11 ,1,135},
                            {130 ,1,235},//130,235
                            {130 ,1,35},//130,35
                            {1   ,1,134},//1,135
                            {0   ,1,1},//0,1
                            {11  ,1,135},
                            {11  ,1,135},
                            {11  ,1,135},
                            {0   ,1,134},//0,135
                            {11  ,1,135},
                            {11  ,1,135},
                            {11  ,1,135},
                            {1   ,1,134},//1,135
                            {0  ,1,145},//0,145
                            {5   ,1,15},//5,15
                            {14  ,1,134},//14,135
                            {12  ,1,134},//2,135
                            {86  ,1,165},//86,165
                            {130 ,1,5},//130,5
                            {230 ,1,1},//230,1
                            {130 ,1,135},//130,135
                            {11  ,1,134},
                            {11  ,1,155},
                            {11  ,1,134}
                        };
    //gPC.printf("\n\n\rInitial values:\n");
    //debug();
    //debug2();
    //gPC.printf("\n\r SIM->SCGC4 = 0x%08X",SIM->SCGC4);
    Timer t;
    //gPC.printf("\n\n\rPTE->DIR = 0x%08X",PTE->PDIR);           
    gPC.printf("\n\n\rSending tc");
    
    Base_tm* tm_ptr = new Long_tm;
    PL_TC(tm_ptr);
    
    //gPC.printf("\n\rPORTE->PCR[24] = 0x%08X",PORTE->PCR[24]);
    //gPC.printf("\n\rPORTE->PCR[25] = 0x%08X\n",PORTE->PCR[25]);
    
    bool write_ack=1,read_ack=1;    
    wait(5);
    while(q<1)
    {
        //printf("\n\rSize:%d\t\tdelay:%d",arr[q][0],arr[q][1]);        
        wait(arr1[q][1]);
        //gPC.printf("\n\rIndex = %d",q);
        //while(i2c_mutex.trylock()==false)
        //{
            //gPC.printf("\n\rMain waiting for mutex");
            i2c_mutex.lock();            
        //}
        //gPC.printf("\n\n\rMutex locked by Main");
        gPC.printf("\n\rGiven TC Size : %d",arr1[q][0]);
        gPC.printf("\n\rGiven TM Size : %d",arr1[q][2]);
        //wait_ms(1200);
        write_ack = FCTN_I2C_WRITE((char*)TC,arr1[q][0]);
        wait(1); //should poll instead of wait
        if(write_ack==0) 
        {
            blink2(1);
            if(BAE_I2C_GPIO == 1)
            {              
                read_ack = FCTN_I2C_READ((char*)TM,arr1[q][2]);
                if(read_ack == 0)
                    blink3(1);
                else if(read_ack ==1)
                    blink3(0.3);
            }
            else
            {
                #if PRINT
                gPC.printf("\nbae_INTR NOT HIGH");
                gPC.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                #endif 
                I2C_busreset();
                #if PRINT
                gPC.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                #endif
            }
            write_ack=1;
        }
        else if (write_ack ==1)
            blink2(0.3);
        i2c_mutex.unlock();
        //gPC.printf("\n\rMutex Unlocked by Main");
        //gPC.printf("\n\Flag = %d",temp_flag);
        temp_flag=0;
        q++;       
    } 
    //wait(5);
    /*while(1)
    {
        write_ack = FCTN_I2C_WRITE((char*)TC,arr1[0][0]);        
        wait_us(1);
    }*/
              
    //debug();
    //debug3();
    //debug();
    //master.stop();
    //wait_ms(2);
    //debug();
    /*while(q<5)
    {
        //printf("\n\rSize:%d\t\tdelay:%d",arr[q][0],arr[q][1]);
        wait(10);
        FCTN_I2C_WRITE((char*)TC,11);
        wait(0.2); 
        if(BAE_I2C_GPIO == 1)
        {            
           // printf("receiving...\r\n");
            FCTN_I2C_READ((char*)TM,arr1[q][2]);                       
        }
        else
        {
            gPC.printf("\nbae_INTR NOT HIGH");
            I2C_reInit();
        }
        q++;
    } 
    */
    while(true){
        Thread::wait(osWaitForever);
        //state = gCOM_MNG_TMTC_THREAD->get_state() + '0';
         gLED1 = !gLED1;
         //wait(1);
        //gPC.putc(state); 
    }
}