generation_mbed2

Dependencies:   202hm2_slave mbed

Fork of 202hm2_slave by Yujing Qian

Committer:
allonq
Date:
Sun Mar 16 07:18:16 2014 +0000
Revision:
3:7544d38698a3
Parent:
2:4457eb1e9b83
Child:
4:54fbe45eef1c
EE202 hm2 generation mode mbed2;

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 3:7544d38698a3 7 DigitalOut out(PTD4);
allonq 0:7c4f7de16626 8 Serial pc(USBTX,USBRX);
allonq 0:7c4f7de16626 9
allonq 0:7c4f7de16626 10 Serial uart(PTE0, PTE1);
allonq 3:7544d38698a3 11
allonq 3:7544d38698a3 12
allonq 2:4457eb1e9b83 13 int loop_num=0;
allonq 3:7544d38698a3 14 int per_num=0;
allonq 3:7544d38698a3 15 int Period=0;
allonq 0:7c4f7de16626 16 int state=0;
allonq 0:7c4f7de16626 17 int buffered=0;
allonq 0:7c4f7de16626 18 uint32_t T2=0;
allonq 0:7c4f7de16626 19 uint32_t T3=0;
allonq 0:7c4f7de16626 20 int delay=0;
allonq 3:7544d38698a3 21 int per=0;
allonq 0:7c4f7de16626 22 char buffer[buffer_size];
allonq 0:7c4f7de16626 23 int buff=0;
allonq 3:7544d38698a3 24 int syc_f=2;
allonq 3:7544d38698a3 25 int syc_interval=1000/syc_f;
allonq 3:7544d38698a3 26 uint32_t syc_cnt=0;
allonq 3:7544d38698a3 27
allonq 3:7544d38698a3 28 uint32_t TIME(){return per_num*Period*48000+loop_num*48000+(TPM0->CNT);}
allonq 0:7c4f7de16626 29
allonq 3:7544d38698a3 30 void Syc(){// calculate delay using hand shake
allonq 3:7544d38698a3 31 //pc.printf("Syc\n");
allonq 3:7544d38698a3 32 uart.getc();
allonq 3:7544d38698a3 33 uart.putc(0);
allonq 3:7544d38698a3 34 uart.getc();
allonq 3:7544d38698a3 35 pc.printf("launch");
allonq 3:7544d38698a3 36 LED=1;
allonq 3:7544d38698a3 37 TPM0->CNT=0;
allonq 3:7544d38698a3 38 TPM0->SC=0x00000048;//0008
allonq 3:7544d38698a3 39 }
allonq 1:76a206e19490 40 void synchronize(){
allonq 2:4457eb1e9b83 41 //pc.printf("state=%d\n",state);
allonq 3:7544d38698a3 42
allonq 3:7544d38698a3 43 buffer[0]=uart.getc();//pc.printf("0=%d\n",buffer[0]);
allonq 3:7544d38698a3 44 uart.putc(0);
allonq 3:7544d38698a3 45 buffer[1]=uart.getc();//pc.printf("1=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 46 uart.putc(0);
allonq 3:7544d38698a3 47 buffer[2]=uart.getc();//pc.printf("2=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 48 uart.putc(0);
allonq 3:7544d38698a3 49 buffer[3]=uart.getc();//pc.printf("3=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 50 uart.putc(0);
allonq 3:7544d38698a3 51 per=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
allonq 2:4457eb1e9b83 52 /*pc.printf("0=%d\n",buffer[0]);
allonq 0:7c4f7de16626 53 pc.printf("1=%d\n",buffer[1]);
allonq 0:7c4f7de16626 54 pc.printf("2=%d\n",buffer[2]);
allonq 2:4457eb1e9b83 55 pc.printf("3=%d\n",buffer[3]);*/
allonq 2:4457eb1e9b83 56
allonq 2:4457eb1e9b83 57
allonq 3:7544d38698a3 58
allonq 3:7544d38698a3 59 Syc();
allonq 3:7544d38698a3 60 //pc.printf("per=%d\n",per);
allonq 3:7544d38698a3 61 //pc.printf("Syc_over\n");
allonq 0:7c4f7de16626 62 loop_num=0;
allonq 3:7544d38698a3 63 Period=per;
allonq 2:4457eb1e9b83 64 //NVIC_EnableIRQ(TPM0_IRQn);
allonq 3:7544d38698a3 65 //pc.printf("T2=%d\n",T2);
allonq 3:7544d38698a3 66 //pc.printf("T3=%d\n",T3);
allonq 3:7544d38698a3 67 //flag=true;
allonq 3:7544d38698a3 68
allonq 0:7c4f7de16626 69 }
allonq 0:7c4f7de16626 70
allonq 3:7544d38698a3 71 void TPM0_IRQHandler(void){
allonq 3:7544d38698a3 72 loop_num++;
allonq 3:7544d38698a3 73 //syc_cnt++;
allonq 3:7544d38698a3 74
allonq 3:7544d38698a3 75 if(loop_num==Period){
allonq 3:7544d38698a3 76 out=!out;LED=!LED;per_num++;loop_num=0;
allonq 0:7c4f7de16626 77 //if((TPM0->SC & 0x0080)==0x0080){
allonq 3:7544d38698a3 78
allonq 3:7544d38698a3 79 //pc.printf("per=%d",Period);
allonq 3:7544d38698a3 80
allonq 3:7544d38698a3 81 //pc.printf("syc_cnt=%d",syc_cnt);
allonq 3:7544d38698a3 82 //pc.printf("flag=%d\n",flag);
allonq 3:7544d38698a3 83 //pc.printf("Global_time=%f\n",(float)(TIME()%48000));
allonq 0:7c4f7de16626 84 //pc.printf(": %d\n",TPM0->CNT);
allonq 3:7544d38698a3 85 }
allonq 3:7544d38698a3 86 //else if(flag&&(syc_cnt>=syc_interval)){
allonq 3:7544d38698a3 87 //Syc();syc_cnt=0;pc.printf("!!!!!syced\n");
allonq 3:7544d38698a3 88 // }
allonq 0:7c4f7de16626 89 TPM0->SC|= 0x000000c8;
allonq 3:7544d38698a3 90 //NVIC_ClearPendingIRQ(TPM0_IRQn);
allonq 0:7c4f7de16626 91 //pc.printf("SC_after=%d\n",TPM0->SC);
allonq 0:7c4f7de16626 92 //}//pc.printf("count=%d",TPM0->CNT);
allonq 3:7544d38698a3 93
allonq 0:7c4f7de16626 94 return;
allonq 0:7c4f7de16626 95 }
allonq 0:7c4f7de16626 96
allonq 0:7c4f7de16626 97
allonq 3:7544d38698a3 98 void cmd_handler(){
allonq 3:7544d38698a3 99 char tmp;
allonq 3:7544d38698a3 100 if(uart.readable()){
allonq 3:7544d38698a3 101 pc.printf("read\n");
allonq 3:7544d38698a3 102 tmp=uart.getc();
allonq 3:7544d38698a3 103 pc.printf(&tmp);
allonq 3:7544d38698a3 104
allonq 3:7544d38698a3 105 if(tmp=='#')uart.putc(0);synchronize();}
allonq 0:7c4f7de16626 106
allonq 0:7c4f7de16626 107 }
allonq 3:7544d38698a3 108
allonq 3:7544d38698a3 109
allonq 3:7544d38698a3 110
allonq 0:7c4f7de16626 111 void Initial(){
allonq 2:4457eb1e9b83 112 for(int i=0;i<4;i++)buffer[i]=255;
allonq 2:4457eb1e9b83 113 loop_num=0;
allonq 3:7544d38698a3 114 per_num=0;
allonq 3:7544d38698a3 115 Period=5000;
allonq 3:7544d38698a3 116 out=0;
allonq 0:7c4f7de16626 117 LED=1;
allonq 3:7544d38698a3 118 SIM->SOPT2=0x05000000;
allonq 3:7544d38698a3 119 SIM->SCGC6=0x01000000;//enable TPM 0
allonq 3:7544d38698a3 120 //TPM0->SC=0x0;
allonq 2:4457eb1e9b83 121
allonq 0:7c4f7de16626 122 TPM0->CNT=0x0;
allonq 0:7c4f7de16626 123 TPM0->SC=0x00000040;
allonq 3:7544d38698a3 124 TPM0->MOD=0x0000bb80;
allonq 0:7c4f7de16626 125 TPM0->SC=0x00000048;//0008
allonq 0:7c4f7de16626 126
allonq 0:7c4f7de16626 127
allonq 0:7c4f7de16626 128 }
allonq 0:7c4f7de16626 129 int main() {
allonq 2:4457eb1e9b83 130 Initial();
allonq 2:4457eb1e9b83 131
allonq 0:7c4f7de16626 132 pc.baud(9600);
allonq 3:7544d38698a3 133 uart.baud(9600);
allonq 2:4457eb1e9b83 134
allonq 0:7c4f7de16626 135 NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
allonq 0:7c4f7de16626 136 NVIC_SetPriority(TPM0_IRQn, 1);
allonq 2:4457eb1e9b83 137 NVIC_EnableIRQ(TPM0_IRQn);
allonq 2:4457eb1e9b83 138
allonq 3:7544d38698a3 139 uart.attach(& cmd_handler);
allonq 0:7c4f7de16626 140 }