//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:
Sun Mar 16 08:13:39 2014 +0000
Revision:
5:54fbe45eef1c
Parent:
4:7544d38698a3
//EE 202 hm2; //This is a program built for the mbed3 in monitoring mode; //This code has been tested and should be function, if you has any problem,; //please mail me.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
allonq 5:54fbe45eef1c 1 //EE 202 hm2
allonq 5:54fbe45eef1c 2 //This is a program built for the mbed2(slave) in generation mode
allonq 5:54fbe45eef1c 3 //This code has been tested and should be function, if you has any problem,
allonq 5:54fbe45eef1c 4 //please mail me.
allonq 5:54fbe45eef1c 5 //Author: Yujing Qian
allonq 5:54fbe45eef1c 6 //y.qian42@gmail.com
allonq 0:7c4f7de16626 7 #include "mbed.h"
allonq 0:7c4f7de16626 8 #include "MKL46Z4.h"
allonq 0:7c4f7de16626 9
allonq 0:7c4f7de16626 10 #define buffer_size 9
allonq 0:7c4f7de16626 11 DigitalOut LED(LED_RED);
allonq 4:7544d38698a3 12 DigitalOut out(PTD4);
allonq 0:7c4f7de16626 13 Serial pc(USBTX,USBRX);
allonq 0:7c4f7de16626 14
allonq 0:7c4f7de16626 15 Serial uart(PTE0, PTE1);
allonq 4:7544d38698a3 16
allonq 4:7544d38698a3 17
allonq 2:4457eb1e9b83 18 int loop_num=0;
allonq 4:7544d38698a3 19 int per_num=0;
allonq 4:7544d38698a3 20 int Period=0;
allonq 0:7c4f7de16626 21 int state=0;
allonq 0:7c4f7de16626 22 int buffered=0;
allonq 0:7c4f7de16626 23 uint32_t T2=0;
allonq 0:7c4f7de16626 24 uint32_t T3=0;
allonq 0:7c4f7de16626 25 int delay=0;
allonq 4:7544d38698a3 26 int per=0;
allonq 0:7c4f7de16626 27 char buffer[buffer_size];
allonq 0:7c4f7de16626 28 int buff=0;
allonq 4:7544d38698a3 29 int syc_f=2;
allonq 4:7544d38698a3 30 int syc_interval=1000/syc_f;
allonq 4:7544d38698a3 31 uint32_t syc_cnt=0;
allonq 4:7544d38698a3 32
allonq 4:7544d38698a3 33 uint32_t TIME(){return per_num*Period*48000+loop_num*48000+(TPM0->CNT);}
allonq 0:7c4f7de16626 34
allonq 4:7544d38698a3 35 void Syc(){// calculate delay using hand shake
allonq 4:7544d38698a3 36 //pc.printf("Syc\n");
allonq 4:7544d38698a3 37 uart.getc();
allonq 4:7544d38698a3 38 uart.putc(0);
allonq 4:7544d38698a3 39 uart.getc();
allonq 4:7544d38698a3 40 pc.printf("launch");
allonq 4:7544d38698a3 41 LED=1;
allonq 4:7544d38698a3 42 TPM0->CNT=0;
allonq 4:7544d38698a3 43 TPM0->SC=0x00000048;//0008
allonq 4:7544d38698a3 44 }
allonq 1:76a206e19490 45 void synchronize(){
allonq 2:4457eb1e9b83 46 //pc.printf("state=%d\n",state);
allonq 4:7544d38698a3 47
allonq 4:7544d38698a3 48 buffer[0]=uart.getc();//pc.printf("0=%d\n",buffer[0]);
allonq 4:7544d38698a3 49 uart.putc(0);
allonq 4:7544d38698a3 50 buffer[1]=uart.getc();//pc.printf("1=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 51 uart.putc(0);
allonq 4:7544d38698a3 52 buffer[2]=uart.getc();//pc.printf("2=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 53 uart.putc(0);
allonq 4:7544d38698a3 54 buffer[3]=uart.getc();//pc.printf("3=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 55 uart.putc(0);
allonq 4:7544d38698a3 56 per=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
allonq 4:7544d38698a3 57
allonq 4:7544d38698a3 58 Syc();
allonq 5:54fbe45eef1c 59
allonq 0:7c4f7de16626 60 loop_num=0;
allonq 4:7544d38698a3 61 Period=per;
allonq 4:7544d38698a3 62
allonq 0:7c4f7de16626 63 }
allonq 0:7c4f7de16626 64
allonq 4:7544d38698a3 65 void TPM0_IRQHandler(void){
allonq 4:7544d38698a3 66 loop_num++;
allonq 4:7544d38698a3 67 //syc_cnt++;
allonq 4:7544d38698a3 68
allonq 4:7544d38698a3 69 if(loop_num==Period){
allonq 4:7544d38698a3 70 out=!out;LED=!LED;per_num++;loop_num=0;
allonq 4:7544d38698a3 71 }
allonq 0:7c4f7de16626 72 TPM0->SC|= 0x000000c8;
allonq 4:7544d38698a3 73
allonq 0:7c4f7de16626 74 return;
allonq 0:7c4f7de16626 75 }
allonq 0:7c4f7de16626 76
allonq 0:7c4f7de16626 77
allonq 4:7544d38698a3 78 void cmd_handler(){
allonq 4:7544d38698a3 79 char tmp;
allonq 4:7544d38698a3 80 if(uart.readable()){
allonq 4:7544d38698a3 81 pc.printf("read\n");
allonq 4:7544d38698a3 82 tmp=uart.getc();
allonq 4:7544d38698a3 83 pc.printf(&tmp);
allonq 4:7544d38698a3 84
allonq 4:7544d38698a3 85 if(tmp=='#')uart.putc(0);synchronize();}
allonq 0:7c4f7de16626 86
allonq 0:7c4f7de16626 87 }
allonq 4:7544d38698a3 88
allonq 4:7544d38698a3 89
allonq 4:7544d38698a3 90
allonq 0:7c4f7de16626 91 void Initial(){
allonq 2:4457eb1e9b83 92 for(int i=0;i<4;i++)buffer[i]=255;
allonq 2:4457eb1e9b83 93 loop_num=0;
allonq 4:7544d38698a3 94 per_num=0;
allonq 4:7544d38698a3 95 Period=5000;
allonq 4:7544d38698a3 96 out=0;
allonq 0:7c4f7de16626 97 LED=1;
allonq 4:7544d38698a3 98 SIM->SOPT2=0x05000000;
allonq 4:7544d38698a3 99 SIM->SCGC6=0x01000000;//enable TPM 0
allonq 4:7544d38698a3 100 //TPM0->SC=0x0;
allonq 2:4457eb1e9b83 101
allonq 0:7c4f7de16626 102 TPM0->CNT=0x0;
allonq 0:7c4f7de16626 103 TPM0->SC=0x00000040;
allonq 4:7544d38698a3 104 TPM0->MOD=0x0000bb80;
allonq 0:7c4f7de16626 105 TPM0->SC=0x00000048;//0008
allonq 0:7c4f7de16626 106
allonq 0:7c4f7de16626 107
allonq 0:7c4f7de16626 108 }
allonq 0:7c4f7de16626 109 int main() {
allonq 2:4457eb1e9b83 110 Initial();
allonq 2:4457eb1e9b83 111
allonq 0:7c4f7de16626 112 pc.baud(9600);
allonq 4:7544d38698a3 113 uart.baud(9600);
allonq 2:4457eb1e9b83 114
allonq 0:7c4f7de16626 115 NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
allonq 0:7c4f7de16626 116 NVIC_SetPriority(TPM0_IRQn, 1);
allonq 2:4457eb1e9b83 117 NVIC_EnableIRQ(TPM0_IRQn);
allonq 2:4457eb1e9b83 118
allonq 4:7544d38698a3 119 uart.attach(& cmd_handler);
allonq 0:7c4f7de16626 120 }