manu jose / Mbed 2 deprecated 1hourSlave

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers problemb1.h Source File

problemb1.h

00001 #ifndef PROBLEMB1_H_
00002 #define PROBLEMB1_H_
00003 #include "mbed.h"
00004 #include "decl.h"
00005 #include "pqueue.h"
00006 
00007 pQueue globalQ;
00008 unsigned int gTime =0;
00009 timeval pps; 
00010 
00011 union {
00012     unsigned int t;
00013     char BYTE[4];
00014     }t1,t2,t3,t4;
00015 signed int offset;
00016 
00017 void initialSetup(void) {
00018     LPC_SC->PCLKSEL0 |= 0x04;  //set the frequency REF: USER MANULA TAB 40,41,42 ?? check
00019 
00020     LPC_SC-> PCONP |= 1 << 1;    // Power on Timer0
00021 
00022     LPC_TIM0->TCR = 0x2;         // Reset and set to timer mode
00023     LPC_TIM0->CTCR = 0x0;
00024     LPC_TIM0->PR = 0;            // No prescale
00025     //LPC_TIM0->MR0 = 0xF0537000 ;       // Match count for 100mS
00026      LPC_TIM0->MR0 = RESET_42;
00027     LPC_TIM0->MCR = 3;           // Interrupt, Stop, and Reset on match
00028 
00029 
00030     LPC_TIM0->TCR = 1;           // Enable Timer0
00031     // Enable the ISR vector
00032     NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
00033     NVIC_EnableIRQ(TIMER0_IRQn);
00034 
00035     //  LPC_TIM0->MCR |= 11; //for mr1 and mr0
00036     //queue set up
00037     globalQ.numEle = 0;
00038     globalQ.head = NULL;
00039 }
00040 void Timer0_IRQHandler(void) {
00041     // LPC_TIM0->IR=0xff;
00042     //LPC_TIM0->MR0 = 0x5370;
00043     if (LPC_TIM0->IR&1) {
00044         gTime++;
00045     //    pc.printf("gtime++ %X",LPC_TIM0->IR);
00046     } 
00047     
00048     if((LPC_TIM0->IR >> 1)&1 ){ //RUN THE RUNAT TIME HIT
00049         qEle *ele = pop(&globalQ);
00050      //   pc.putc('1'); 
00051      //   pc.printf("ACtual time = %d and %d\n",ele->t.tv_sec, ele->t.tv_usec);
00052         ele->foo();
00053         free(ele);
00054     }
00055     //check if more ele for schedule this time ?
00056     if ((globalQ.numEle >0) && (globalQ.head->sched ==0)&& (globalQ.head->t.tv_sec <((gTime+1)*42))) {
00057         globalQ.head->sched = 1;
00058  
00059         LPC_TIM0->MR1 = (globalQ.head->t.tv_sec-(gTime*42))*CLK_FREQUENCY +globalQ.head->t.tv_usec*CLK_FRQ;
00060         LPC_TIM0->MCR |= 8;
00061  
00062     } else if (globalQ.head->sched == 0) //NO events in this gTime update yet .
00063     { //turn off the match regiester 1 interrupts.
00064         LPC_TIM0->MCR &= 3;
00065     }
00066     
00067  //   timeval t;
00068    // getTime(&t);
00069     LPC_TIM0->IR = 0xff;
00070   //  pc.printf("INCREMENTING COUNTER or event %d  \n",t.tv_sec);
00071 }
00072 
00073 
00074 void getTime(timeval *tv) {
00075     unsigned int nMSec = (LPC_TIM0->TC)/CLK_FRQ; //gives num of micro sec
00076   //  unsigned int nMSec = (LPC_TIM0->TC + offset)/72; 
00077     unsigned int nSec = nMSec/1000000;
00078     tv->tv_sec = gTime*42 + nSec;
00079     // tv->tv_usec = (LPC_TIM0->TC)*1000 + (float)(LPC_TIM0->PC)/( CLK_FREQUENCY * 1000000);
00080     tv->tv_usec = nMSec-(nSec*1000000);
00081 }
00082 
00083 int curTimeEqualGR(timeval *tv) {
00084     timeval curT;
00085     getTime(&curT);
00086     if (curT.tv_sec == tv->tv_sec) {
00087         if (curT.tv_usec == tv->tv_usec)
00088             return 1;
00089         else
00090             pc.printf("WROING MICRO CALIBERATION\n");
00091         return 1;
00092     } else if (curT.tv_sec > tv->tv_sec) {
00093         pc.printf("WRONG cur = %d and req = %d \n",curT.tv_sec, tv->tv_sec);
00094         return 1;
00095     }
00096     return 0;
00097 }
00098 
00099 int runAtTime(void (*schedFunc)(void), timeval *tv) {
00100     int ret =0;
00101     ret = enqueue(&globalQ, *tv, schedFunc);
00102     if (tv->tv_sec < (gTime+1)*42) {
00103 
00104         if (globalQ.head->sched == 0) {
00105         
00106             LPC_TIM0->MR1 = ((globalQ.head->t.tv_sec%42 )*CLK_FREQUENCY+(globalQ.head->t.tv_usec*CLK_FRQ));
00107             globalQ.head->sched = 1;
00108             LPC_TIM0->MCR |= 8;
00109         }
00110     }
00111     return ret;
00112 }
00113 void trigEX(timeval *tv) {
00114     pc.printf(" Triggered at %d\n",tv->tv_sec);
00115 }
00116 void trigger(void) {
00117     timeval curT;
00118     getTime(&curT);
00119     gtrigFunc(&curT);
00120 }
00121 void runAtTrigger(void(*trigFunc)(timeval *tv)) {
00122     gtrigFunc = trigFunc;
00123     trig.rise(&trigger);
00124    // pc.printf("Runing runAtTrigger\n");
00125 }
00126 
00127 
00128 void sync_with_master(void) {
00129     t1.t = LPC_TIM0->TC ;
00130 
00131     // trigger the master
00132 
00133     sync.putc('x');
00134  //   pc.printf("\n sync_with_master t1.t=%X\n", t1.t); 
00135 }
00136 
00137 void calculate_offset(void) {
00138 
00139 llong  t2minust1, t4minust3; 
00140     t4.t = LPC_TIM0->TC;
00141 
00142     sync.attach(NULL); 
00143     
00144 // serially receive data from master
00145 
00146     //t2.t = 0;
00147     //t3.t = 0;
00148       
00149       t2.BYTE[0] = sync.getc();
00150       t2.BYTE[1] = sync.getc();
00151       t2.BYTE[2] = sync.getc();
00152       t2.BYTE[3] = sync.getc();
00153       
00154       t3.BYTE[0] = sync.getc();
00155       t3.BYTE[1] = sync.getc();
00156       t3.BYTE[2] = sync.getc();
00157       t3.BYTE[3] = sync.getc();
00158         
00159         
00160     
00161     //exhaust buffer 
00162     
00163     sync.attach(&calculate_offset); 
00164     
00165     //pc.printf(" \n t1= %X, t2 = %X, t3 = %X, t4 = %X",t1.t,t2.t,t3.t,t4.t);
00166 
00167 // account for the case where the TC has overflowed.
00168 
00169     if ( t1.t > t4.t )
00170     {
00171         t4.t = RESET_42 + t4.t;
00172 }
00173     if (t2.t > t3.t)
00174  {   
00175         t3.t = RESET_42 + t3.t;
00176 }
00177 
00178     t2minust1 = (llong)t2.t-t1.t;
00179     t4minust3 = (llong)t4.t-t3.t;
00180 
00181   //  pc.printf(" \n t2-t1 = %X, t4-t3 = %X",t2minust1, t4minust3);
00182     
00183     offset =(int) ((t2minust1-t4minust3)/2);
00184     
00185     LPC_TIM0->TC = (LPC_TIM0->TC + offset);
00186     if(LPC_TIM0->TC >  RESET_42)
00187         LPC_TIM0->TC = LPC_TIM0->TC - RESET_42;
00188         
00189     offset = 0; 
00190     while(sync.readable())
00191     sync.getc(); 
00192  
00193     //pc.printf(" offset = %d\n",offset); 
00194 }
00195 
00196 
00197 void pinToggle(void)
00198 {
00199     toggle = !toggle; 
00200    // timeval t;
00201    // getTime(&t);
00202    // pc.printf(" THE TOGGLE TIME = %d and %d \n",t.tv_sec, t.tv_usec);
00203    // myLED = !myLED; 
00204    // pc.printf("\nToggle");
00205    // pps.tv_sec++; 
00206     //runAtTime(&pinToggle,&pps);
00207       
00208 }
00209 
00210 
00211 
00212 #endif