//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.
Fork of 202hm2_master by
Diff: main.cpp
- Revision:
- 0:fac0542384d7
- Child:
- 1:55f3661a2050
diff -r 000000000000 -r fac0542384d7 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 13 21:28:39 2014 +0000 @@ -0,0 +1,222 @@ +//this is a program built for the master mbed(bed1) +#include "mbed.h" +//#include "MKL46Z4.h" + +#define mod 0x0000bb80 +#define duty_cycle 0.5 +#define high_cycle mod*duty_cycle +#define low_cycle mod-high_cycle +#define buffer_size 9 +#define BBBbuff_size 30 + +DigitalOut LED(LED_RED); +Serial pc(USBTX,USBRX); +Serial uart(PTE0, PTE1); + +static int loop_num=0; +int state=0; +uint32_t T1=0; +uint32_t T2=0; +uint32_t T3=0; +uint32_t T4=0; +int delay=0; +uint32_t Mod=mod; +char BBBbuffer[BBBbuff_size]; +bool cmd_received=false; +int BBBbuff=0; +char buffer[4]; + +uint32_t TIME(){return loop_num*Mod+TPM0->CNT;} + +void receive_handler(){ + while( pc.readable() && BBBbuff<BBBbuff_size){ + BBBbuffer[BBBbuff] =pc.getc(); + pc.putc(BBBbuffer[BBBbuff]); + //pc.printf(& rx_buffer[buff]); + if (BBBbuffer[BBBbuff] == '#'){ + BBBbuffer[BBBbuff] = '\0'; + cmd_received=true; + BBBbuff++; + pc.printf("cmd received\n"); + break; + } + BBBbuff++; + } + return; + } +void TPM1_IRQHandler(void); + +void sycronize(uint32_t Cmd_mod){ + switch(state){ + case 0: { + + + char* tmp=(char*)&Cmd_mod; + uint32_t T1=TIME(); + pc.printf("T1=%d\n",T1); + uart.putc((*tmp));pc.printf("0=%d\n",*tmp); + uart.putc((*(tmp+1)));pc.printf("1=%d\n",(*(tmp+1))); + uart.putc((*(tmp+2)));pc.printf("2=%d\n",(*(tmp+2))); + uart.putc((*(tmp+3)));pc.printf("3=%d\n",(*(tmp+3))); + pc.printf("T1 done%d\n"); + state++; + break;}//sending a 5 char command + case 1:{ + buffer[0]=uart.getc(); + buffer[1]=uart.getc(); + buffer[2]=uart.getc(); + buffer[3]=uart.getc(); + T4=TIME(); + T2=((int)buffer[0])<<24+((int)buffer[1])<<16+((int)buffer[2])<<8+((int)buffer[3]); + state++;break; + }//receiving t2 from slave + + case 2:{ + buffer[0]=uart.getc(); + buffer[1]=uart.getc(); + buffer[2]=uart.getc(); + buffer[3]=uart.getc(); + //receiveing t3 + T3=((int)buffer[0])<<24+((int)buffer[1])<<16+((int)buffer[2])<<8+((int)buffer[3]); + + delay= (T4-T3+T2-T1)/2; + //delay=0x0000ffff; + TPM1->MOD=delay; + + TPM1->CNT=0x0; + TPM1->SC=0x00000048; + + //NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler); + //NVIC_SetPriority(TPM0_IRQn, 0); + //NVIC_EnableIRQ(TPM1_IRQn); + // open tpm1 + + uart.putc(0xff); + uart.putc(0xff); + uart.putc(0xff); + 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("SC=%d",TPM0->SC); + pc.printf("Global_time=%d\n",TIME()); + //pc.printf(": %d\n",TPM0->CNT); + TPM0->SC|= 0x000000c8; + 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 + //TPM0->SC= 0x000000c8; + TPM0->CNT=0x0; + TPM0->SC=0x00000048; + NVIC_EnableIRQ(TPM0_IRQn); + pc.printf("AfterMOD=%d\n",TPM0->MOD); + //LED=1; + + TPM1->CNT=0x0; + 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"); + NVIC_DisableIRQ(TPM1_IRQn); + NVIC_ClearPendingIRQ(TPM1_IRQn); + + 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){sycronize(Cmd_mod);} + + } + +void Initial(){ + LED=1; + SIM->SOPT2=0x07000000; + SIM->SCGC6=0x03000000;//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; + ptrMyReg = (volatile uint32_t *) 0x4003900C;//C1SC + prev = *ptrMyReg; + prev = prev | 0x00000040; + *ptrMyReg = prev; + //---------------------------------------------- + TPM0->CNT=0x0; + TPM0->SC=0x00000040; + TPM0->MOD=0x0000bb80; + TPM0->SC=0x00000048;//0008 + + TPM1->CNT=0x0; + TPM1->SC=0x00000040; + TPM1->MOD=0x0000ffff; + TPM1->SC=0x00000008;//0008 + } +int main() { + + cmd_received=false; + pc.attach(&receive_handler); + int CMD_MOD=0; + Initial(); + + pc.baud(9600); + pc.attach(&receive_handler); + uart.baud(9600); + + NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler); + NVIC_SetPriority(TPM0_IRQn, 1); + NVIC_EnableIRQ(TPM0_IRQn); + + NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler); + NVIC_SetPriority(TPM1_IRQn, 0); + NVIC_EnableIRQ(TPM1_IRQn); + wait(5); + pc.printf("Initial done\n"); + + + //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_received=false; + BBBbuff=0; + TPM0->MOD=CMD_MOD; + pc.printf("before_syc"); + Syc(CMD_MOD); + } + + } + + +} \ No newline at end of file