generation_mbed2

Dependencies:   mbed 202hm2_slave

Dependents:   202hm2_slave 202hm2_slave_generation 202hm2_slave_monitor

Committer:
allonq
Date:
Sun Mar 16 08:15:50 2014 +0000
Revision:
5:019db60eb5a6
Parent:
2:4457eb1e9b83
//EE 202 hm2; //This is a program built for the mbed2(slave) in monitor 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:019db60eb5a6 1 //EE 202 hm2
allonq 5:019db60eb5a6 2 //This is a program built for the mbed2(slave) in monitor mode
allonq 5:019db60eb5a6 3 //This code has been tested and should be function, if you has any problem,
allonq 5:019db60eb5a6 4 //please mail me.
allonq 5:019db60eb5a6 5 //Author: Yujing Qian
allonq 5:019db60eb5a6 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 5:019db60eb5a6 12 InterruptIn dig_in(PTD4);
allonq 0:7c4f7de16626 13 Serial pc(USBTX,USBRX);
allonq 5:019db60eb5a6 14 Serial uart_bbb(PTE16,PTE17);
allonq 0:7c4f7de16626 15
allonq 0:7c4f7de16626 16 Serial uart(PTE0, PTE1);
allonq 5:019db60eb5a6 17
allonq 2:4457eb1e9b83 18 int loop_num=0;
allonq 5:019db60eb5a6 19 int per_num=0;
allonq 5:019db60eb5a6 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 5:019db60eb5a6 26 int per=0;
allonq 0:7c4f7de16626 27 char buffer[buffer_size];
allonq 0:7c4f7de16626 28 int buff=0;
allonq 5:019db60eb5a6 29 int drift_cnt=0;
allonq 5:019db60eb5a6 30 uint32_t second=0;
allonq 5:019db60eb5a6 31 bool flag=false;
allonq 5:019db60eb5a6 32 uint32_t TIME(){
allonq 5:019db60eb5a6 33 return (per_num*Period*48000+loop_num*48000+(TPM0->CNT));
allonq 5:019db60eb5a6 34 }
allonq 0:7c4f7de16626 35
allonq 5:019db60eb5a6 36 void report(){
allonq 5:019db60eb5a6 37 int us=(TPM0->CNT);
allonq 5:019db60eb5a6 38 uint32_t ms=per_num*Period+loop_num;
allonq 5:019db60eb5a6 39 uint32_t s=ms/1000;//second
allonq 5:019db60eb5a6 40 ms=ms%1000;
allonq 5:019db60eb5a6 41 uart_bbb.printf("n%u:%d\n",s*1000+ms,us);
allonq 5:019db60eb5a6 42 pc.printf("n%u:%u:%d\n",s,ms,us);
allonq 5:019db60eb5a6 43 drift_cnt++;
allonq 5:019db60eb5a6 44 }
allonq 5:019db60eb5a6 45 void Syc(){// calculate delay using hand shake
allonq 5:019db60eb5a6 46 uart.getc();
allonq 5:019db60eb5a6 47 uart.putc(0);
allonq 5:019db60eb5a6 48 uart.getc();
allonq 5:019db60eb5a6 49 LED=1;
allonq 5:019db60eb5a6 50 TPM0->CNT=0;
allonq 5:019db60eb5a6 51 TPM0->SC=0x00000048;//0008
allonq 5:019db60eb5a6 52 }
allonq 1:76a206e19490 53 void synchronize(){
allonq 2:4457eb1e9b83 54 //pc.printf("state=%d\n",state);
allonq 5:019db60eb5a6 55
allonq 5:019db60eb5a6 56 buffer[0]=uart.getc();//pc.printf("0=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 57 uart.putc(0);
allonq 5:019db60eb5a6 58 buffer[1]=uart.getc();//pc.printf("1=%d\n",buffer[0]);
allonq 2:4457eb1e9b83 59 uart.putc(0);
allonq 5:019db60eb5a6 60 buffer[2]=uart.getc();//pc.printf("2=%d\n",buffer[0]);
allonq 5:019db60eb5a6 61 uart.putc(0);
allonq 5:019db60eb5a6 62 buffer[3]=uart.getc();//pc.printf("3=%d\n",buffer[0]);
allonq 5:019db60eb5a6 63 uart.putc(0);
allonq 5:019db60eb5a6 64 per=((int)buffer[3]<<24)+((int)buffer[2]<<16)+((int)buffer[1]<<8)+((int)buffer[0]);
allonq 2:4457eb1e9b83 65
allonq 5:019db60eb5a6 66 Syc();
allonq 2:4457eb1e9b83 67
allonq 5:019db60eb5a6 68 //pc.printf("Syc_over\n");
allonq 0:7c4f7de16626 69 loop_num=0;
allonq 5:019db60eb5a6 70 per_num=0;
allonq 5:019db60eb5a6 71 Period=per;
allonq 5:019db60eb5a6 72 drift_cnt=0;
allonq 5:019db60eb5a6 73 second=0;
allonq 5:019db60eb5a6 74 flag=true;
allonq 5:019db60eb5a6 75
allonq 0:7c4f7de16626 76 }
allonq 0:7c4f7de16626 77
allonq 5:019db60eb5a6 78 void TPM0_IRQHandler(void){
allonq 5:019db60eb5a6 79 loop_num++;
allonq 5:019db60eb5a6 80 if(loop_num==Period){
allonq 5:019db60eb5a6 81
allonq 5:019db60eb5a6 82 LED=!LED;
allonq 5:019db60eb5a6 83 per_num++;
allonq 5:019db60eb5a6 84 loop_num=0;
allonq 5:019db60eb5a6 85 }
allonq 0:7c4f7de16626 86 TPM0->SC|= 0x000000c8;
allonq 0:7c4f7de16626 87 return;
allonq 0:7c4f7de16626 88 }
allonq 0:7c4f7de16626 89
allonq 0:7c4f7de16626 90
allonq 5:019db60eb5a6 91 void cmd_handler(){
allonq 5:019db60eb5a6 92 char tmp;
allonq 5:019db60eb5a6 93 if(uart.readable()){
allonq 5:019db60eb5a6 94 pc.printf("read\n");
allonq 5:019db60eb5a6 95 tmp=uart.getc();
allonq 5:019db60eb5a6 96 pc.printf(&tmp);
allonq 5:019db60eb5a6 97
allonq 5:019db60eb5a6 98 if(tmp=='#')uart.putc(0);synchronize();}
allonq 0:7c4f7de16626 99
allonq 0:7c4f7de16626 100 }
allonq 5:019db60eb5a6 101
allonq 5:019db60eb5a6 102
allonq 5:019db60eb5a6 103
allonq 0:7c4f7de16626 104 void Initial(){
allonq 2:4457eb1e9b83 105 for(int i=0;i<4;i++)buffer[i]=255;
allonq 2:4457eb1e9b83 106 loop_num=0;
allonq 5:019db60eb5a6 107 per_num=0;
allonq 5:019db60eb5a6 108 Period=5000;
allonq 5:019db60eb5a6 109 //out=0;
allonq 0:7c4f7de16626 110 LED=1;
allonq 5:019db60eb5a6 111 SIM->SOPT2=0x05000000;
allonq 5:019db60eb5a6 112 SIM->SCGC6=0x01000000;//enable TPM 0
allonq 5:019db60eb5a6 113 //TPM0->SC=0x0;
allonq 2:4457eb1e9b83 114
allonq 0:7c4f7de16626 115 TPM0->CNT=0x0;
allonq 0:7c4f7de16626 116 TPM0->SC=0x00000040;
allonq 5:019db60eb5a6 117 TPM0->MOD=0x0000bb80;
allonq 0:7c4f7de16626 118 TPM0->SC=0x00000048;//0008
allonq 0:7c4f7de16626 119
allonq 0:7c4f7de16626 120
allonq 0:7c4f7de16626 121 }
allonq 0:7c4f7de16626 122 int main() {
allonq 2:4457eb1e9b83 123 Initial();
allonq 2:4457eb1e9b83 124
allonq 0:7c4f7de16626 125 pc.baud(9600);
allonq 5:019db60eb5a6 126 uart.baud(9600);
allonq 5:019db60eb5a6 127 uart_bbb.baud(9600);
allonq 2:4457eb1e9b83 128
allonq 0:7c4f7de16626 129 NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
allonq 0:7c4f7de16626 130 NVIC_SetPriority(TPM0_IRQn, 1);
allonq 2:4457eb1e9b83 131 NVIC_EnableIRQ(TPM0_IRQn);
allonq 0:7c4f7de16626 132
allonq 5:019db60eb5a6 133 dig_in.rise(report);
allonq 5:019db60eb5a6 134 uart.attach(& cmd_handler);
allonq 0:7c4f7de16626 135 }