PYLD_CDMS

Dependencies:   mbed

Fork of PLYD_CDMS24 by Siva ram

main.cpp

Committer:
Piasiv1206
Date:
2016-02-27
Revision:
6:090151fca074
Parent:
5:f22f960f804b

File content as of revision 6:090151fca074:



// Payload uC (Tiva) -- > SPI master
// CDMS uC (Freescale) --> SPI slave
// packet length = 32*16 bits (32 bins --> each 2 bytes)
 
#include "mbed.h"
#include "MKL46Z4.h"
//int f;
void FUNC_MASTER_WRITE(void);
const int addr = 0x20<<1;
const int addr1 = (0x20<<1|0);
const int addr2 = (0x20<<1|1);
//Timer Siv ;
int FLAG = 1 ;
I2C master (D14,D15);
InterruptIn PYLD_I2C_Int(PTA13);
//gitalIn test(PTA13);
SPISlave device(PTD6, PTD7, PTD5,PTD4 ); // mosi, miso, sclk, ssel --> using SPI1
InterruptIn PYLD_SPI_Interrupt(PTD2);
DigitalOut flash(LED4);
int payloadBins = 1024;
float Stability_delay = 0.005228 ;// 0.005228
#define CRC16                   0x8010

uint16_t CRC_16_Calc(const char *data, uint16_t size);

Serial pc(USBTX, USBRX); // tx, rx  --> serial feedback for debug

void payloadProcess() {
  
   flash = !flash;
    //wait(Stability_delay); // wait after interrupt for stability
    unsigned short scienceRawPacket[payloadBins];
    
    for(int i=0; i<payloadBins;i++){
       
        
        
        while(!device.receive()); // blocking statement --> waiting for data from Payload
        scienceRawPacket[i] = device.read();   // Read byte from master

    }
    // serial feedback
    pc.printf("Packet recieved\r\n");
    for(int i=0;i<payloadBins;i++){
        pc.printf("%d\t",scienceRawPacket[i]);
        scienceRawPacket[i] = 0;
    }
    pc.printf("\r\n");
 
} 
int dt3,pia = 0;
bool f = 1;
char rdata[140],ta2;
int loop=1;
char *writedata = new char;
char data = 1,data1 = 2, data2 = 3,  test[136] ; 
char pia1[13] , siv[13] ;
void readds()
{   

               //wait(Stability_delay);
    flash = !flash;
    

     
             //while(!f)
             //{    
               f=  master.read(addr1,siv,13);
             //}
    // master.start();
        //for(int i = 0;i<26;i++)
           //{
     
           //     master.write(addr2);
                
             //   rdata[i] = master.read(1);
                //wait_us(20);

    
    //        }
     
   //  master.stop();
 /*
     for(pia = 0 ;pia<26;pia ++)
      {
               master.read(addr1,&rdata[pia],1); 
    }
     */        
              // dt3 = master.read(1);
               
               
}   
 


void INIT_TC_TEMP (void)
{
    
    uint16_t CRC_VALUE = 0 ;
    
    pia1[0]    = 123  ;
    pia1[1]    = 0xff ;
    pia1[2]    = 0x81 ;
    pia1[3]    = 0xD0 ;
    pia1[4]    = 0x00 ;
    pia1[5]    = 0x00 ;
    pia1[6]    = 0x00 ;
    pia1[7]    = 0x00 ;
    pia1[8]    = 0x00 ;
    CRC_VALUE = CRC_16_Calc(pia1,9) ;
    pia1[9]    = (char)((CRC_VALUE >> 8) & 0xff);
    pia1[10]   = (char)((CRC_VALUE >> 0) & 0xff);
    
    
}
   
int CHECK_TM (void)
{
        uint16_t CRC_Value = 0 , check = 0;
        CRC_Value = CRC_Value | ((uint16_t) siv[11]) ;
        CRC_Value = CRC_Value << 8;
        CRC_Value = CRC_Value | ((uint16_t) siv[12]) ;
        if (CRC_Value == CRC_16_Calc (siv,11))
        {
            check++;
        }
        else
        {
            return(10);
        }
        if ( siv[1] == 123 )
        {
            check++;
        }
        else
        {
            return (9);
        }
        if ( siv[3] == 160 )
        {
            check ++ ;
        }
        else
        {
            return (8);
        }
        
        FLAG = 0 ;
        return (check) ;
}

uint16_t CRC_16_Calc(const char *data, uint16_t size)
{
    uint16_t CRC_OUT = 0;
    int bits_read = 0, bit_flag;



    while(size > 0)
    {
        bit_flag = CRC_OUT >> 15;

        /* Get next bit: */
        CRC_OUT <<= 1;
        CRC_OUT |= (*data >> (7 - bits_read)) & 1;

        /* Increment bit counter: */
        bits_read++;
        if(bits_read > 7)
        {
            bits_read = 0;
            data++;
            size--;
        }

        /* Cycle check: */
        if(bit_flag)
            CRC_OUT ^= CRC16;

    }
    return CRC_OUT;
}

 
void FUNC_MASTER_WRITE(void)
{  

for (dt3 = 0;dt3 <135 ; dt3++)
{
test [dt3] = dt3 ;
} 
  int data5 = pc.getc();      
      loop=1;
 
 
 master.frequency(100000); 
  *writedata = data1; 
     while(loop)
     {
         
         bool check = (bool)master.write(addr,pia1,11);
        // bool check = (bool)master.write(addr,&test[3],1,false);
         // check = (bool)master.write(addr,&test[4],1);
         
         //wait_us(10);
         
         //check = (bool)master.write(addr,&data1,1);
        // check = (bool)master.write(addr,&data1,1);
         //check = (bool)master.write(addr,&data2,1);
         //bool check = master.write(addr,test,3);
         
        // while(!test);
         
        // readds();
       /*
         master.start();
         master.write(addr1);   
            
        for(  pia = 0 ; pia<2 ; pia++)
        {
            
            master.write(test[pia]);
           //wait_us(2);
            
            
        }
        

        
        
        for(  pia = 2 ; pia<13 ; pia++)
        {
            
            master.write(test[pia]);
           //wait_us(2);
            
            
        }
        master.stop();
        
        */
        /*
        master.start();
        bool check1 = master.write(addr1);
       // wait_us(10);
        bool check2 = master.write(1);
        //wait_us(10);
         check2 = master.write(2);
         master.write(3);
         master.write(4);
         master.write(5);
         master.write(6);
         master.write(7);
         //wait_us(10);
         master.write(8);
         master.write(9);
         master.write(10);
         master.write(11);
         master.write(12);
         master.write(13);
         
         master.stop();
         
         */
         

         loop = 0;
     }
}  

int main() {
    int dt1 = 0,dt2 = 0 ,P= 0;
    device.format(16,0);  // SPI format --> 16 bits, mode = 0
    device.frequency(1000000);
    master.frequency(400000);
       
    pc.printf("Example code demonstarting Payload - CDMS SPI & I2C communication!\r\n");
    PYLD_SPI_Interrupt.rise(&payloadProcess);
    PYLD_I2C_Int.rise(&readds);
    INIT_TC_TEMP();
    
    
    while(1)
    {
    FUNC_MASTER_WRITE();
    wait(1);
    //dt1 = rdata[0] ;
    //dt2 = rdata[1] ;
    for(P = 0 ; P < 13 ; P++ )
    {
    pc.printf("Temperature %d \n\r",siv[P]);
   }
  // while ( FLAG == 1)
  // {}
        pc.printf(" Packect Recieved \n \r");
        
        if( CHECK_TM() == 3 )
        {
            pc.printf(" ACK Recieved \n \r");
        }
        else if ( CHECK_TM() == 10 )
        {
            pc.printf("Error Code 10 : CRC_FAIL \n \r" );
        }
        else if ( CHECK_TM() == 9 )
        {
            pc.printf("Error Code 9 : PSC FAIL \n \r" );
        }
        else if ( CHECK_TM() == 8 )
        {
            pc.printf("Error Code 8 : NACK ACK FAIL \n \r" );
        }
 wait (1) ;
    
     for(P = 0 ; P < 13 ; P++ )
   {
    pc.printf("Temperature %d \n\r",siv[P]);
   }
    
    pc.printf(" Packect Recieved \n \r");
    uint16_t CRC_Value = 0 ;
        CRC_Value = CRC_Value | (siv[11]) ;
        CRC_Value = CRC_Value << 8;
        CRC_Value = CRC_Value | (siv[12]) ;
        
        if (CRC_Value == CRC_16_Calc (siv,11))
        {
            pc.printf("CRC_PASS Temperature of PYLD uC is : %d \n \r",siv[3]);
        }
        else 
        {
            pc.printf("Data CRC_Fail \n \r");
        }
    
   
    pc.printf(" I2C data register %d \n \r", I2C1 -> S);
   // pc.printf("Temperature %d \n\r",dt1);
   // pc.printf("Temperature2 %d \n\r",dt2);
    //pc.printf("Temperature3 %d \n\r",dt3);
    

    }
}