//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

Committer:
allonq
Date:
Fri Mar 14 02:04:33 2014 +0000
Revision:
2:4457eb1e9b83
Parent:
1:76a206e19490
Child:
3:019db60eb5a6
Child:
4:7544d38698a3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
allonq 0:7c4f7de16626 1 //this is a program built for the slave mbed(bed1)
allonq 0:7c4f7de16626 2 #include "mbed.h"
allonq 0:7c4f7de16626 3 #include "MKL46Z4.h"
allonq 0:7c4f7de16626 4
allonq 0:7c4f7de16626 5 #define buffer_size 9
allonq 0:7c4f7de16626 6 DigitalOut LED(LED_RED);
allonq 0:7c4f7de16626 7 Serial pc(USBTX,USBRX);
allonq 0:7c4f7de16626 8
allonq 0:7c4f7de16626 9 Serial uart(PTE0, PTE1);
allonq 2:4457eb1e9b83 10 int loop_num=0;
allonq 0:7c4f7de16626 11 int state=0;
allonq 0:7c4f7de16626 12 int buffered=0;
allonq 0:7c4f7de16626 13 uint32_t T2=0;
allonq 0:7c4f7de16626 14 uint32_t T3=0;
allonq 0:7c4f7de16626 15 int delay=0;
allonq 2:4457eb1e9b83 16 int mod=0;
allonq 0:7c4f7de16626 17 char buffer[buffer_size];
allonq 0:7c4f7de16626 18 int buff=0;
allonq 2:4457eb1e9b83 19 uint32_t TIME(){return loop_num*(TPM0->MOD)+(TPM0->CNT);}
allonq 0:7c4f7de16626 20
allonq 0:7c4f7de16626 21
allonq 1:76a206e19490 22 void synchronize(){
allonq 2:4457eb1e9b83 23 //pc.printf("state=%d\n",state);
allonq 0:7c4f7de16626 24 switch(state){
allonq 2:4457eb1e9b83 25 case 0:{
allonq 2:4457eb1e9b83 26 buffer[0]=uart.getc();
allonq 2:4457eb1e9b83 27 uart.putc(0);
allonq 2:4457eb1e9b83 28 buffer[1]=uart.getc();
allonq 2:4457eb1e9b83 29 uart.putc(0);
allonq 2:4457eb1e9b83 30 buffer[2]=uart.getc();
allonq 2:4457eb1e9b83 31 uart.putc(0);
allonq 2:4457eb1e9b83 32 buffer[3]=uart.getc();
allonq 2:4457eb1e9b83 33
allonq 2:4457eb1e9b83 34 /*pc.printf("0=%d\n",buffer[0]);
allonq 0:7c4f7de16626 35 pc.printf("1=%d\n",buffer[1]);
allonq 0:7c4f7de16626 36 pc.printf("2=%d\n",buffer[2]);
allonq 2:4457eb1e9b83 37 pc.printf("3=%d\n",buffer[3]);*/
allonq 2:4457eb1e9b83 38
allonq 0:7c4f7de16626 39 T2=TIME();
allonq 2:4457eb1e9b83 40
allonq 2:4457eb1e9b83 41 mod=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
allonq 2:4457eb1e9b83 42 //pc.printf("mod=%d",mod);
allonq 2:4457eb1e9b83 43 //pc.printf("\n");
allonq 2:4457eb1e9b83 44 TPM0->MOD=mod;//buff=0;
allonq 2:4457eb1e9b83 45 //pc.printf("MOD=%d\n",mod);
allonq 2:4457eb1e9b83 46 state++;break;//sending a 9 char command
allonq 2:4457eb1e9b83 47 //else {break;}
allonq 1:76a206e19490 48 }
allonq 0:7c4f7de16626 49 case 1: {
allonq 0:7c4f7de16626 50 T3=TIME();
allonq 2:4457eb1e9b83 51 pc.printf("T3_test=%d\n",T3);
allonq 0:7c4f7de16626 52 char* tmp=(char*)&T2;
allonq 0:7c4f7de16626 53 uart.putc((*tmp));
allonq 2:4457eb1e9b83 54 uart.getc();
allonq 0:7c4f7de16626 55 uart.putc((*(tmp+1)));
allonq 2:4457eb1e9b83 56 uart.getc();
allonq 0:7c4f7de16626 57 uart.putc((*(tmp+2)));
allonq 2:4457eb1e9b83 58 uart.getc();
allonq 0:7c4f7de16626 59 uart.putc((*(tmp+3)));
allonq 2:4457eb1e9b83 60
allonq 0:7c4f7de16626 61 state++;break;
allonq 0:7c4f7de16626 62 }// send T2 value
allonq 0:7c4f7de16626 63 case 2:{
allonq 0:7c4f7de16626 64
allonq 0:7c4f7de16626 65 char* tmp=(char*)&T3;
allonq 0:7c4f7de16626 66 uart.putc((*tmp));
allonq 2:4457eb1e9b83 67 uart.getc();
allonq 0:7c4f7de16626 68 uart.putc((*(tmp+1)));
allonq 2:4457eb1e9b83 69 uart.getc();
allonq 0:7c4f7de16626 70 uart.putc((*(tmp+2)));
allonq 2:4457eb1e9b83 71 uart.getc();
allonq 0:7c4f7de16626 72 uart.putc((*(tmp+3)));
allonq 0:7c4f7de16626 73 state++;break;
allonq 0:7c4f7de16626 74 }//send T3
allonq 0:7c4f7de16626 75 case 3:{
allonq 2:4457eb1e9b83 76 //pc.printf("wait for lanch state=%d\n",state);
allonq 2:4457eb1e9b83 77 uart.getc();
allonq 2:4457eb1e9b83 78 uart.putc(0);
allonq 2:4457eb1e9b83 79 uart.getc();
allonq 2:4457eb1e9b83 80 uart.putc(0);
allonq 2:4457eb1e9b83 81 uart.getc();
allonq 2:4457eb1e9b83 82 uart.putc(0);
allonq 2:4457eb1e9b83 83 uart.getc();
allonq 2:4457eb1e9b83 84
allonq 0:7c4f7de16626 85
allonq 0:7c4f7de16626 86 loop_num=0;
allonq 0:7c4f7de16626 87 TPM0->CNT=0x0;
allonq 0:7c4f7de16626 88 TPM0->SC=0x00000048;
allonq 2:4457eb1e9b83 89 //NVIC_EnableIRQ(TPM0_IRQn);
allonq 2:4457eb1e9b83 90 pc.printf("T2=%d\n",T2);
allonq 2:4457eb1e9b83 91 pc.printf("T3=%d\n",T3);
allonq 0:7c4f7de16626 92 state++;
allonq 0:7c4f7de16626 93 break;//luanch
allonq 0:7c4f7de16626 94 }
allonq 0:7c4f7de16626 95 }
allonq 0:7c4f7de16626 96 }
allonq 0:7c4f7de16626 97
allonq 0:7c4f7de16626 98 void TPM0_IRQHandler(void){
allonq 0:7c4f7de16626 99 //if((TPM0->SC & 0x0080)==0x0080){
allonq 0:7c4f7de16626 100 if(LED){LED=0;}
allonq 0:7c4f7de16626 101 else {LED=1;loop_num++;}
allonq 0:7c4f7de16626 102 //pc.printf("MOD=%d",TPM0->MOD);
allonq 2:4457eb1e9b83 103 pc.printf("Global_time=%d\n",TIME());
allonq 0:7c4f7de16626 104 //pc.printf(": %d\n",TPM0->CNT);
allonq 0:7c4f7de16626 105 TPM0->SC|= 0x000000c8;
allonq 0:7c4f7de16626 106 NVIC_ClearPendingIRQ(TPM0_IRQn);
allonq 0:7c4f7de16626 107 //pc.printf("SC_after=%d\n",TPM0->SC);
allonq 0:7c4f7de16626 108 //}//pc.printf("count=%d",TPM0->CNT);
allonq 0:7c4f7de16626 109 return;
allonq 0:7c4f7de16626 110 }
allonq 0:7c4f7de16626 111
allonq 0:7c4f7de16626 112
allonq 0:7c4f7de16626 113
allonq 0:7c4f7de16626 114 void Syc(){
allonq 0:7c4f7de16626 115 state=0;
allonq 2:4457eb1e9b83 116 //NVIC_DisableIRQ(TPM0_IRQn);
allonq 0:7c4f7de16626 117 NVIC_ClearPendingIRQ(TPM0_IRQn);
allonq 0:7c4f7de16626 118 pc.printf("set=%d\n",state);
allonq 1:76a206e19490 119 while(state<4){synchronize();}
allonq 0:7c4f7de16626 120
allonq 0:7c4f7de16626 121 }
allonq 0:7c4f7de16626 122 void Initial(){
allonq 2:4457eb1e9b83 123 for(int i=0;i<4;i++)buffer[i]=255;
allonq 2:4457eb1e9b83 124 loop_num=0;
allonq 0:7c4f7de16626 125 LED=1;
allonq 0:7c4f7de16626 126 SIM->SOPT2=0x07000000;
allonq 0:7c4f7de16626 127 SIM->SCGC6=0x01000000;//enable TPM 0,1
allonq 0:7c4f7de16626 128 TPM0->SC=0x0;
allonq 2:4457eb1e9b83 129
allonq 0:7c4f7de16626 130 TPM0->CNT=0x0;
allonq 0:7c4f7de16626 131 TPM0->SC=0x00000040;
allonq 2:4457eb1e9b83 132 TPM0->MOD=0x00000fff;
allonq 0:7c4f7de16626 133 TPM0->SC=0x00000048;//0008
allonq 0:7c4f7de16626 134
allonq 0:7c4f7de16626 135
allonq 0:7c4f7de16626 136 }
allonq 0:7c4f7de16626 137 int main() {
allonq 2:4457eb1e9b83 138 Initial();
allonq 2:4457eb1e9b83 139
allonq 0:7c4f7de16626 140 pc.baud(9600);
allonq 2:4457eb1e9b83 141
allonq 0:7c4f7de16626 142 NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
allonq 0:7c4f7de16626 143 NVIC_SetPriority(TPM0_IRQn, 1);
allonq 2:4457eb1e9b83 144 NVIC_EnableIRQ(TPM0_IRQn);
allonq 2:4457eb1e9b83 145
allonq 0:7c4f7de16626 146 char tmp;
allonq 2:4457eb1e9b83 147 //pc.printf("done");
allonq 0:7c4f7de16626 148
allonq 0:7c4f7de16626 149
allonq 0:7c4f7de16626 150 while(1){
allonq 0:7c4f7de16626 151 if(uart.readable()){pc.printf("???\n");
allonq 0:7c4f7de16626 152 tmp=uart.getc();pc.printf(&tmp);
allonq 0:7c4f7de16626 153 if(tmp=='#')Syc();}
allonq 0:7c4f7de16626 154 }
allonq 0:7c4f7de16626 155 }