Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 2:31495b76a1de
- Parent:
- 1:55f3661a2050
diff -r 55f3661a2050 -r 31495b76a1de main.cpp
--- a/main.cpp Fri Mar 14 02:04:39 2014 +0000
+++ b/main.cpp Sun Mar 16 08:11:31 2014 +0000
@@ -1,4 +1,9 @@
-//this is a program built for the master mbed(bed1)
+//EE 202 hm2
+//This is a program built for the mbed1 in monitor mode
+//This code has been tested and should be function, if you has any problem,
+//please mail me.
+//Author: Yujing Qian
+//y.qian42@gmail.com
#include "mbed.h"
//#include "MKL46Z4.h"
@@ -10,30 +15,45 @@
#define BBBbuff_size 30
DigitalOut LED(LED_RED);
+InterruptIn dig_in(PTD4);
Serial pc(USBTX,USBRX);
Serial uart(PTE0, PTE1);
+Serial uart_bbb(PTE16,PTE17);
-int loop_num=0;
-int state=0;
+uint32_t loop_num=0;
+uint32_t per_num=0;
+uint32_t Period=5000;
uint32_t T1=0;
uint32_t T2=0;
uint32_t T3=0;
uint32_t T4=0;
-int delay=0;
+bool flag=false;
+
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);}
+int drift_cnt=0;
+uint32_t TIME(){return per_num*Period*48000+loop_num*48000+(TPM0->CNT);}
+void report(){
+ int us=(TPM0->CNT);
+ uint32_t ms=per_num*Period+loop_num;
+ uint32_t s=ms/1000;//second
+ ms=ms%1000;
+
+ uart_bbb.printf("m%u:%d\n",s*1000+ms,us);
+ pc.printf("m%u:%u:%d\n",s,ms,us);
+ drift_cnt++;
+ }
void receive_handler(){
- while( pc.readable() && BBBbuff<BBBbuff_size){
+ while(pc.readable() && BBBbuff<BBBbuff_size){
BBBbuffer[BBBbuff] =pc.getc();
pc.putc(BBBbuffer[BBBbuff]);
//pc.printf(& rx_buffer[buff]);
- if (BBBbuffer[BBBbuff] == '#'){
+ if (BBBbuffer[BBBbuff] == 's'){
BBBbuffer[BBBbuff] = '\0';
cmd_received=true;
BBBbuff++;
@@ -45,135 +65,84 @@
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){
+
TPM1->MOD=delay;
-
TPM1->CNT=0x0;
- TPM1->SC=0x00000048;
+ //loop_num=0;
+ TPM1->SC=0x00000048;
+ NVIC_EnableIRQ(TPM1_IRQn);
+ // open tpm1
+ uart.putc(0);//send signal to slave
+ }
+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
- //NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler);
- //NVIC_SetPriority(TPM0_IRQn, 0);
- NVIC_EnableIRQ(TPM1_IRQn);
- // open tpm1
+ Period=Cmd_per;
+
+ Syc(calculate_delay());
+
+ loop_num=0;//clear clock
+ per_num=0;
+ flag=true;
+ }
+
+void TPM0_IRQHandler(void){
+
+ loop_num++;
+
+ if(loop_num==Period){
+
+ LED=!LED;
+ per_num++;
+ loop_num=0;
+ }
+ TPM0->SC|= 0x000000c8;
- 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("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;
-
+void TPM1_IRQHandler(void){// time to restart the system
+ LED=1;
+
+ TPM0->CNT=0x0;
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);
-
+ flag=true;
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=5000;
LED=1;
- SIM->SOPT2=0x07000000;
+ //out=0;
+ SIM->SOPT2=0x05000000;
SIM->SCGC6=0x03000000;//enable TPM 0,1
TPM0->SC=0x0;
@@ -191,11 +160,12 @@
loop_num=0;
cmd_received=false;
pc.attach(&receive_handler);
- int CMD_MOD=0;
+ int CMD_Per=0;
Initial();
pc.baud(9600);
pc.attach(&receive_handler);
+
uart.baud(9600);
NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
@@ -205,27 +175,18 @@
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);
-
+ dig_in.rise(report);
+
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);
- }
-
+
+ uart.putc('#');
+ synchronize(2000);
+
+ }
}
-
}
\ No newline at end of file