Jingyuan Dong / Mbed 2 deprecated Part1_sync_with_getTime_Slave

main.cpp

Committer:
guruimage
Date:
2010-12-04
Revision:
0:ae7a710b93e5

File content as of revision 0:ae7a710b93e5:

#include "mbed.h"


 struct timeval
{
 unsigned long long int  tv_sec;        /* Seconds. */
 unsigned long long int tv_usec;    /* Microseconds. */
};
void sync(void);
void getValue(void);
void readTime(void);
void SetupTimer1(void);
void SetupTimer0(void);
void Timer1_IRQHandler(void);
void Timer0_IRQHandler(void);
unsigned long long int atoi1(char *str);
void getTime(struct timeval *tv);
Serial device(p9, p10);
Serial pc(USBTX, USBRX);
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
char* store=new char;
int i=0;
int k=0;
int timeNum=0;
unsigned long long int t1=0;
unsigned long long int t2=0;
unsigned long long int t3=0;
unsigned long long int t4=0;
long long int t11=0;
long long int t22=0;
long long int t33=0;
long long int t44=0;
long long int offset;
char buffRx[100];
int handleTrue = 0;


int main() {
wait(0.001);
for(int i =0;i<100;i++){
  buffRx[i]=NULL;
}
timeval *currentTime = new timeval;

   SetupTimer1();
   
   pc.printf("out of the loop!");
   while(1){
       wait(1);
       sync();      
   }
}
   
void sync(void){
  char temp1[40];
  char temp2[40];
  int numcount=0;

  t1=(unsigned long long int)(LPC_TIM1->TC+4294967296*timeNum);
  device.putc('s');// send s to initialize synchronization.
  device.attach(&getValue);
  if(handleTrue == 1){
        myled1=!myled1;
       handleTrue = 0;
       k=0;
     if(buffRx[0] == 'd'){
          t4=(unsigned long long int)(LPC_TIM1->TC+4294967296*timeNum);
          myled2=!myled2;
        
     }
     for(int i=0; i<40;i++){
           temp1[i]=NULL;
           temp2[i]=NULL;
           }
     if(buffRx[1] == 'm'){
          for(int i=2;buffRx[i]!='f';i++){
              temp1[i-2]=buffRx[i];
              numcount = i;
          }
          t2=atoi1(temp1);
           for(int i=0; i<40;i++){
           temp1[i]=NULL;
           }

          for(int i=numcount+2;buffRx[i]!='n';i++){
              temp2[i-numcount-2]=buffRx[i];
          }
          numcount=0;
          t3=atoi1(temp2);
         
          for(int i=0; i<40;i++){
            temp2[i]=NULL;
          }

          t11=(long long int)t1;
          t22=(long long int)t2;
          t33=(long long int)t3;
          t44=(long long int)t4;
          offset = (long long int)(((t22-t11)-(t44-t33))/2);
           //LPC_TIM1->TC=LPC_TIM1->TC-offset;
       for(int i=0;buffRx[i]!=NULL;i++){
         buffRx[i]=NULL;
      }
          //pc.printf("\r\nOffset is %llu!\r\n",offset);
     }
      }
      pc.printf("\r\nT1: %llu, T2: %llu, T3: %llu T4: %llu timeNum: %d\r\nOffset is %lld!\r\n",t1,t2,t3,t4, timeNum,offset);
          
}

void getValue(void){
  buffRx[k]=device.getc();
  //pc.printf("%c",buffRx[k]);
  if(buffRx[k]=='n'){
      handleTrue = 1;
  }
      k++;
}

void SetupTimer1(void)
{
 LPC_SC->PCONP |= 1 << 2;     // Power on Timer`
 LPC_SC->PCLKSEL0 |= 1 << 4;
 LPC_TIM1->TCR = 0x2;         // Reset and set to timer mode
 LPC_TIM1->CTCR = 0x0;
 LPC_TIM1->PR = 0;            // No prescale
 LPC_TIM1->MR0 = 0xFFFFFFFF;        // Match count for 100mS
 LPC_TIM1->MCR = 3;           // Interrupt and Reset on Match
 LPC_TIM1->TCR = 1;           // Enable Timer

 // Enable the ISR vector
 NVIC_SetVector (TIMER1_IRQn, (uint32_t)&Timer1_IRQHandler);
 NVIC_EnableIRQ(TIMER1_IRQn);
}
void Timer1_IRQHandler(void)
{
 timeNum++;
 LPC_TIM1->IR = 1;
}

void getTime(struct timeval *tv) {
      tv->tv_sec=(unsigned long long int)((LPC_TIM1->TC+4294967296*timeNum-offset)/96000000);
      tv->tv_usec=(unsigned long long int)((LPC_TIM1->TC+4294967296*timeNum-offset)/96000);
}


unsigned long long int atoi1(char *str){
unsigned long long int result=0;
for(int i =0;str[i]!=NULL;i++){
int temp = str[i]-'0';
result=result*10+temp;
}
return result;
}