//EE 202 hm2 //This is a program built for the mbed1(master mbed) in generation mode //This code has been tested and should be function, if you has any problem, //please mail me.
main.cpp@3:5149e55c21e3, 2014-03-16 (annotated)
- Committer:
- allonq
- Date:
- Sun Mar 16 08:06:07 2014 +0000
- Revision:
- 3:5149e55c21e3
- Parent:
- 2:1c7e1110ac61
//EE 202 hm2; //This is a program built for the mbed1(master mbed) in generation mode; //This code has been tested and should be function, if you has any problem,; //please mail me.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
allonq | 3:5149e55c21e3 | 1 | //EE 202 hm2 |
allonq | 3:5149e55c21e3 | 2 | //This is a program built for the mbed1(master mbed) in generation mode |
allonq | 3:5149e55c21e3 | 3 | //This code has been tested and should be function, if you has any problem, |
allonq | 3:5149e55c21e3 | 4 | //please mail me. |
allonq | 3:5149e55c21e3 | 5 | //Author: Yujing Qian |
allonq | 3:5149e55c21e3 | 6 | //y.qian42@gmail.com |
allonq | 0:fac0542384d7 | 7 | #include "mbed.h" |
allonq | 0:fac0542384d7 | 8 | |
allonq | 0:fac0542384d7 | 9 | #define mod 0x0000bb80 |
allonq | 0:fac0542384d7 | 10 | #define duty_cycle 0.5 |
allonq | 0:fac0542384d7 | 11 | #define high_cycle mod*duty_cycle |
allonq | 0:fac0542384d7 | 12 | #define low_cycle mod-high_cycle |
allonq | 0:fac0542384d7 | 13 | #define buffer_size 9 |
allonq | 0:fac0542384d7 | 14 | #define BBBbuff_size 30 |
allonq | 0:fac0542384d7 | 15 | |
allonq | 0:fac0542384d7 | 16 | DigitalOut LED(LED_RED); |
allonq | 2:1c7e1110ac61 | 17 | DigitalOut out(PTD4); |
allonq | 2:1c7e1110ac61 | 18 | Serial uart_bbb(PTE16,PTE17); |
allonq | 2:1c7e1110ac61 | 19 | Serial uart(PTE0, PTE1); |
allonq | 0:fac0542384d7 | 20 | Serial pc(USBTX,USBRX); |
allonq | 0:fac0542384d7 | 21 | |
allonq | 1:55f3661a2050 | 22 | int loop_num=0; |
allonq | 2:1c7e1110ac61 | 23 | int per_num=0; |
allonq | 2:1c7e1110ac61 | 24 | int Period=500; |
allonq | 0:fac0542384d7 | 25 | int state=0; |
allonq | 0:fac0542384d7 | 26 | uint32_t T1=0; |
allonq | 0:fac0542384d7 | 27 | uint32_t T2=0; |
allonq | 0:fac0542384d7 | 28 | uint32_t T3=0; |
allonq | 0:fac0542384d7 | 29 | uint32_t T4=0; |
allonq | 2:1c7e1110ac61 | 30 | bool flag=false; |
allonq | 2:1c7e1110ac61 | 31 | int syc_f=2; |
allonq | 2:1c7e1110ac61 | 32 | int syc_interval=1000/syc_f; |
allonq | 2:1c7e1110ac61 | 33 | uint32_t syc_cnt=0; |
allonq | 2:1c7e1110ac61 | 34 | |
allonq | 0:fac0542384d7 | 35 | uint32_t Mod=mod; |
allonq | 0:fac0542384d7 | 36 | char BBBbuffer[BBBbuff_size]; |
allonq | 0:fac0542384d7 | 37 | bool cmd_received=false; |
allonq | 0:fac0542384d7 | 38 | int BBBbuff=0; |
allonq | 0:fac0542384d7 | 39 | char buffer[4]; |
allonq | 1:55f3661a2050 | 40 | int buff=0; |
allonq | 2:1c7e1110ac61 | 41 | uint32_t TIME(){return per_num*Period*48000+loop_num*48000+(TPM0->CNT);} |
allonq | 0:fac0542384d7 | 42 | |
allonq | 0:fac0542384d7 | 43 | void receive_handler(){ |
allonq | 3:5149e55c21e3 | 44 | while( pc.readable() && BBBbuff<BBBbuff_size){ |
allonq | 3:5149e55c21e3 | 45 | BBBbuffer[BBBbuff] =pc.getc(); |
allonq | 3:5149e55c21e3 | 46 | pc.putc(BBBbuffer[BBBbuff]); |
allonq | 0:fac0542384d7 | 47 | //pc.printf(& rx_buffer[buff]); |
allonq | 0:fac0542384d7 | 48 | if (BBBbuffer[BBBbuff] == '#'){ |
allonq | 0:fac0542384d7 | 49 | BBBbuffer[BBBbuff] = '\0'; |
allonq | 0:fac0542384d7 | 50 | cmd_received=true; |
allonq | 0:fac0542384d7 | 51 | BBBbuff++; |
allonq | 0:fac0542384d7 | 52 | pc.printf("cmd received\n"); |
allonq | 0:fac0542384d7 | 53 | break; |
allonq | 0:fac0542384d7 | 54 | } |
allonq | 0:fac0542384d7 | 55 | BBBbuff++; |
allonq | 0:fac0542384d7 | 56 | } |
allonq | 0:fac0542384d7 | 57 | return; |
allonq | 0:fac0542384d7 | 58 | } |
allonq | 0:fac0542384d7 | 59 | void TPM1_IRQHandler(void); |
allonq | 2:1c7e1110ac61 | 60 | uint32_t calculate_delay(){// calculate delay using hand shake |
allonq | 2:1c7e1110ac61 | 61 | |
allonq | 1:55f3661a2050 | 62 | |
allonq | 2:1c7e1110ac61 | 63 | T1=TIME(); |
allonq | 2:1c7e1110ac61 | 64 | uart.putc('a'); |
allonq | 2:1c7e1110ac61 | 65 | uart.getc(); |
allonq | 0:fac0542384d7 | 66 | T4=TIME(); |
allonq | 2:1c7e1110ac61 | 67 | |
allonq | 2:1c7e1110ac61 | 68 | return (T4-T1)/2; |
allonq | 2:1c7e1110ac61 | 69 | } |
allonq | 2:1c7e1110ac61 | 70 | void Syc(uint32_t delay){ |
allonq | 2:1c7e1110ac61 | 71 | //pc.printf("delay=%d\n",delay); |
allonq | 0:fac0542384d7 | 72 | TPM1->MOD=delay; |
allonq | 0:fac0542384d7 | 73 | TPM1->CNT=0x0; |
allonq | 2:1c7e1110ac61 | 74 | //loop_num=0; |
allonq | 2:1c7e1110ac61 | 75 | TPM1->SC=0x000000c8; |
allonq | 2:1c7e1110ac61 | 76 | NVIC_EnableIRQ(TPM1_IRQn); |
allonq | 2:1c7e1110ac61 | 77 | // open tpm1 |
allonq | 2:1c7e1110ac61 | 78 | uart.putc(0);//send signal to slave |
allonq | 2:1c7e1110ac61 | 79 | } |
allonq | 2:1c7e1110ac61 | 80 | void synchronize(uint32_t Cmd_per){ |
allonq | 2:1c7e1110ac61 | 81 | |
allonq | 2:1c7e1110ac61 | 82 | char* tmp=(char*)&Cmd_per; |
allonq | 2:1c7e1110ac61 | 83 | |
allonq | 2:1c7e1110ac61 | 84 | uart.getc(); |
allonq | 2:1c7e1110ac61 | 85 | //pc.printf("TFS love xuemei"); |
allonq | 2:1c7e1110ac61 | 86 | uart.putc((*tmp));//pc.printf("0=%d\n",*tmp); |
allonq | 2:1c7e1110ac61 | 87 | uart.getc(); |
allonq | 2:1c7e1110ac61 | 88 | uart.putc((*(tmp+1)));//pc.printf("1=%d\n",(*(tmp+1))); |
allonq | 2:1c7e1110ac61 | 89 | uart.getc(); |
allonq | 2:1c7e1110ac61 | 90 | uart.putc((*(tmp+2)));//pc.printf("2=%d\n",(*(tmp+2))); |
allonq | 2:1c7e1110ac61 | 91 | uart.getc(); |
allonq | 2:1c7e1110ac61 | 92 | uart.putc((*(tmp+3)));//pc.printf("3=%d\n",(*(tmp+3))); |
allonq | 3:5149e55c21e3 | 93 | uart.getc();//clear the buffer |
allonq | 2:1c7e1110ac61 | 94 | Period=Cmd_per; |
allonq | 3:5149e55c21e3 | 95 | Syc(calculate_delay()); |
allonq | 3:5149e55c21e3 | 96 | loop_num=0;//clear clock |
allonq | 2:1c7e1110ac61 | 97 | } |
allonq | 2:1c7e1110ac61 | 98 | |
allonq | 2:1c7e1110ac61 | 99 | void TPM0_IRQHandler(void){ |
allonq | 3:5149e55c21e3 | 100 | |
allonq | 2:1c7e1110ac61 | 101 | loop_num++; |
allonq | 3:5149e55c21e3 | 102 | |
allonq | 2:1c7e1110ac61 | 103 | if(loop_num==Period){ |
allonq | 2:1c7e1110ac61 | 104 | out=!out; |
allonq | 2:1c7e1110ac61 | 105 | LED=!LED; |
allonq | 2:1c7e1110ac61 | 106 | per_num++; |
allonq | 2:1c7e1110ac61 | 107 | loop_num=0; |
allonq | 2:1c7e1110ac61 | 108 | } |
allonq | 0:fac0542384d7 | 109 | TPM0->SC|= 0x000000c8; |
allonq | 2:1c7e1110ac61 | 110 | |
allonq | 0:fac0542384d7 | 111 | return; |
allonq | 0:fac0542384d7 | 112 | } |
allonq | 0:fac0542384d7 | 113 | |
allonq | 2:1c7e1110ac61 | 114 | void TPM1_IRQHandler(void){// time to restart the system |
allonq | 2:1c7e1110ac61 | 115 | LED=1; |
allonq | 0:fac0542384d7 | 116 | //TPM0->SC= 0x000000c8; |
allonq | 3:5149e55c21e3 | 117 | TPM0->CNT=0x0; |
allonq | 0:fac0542384d7 | 118 | TPM1->CNT=0x0; |
allonq | 2:1c7e1110ac61 | 119 | //pc.printf("SC=%d",TPM1->SC); |
allonq | 0:fac0542384d7 | 120 | TPM1->SC= 0x00000080; |
allonq | 2:1c7e1110ac61 | 121 | //TPM1->SC= 0x00000000; |
allonq | 2:1c7e1110ac61 | 122 | //pc.printf("SC_after=%d\n",TPM1->SC); |
allonq | 2:1c7e1110ac61 | 123 | //pc.printf("Tic-Toc\n"); |
allonq | 0:fac0542384d7 | 124 | NVIC_DisableIRQ(TPM1_IRQn); |
allonq | 2:1c7e1110ac61 | 125 | //NVIC_ClearPendingIRQ(TPM1_IRQn); |
allonq | 2:1c7e1110ac61 | 126 | //flag=true; |
allonq | 2:1c7e1110ac61 | 127 | //pc.printf("Tic-Toc\n"); |
allonq | 0:fac0542384d7 | 128 | return; |
allonq | 0:fac0542384d7 | 129 | } |
allonq | 0:fac0542384d7 | 130 | |
allonq | 2:1c7e1110ac61 | 131 | |
allonq | 0:fac0542384d7 | 132 | |
allonq | 0:fac0542384d7 | 133 | void Initial(){ |
allonq | 2:1c7e1110ac61 | 134 | Period=1000; |
allonq | 0:fac0542384d7 | 135 | LED=1; |
allonq | 2:1c7e1110ac61 | 136 | out=0; |
allonq | 2:1c7e1110ac61 | 137 | SIM->SOPT2=0x05000000; |
allonq | 0:fac0542384d7 | 138 | SIM->SCGC6=0x03000000;//enable TPM 0,1 |
allonq | 0:fac0542384d7 | 139 | TPM0->SC=0x0; |
allonq | 1:55f3661a2050 | 140 | |
allonq | 0:fac0542384d7 | 141 | TPM0->CNT=0x0; |
allonq | 0:fac0542384d7 | 142 | TPM0->SC=0x00000040; |
allonq | 0:fac0542384d7 | 143 | TPM0->MOD=0x0000bb80; |
allonq | 0:fac0542384d7 | 144 | TPM0->SC=0x00000048;//0008 |
allonq | 0:fac0542384d7 | 145 | |
allonq | 0:fac0542384d7 | 146 | TPM1->CNT=0x0; |
allonq | 0:fac0542384d7 | 147 | TPM1->SC=0x00000040; |
allonq | 0:fac0542384d7 | 148 | TPM1->MOD=0x0000ffff; |
allonq | 0:fac0542384d7 | 149 | TPM1->SC=0x00000008;//0008 |
allonq | 0:fac0542384d7 | 150 | } |
allonq | 0:fac0542384d7 | 151 | int main() { |
allonq | 1:55f3661a2050 | 152 | loop_num=0; |
allonq | 0:fac0542384d7 | 153 | cmd_received=false; |
allonq | 3:5149e55c21e3 | 154 | pc.attach(&receive_handler); |
allonq | 2:1c7e1110ac61 | 155 | int CMD_Per=0; |
allonq | 0:fac0542384d7 | 156 | Initial(); |
allonq | 0:fac0542384d7 | 157 | |
allonq | 3:5149e55c21e3 | 158 | pc.baud(9600); |
allonq | 3:5149e55c21e3 | 159 | pc.attach(&receive_handler); |
allonq | 2:1c7e1110ac61 | 160 | |
allonq | 2:1c7e1110ac61 | 161 | uart.baud(9600); |
allonq | 0:fac0542384d7 | 162 | pc.baud(9600); |
allonq | 0:fac0542384d7 | 163 | |
allonq | 0:fac0542384d7 | 164 | NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler); |
allonq | 0:fac0542384d7 | 165 | NVIC_SetPriority(TPM0_IRQn, 1); |
allonq | 0:fac0542384d7 | 166 | NVIC_EnableIRQ(TPM0_IRQn); |
allonq | 0:fac0542384d7 | 167 | |
allonq | 0:fac0542384d7 | 168 | NVIC_SetVector(TPM1_IRQn, (uint32_t)&TPM1_IRQHandler); |
allonq | 0:fac0542384d7 | 169 | NVIC_SetPriority(TPM1_IRQn, 0); |
allonq | 0:fac0542384d7 | 170 | NVIC_EnableIRQ(TPM1_IRQn); |
allonq | 2:1c7e1110ac61 | 171 | //wait(5); |
allonq | 2:1c7e1110ac61 | 172 | //pc.printf("Initial done\n"); |
allonq | 0:fac0542384d7 | 173 | |
allonq | 0:fac0542384d7 | 174 | |
allonq | 2:1c7e1110ac61 | 175 | pc.printf("please enter T(in us)...\n"); |
allonq | 2:1c7e1110ac61 | 176 | //Syc(2000000); |
allonq | 2:1c7e1110ac61 | 177 | |
allonq | 0:fac0542384d7 | 178 | //Syc(48000); |
allonq | 0:fac0542384d7 | 179 | |
allonq | 0:fac0542384d7 | 180 | while(1){ |
allonq | 0:fac0542384d7 | 181 | if(cmd_received){ |
allonq | 0:fac0542384d7 | 182 | pc.printf(BBBbuffer); |
allonq | 0:fac0542384d7 | 183 | for(int i=0;i<BBBbuff-1;i++){ |
allonq | 2:1c7e1110ac61 | 184 | CMD_Per*=10; |
allonq | 2:1c7e1110ac61 | 185 | CMD_Per+=(BBBbuffer[i]-'0'); |
allonq | 0:fac0542384d7 | 186 | } |
allonq | 0:fac0542384d7 | 187 | cmd_received=false; |
allonq | 0:fac0542384d7 | 188 | BBBbuff=0; |
allonq | 2:1c7e1110ac61 | 189 | Period=CMD_Per; |
allonq | 2:1c7e1110ac61 | 190 | uart.putc('#'); |
allonq | 2:1c7e1110ac61 | 191 | synchronize(CMD_Per); |
allonq | 2:1c7e1110ac61 | 192 | CMD_Per=0; |
allonq | 0:fac0542384d7 | 193 | } |
allonq | 0:fac0542384d7 | 194 | |
allonq | 0:fac0542384d7 | 195 | } |
allonq | 0:fac0542384d7 | 196 | |
allonq | 0:fac0542384d7 | 197 | |
allonq | 0:fac0542384d7 | 198 | } |