//EE 202 hm2 //This is a program built for the mbed2 monitor mode //This code has been tested and should be function, if you has any problem, //please mail me.

Dependencies:   202hm2_slave mbed

Fork of 202hm2_slave by Yujing Qian

Revision:
2:4457eb1e9b83
Parent:
1:76a206e19490
Child:
3:019db60eb5a6
Child:
4:7544d38698a3
--- a/main.cpp	Thu Mar 13 22:05:48 2014 +0000
+++ b/main.cpp	Fri Mar 14 02:04:33 2014 +0000
@@ -7,65 +7,88 @@
 Serial pc(USBTX,USBRX);
 
 Serial uart(PTE0, PTE1);
-static int loop_num=0;
+int loop_num=0;
 int state=0;
 int buffered=0;
 uint32_t T2=0;
 uint32_t T3=0;
 int delay=0;
-uint32_t mod=0;
+int mod=0;
 char buffer[buffer_size];
 int buff=0;
-uint32_t TIME(){return loop_num*mod+TPM0->CNT;}
+uint32_t TIME(){return loop_num*(TPM0->MOD)+(TPM0->CNT);}
 
 
 void synchronize(){
+    //pc.printf("state=%d\n",state);
     switch(state){
-    case 0:{pc.printf("state0");
-            if(uart.readable()&&buff<4){buffer[buff]=uart.getc();buff++;}
-            else if(buff==4){
-            pc.printf("0=%d\n",buffer[0]);
+    case 0:{
+            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();
+
+            /*pc.printf("0=%d\n",buffer[0]);
             pc.printf("1=%d\n",buffer[1]);
             pc.printf("2=%d\n",buffer[2]);
-            pc.printf("3=%d\n",buffer[3]);
+            pc.printf("3=%d\n",buffer[3]);*/
+
             T2=TIME();
-            mod=((int)buffer[3])<<24+((int)buffer[2])<<16+((int)buffer[1])<<8+((int)buffer[0]);
-                pc.printf("mod=%d",mod);
-                pc.printf("\n");
-            TPM0->MOD=mod;           
-            state++;break;}//sending a 9 char command
-            else break;
+            
+            mod=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
+                //pc.printf("mod=%d",mod);
+                //pc.printf("\n");
+            TPM0->MOD=mod;//buff=0;
+            //pc.printf("MOD=%d\n",mod);           
+            state++;break;//sending a 9 char command
+            //else {break;}
             }
     case 1: {
         T3=TIME();
+        pc.printf("T3_test=%d\n",T3);
         char* tmp=(char*)&T2;
-        pc.printf("state1\n");
         uart.putc((*tmp));
+        uart.getc();
         uart.putc((*(tmp+1)));
+        uart.getc();
         uart.putc((*(tmp+2)));
+        uart.getc();
         uart.putc((*(tmp+3)));
+        
         state++;break;
         }// send T2 value
     case 2:{
         
         char* tmp=(char*)&T3;
         uart.putc((*tmp));
+        uart.getc();
         uart.putc((*(tmp+1)));
+        uart.getc();
         uart.putc((*(tmp+2)));
+        uart.getc();
         uart.putc((*(tmp+3)));
         state++;break;
         }//send T3           
     case 3:{
-        pc.printf("wait for lanch state=%d\n",state);
-            buffer[0]=uart.getc();
-            buffer[1]=uart.getc();
-            buffer[2]=uart.getc();
-            buffer[3]=uart.getc();
+        //pc.printf("wait for lanch state=%d\n",state);
+            uart.getc();
+            uart.putc(0);
+            uart.getc();
+            uart.putc(0);
+            uart.getc();
+            uart.putc(0);
+            uart.getc();
+            
             
             loop_num=0;
             TPM0->CNT=0x0;
             TPM0->SC=0x00000048;
-            NVIC_DisableIRQ(TPM0_IRQn);    
+            //NVIC_EnableIRQ(TPM0_IRQn);
+            pc.printf("T2=%d\n",T2);    
+            pc.printf("T3=%d\n",T3);   
             state++;
             break;//luanch   
         }
@@ -77,7 +100,7 @@
             if(LED){LED=0;}
             else {LED=1;loop_num++;}
             //pc.printf("MOD=%d",TPM0->MOD);
-            //pc.printf("Global_time=%d",loop_num);
+            pc.printf("Global_time=%d\n",TIME());
             //pc.printf(": %d\n",TPM0->CNT);
             TPM0->SC|= 0x000000c8;
             NVIC_ClearPendingIRQ(TPM0_IRQn);
@@ -90,41 +113,38 @@
     
 void Syc(){    
     state=0;
-    NVIC_DisableIRQ(TPM0_IRQn);
+    //NVIC_DisableIRQ(TPM0_IRQn);
     NVIC_ClearPendingIRQ(TPM0_IRQn);  
     pc.printf("set=%d\n",state);     
     while(state<4){synchronize();}
       
     }
 void Initial(){
+    for(int i=0;i<4;i++)buffer[i]=255;
+    loop_num=0;
     LED=1;
     SIM->SOPT2=0x07000000;
     SIM->SCGC6=0x01000000;//enable TPM 0,1
     TPM0->SC=0x0;
-    //-------------------CnSC-----------------------
-    volatile uint32_t * ptrMyReg;
-    volatile uint32_t prev;
-    ptrMyReg = (volatile uint32_t *) 0x4003800C;//C0SC 
-    prev = *ptrMyReg;
-    prev = prev | 0x00000040;
-    *ptrMyReg = prev;
-    //----------------------------------------------
+    
     TPM0->CNT=0x0;
     TPM0->SC=0x00000040;
-    TPM0->MOD=0x000000ff;
+    TPM0->MOD=0x00000fff;
     TPM0->SC=0x00000048;//0008
     
 
     }
 int main() {
-    Initial();    
+    Initial();
+        
     pc.baud(9600);
-        
+    
     NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
     NVIC_SetPriority(TPM0_IRQn, 1);
-    NVIC_EnableIRQ(TPM0_IRQn); 
+    NVIC_EnableIRQ(TPM0_IRQn);
+     
     char tmp;
-    pc.printf("done");
+    //pc.printf("done");
     
     
     while(1){