Dependencies:   mbed

Committer:
sun831011
Date:
Fri Dec 03 22:48:04 2010 +0000
Revision:
0:d3b34dbeb9bd

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sun831011 0:d3b34dbeb9bd 1 #include "mbed.h"
sun831011 0:d3b34dbeb9bd 2
sun831011 0:d3b34dbeb9bd 3 Serial pc(USBTX, USBRX); // tx, rx
sun831011 0:d3b34dbeb9bd 4 Serial ss(p9, p10); //tx rx
sun831011 0:d3b34dbeb9bd 5 Serial ss_sne(p13, p14);
sun831011 0:d3b34dbeb9bd 6 DigitalOut myled(LED3);
sun831011 0:d3b34dbeb9bd 7 DigitalOut rst1(p11);
sun831011 0:d3b34dbeb9bd 8 DigitalOut slaveOut(p23);
sun831011 0:d3b34dbeb9bd 9
sun831011 0:d3b34dbeb9bd 10
sun831011 0:d3b34dbeb9bd 11 void Timer0_init(void);
sun831011 0:d3b34dbeb9bd 12 void Timer0_IRQHandler(void);
sun831011 0:d3b34dbeb9bd 13 void slavesyn(void);
sun831011 0:d3b34dbeb9bd 14 void backtime(void);
sun831011 0:d3b34dbeb9bd 15 long long assembletime(long long t);
sun831011 0:d3b34dbeb9bd 16 int runAtTime (void (*fptr)(void), struct timeval tv);
sun831011 0:d3b34dbeb9bd 17 void getTime(struct timeval *tv);
sun831011 0:d3b34dbeb9bd 18 void toggle(void);
sun831011 0:d3b34dbeb9bd 19
sun831011 0:d3b34dbeb9bd 20 long counter = 0;
sun831011 0:d3b34dbeb9bd 21 int divider = 4;
sun831011 0:d3b34dbeb9bd 22 long long t1;
sun831011 0:d3b34dbeb9bd 23 long long t2, t3, t4, t5, t6;
sun831011 0:d3b34dbeb9bd 24 long long delay;
sun831011 0:d3b34dbeb9bd 25 long long offdri;
sun831011 0:d3b34dbeb9bd 26 char test;
sun831011 0:d3b34dbeb9bd 27 long long receive(void);
sun831011 0:d3b34dbeb9bd 28 long long receive_sne(void);
sun831011 0:d3b34dbeb9bd 29 long long snesig;
sun831011 0:d3b34dbeb9bd 30 long long sne(void);
sun831011 0:d3b34dbeb9bd 31 int task_flag = 0;
sun831011 0:d3b34dbeb9bd 32
sun831011 0:d3b34dbeb9bd 33 typedef struct timeval {
sun831011 0:d3b34dbeb9bd 34 unsigned long tv_sec;
sun831011 0:d3b34dbeb9bd 35 unsigned long tv_usec;
sun831011 0:d3b34dbeb9bd 36 } timeval;
sun831011 0:d3b34dbeb9bd 37
sun831011 0:d3b34dbeb9bd 38 struct timeval t_run;
sun831011 0:d3b34dbeb9bd 39
sun831011 0:d3b34dbeb9bd 40 int main( )
sun831011 0:d3b34dbeb9bd 41 {
sun831011 0:d3b34dbeb9bd 42 pc.printf("\nbegin\n");
sun831011 0:d3b34dbeb9bd 43 Timer0_init( );
sun831011 0:d3b34dbeb9bd 44 t_run.tv_sec = 1000;
sun831011 0:d3b34dbeb9bd 45 t_run.tv_usec = 0;
sun831011 0:d3b34dbeb9bd 46
sun831011 0:d3b34dbeb9bd 47 while (1)
sun831011 0:d3b34dbeb9bd 48 {
sun831011 0:d3b34dbeb9bd 49 myled = 0;
sun831011 0:d3b34dbeb9bd 50
sun831011 0:d3b34dbeb9bd 51 if (!task_flag){
sun831011 0:d3b34dbeb9bd 52 task_flag = runAtTime (&toggle, t_run);
sun831011 0:d3b34dbeb9bd 53 }
sun831011 0:d3b34dbeb9bd 54
sun831011 0:d3b34dbeb9bd 55 if (ss_sne.readable()){
sun831011 0:d3b34dbeb9bd 56 snesig = receive_sne();
sun831011 0:d3b34dbeb9bd 57 t_run.tv_sec = snesig/1000000;
sun831011 0:d3b34dbeb9bd 58 t_run.tv_usec = snesig%1000000;
sun831011 0:d3b34dbeb9bd 59 pc.printf("\nSNE is %llu\n",snesig);
sun831011 0:d3b34dbeb9bd 60 }
sun831011 0:d3b34dbeb9bd 61
sun831011 0:d3b34dbeb9bd 62 if (ss.readable( )){
sun831011 0:d3b34dbeb9bd 63 myled = 1;
sun831011 0:d3b34dbeb9bd 64 test = ss.getc();
sun831011 0:d3b34dbeb9bd 65 pc.printf("%d\n", test-'S');
sun831011 0:d3b34dbeb9bd 66
sun831011 0:d3b34dbeb9bd 67 if (test == 'S' ){
sun831011 0:d3b34dbeb9bd 68 slavesyn();
sun831011 0:d3b34dbeb9bd 69
sun831011 0:d3b34dbeb9bd 70 }
sun831011 0:d3b34dbeb9bd 71 else{
sun831011 0:d3b34dbeb9bd 72 if (test == 'T'){
sun831011 0:d3b34dbeb9bd 73 backtime();
sun831011 0:d3b34dbeb9bd 74 }
sun831011 0:d3b34dbeb9bd 75 else{
sun831011 0:d3b34dbeb9bd 76 pc.printf("\nSystem Error--1!!\n");
sun831011 0:d3b34dbeb9bd 77 }
sun831011 0:d3b34dbeb9bd 78 }
sun831011 0:d3b34dbeb9bd 79 }
sun831011 0:d3b34dbeb9bd 80 }
sun831011 0:d3b34dbeb9bd 81 }
sun831011 0:d3b34dbeb9bd 82
sun831011 0:d3b34dbeb9bd 83 void Timer0_init(void)
sun831011 0:d3b34dbeb9bd 84 {
sun831011 0:d3b34dbeb9bd 85 LPC_SC->PCLKSEL0 = 0x0066;
sun831011 0:d3b34dbeb9bd 86 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS
sun831011 0:d3b34dbeb9bd 87 LPC_TIM0->MCR = 3; // Interrupt and Reset on Match
sun831011 0:d3b34dbeb9bd 88 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:d3b34dbeb9bd 89 NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
sun831011 0:d3b34dbeb9bd 90 NVIC_EnableIRQ(TIMER0_IRQn);
sun831011 0:d3b34dbeb9bd 91 }
sun831011 0:d3b34dbeb9bd 92
sun831011 0:d3b34dbeb9bd 93
sun831011 0:d3b34dbeb9bd 94
sun831011 0:d3b34dbeb9bd 95 void Timer0_IRQHandler(void)
sun831011 0:d3b34dbeb9bd 96 {
sun831011 0:d3b34dbeb9bd 97 LPC_TIM0->IR = 1;
sun831011 0:d3b34dbeb9bd 98 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS
sun831011 0:d3b34dbeb9bd 99 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:d3b34dbeb9bd 100 counter = counter + 1;
sun831011 0:d3b34dbeb9bd 101 }
sun831011 0:d3b34dbeb9bd 102
sun831011 0:d3b34dbeb9bd 103 void slavesyn(void)
sun831011 0:d3b34dbeb9bd 104 {
sun831011 0:d3b34dbeb9bd 105 pc.printf("in to fuc\n");
sun831011 0:d3b34dbeb9bd 106 t2 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:d3b34dbeb9bd 107 pc.printf("t2 = %llu\n",t2);
sun831011 0:d3b34dbeb9bd 108
sun831011 0:d3b34dbeb9bd 109 t1 = receive();
sun831011 0:d3b34dbeb9bd 110 pc.printf("t1 = %llu\n",t1);
sun831011 0:d3b34dbeb9bd 111
sun831011 0:d3b34dbeb9bd 112 wait(2);
sun831011 0:d3b34dbeb9bd 113 t3 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:d3b34dbeb9bd 114 ss.putc('S');
sun831011 0:d3b34dbeb9bd 115 pc.printf("t3 = %llu\n", t3);
sun831011 0:d3b34dbeb9bd 116
sun831011 0:d3b34dbeb9bd 117 t4 = receive();
sun831011 0:d3b34dbeb9bd 118 pc.printf("t4 = %llu\n",t4);
sun831011 0:d3b34dbeb9bd 119
sun831011 0:d3b34dbeb9bd 120
sun831011 0:d3b34dbeb9bd 121 t1 = assembletime(t1);
sun831011 0:d3b34dbeb9bd 122 t2 = assembletime(t2);
sun831011 0:d3b34dbeb9bd 123 t3 = assembletime(t3);
sun831011 0:d3b34dbeb9bd 124 t4 = assembletime(t4);
sun831011 0:d3b34dbeb9bd 125
sun831011 0:d3b34dbeb9bd 126 // pc.printf("t2-t1 = %ll\n t1-t2 = %ll \n", (lld)t2-t1,(lld)t1-t2);
sun831011 0:d3b34dbeb9bd 127
sun831011 0:d3b34dbeb9bd 128
sun831011 0:d3b34dbeb9bd 129 offdri = (t2/4294967296 - t1/4294967296) *30*1000000000 + (t2%4294967296 - t1%4294967296);
sun831011 0:d3b34dbeb9bd 130 pc.printf("offderi = %llu\n",offdri);
sun831011 0:d3b34dbeb9bd 131
sun831011 0:d3b34dbeb9bd 132 t_run.tv_sec = t_run.tv_sec + (t2 - t1)/1000000000;
sun831011 0:d3b34dbeb9bd 133 t_run.tv_usec = t_run.tv_usec + ((t2-t1)/1000)%1000000;
sun831011 0:d3b34dbeb9bd 134
sun831011 0:d3b34dbeb9bd 135 delay = (t4/4294967296 - t3/4294967296) *4294967296 + (t4%4294967296 - t3%4294967296);
sun831011 0:d3b34dbeb9bd 136 pc.printf("delay = %llu\n",delay);
sun831011 0:d3b34dbeb9bd 137
sun831011 0:d3b34dbeb9bd 138 //assume t2>t1 since slave reset first
sun831011 0:d3b34dbeb9bd 139 if(t3>t4){
sun831011 0:d3b34dbeb9bd 140 if ((t2-t1)>(t3-t4)){
sun831011 0:d3b34dbeb9bd 141 delay = ((t2-t1)-(t3-t4))/2;
sun831011 0:d3b34dbeb9bd 142 offdri = ((t2-t1)+(t3-t4))/2;
sun831011 0:d3b34dbeb9bd 143 }
sun831011 0:d3b34dbeb9bd 144 else{
sun831011 0:d3b34dbeb9bd 145 delay = ((t3-t4)-(t2-t1))/2;
sun831011 0:d3b34dbeb9bd 146 offdri = ((t2-t1)+(t3-t4))/2;
sun831011 0:d3b34dbeb9bd 147 }
sun831011 0:d3b34dbeb9bd 148 }
sun831011 0:d3b34dbeb9bd 149 else{ //t4>t3
sun831011 0:d3b34dbeb9bd 150 if ((t2-t1)<(t4-t3)){
sun831011 0:d3b34dbeb9bd 151 delay = ((t2-t1)+(t4-t3))/2;
sun831011 0:d3b34dbeb9bd 152 offdri = ((t4-t3)-(t2-t1))/2;
sun831011 0:d3b34dbeb9bd 153 }
sun831011 0:d3b34dbeb9bd 154 else{
sun831011 0:d3b34dbeb9bd 155 delay = ((t2-t1)+(t4-t3))/2;
sun831011 0:d3b34dbeb9bd 156 offdri = ((t2-t1)-(t4-t3))/2;
sun831011 0:d3b34dbeb9bd 157 }
sun831011 0:d3b34dbeb9bd 158 }
sun831011 0:d3b34dbeb9bd 159
sun831011 0:d3b34dbeb9bd 160 pc.printf("Delay = %llu\n",delay);
sun831011 0:d3b34dbeb9bd 161 pc.printf("offderi = %llu\n",offdri);
sun831011 0:d3b34dbeb9bd 162
sun831011 0:d3b34dbeb9bd 163
sun831011 0:d3b34dbeb9bd 164 }
sun831011 0:d3b34dbeb9bd 165
sun831011 0:d3b34dbeb9bd 166 long long receive(void){
sun831011 0:d3b34dbeb9bd 167 char x;
sun831011 0:d3b34dbeb9bd 168 long long y;
sun831011 0:d3b34dbeb9bd 169
sun831011 0:d3b34dbeb9bd 170 y=0;
sun831011 0:d3b34dbeb9bd 171 while(1){
sun831011 0:d3b34dbeb9bd 172 if(ss.readable()){
sun831011 0:d3b34dbeb9bd 173 x = ss.getc();
sun831011 0:d3b34dbeb9bd 174 if(x == 'E'){
sun831011 0:d3b34dbeb9bd 175 return y;
sun831011 0:d3b34dbeb9bd 176 }
sun831011 0:d3b34dbeb9bd 177 else{
sun831011 0:d3b34dbeb9bd 178 y = y*10 + (int)(x-48);
sun831011 0:d3b34dbeb9bd 179 }
sun831011 0:d3b34dbeb9bd 180 }
sun831011 0:d3b34dbeb9bd 181 }
sun831011 0:d3b34dbeb9bd 182 }
sun831011 0:d3b34dbeb9bd 183
sun831011 0:d3b34dbeb9bd 184 long long assembletime(long long t){
sun831011 0:d3b34dbeb9bd 185 long long temp;
sun831011 0:d3b34dbeb9bd 186
sun831011 0:d3b34dbeb9bd 187 temp = (t/4294967296)*30*1000000000 + (t%4294967296)*10;
sun831011 0:d3b34dbeb9bd 188 return temp;
sun831011 0:d3b34dbeb9bd 189 }
sun831011 0:d3b34dbeb9bd 190
sun831011 0:d3b34dbeb9bd 191 void backtime(){
sun831011 0:d3b34dbeb9bd 192 long long t;
sun831011 0:d3b34dbeb9bd 193 long long temp;
sun831011 0:d3b34dbeb9bd 194 t = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:d3b34dbeb9bd 195 t = assembletime(t);
sun831011 0:d3b34dbeb9bd 196 t = t - offdri + delay;
sun831011 0:d3b34dbeb9bd 197 /*
sun831011 0:d3b34dbeb9bd 198 temp = t/30/1000000000;
sun831011 0:d3b34dbeb9bd 199 t = (t%30000000000)/10 + temp*4294967296;
sun831011 0:d3b34dbeb9bd 200 */
sun831011 0:d3b34dbeb9bd 201 ss.printf("%llu",t);
sun831011 0:d3b34dbeb9bd 202 ss.putc('E');
sun831011 0:d3b34dbeb9bd 203 }
sun831011 0:d3b34dbeb9bd 204
sun831011 0:d3b34dbeb9bd 205 void toggle(){
sun831011 0:d3b34dbeb9bd 206 slaveOut = !slaveOut;
sun831011 0:d3b34dbeb9bd 207 }
sun831011 0:d3b34dbeb9bd 208
sun831011 0:d3b34dbeb9bd 209 long long receive_sne(void){
sun831011 0:d3b34dbeb9bd 210 char x;
sun831011 0:d3b34dbeb9bd 211 long long y;
sun831011 0:d3b34dbeb9bd 212
sun831011 0:d3b34dbeb9bd 213 y=0;
sun831011 0:d3b34dbeb9bd 214 while(1){
sun831011 0:d3b34dbeb9bd 215 if(ss_sne.readable()){
sun831011 0:d3b34dbeb9bd 216 x = ss_sne.getc();
sun831011 0:d3b34dbeb9bd 217 if(x == 'E'){
sun831011 0:d3b34dbeb9bd 218 return y;
sun831011 0:d3b34dbeb9bd 219 }
sun831011 0:d3b34dbeb9bd 220 else{
sun831011 0:d3b34dbeb9bd 221 y = y*10 + (int)(x-48);
sun831011 0:d3b34dbeb9bd 222 }
sun831011 0:d3b34dbeb9bd 223 }
sun831011 0:d3b34dbeb9bd 224 }
sun831011 0:d3b34dbeb9bd 225 }
sun831011 0:d3b34dbeb9bd 226
sun831011 0:d3b34dbeb9bd 227 void getTime(struct timeval *tv){
sun831011 0:d3b34dbeb9bd 228 unsigned long ns, hs;
sun831011 0:d3b34dbeb9bd 229 ns = LPC_TIM0->TC;
sun831011 0:d3b34dbeb9bd 230 hs = counter;
sun831011 0:d3b34dbeb9bd 231 tv->tv_sec = hs*30 + ns/100000000;
sun831011 0:d3b34dbeb9bd 232 tv->tv_usec = (ns%100000000)/100;
sun831011 0:d3b34dbeb9bd 233 }
sun831011 0:d3b34dbeb9bd 234
sun831011 0:d3b34dbeb9bd 235 int runAtTime (void (*fptr)(void), struct timeval tv){
sun831011 0:d3b34dbeb9bd 236 struct timeval current_t;
sun831011 0:d3b34dbeb9bd 237 getTime(&current_t);
sun831011 0:d3b34dbeb9bd 238 if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){
sun831011 0:d3b34dbeb9bd 239 (*fptr) ( );
sun831011 0:d3b34dbeb9bd 240 return 1;
sun831011 0:d3b34dbeb9bd 241 }
sun831011 0:d3b34dbeb9bd 242 else
sun831011 0:d3b34dbeb9bd 243 return 0;
sun831011 0:d3b34dbeb9bd 244 }
sun831011 0:d3b34dbeb9bd 245