Dependencies:   mbed

Committer:
sun831011
Date:
Fri Dec 03 22:49:17 2010 +0000
Revision:
0:9456245a7755

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sun831011 0:9456245a7755 1 #include "mbed.h"
sun831011 0:9456245a7755 2
sun831011 0:9456245a7755 3 Serial pc(USBTX, USBRX); // tx, rx
sun831011 0:9456245a7755 4 Serial ss(p9, p10); //tx rx
sun831011 0:9456245a7755 5 Serial ss_sne(p13, p14); //tx rx
sun831011 0:9456245a7755 6
sun831011 0:9456245a7755 7 DigitalOut myled1(LED1);
sun831011 0:9456245a7755 8 DigitalOut myled2(LED2);
sun831011 0:9456245a7755 9
sun831011 0:9456245a7755 10 DigitalOut masterOut(p23);
sun831011 0:9456245a7755 11 InterruptIn masterIn(p22);
sun831011 0:9456245a7755 12 InterruptIn slaveIn(p21);
sun831011 0:9456245a7755 13
sun831011 0:9456245a7755 14 unsigned long counter = 0;
sun831011 0:9456245a7755 15 int divider = 1;
sun831011 0:9456245a7755 16 int pro_flag = 0;
sun831011 0:9456245a7755 17 int task_flag = 0;
sun831011 0:9456245a7755 18 unsigned long long t1, t2, t3, t4;
sun831011 0:9456245a7755 19 typedef struct timeval {
sun831011 0:9456245a7755 20 unsigned long tv_sec;
sun831011 0:9456245a7755 21 unsigned long tv_usec;
sun831011 0:9456245a7755 22 } timeval;
sun831011 0:9456245a7755 23
sun831011 0:9456245a7755 24 void Timer0_init(void);
sun831011 0:9456245a7755 25 void Timer0_IRQHandler(void);
sun831011 0:9456245a7755 26 void Sync_protocol(void);
sun831011 0:9456245a7755 27 void getTime(struct timeval *tv);
sun831011 0:9456245a7755 28 int runAtTime (void (*fptr)(void), struct timeval tv);
sun831011 0:9456245a7755 29 void toggle(void);
sun831011 0:9456245a7755 30 void runAtTrigger( );
sun831011 0:9456245a7755 31 void reportToggle(struct timeval *tv);
sun831011 0:9456245a7755 32 long long sne(void);
sun831011 0:9456245a7755 33
sun831011 0:9456245a7755 34 int main( )
sun831011 0:9456245a7755 35 {
sun831011 0:9456245a7755 36 struct timeval t_run;
sun831011 0:9456245a7755 37 long long t_run_m;
sun831011 0:9456245a7755 38 t_run.tv_sec = 1000;
sun831011 0:9456245a7755 39 t_run.tv_usec = 0;
sun831011 0:9456245a7755 40 wait(1);
sun831011 0:9456245a7755 41 // masterIn.rise(&runAtTrigger);
sun831011 0:9456245a7755 42 // masterIn.fall(&runAtTrigger);
sun831011 0:9456245a7755 43 slaveIn.rise(&runAtTrigger);
sun831011 0:9456245a7755 44 slaveIn.fall(&runAtTrigger);
sun831011 0:9456245a7755 45 Timer0_init( );
sun831011 0:9456245a7755 46 while (1)
sun831011 0:9456245a7755 47 {
sun831011 0:9456245a7755 48 if (pc.readable( )){
sun831011 0:9456245a7755 49 t_run_m = sne( );
sun831011 0:9456245a7755 50 t_run.tv_sec = t_run_m/1000000;
sun831011 0:9456245a7755 51 t_run.tv_usec = t_run_m%1000000;
sun831011 0:9456245a7755 52 pc.printf("%lu\n",t_run.tv_sec);
sun831011 0:9456245a7755 53 pc.printf("%lu\n",t_run.tv_usec);
sun831011 0:9456245a7755 54 }
sun831011 0:9456245a7755 55 if (!task_flag)
sun831011 0:9456245a7755 56 task_flag = runAtTime (&toggle, t_run);
sun831011 0:9456245a7755 57 if (pro_flag == 1){
sun831011 0:9456245a7755 58 Sync_protocol( );
sun831011 0:9456245a7755 59 pro_flag = 0;
sun831011 0:9456245a7755 60 }
sun831011 0:9456245a7755 61 }
sun831011 0:9456245a7755 62 }
sun831011 0:9456245a7755 63
sun831011 0:9456245a7755 64 void Timer0_init(void)
sun831011 0:9456245a7755 65 {
sun831011 0:9456245a7755 66 LPC_SC->PCLKSEL0 = 0x0066;
sun831011 0:9456245a7755 67 LPC_SC->PCONP |= 1<<1; // Timer0 Power On
sun831011 0:9456245a7755 68 LPC_TIM0->MR0 = 2880000000; // Match count for 30S
sun831011 0:9456245a7755 69 LPC_TIM0->MCR = 3; // Interrupt and Reset on Match
sun831011 0:9456245a7755 70 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:9456245a7755 71 NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
sun831011 0:9456245a7755 72 NVIC_EnableIRQ(TIMER0_IRQn);
sun831011 0:9456245a7755 73 }
sun831011 0:9456245a7755 74
sun831011 0:9456245a7755 75 void Timer0_IRQHandler(void)
sun831011 0:9456245a7755 76 {
sun831011 0:9456245a7755 77 LPC_TIM0->IR = 1;
sun831011 0:9456245a7755 78 LPC_TIM0->MR0 = 2880000000; // Match count for 30S
sun831011 0:9456245a7755 79 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:9456245a7755 80 counter = counter + 1;
sun831011 0:9456245a7755 81 if ( counter % divider ==0 )
sun831011 0:9456245a7755 82 pro_flag = 1;
sun831011 0:9456245a7755 83 }
sun831011 0:9456245a7755 84
sun831011 0:9456245a7755 85 void Sync_protocol(void)
sun831011 0:9456245a7755 86 {
sun831011 0:9456245a7755 87 char x;
sun831011 0:9456245a7755 88 t1 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:9456245a7755 89 ss.putc('S');
sun831011 0:9456245a7755 90 pc.printf("t1 = %llu\n", t1);
sun831011 0:9456245a7755 91 ss.printf("%llu", t1);
sun831011 0:9456245a7755 92 ss.putc('E');
sun831011 0:9456245a7755 93 while (1){
sun831011 0:9456245a7755 94 if (ss.readable( )){
sun831011 0:9456245a7755 95 x = ss.getc( );
sun831011 0:9456245a7755 96 t4 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:9456245a7755 97 ss.printf("%llu", t4);
sun831011 0:9456245a7755 98 ss.putc('E');
sun831011 0:9456245a7755 99 pc.printf("%c\n", x);
sun831011 0:9456245a7755 100 pc.printf("t4 = %llu\n", t4);
sun831011 0:9456245a7755 101 break;
sun831011 0:9456245a7755 102 }
sun831011 0:9456245a7755 103 }
sun831011 0:9456245a7755 104 }
sun831011 0:9456245a7755 105
sun831011 0:9456245a7755 106 void getTime(struct timeval *tv){
sun831011 0:9456245a7755 107 unsigned long ns, hs;
sun831011 0:9456245a7755 108 ns = LPC_TIM0->TC;
sun831011 0:9456245a7755 109 hs = counter;
sun831011 0:9456245a7755 110 tv->tv_sec = hs*30 + ns/100000000;
sun831011 0:9456245a7755 111 tv->tv_usec = (ns%100000000)/100;
sun831011 0:9456245a7755 112 }
sun831011 0:9456245a7755 113
sun831011 0:9456245a7755 114 int runAtTime (void (*fptr)(void), struct timeval tv){
sun831011 0:9456245a7755 115 struct timeval current_t;
sun831011 0:9456245a7755 116 getTime(&current_t);
sun831011 0:9456245a7755 117 if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){
sun831011 0:9456245a7755 118 (*fptr) ( );
sun831011 0:9456245a7755 119 return 1;
sun831011 0:9456245a7755 120 }
sun831011 0:9456245a7755 121 else
sun831011 0:9456245a7755 122 return 0;
sun831011 0:9456245a7755 123 }
sun831011 0:9456245a7755 124
sun831011 0:9456245a7755 125 void toggle(void){
sun831011 0:9456245a7755 126 masterOut = !masterOut;
sun831011 0:9456245a7755 127 }
sun831011 0:9456245a7755 128
sun831011 0:9456245a7755 129 void runAtTrigger(void){
sun831011 0:9456245a7755 130 struct timeval response_t;
sun831011 0:9456245a7755 131 reportToggle(&response_t);
sun831011 0:9456245a7755 132 }
sun831011 0:9456245a7755 133
sun831011 0:9456245a7755 134 void reportToggle(struct timeval *tv)
sun831011 0:9456245a7755 135 {
sun831011 0:9456245a7755 136 getTime(tv);
sun831011 0:9456245a7755 137 pc.printf("%u\n", tv->tv_sec);
sun831011 0:9456245a7755 138 pc.printf("%u\n", tv->tv_usec);
sun831011 0:9456245a7755 139 myled2 = !myled2;
sun831011 0:9456245a7755 140 }
sun831011 0:9456245a7755 141
sun831011 0:9456245a7755 142 long long sne( ){
sun831011 0:9456245a7755 143 char temp;
sun831011 0:9456245a7755 144 char incode[100];
sun831011 0:9456245a7755 145 int i=0;
sun831011 0:9456245a7755 146 int j=0;
sun831011 0:9456245a7755 147 long long sum = 0;
sun831011 0:9456245a7755 148 while ((temp = pc.getc())!= 'E') { //getchar
sun831011 0:9456245a7755 149 incode[i] = temp; //put the original char in incode
sun831011 0:9456245a7755 150 i++;
sun831011 0:9456245a7755 151 }
sun831011 0:9456245a7755 152 for (j=1;j<i;j++){
sun831011 0:9456245a7755 153 sum = (incode[j]-'0')+sum*10;
sun831011 0:9456245a7755 154 }
sun831011 0:9456245a7755 155 if (incode[0]=='S'){
sun831011 0:9456245a7755 156 ss_sne.printf("%llu", sum);
sun831011 0:9456245a7755 157 ss_sne.putc('E');
sun831011 0:9456245a7755 158 return sum;
sun831011 0:9456245a7755 159 }
sun831011 0:9456245a7755 160 else
sun831011 0:9456245a7755 161 return 0;
sun831011 0:9456245a7755 162 }