Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX, USBRX); // tx, rx 00004 Serial ss(p9, p10); //tx rx 00005 Serial ss_sne(p13, p14); 00006 DigitalOut myled(LED3); 00007 DigitalOut rst1(p11); 00008 DigitalOut slaveOut(p23); 00009 00010 00011 void Timer0_init(void); 00012 void Timer0_IRQHandler(void); 00013 void slavesyn(void); 00014 void backtime(void); 00015 long long assembletime(long long t); 00016 int runAtTime (void (*fptr)(void), struct timeval tv); 00017 void getTime(struct timeval *tv); 00018 void toggle(void); 00019 00020 long counter = 0; 00021 int divider = 4; 00022 long long t1; 00023 long long t2, t3, t4, t5, t6; 00024 long long delay; 00025 long long offdri; 00026 char test; 00027 long long receive(void); 00028 long long receive_sne(void); 00029 long long snesig; 00030 long long sne(void); 00031 int task_flag = 0; 00032 00033 typedef struct timeval { 00034 unsigned long tv_sec; 00035 unsigned long tv_usec; 00036 } timeval; 00037 00038 struct timeval t_run; 00039 struct timeval t_run2; 00040 00041 int main( ) 00042 { 00043 pc.printf("\nbegin\n"); 00044 Timer0_init( ); 00045 t_run.tv_sec = 1000; 00046 t_run.tv_usec = 0; 00047 00048 t_run2.tv_sec = 1000; 00049 t_run2.tv_usec = 0; 00050 00051 while (1) 00052 { 00053 myled = 0; 00054 t2 = 100000000000000000; 00055 if (!task_flag){ 00056 task_flag = runAtTime (&toggle, t_run2); 00057 } 00058 00059 if (ss_sne.readable()){ 00060 snesig = receive_sne(); 00061 t_run.tv_sec = snesig/1000000; 00062 t_run.tv_usec = snesig%1000000; 00063 // pc.printf("\nSNE is %llu\n",snesig); 00064 // pc.printf("\ntv_sec = %u, tv_usec = %u\n",t_run.tv_sec,t_run.tv_usec); 00065 task_flag = 0; 00066 } 00067 00068 if (ss.readable( )){ 00069 myled = 1; 00070 test = ss.getc(); 00071 // pc.printf("%d\n", test-'S'); 00072 00073 if (test == 'S' ){ 00074 slavesyn(); 00075 00076 } 00077 else{ 00078 if (test == 'T'){ 00079 backtime(); 00080 } 00081 else{ 00082 pc.printf("\nSystem Error--1!!\n"); 00083 } 00084 } 00085 } 00086 } 00087 } 00088 00089 void Timer0_init(void) 00090 { 00091 LPC_SC->PCLKSEL0 = 0x0066; 00092 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS 00093 LPC_TIM0->MCR = 3; // Interrupt and Reset on Match 00094 LPC_TIM0->TCR = 1; // Enable Timer0 00095 NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler); 00096 NVIC_EnableIRQ(TIMER0_IRQn); 00097 } 00098 00099 00100 00101 void Timer0_IRQHandler(void) 00102 { 00103 LPC_TIM0->IR = 1; 00104 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS 00105 LPC_TIM0->TCR = 1; // Enable Timer0 00106 counter = counter + 1; 00107 } 00108 00109 void slavesyn(void) 00110 { 00111 // pc.printf("in to fuc\n"); 00112 t2 = counter * 4294967296 + LPC_TIM0->TC; 00113 // pc.printf("t2 = %llu\n",t2); 00114 00115 t1 = receive(); 00116 // pc.printf("t1 = %llu\n",t1); 00117 00118 // wait(0.1); 00119 t3 = counter * 4294967296 + LPC_TIM0->TC; 00120 ss.putc('S'); 00121 // pc.printf("t3 = %llu\n", t3); 00122 00123 t4 = receive(); 00124 // pc.printf("t4 = %llu\n",t4); 00125 00126 00127 t1 = assembletime(t1); 00128 t2 = assembletime(t2); 00129 t3 = assembletime(t3); 00130 t4 = assembletime(t4); 00131 00132 // pc.printf("t2-t1 = %ll\n t1-t2 = %ll \n", (lld)t2-t1,(lld)t1-t2); 00133 00134 /* 00135 offdri = (t2/4294967296 - t1/4294967296) *30*1000000000 + (t2%4294967296 - t1%4294967296); 00136 // pc.printf("offderi = %llu\n",offdri); 00137 00138 delay = (t4/4294967296 - t3/4294967296) *4294967296 + (t4%4294967296 - t3%4294967296); 00139 // pc.printf("delay = %llu\n",delay); 00140 */ 00141 00142 00143 //assume t2>t1 since slave reset first 00144 if(t3>t4){ 00145 if ((t2-t1)>(t3-t4)){ 00146 delay = ((t2-t1)-(t3-t4))/2; 00147 offdri = ((t2-t1)+(t3-t4))/2; 00148 } 00149 else{ 00150 delay = ((t3-t4)-(t2-t1))/2; 00151 offdri = ((t2-t1)+(t3-t4))/2; 00152 } 00153 } 00154 else{ //t4>t3 00155 if ((t2-t1)<(t4-t3)){ 00156 delay = ((t2-t1)+(t4-t3))/2; 00157 offdri = ((t4-t3)-(t2-t1))/2; 00158 } 00159 else{ 00160 delay = ((t2-t1)+(t4-t3))/2; 00161 offdri = ((t2-t1)-(t4-t3))/2; 00162 } 00163 } 00164 t_run2.tv_sec = t_run.tv_sec + offdri/1000000000; 00165 t_run2.tv_usec = t_run.tv_usec + (offdri%1000000000)/1000; 00166 // pc.printf("\nt2v_sec = %u, t2v_usec = %u\n",t_run2.tv_sec,t_run2.tv_usec); 00167 // pc.printf("Delay = %llu\n",delay); 00168 // pc.printf("offderi = %llu\n",offdri); 00169 00170 00171 } 00172 00173 long long receive(void){ 00174 char x; 00175 long long y; 00176 00177 y=0; 00178 while(1){ 00179 if(ss.readable()){ 00180 x = ss.getc(); 00181 if(x == 'E'){ 00182 return y; 00183 } 00184 else{ 00185 y = y*10 + (int)(x-48); 00186 } 00187 } 00188 } 00189 } 00190 00191 long long assembletime(long long t){ 00192 long long temp; 00193 00194 temp = (t/4294967296)*30*1000000000 + (t%4294967296)*10; 00195 return temp; 00196 } 00197 00198 void backtime(){ 00199 long long t; 00200 long long temp; 00201 t = counter * 4294967296 + LPC_TIM0->TC; 00202 t = assembletime(t); 00203 t = t - offdri + delay; 00204 /* 00205 temp = t/30/1000000000; 00206 t = (t%30000000000)/10 + temp*4294967296; 00207 */ 00208 ss.printf("%llu",t); 00209 ss.putc('E'); 00210 } 00211 00212 void toggle(){ 00213 slaveOut = !slaveOut; 00214 } 00215 00216 long long receive_sne(void){ 00217 char x; 00218 long long y; 00219 00220 y=0; 00221 while(1){ 00222 if(ss_sne.readable()){ 00223 x = ss_sne.getc(); 00224 if(x == 'E'){ 00225 return y; 00226 } 00227 else{ 00228 y = y*10 + (int)(x-48); 00229 } 00230 } 00231 } 00232 } 00233 00234 void getTime(struct timeval *tv){ 00235 unsigned long ns, hs; 00236 ns = LPC_TIM0->TC; 00237 hs = counter; 00238 tv->tv_sec = hs*30 + ns/100000000; 00239 tv->tv_usec = (ns%100000000)/100; 00240 } 00241 00242 int runAtTime (void (*fptr)(void), struct timeval tv){ 00243 struct timeval current_t; 00244 getTime(¤t_t); 00245 if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){ 00246 (*fptr) ( ); 00247 return 1; 00248 } 00249 else 00250 return 0; 00251 } 00252
Generated on Wed Jul 13 2022 22:17:25 by
1.7.2