Master 1 hr . takes input form the serial connection of PC

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
manujose
Date:
Sat Dec 04 06:10:16 2010 +0000
Commit message:

Changed in this revision

decl.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
pqueue.h Show annotated file Show diff for this revision Revisions of this file
problemb1.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 219eacd4c264 decl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/decl.h	Sat Dec 04 06:10:16 2010 +0000
@@ -0,0 +1,42 @@
+#ifndef _DECL_
+#define _DECL_
+//Variable declarations.
+
+#define NUM_MICRO_SEC 1000000
+#define CLK_FREQUENCY 96000000
+#define CLK_FREQ_MICRO 96000000000000
+#define CLK_FRQ  96
+#define RESET_42 0xF0537000
+
+typedef struct time_val {
+    time_t tv_sec;
+    time_t tv_usec;
+} timeval;
+//timeval global_time;
+#define QUEUE_MAX 100
+
+
+
+
+Serial pc(USBTX, USBRX);
+Serial sync(p9,p10);
+ Serial serial2(p13,p14); 
+ 
+InterruptIn trig(p29); 
+
+DigitalOut myled(LED1);
+DigitalOut myled2(LED2);
+DigitalOut toggle(p11);
+
+
+
+//FUNCTION DECLARATION
+int startTimer(void);
+int curTimeEqualGR(timeval *tv);
+void(*gtrigFunc)(timeval *tv);
+void Timer0_IRQHandler(void);
+void getTime(timeval *tv);
+void reportToggle(void);  
+
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 219eacd4c264 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Dec 04 06:10:16 2010 +0000
@@ -0,0 +1,90 @@
+
+#include "problemb1.h"
+
+//void Timer2_IRQHandler(void);
+
+//Serial pc(p9,p10);
+
+
+
+
+
+
+Ticker debug;
+//static struct pt pt1,pt2,pt3;
+int main() {
+
+//wait(2); 
+
+    pc.printf("ENTERING MAIN JOBS\n");
+    sync.baud(460800); 
+    initialSetup();
+
+   
+    int p;
+
+    //On any sync request from slave, respond to it
+    sync.attach(&resp_sync_request);
+
+
+    timeval rt;
+
+     runAtTrigger(&reportToggle);
+
+
+    char c;
+    int i;
+    int count =0;
+    unsigned int tt;
+    union {
+        timeval t;
+        char BYTE[8];
+    } ttt;
+    tt=NUM_MICRO_SEC*15;
+    wait (4);
+    while (1) {
+     count++;
+        if (pc.readable()) {
+            c ='k';
+            i=0;
+       
+        pc.printf("DAT REC\n");
+           while (pc.readable() && (c !='S'))
+                c=pc.getc();
+            c=pc.getc();
+            while (pc.readable() &&(c !='E')) {
+                if (c!=' ') {
+                    tt=tt*10+atoi(&c);
+                }
+                c  = pc.getc();
+            }
+
+            while (pc.readable())
+               c = pc.getc();
+       // pc.printf(" RECV = %d \n",tt);
+            if (tt>= NUM_MICRO_SEC) {
+                ttt.t.tv_sec = tt/NUM_MICRO_SEC;
+                ttt.t.tv_usec = tt - ttt.t.tv_sec*NUM_MICRO_SEC;
+            } else {
+                ttt.t.tv_sec =0; //less than a sec
+                ttt.t.tv_usec = tt;
+            }
+            serial2.putc(ttt.BYTE[0]);
+            serial2.putc(ttt.BYTE[1]);
+            serial2.putc(ttt.BYTE[2]);
+            serial2.putc(ttt.BYTE[3]);
+            serial2.putc(ttt.BYTE[4]);
+            serial2.putc(ttt.BYTE[5]);
+            serial2.putc(ttt.BYTE[6]);
+            serial2.putc(ttt.BYTE[7]);
+            runAtTime(&pinToggle, &ttt.t);
+            
+            pc.printf("DATA RECV :%d SEC %d AND MICRO %d \n",tt,ttt.t.tv_sec,ttt.t.tv_usec);
+            
+        }
+
+     
+
+    }
+
+}
diff -r 000000000000 -r 219eacd4c264 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Dec 04 06:10:16 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
diff -r 000000000000 -r 219eacd4c264 pqueue.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pqueue.h	Sat Dec 04 06:10:16 2010 +0000
@@ -0,0 +1,69 @@
+#ifndef _PQUEUE_H_
+#define _PQUEUE_H_
+
+typedef struct prioQueueEle {
+    timeval t;
+    void (*foo)(void);
+    int sched;
+    struct prioQueueEle *next;
+}qEle;
+typedef struct prioQueue {
+    int numEle;
+    qEle *head;
+
+}pQueue;
+
+
+
+int enqueue(pQueue *xP, timeval t, void (*schedFunc)(void)) {
+    if (xP->numEle == QUEUE_MAX)
+        return 0; //ERROR reached max .
+
+    xP->numEle++;
+    qEle *newEle = (qEle*)malloc(sizeof(qEle));
+    newEle->t.tv_sec = t.tv_sec;
+    newEle->t.tv_usec = t.tv_usec;
+    newEle->foo = schedFunc;
+    newEle->sched = 0;
+    newEle->next = NULL;
+    if (xP->head == NULL) { //first ele;
+        xP->head = newEle;
+        return 1;
+    } else {
+        if ((xP->head->t.tv_sec > t.tv_sec) ||
+                ((xP->head->t.tv_sec == t.tv_sec ) && (xP->head->t.tv_usec > t.tv_usec))  ) {
+            newEle->next = xP->head;
+            xP->head = newEle;
+        
+            return 1;
+        } else if (xP->head->next == NULL) {
+            xP->head->next = newEle;
+            return 1;
+        }
+    }
+    qEle *e = xP->head;
+    while (e->next->next !=NULL) {
+        if ((e->next->t.tv_sec > t.tv_sec) ||
+                ((e->next->t.tv_sec == t.tv_sec) && (e->next->t.tv_usec > t.tv_usec))  ) {
+            newEle->next = e->next;
+            e->next = newEle;
+            return 1;
+        }
+        e= e->next;
+    }
+    e->next->next = newEle;
+    return 1;
+}
+
+
+
+
+qEle* pop(pQueue *xP) {
+    qEle *p = xP->head;
+    xP->head = p->next;
+    xP->numEle--;
+    return p;
+//    free(p);
+}
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 219eacd4c264 problemb1.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/problemb1.h	Sat Dec 04 06:10:16 2010 +0000
@@ -0,0 +1,260 @@
+#ifndef PROBLEMB1_H_
+#define PROBLEMB1_H_
+#include "mbed.h"
+#include "decl.h"
+#include "pqueue.h"
+
+pQueue globalQ;
+unsigned int gTime =0;
+int B;
+
+
+timeval ideal_trg_time[100];
+int ideal_trg_time_index = 0;
+
+timeval pps;
+
+union {
+    unsigned int t;
+    char BYTE[4];
+}t1,t2,t3,t4;
+
+
+
+void initialSetup(void) {
+    // doing capture register for now, so use timer 2
+    LPC_SC->PCLKSEL1 |= (0x01 << 12);  //set the frequency REF: USER MANULA TAB 40,41,42 ?? check
+    LPC_SC-> PCONP |= (0x3 << 22);  // timer 2
+
+    //LPC_TIM2->TCR = 0x2;         // Reset and set to timer mode
+
+    // Select PIN
+
+    LPC_PINCON->PINSEL0 |= (0xf << 8);
+    LPC_TIM2->CCR |= 0x07;// doing capture register for now, so use timer 2
+
+    LPC_TIM2->MR0 = RESET_42;
+    LPC_TIM2->MCR = 3;           // Interrupt, Stop, and Reset on match
+
+    //LPC_TIM2->TCR = 0x2;         // Reset and set to timer mode
+
+    LPC_TIM2->TCR = 0x1;
+
+
+    // ================ //
+
+
+
+
+    /*LPC_SC->PCLKSEL0 |= 0x04;  //set the frequency REF: USER MANULA TAB 40,41,42 ?? check
+
+    LPC_SC-> PCONP |= 1 << 1;    // Power on Timer0
+
+    LPC_TIM0->TCR = 0x2;         // Reset and set to timer mode
+    LPC_TIM0->CTCR = 0x0;
+    LPC_TIM0->PR = 0;            // No prescale
+    //LPC_TIM0->MR0 = 0xF0537000 ;       // Match count for 100mS
+     LPC_TIM0->MR0 = RESET_42;
+    LPC_TIM0->MCR = 3;           // Interrupt, Stop, and Reset on match
+
+
+    LPC_TIM0->TCR = 1;           // Enable Timer0
+    // Enable the ISR vector
+    */
+    NVIC_SetVector (TIMER2_IRQn, (uint32_t)&Timer0_IRQHandler);
+    NVIC_EnableIRQ(TIMER2_IRQn);
+
+    //  LPC_TIM0->MCR |= 11; //for mr1 and mr0
+    //queue set up
+    globalQ.numEle = 0;
+    globalQ.head = NULL;
+}
+void Timer0_IRQHandler(void) {
+    // LPC_TIM0->IR=0xff;
+    //LPC_TIM0->MR0 = 0x5370;
+    if (LPC_TIM2->IR&1) {
+        gTime++;
+        //  pc.printf("\ngtime = %X",LPC_TIM2->IR);
+    }
+    if ( (LPC_TIM2->IR >> 1)&1 ) { //RUN THE RUNAT TIME HIT
+        qEle *ele = pop(&globalQ);
+
+
+        ele->foo();
+
+        free(ele);
+
+    }
+    if (  (LPC_TIM2->IR >> 4 )& 1) { // run report toggle
+        timeval curT;
+        unsigned int nMSec = (LPC_TIM2->CR0)/CLK_FRQ;
+        //  pc.printf(" THE mMSec = %d \n",nMSec);
+        unsigned int nSec = nMSec/1000000;
+        curT.tv_sec = gTime*42 + nSec;
+        curT.tv_usec = nMSec-(nSec*1000000);
+        gtrigFunc(&curT);
+
+    }
+
+    //check if more ele for schedule this time ?
+//C   if ((globalQ.numEle >0) && (globalQ.head->sched ==0)&& (globalQ.head->t.tv_sec <((gTime+1)*42))) {
+    //C    globalQ.head->sched = 1;
+
+    // LPC_TIM0->MR1 = (globalQ.head->t.tv_sec-gTime*42)*96000000 +globalQ.head->t.tv_usec*96;
+    //C   LPC_TIM2->MR1 = (globalQ.head->t.tv_sec-gTime*42)*CLK_FREQUENCY +globalQ.head->t.tv_usec*CLK_FRQ;
+    //C   LPC_TIM2->MCR |= 8;
+    // pc.printf("\n MR1 = %X",LPC_TIM2->MR1);
+
+
+    //C } else if (globalQ.head->sched == 0) //NO events in this gTime update yet .
+    //C  { //turn off the match regiester 1 interrupts.
+    //C    LPC_TIM2->MCR &= 3;
+    //C }
+
+    //Ctimeval t;
+    //C getTime(&t);
+    LPC_TIM2->IR = 0xff;
+    //  pc.printf("INCREMENTING COUNTER or event %d  \n",t.tv_sec);
+}
+
+
+void getTime(timeval *tv) {
+    unsigned int nMSec = (LPC_TIM2->TC)/CLK_FRQ; //gives num of micro sec
+    //  unsigned int nMSec = (LPC_TIM0->TC + offset)/72;
+    unsigned int nSec = nMSec/1000000;
+    tv->tv_sec = gTime*42 + nSec;
+    // tv->tv_usec = (LPC_TIM0->TC)*1000 + (float)(LPC_TIM0->PC)/( CLK_FREQUENCY * 1000000);
+    tv->tv_usec = nMSec-(nSec*1000000);
+}
+
+int curTimeEqualGR(timeval *tv) {
+    timeval curT;
+    getTime(&curT);
+    if (curT.tv_sec == tv->tv_sec) {
+        if (curT.tv_usec == tv->tv_usec)
+            return 1;
+        else
+            pc.printf("WROING MICRO CALIBERATION\n");
+        return 1;
+    } else if (curT.tv_sec > tv->tv_sec) {
+        pc.printf("WRONG cur = %d and req = %d \n",curT.tv_sec, tv->tv_sec);
+        return 1;
+    }
+    return 0;
+}
+int runAtTime(void (*schedFunc)(void), timeval *tv) {
+    int ret =0;
+    ret = enqueue(&globalQ, *tv, schedFunc);
+    //C  if (tv->tv_sec < (gTime+1)*42) {
+
+    //C   if (globalQ.head->sched == 0) {
+
+    //C     LPC_TIM2->MR1 = ((globalQ.head->t.tv_sec%42 )*CLK_FREQUENCY+(globalQ.head->t.tv_usec*CLK_FRQ));
+    //C     globalQ.head->sched = 1;
+    //C    LPC_TIM2->MCR |= 8;
+    //C }
+    //C }
+    return ret;
+}
+void trigEX(timeval *tv) {
+    pc.printf(" Triggered at %d\n",tv->tv_sec);
+}
+void trigger(void) {
+    timeval curT;
+    unsigned int nMSec = (LPC_TIM2->CR0)/CLK_FRQ;
+    unsigned int nSec = nMSec/1000000;
+    curT.tv_sec = gTime*42 + nSec;
+    curT.tv_usec = nMSec-(nSec*1000000);
+    gtrigFunc(&curT);
+}
+void runAtTrigger(void(*trigFunc)(timeval *tv)) {
+    gtrigFunc = trigFunc;
+    //  trig.rise(&trigger);
+    // trig.fall(&trigger);
+    //  pc.printf("Runing runAtTrigger\n");
+}
+
+
+void resp_sync_request(void) {
+    union {
+        unsigned int t;
+        char BYTE[4];
+    } t2,t3;
+
+    t2.t = LPC_TIM2->TC;
+
+// read the byte ;
+    sync.getc();
+//    pc.printf(" \n resp_sync_request t2 = %X \n",t2.t);
+
+    wait(0.5);
+// read the present time
+// serially send out the data over to the slave
+
+    sync.putc(t2.BYTE[0]);
+    sync.putc(t2.BYTE[1]);
+    sync.putc(t2.BYTE[2]);
+    sync.putc(t2.BYTE[3]);
+
+    // Get the present time;
+
+    t3.t = LPC_TIM2->TC;
+    sync.putc(t3.BYTE[0]);
+    sync.putc(t3.BYTE[1]);
+    sync.putc(t3.BYTE[2]);
+    sync.putc(t3.BYTE[3]);
+    B+=8;
+    // pc.printf(" Sync request %X & %X",t2.t, t3.t);
+}
+
+void pinToggle(void) {
+    toggle = !toggle;
+    //C pc.putc('T');
+    //C pps.tv_sec++;
+    //C runAtTime(&pinToggle,&pps);
+}
+
+
+void reportToggle(timeval *rt) {
+    int slv_trg_val = 0;
+    //slv_trg_val = LPC_TIM2->CR;
+
+    //pc.putc('T');
+
+    int slv_diff;
+    // load the CR0 value
+
+    // slv_diff = (rt->tv_sec - ideal_trg_time[ideal_trg_time_index].tv_sec)*1000000 +
+    // rt->tv_usec - ideal_trg_time[ideal_trg_time_index].tv_usec;
+
+    // pc.printf("\n %d %d    %d %d ", rt->tv_sec, rt->tv_usec, ideal_trg_time[ideal_trg_time_index].tv_sec, \
+    // ideal_trg_time[ideal_trg_time_index].tv_usec);
+
+    //  ideal_trg_time_index++;
+//1 pps part.
+//slv_diff = rt->tv_usec -ideal_trg_time[0].tv_usec;
+     timeval t;
+    getTime(&t);
+    if((globalQ.head->t.tv_sec > t.tv_sec) )
+        pc.printf(" NOT POPING ele = %d and usec =%d\n", globalQ.head->t.tv_sec, globalQ.head->t.tv_usec);
+   
+    else 
+    {
+    qEle *ele = pop(&globalQ);
+    if (ele) {
+        ele->foo();
+        free(ele);
+        slv_diff = rt->tv_usec - ele->t.tv_usec;
+      //  pc.printf("THE DIFF is %d at sec =q %d --oc  %d and usec = q %d --oc %d\n",slv_diff, ele->t.tv_sec,rt->tv_sec, ele->t.tv_usec,rt->tv_usec);
+    timeval t;
+    getTime(&t);
+    pc.printf(" %d and sec %d and usec = %d and from there %d \n",slv_diff,t.tv_sec, t.tv_usec,rt->tv_usec);
+    }
+    }
+
+}
+
+
+
+
+#endif
\ No newline at end of file