//EE 202 hm2 //This is a program built for the mbed1(master mbed) in generation mode //This code has been tested and should be function, if you has any problem, //please mail me.

Dependencies:   mbed

Fork of 202hm2_master by Yujing Qian

Revision:
2:1c7e1110ac61
Parent:
1:55f3661a2050
Child:
3:5149e55c21e3
diff -r 55f3661a2050 -r 1c7e1110ac61 main.cpp
--- a/main.cpp	Fri Mar 14 02:04:39 2014 +0000
+++ b/main.cpp	Sun Mar 16 07:17:21 2014 +0000
@@ -10,28 +10,36 @@
 #define BBBbuff_size 30
 
 DigitalOut LED(LED_RED);
+DigitalOut out(PTD4);
+Serial uart_bbb(PTE16,PTE17);
+Serial uart(PTE0, PTE1);
 Serial pc(USBTX,USBRX);
-Serial uart(PTE0, PTE1);
 
 int loop_num=0;
+int per_num=0;
+int Period=500;
 int state=0;
 uint32_t T1=0;
 uint32_t T2=0;
 uint32_t T3=0;
 uint32_t T4=0;
-int delay=0;
+bool flag=false;
+int syc_f=2;
+int syc_interval=1000/syc_f;
+uint32_t syc_cnt=0;
+
 uint32_t Mod=mod;
 char BBBbuffer[BBBbuff_size];
 bool cmd_received=false;
 int BBBbuff=0;
 char buffer[4];
 int buff=0;
-uint32_t TIME(){return loop_num*(TPM0->MOD)+(TPM0->CNT);}
+uint32_t TIME(){return per_num*Period*48000+loop_num*48000+(TPM0->CNT);}
 
 void receive_handler(){
-    while( pc.readable() && BBBbuff<BBBbuff_size){
-        BBBbuffer[BBBbuff] =pc.getc();
-        pc.putc(BBBbuffer[BBBbuff]);
+    while( uart_bbb.readable() && BBBbuff<BBBbuff_size){
+        BBBbuffer[BBBbuff] =uart_bbb.getc();
+        uart_bbb.putc(BBBbuffer[BBBbuff]);
         //pc.printf(& rx_buffer[buff]);
         if (BBBbuffer[BBBbuff] == '#'){
             BBBbuffer[BBBbuff] = '\0';
@@ -45,135 +53,112 @@
         return;
     }
 void TPM1_IRQHandler(void);
-
-void synchronize(uint32_t Cmd_mod){
-    switch(state){
-    case 0: {
-
-        
-        char* tmp=(char*)&Cmd_mod;
-        T1=TIME();
-        pc.printf("T1=%d\n",T1);
-        uart.putc((*tmp));pc.printf("0=%d\n",*tmp);
-        uart.getc();
-        uart.putc((*(tmp+1)));pc.printf("1=%d\n",(*(tmp+1)));
-        uart.getc();   
-        uart.putc((*(tmp+2)));pc.printf("2=%d\n",(*(tmp+2)));
-        uart.getc();  
-        uart.putc((*(tmp+3)));pc.printf("3=%d\n",(*(tmp+3)));
-         pc.printf("T1.5=%d",TIME()); 
-        //pc.printf("T1 done%d\n");
-        state++;
-        break;}//sending a 5 char command
-    case 1:{
+uint32_t calculate_delay(){// calculate delay using hand shake
+            
             
-            buffer[0]=uart.getc();
-            uart.putc(0);
-            buffer[1]=uart.getc();
-            uart.putc(0);
-            buffer[2]=uart.getc();
-            uart.putc(0);
-            buffer[3]=uart.getc();
+            T1=TIME();
+            uart.putc('a');
+            uart.getc();
             T4=TIME();
-            T2=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);         
-            //pc.printf("T2=%d",T2);
-            state++;break;
-        }//receiving t2 from slave
-                   
-    case 2:{
-            buffer[0]=uart.getc();
-            uart.putc(0);
-            buffer[1]=uart.getc();
-            uart.putc(0);
-            buffer[2]=uart.getc();
-            uart.putc(0);
-            buffer[3]=uart.getc();
-            //receiveing t3            
-            T3=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
-            
-            delay= (T4-T3+T2-T1)/2;
-            pc.printf("delay=%d\n",delay);
-            pc.printf("T1=%d\n",T1);
-            pc.printf("T2=%d\n",T2);
-            pc.printf("T3=%d\n",T3);
-            pc.printf("T4=%d\n",T4);
-            //delay=0x0000ffff;
+                        
+            return (T4-T1)/2;           
+    }
+void Syc(uint32_t delay){
+            //pc.printf("delay=%d\n",delay);
             TPM1->MOD=delay;
-            
             TPM1->CNT=0x0;
-            TPM1->SC=0x00000048;
+            //loop_num=0;
+            TPM1->SC=0x000000c8;          
+            NVIC_EnableIRQ(TPM1_IRQn);
+            // open tpm1           
+            uart.putc(0);//send signal to slave
+            //pc.printf("done\n");
+    }
+void synchronize(uint32_t Cmd_per){
+       
+        char* tmp=(char*)&Cmd_per;
+
+        uart.getc();
+        //pc.printf("TFS love xuemei");
+        uart.putc((*tmp));//pc.printf("0=%d\n",*tmp);
+        uart.getc();
+        uart.putc((*(tmp+1)));//pc.printf("1=%d\n",(*(tmp+1)));
+        uart.getc();   
+        uart.putc((*(tmp+2)));//pc.printf("2=%d\n",(*(tmp+2)));
+        uart.getc();  
+        uart.putc((*(tmp+3)));//pc.printf("3=%d\n",(*(tmp+3)));
+        uart.getc();//clear the buffer
+        // pc.printf("T1.5=%d",TIME()); 
             
-            //NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler);
-            //NVIC_SetPriority(TPM0_IRQn, 0);
-            NVIC_EnableIRQ(TPM1_IRQn);
-            // open tpm1
+            Period=Cmd_per;
+
+            //uart.getc();
+            //int t5=TIME();
+            Syc(calculate_delay());
+            //int t6=TIME();
+            //pc.printf("NOTICE,TIME=%d\n",t6-t5);            
+            loop_num=0;//clear clock 
+                    
+    }
+
+void TPM0_IRQHandler(void){
+            //syc_cnt++;
+            loop_num++;
+            //pc.printf("ln=%d",loop_num);
+            if(loop_num==Period){
+                out=!out;
+                LED=!LED;
+                per_num++;
+                loop_num=0;
+                //syc_cnt=0;
+        //if((TPM0->SC & 0x0080)==0x0080){
             
-            uart.putc(0);
-            uart.getc();
-            uart.putc(0xff);
-            uart.getc();
-            uart.putc(0);
-            uart.getc();
-            uart.putc(0xff);     
-            state++;
-            break;//}
-        }//receiving t3 from slave, calculate and send transmit command to slave    
-    
-    }
-    }
-void TPM0_IRQHandler(void){
-    
-        //if((TPM0->SC & 0x0080)==0x0080){
-            if(LED){LED=0;}
-            else {LED=1;loop_num++;}
-            pc.printf("MOD=%d",TPM0->MOD);
+            //pc.printf("MOD=%d",TPM0->MOD);
             //pc.printf("SC=%d",TPM0->SC);
-            pc.printf("Global_time=%d\n",TIME());
+            //pc.printf("Global_time=%d\n",TIME());
+            //pc.printf("Global_time=%f\n",(float)(TIME()%48000));
+            //pc.printf("out=%d\n",out);
             //pc.printf(": %d\n",TPM0->CNT);
+            }
+            //else if(flag&&(syc_cnt>=syc_interval)){
+                //Syc(calculate_delay());syc_cnt=0;  
+                //}
             TPM0->SC|= 0x000000c8;
-            NVIC_ClearPendingIRQ(TPM0_IRQn); 
+            //NVIC_ClearPendingIRQ(TPM0_IRQn); 
             //pc.printf("SC_after=%d\n",TPM0->SC);
             //}//pc.printf("count=%d",TPM0->CNT);
+            
             return;               
     }
 
-void TPM1_IRQHandler(void){// time to restart the system
-            TPM0->SC=0x00000040;
-            
-            TPM0->MOD=mod;
-            loop_num=0;//clear clock            
+void TPM1_IRQHandler(void){// time to restart the system           
+            LED=1;
             //TPM0->SC= 0x000000c8;        
             TPM0->CNT=0x0;
-            TPM0->SC=0x00000048;
+            //TPM0->SC=0x00000048;
             //NVIC_EnableIRQ(TPM0_IRQn);
-            pc.printf("AfterMOD=%d\n",TPM0->MOD);
-            //LED=1;
-            
+            //pc.printf("AfterMOD=%d\n",TPM0->MOD);
+            //LED=1;           
             TPM1->CNT=0x0;
-            pc.printf("SC=%d",TPM1->SC);
+            //pc.printf("SC=%d",TPM1->SC);
             TPM1->SC= 0x00000080;  
-            TPM1->SC= 0x00000000;
-            pc.printf("SC_after=%d\n",TPM1->SC);
-            pc.printf("Tic-Toc\n");
+            //TPM1->SC= 0x00000000;
+            //pc.printf("SC_after=%d\n",TPM1->SC);
+            //pc.printf("Tic-Toc\n");
             NVIC_DisableIRQ(TPM1_IRQn);
-            NVIC_ClearPendingIRQ(TPM1_IRQn);
-
+            //NVIC_ClearPendingIRQ(TPM1_IRQn);
+            //flag=true;
+            //pc.printf("Tic-Toc\n");
             return;               
     }
     
-void Syc(uint32_t Cmd_mod){
-    //NVIC_DisableIRQ(TPM0_IRQn);
-    //NVIC_ClearPendingIRQ(TPM0_IRQn);    
-    state=0; 
-    pc.printf("set=%d\n",state);
-    uart.putc('#');     
-    while(state<3){synchronize(Cmd_mod);}
-      
-    }
+
     
 void Initial(){
+    Period=1000;
     LED=1;
-    SIM->SOPT2=0x07000000;
+    out=0;
+    SIM->SOPT2=0x05000000;
     SIM->SCGC6=0x03000000;//enable TPM 0,1
     TPM0->SC=0x0;
 
@@ -190,13 +175,15 @@
 int main() {
     loop_num=0;
     cmd_received=false;
-    pc.attach(&receive_handler);    
-    int CMD_MOD=0;
+    uart_bbb.attach(&receive_handler);    
+    int CMD_Per=0;
     Initial();
     
+    uart_bbb.baud(9600);
+    uart_bbb.attach(&receive_handler);
+    
+    uart.baud(9600);
     pc.baud(9600);
-    pc.attach(&receive_handler);
-    uart.baud(9600);
       
     NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
     NVIC_SetPriority(TPM0_IRQn, 1);
@@ -205,24 +192,30 @@
     NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler);
     NVIC_SetPriority(TPM1_IRQn, 0);
     NVIC_EnableIRQ(TPM1_IRQn);
-    wait(5);
-    pc.printf("Initial done\n");
+    //wait(5);
+    //pc.printf("Initial done\n");
 
     
+    pc.printf("please enter T(in us)...\n");
+    //Syc(2000000);
+   
     //Syc(48000);
     
     while(1){
         if(cmd_received){
             pc.printf(BBBbuffer);
             for(int i=0;i<BBBbuff-1;i++){
-                CMD_MOD*=10;                
-                CMD_MOD+=(BBBbuffer[i]-'0');
+                CMD_Per*=10;                
+                CMD_Per+=(BBBbuffer[i]-'0');
                 }
             cmd_received=false;
             BBBbuff=0;
-            TPM0->MOD=CMD_MOD;
-            pc.printf("before_syc");
-            Syc(CMD_MOD);    
+            Period=CMD_Per;
+            //pc.printf("before_syc");
+                //pc.printf("set=%d\n",state);
+            uart.putc('#');     
+            synchronize(CMD_Per);
+            CMD_Per=0;
             }
             
         }