manu jose
/
6hrmaster
B.3 : THE 6 HOUR PROGRAM WITH 1 PPS SIGNAL THE MASTER TO BE CONNECTED TO 11 PIN.
Embed:
(wiki syntax)
Show/hide line numbers
problemb1.h
00001 #ifndef PROBLEMB1_H_ 00002 #define PROBLEMB1_H_ 00003 #include "mbed.h" 00004 #include "pt.h" 00005 #include "pt-sem.h" 00006 #include "decl.h" 00007 #include "pqueue.h" 00008 void pinToggle(void); 00009 pQueue globalQ; 00010 unsigned int gTime =0; 00011 int B; 00012 00013 00014 timeval ideal_trg_time[100]; 00015 int ideal_trg_time_index = 0; 00016 00017 timeval pps; 00018 00019 union { 00020 unsigned int t; 00021 char BYTE[4]; 00022 }t1,t2,t3,t4; 00023 00024 00025 00026 void initialSetup(void) { 00027 // doing capture register for now, so use timer 2 00028 LPC_SC->PCLKSEL1 |= (0x01 << 12); //set the frequency REF: USER MANULA TAB 40,41,42 ?? check 00029 LPC_SC-> PCONP |= (0x3 << 22); // timer 2 00030 00031 //LPC_TIM2->TCR = 0x2; // Reset and set to timer mode 00032 00033 // Select PIN 00034 00035 LPC_PINCON->PINSEL0 |= (0xf << 8); 00036 //LPC_TIM2->CCR |= 0x07;// doing capture register for now, so use timer 2 00037 LPC_TIM2->CCR |= 0x05; 00038 LPC_TIM2->MR0 = RESET_42; 00039 LPC_TIM2->MCR = 3; // Interrupt, Stop, and Reset on match 00040 00041 //LPC_TIM2->TCR = 0x2; // Reset and set to timer mode 00042 00043 LPC_TIM2->TCR = 0x1; 00044 00045 00046 // ================ // 00047 00048 00049 00050 00051 /*LPC_SC->PCLKSEL0 |= 0x04; //set the frequency REF: USER MANULA TAB 40,41,42 ?? check 00052 00053 LPC_SC-> PCONP |= 1 << 1; // Power on Timer0 00054 00055 LPC_TIM0->TCR = 0x2; // Reset and set to timer mode 00056 LPC_TIM0->CTCR = 0x0; 00057 LPC_TIM0->PR = 0; // No prescale 00058 //LPC_TIM0->MR0 = 0xF0537000 ; // Match count for 100mS 00059 LPC_TIM0->MR0 = RESET_42; 00060 LPC_TIM0->MCR = 3; // Interrupt, Stop, and Reset on match 00061 00062 00063 LPC_TIM0->TCR = 1; // Enable Timer0 00064 // Enable the ISR vector 00065 */ 00066 NVIC_SetVector (TIMER2_IRQn, (uint32_t)&Timer0_IRQHandler); 00067 NVIC_EnableIRQ(TIMER2_IRQn); 00068 00069 // LPC_TIM0->MCR |= 11; //for mr1 and mr0 00070 //queue set up 00071 globalQ.numEle = 0; 00072 globalQ.head = NULL; 00073 } 00074 void Timer0_IRQHandler(void) { 00075 // LPC_TIM0->IR=0xff; 00076 //LPC_TIM0->MR0 = 0x5370; 00077 // pc.printf(" THE INTERERUPT\n"); 00078 if (LPC_TIM2->IR&1) { 00079 gTime++; 00080 // pc.printf("\ngtime = %X",LPC_TIM2->IR); 00081 } if ( (LPC_TIM2->IR >> 1)&1 ){ //RUN THE RUNAT TIME HIT 00082 // M qEle *ele = pop(&globalQ); 00083 00084 // pc.printf("RUN AT TIME TRIGGERED\n"); 00085 //M ele->foo(); 00086 pinToggle(); 00087 //M globalQ.head->foo(); 00088 // M free(ele); 00089 00090 } 00091 if( (LPC_TIM2->IR >> 4 )& 1){ // run report toggle 00092 timeval curT; 00093 unsigned int nMSec = (LPC_TIM2->CR0)/CLK_FRQ; 00094 // pc.printf(" THE SEC = %d \n",nMSec); 00095 unsigned int nSec = nMSec/1000000; 00096 curT.tv_sec = gTime*42 + nSec; 00097 curT.tv_usec = nMSec-(nSec*1000000); 00098 gtrigFunc(&curT); 00099 00100 } 00101 00102 //check if more ele for schedule this time ? 00103 //M if ((globalQ.numEle >0) && (globalQ.head->sched ==0)&& (globalQ.head->t.tv_sec <((gTime+1)*42))) { 00104 //M globalQ.head->sched = 1; 00105 00106 // LPC_TIM0->MR1 = (globalQ.head->t.tv_sec-gTime*42)*96000000 +globalQ.head->t.tv_usec*96; 00107 //M LPC_TIM2->MR1 = (globalQ.head->t.tv_sec-gTime*42)*CLK_FREQUENCY +globalQ.head->t.tv_usec*CLK_FRQ; 00108 //M LPC_TIM2->MCR |= 8; 00109 // pc.printf("\n MR1 = %X",LPC_TIM2->MR1); 00110 00111 00112 //M } else if (globalQ.head->sched == 0) //NO events in this gTime update yet . 00113 //M { //turn off the match regiester 1 interrupts. 00114 //M LPC_TIM2->MCR &= 3; 00115 //M} 00116 00117 // timeval t; 00118 // getTime(&t); 00119 LPC_TIM2->IR = 0xff; 00120 // pc.printf("INCREMENTING COUNTER or event %d \n",t.tv_sec); 00121 } 00122 00123 00124 void getTime(timeval *tv) { 00125 unsigned int nMSec = (LPC_TIM2->TC)/CLK_FRQ; //gives num of micro sec 00126 // unsigned int nMSec = (LPC_TIM0->TC + offset)/72; 00127 unsigned int nSec = nMSec/1000000; 00128 tv->tv_sec = gTime*42 + nSec; 00129 // tv->tv_usec = (LPC_TIM0->TC)*1000 + (float)(LPC_TIM0->PC)/( CLK_FREQUENCY * 1000000); 00130 tv->tv_usec = nMSec-(nSec*1000000); 00131 } 00132 00133 int curTimeEqualGR(timeval *tv) { 00134 timeval curT; 00135 getTime(&curT); 00136 if (curT.tv_sec == tv->tv_sec) { 00137 if (curT.tv_usec == tv->tv_usec) 00138 return 1; 00139 else 00140 pc.printf("WROING MICRO CALIBERATION\n"); 00141 return 1; 00142 } else if (curT.tv_sec > tv->tv_sec) { 00143 pc.printf("WRONG cur = %d and req = %d \n",curT.tv_sec, tv->tv_sec); 00144 return 1; 00145 } 00146 return 0; 00147 } 00148 int x =0; 00149 int runAtTime(void (*schedFunc)(void), timeval *tv) { 00150 int ret =0; 00151 if(!x) 00152 { 00153 x=1; 00154 ret = enqueue(&globalQ, *tv, schedFunc); 00155 } 00156 // pc.printf("The sec = %d and pps = %d\n",tv->tv_sec, tv->tv_usec); 00157 globalQ.head->t.tv_sec = tv->tv_sec; 00158 globalQ.head->t.tv_usec = tv->tv_usec; 00159 if (tv->tv_sec < (gTime+1)*42) { 00160 00161 // if (globalQ.head->sched == 0) { 00162 00163 LPC_TIM2->MR1 = ((globalQ.head->t.tv_sec%42 )*CLK_FREQUENCY+(globalQ.head->t.tv_usec*CLK_FRQ)); 00164 globalQ.head->sched = 1; 00165 // pc.printf("SCHEUDLIN MR1 =%X and TC = %X\n",LPC_TIM2->MR1,LPC_TIM2->TC); 00166 LPC_TIM2->MCR |= 8; 00167 // } 00168 } 00169 return 1; 00170 } 00171 void trigEX(timeval *tv) { 00172 pc.printf(" Triggered at %d\n",tv->tv_sec); 00173 } 00174 void trigger(void) { 00175 timeval curT; 00176 unsigned int nMSec = (LPC_TIM2->CR0)/CLK_FRQ; 00177 unsigned int nSec = nMSec/1000000; 00178 curT.tv_sec = gTime*42 + nSec; 00179 curT.tv_usec = nMSec-(nSec*1000000); 00180 // pc.printf("nMsec and nSec = %d \n",nMSec); 00181 gtrigFunc(&curT); 00182 } 00183 void runAtTrigger(void(*trigFunc)(timeval *tv)) { 00184 gtrigFunc = trigFunc; 00185 trig.rise(&trigger); 00186 trig.fall(&trigger); 00187 // pc.printf("Runing runAtTrigger\n"); 00188 } 00189 00190 00191 void resp_sync_request(void) { 00192 union { 00193 unsigned int t; 00194 char BYTE[4]; 00195 } t2,t3; 00196 00197 t2.t = LPC_TIM2->TC; 00198 00199 // read the byte ; 00200 sync.getc(); 00201 // pc.printf(" \n resp_sync_request t2 = %X \n",t2.t); 00202 00203 wait(0.5); 00204 // read the present time 00205 // serially send out the data over to the slave 00206 00207 sync.putc(t2.BYTE[0]); 00208 sync.putc(t2.BYTE[1]); 00209 sync.putc(t2.BYTE[2]); 00210 sync.putc(t2.BYTE[3]); 00211 00212 // Get the present time; 00213 00214 t3.t = LPC_TIM2->TC; 00215 sync.putc(t3.BYTE[0]); 00216 sync.putc(t3.BYTE[1]); 00217 sync.putc(t3.BYTE[2]); 00218 sync.putc(t3.BYTE[3]); 00219 B+=8; 00220 // pc.printf(" Sync request %X & %X",t2.t, t3.t); 00221 } 00222 00223 void pinToggle(void) 00224 { 00225 toggle = !toggle; 00226 myled =!myled; 00227 // pc.putc('T'); 00228 pps.tv_usec+=500000; 00229 if(pps.tv_usec == 1200000) 00230 { 00231 pps.tv_usec = 200000; 00232 pps.tv_sec++; 00233 } 00234 00235 LPC_TIM2->MR1 = (pps.tv_sec%42 )*CLK_FREQUENCY+(pps.tv_usec*CLK_FRQ); 00236 00237 // pc.printf("SCHEUDLIN MR1 =%X and TC = %X\n",LPC_TIM2->MR1,LPC_TIM2->TC); 00238 LPC_TIM2->MCR |= 8; 00239 //M runAtTime(&pinToggle,&pps); 00240 } 00241 00242 00243 void reportToggle(timeval *rt) 00244 { 00245 int slv_trg_val = 0; 00246 //slv_trg_val = LPC_TIM2->CR; 00247 00248 //pc.putc('T'); 00249 00250 int slv_diff; 00251 // load the CR0 value 00252 00253 // slv_diff = (rt->tv_sec - ideal_trg_time[ideal_trg_time_index].tv_sec)*1000000 + 00254 // rt->tv_usec - ideal_trg_time[ideal_trg_time_index].tv_usec; 00255 00256 // pc.printf("\n %d %d \n", rt->tv_sec, rt->tv_usec); 00257 // ideal_trg_time[ideal_trg_time_index].tv_usec); 00258 00259 // ideal_trg_time_index++; 00260 //1 pps part. 00261 00262 slv_diff = rt->tv_usec - pps.tv_usec; 00263 00264 //ideal_trg_time[0].tv_sec++ ; 00265 //ideal_trg_time_index--; 00266 00267 00268 pc.printf("%\n diff = %d",slv_diff); 00269 00270 00271 } 00272 00273 00274 00275 00276 #endif
Generated on Fri Jul 15 2022 01:49:01 by 1.7.2