yuwen sun
/
Hw3B_Final_100Ram_Slave
main.cpp@0:1aa8cad8b581, 2010-12-03 (annotated)
- Committer:
- sun831011
- Date:
- Fri Dec 03 22:26:40 2010 +0000
- Revision:
- 0:1aa8cad8b581
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sun831011 | 0:1aa8cad8b581 | 1 | #include "mbed.h" |
sun831011 | 0:1aa8cad8b581 | 2 | |
sun831011 | 0:1aa8cad8b581 | 3 | Serial pc(USBTX, USBRX); // tx, rx |
sun831011 | 0:1aa8cad8b581 | 4 | Serial ss(p9, p10); //tx rx |
sun831011 | 0:1aa8cad8b581 | 5 | Serial ss_sne(p13, p14); |
sun831011 | 0:1aa8cad8b581 | 6 | DigitalOut myled(LED3); |
sun831011 | 0:1aa8cad8b581 | 7 | DigitalOut rst1(p11); |
sun831011 | 0:1aa8cad8b581 | 8 | DigitalOut slaveOut(p23); |
sun831011 | 0:1aa8cad8b581 | 9 | |
sun831011 | 0:1aa8cad8b581 | 10 | |
sun831011 | 0:1aa8cad8b581 | 11 | void Timer0_init(void); |
sun831011 | 0:1aa8cad8b581 | 12 | void Timer0_IRQHandler(void); |
sun831011 | 0:1aa8cad8b581 | 13 | void slavesyn(void); |
sun831011 | 0:1aa8cad8b581 | 14 | void backtime(void); |
sun831011 | 0:1aa8cad8b581 | 15 | long long assembletime(long long t); |
sun831011 | 0:1aa8cad8b581 | 16 | int runAtTime (void (*fptr)(void), struct timeval tv); |
sun831011 | 0:1aa8cad8b581 | 17 | void getTime(struct timeval *tv); |
sun831011 | 0:1aa8cad8b581 | 18 | void toggle(void); |
sun831011 | 0:1aa8cad8b581 | 19 | |
sun831011 | 0:1aa8cad8b581 | 20 | long counter = 0; |
sun831011 | 0:1aa8cad8b581 | 21 | int divider = 4; |
sun831011 | 0:1aa8cad8b581 | 22 | long long t1; |
sun831011 | 0:1aa8cad8b581 | 23 | long long t2, t3, t4, t5, t6; |
sun831011 | 0:1aa8cad8b581 | 24 | long long delay; |
sun831011 | 0:1aa8cad8b581 | 25 | long long offdri; |
sun831011 | 0:1aa8cad8b581 | 26 | char test; |
sun831011 | 0:1aa8cad8b581 | 27 | long long receive(void); |
sun831011 | 0:1aa8cad8b581 | 28 | long long receive_sne(void); |
sun831011 | 0:1aa8cad8b581 | 29 | long long snesig; |
sun831011 | 0:1aa8cad8b581 | 30 | long long sne(void); |
sun831011 | 0:1aa8cad8b581 | 31 | int task_flag = 0; |
sun831011 | 0:1aa8cad8b581 | 32 | |
sun831011 | 0:1aa8cad8b581 | 33 | typedef struct timeval { |
sun831011 | 0:1aa8cad8b581 | 34 | unsigned long tv_sec; |
sun831011 | 0:1aa8cad8b581 | 35 | unsigned long tv_usec; |
sun831011 | 0:1aa8cad8b581 | 36 | } timeval; |
sun831011 | 0:1aa8cad8b581 | 37 | |
sun831011 | 0:1aa8cad8b581 | 38 | struct timeval t_run; |
sun831011 | 0:1aa8cad8b581 | 39 | struct timeval t_run2; |
sun831011 | 0:1aa8cad8b581 | 40 | |
sun831011 | 0:1aa8cad8b581 | 41 | int main( ) |
sun831011 | 0:1aa8cad8b581 | 42 | { |
sun831011 | 0:1aa8cad8b581 | 43 | pc.printf("\nbegin\n"); |
sun831011 | 0:1aa8cad8b581 | 44 | Timer0_init( ); |
sun831011 | 0:1aa8cad8b581 | 45 | t_run.tv_sec = 1000; |
sun831011 | 0:1aa8cad8b581 | 46 | t_run.tv_usec = 0; |
sun831011 | 0:1aa8cad8b581 | 47 | |
sun831011 | 0:1aa8cad8b581 | 48 | t_run2.tv_sec = 1000; |
sun831011 | 0:1aa8cad8b581 | 49 | t_run2.tv_usec = 0; |
sun831011 | 0:1aa8cad8b581 | 50 | |
sun831011 | 0:1aa8cad8b581 | 51 | while (1) |
sun831011 | 0:1aa8cad8b581 | 52 | { |
sun831011 | 0:1aa8cad8b581 | 53 | myled = 0; |
sun831011 | 0:1aa8cad8b581 | 54 | t2 = 100000000000000000; |
sun831011 | 0:1aa8cad8b581 | 55 | if (!task_flag){ |
sun831011 | 0:1aa8cad8b581 | 56 | task_flag = runAtTime (&toggle, t_run2); |
sun831011 | 0:1aa8cad8b581 | 57 | } |
sun831011 | 0:1aa8cad8b581 | 58 | |
sun831011 | 0:1aa8cad8b581 | 59 | if (ss_sne.readable()){ |
sun831011 | 0:1aa8cad8b581 | 60 | snesig = receive_sne(); |
sun831011 | 0:1aa8cad8b581 | 61 | t_run.tv_sec = snesig/1000000; |
sun831011 | 0:1aa8cad8b581 | 62 | t_run.tv_usec = snesig%1000000; |
sun831011 | 0:1aa8cad8b581 | 63 | pc.printf("\nSNE is %llu\n",snesig); |
sun831011 | 0:1aa8cad8b581 | 64 | pc.printf("\ntv_sec = %u, tv_usec = %u\n",t_run.tv_sec,t_run.tv_usec); |
sun831011 | 0:1aa8cad8b581 | 65 | task_flag = 0; |
sun831011 | 0:1aa8cad8b581 | 66 | } |
sun831011 | 0:1aa8cad8b581 | 67 | |
sun831011 | 0:1aa8cad8b581 | 68 | if (ss.readable( )){ |
sun831011 | 0:1aa8cad8b581 | 69 | myled = 1; |
sun831011 | 0:1aa8cad8b581 | 70 | test = ss.getc(); |
sun831011 | 0:1aa8cad8b581 | 71 | pc.printf("%d\n", test-'S'); |
sun831011 | 0:1aa8cad8b581 | 72 | |
sun831011 | 0:1aa8cad8b581 | 73 | if (test == 'S' ){ |
sun831011 | 0:1aa8cad8b581 | 74 | slavesyn(); |
sun831011 | 0:1aa8cad8b581 | 75 | |
sun831011 | 0:1aa8cad8b581 | 76 | } |
sun831011 | 0:1aa8cad8b581 | 77 | else{ |
sun831011 | 0:1aa8cad8b581 | 78 | if (test == 'T'){ |
sun831011 | 0:1aa8cad8b581 | 79 | backtime(); |
sun831011 | 0:1aa8cad8b581 | 80 | } |
sun831011 | 0:1aa8cad8b581 | 81 | else{ |
sun831011 | 0:1aa8cad8b581 | 82 | pc.printf("\nSystem Error--1!!\n"); |
sun831011 | 0:1aa8cad8b581 | 83 | } |
sun831011 | 0:1aa8cad8b581 | 84 | } |
sun831011 | 0:1aa8cad8b581 | 85 | } |
sun831011 | 0:1aa8cad8b581 | 86 | } |
sun831011 | 0:1aa8cad8b581 | 87 | } |
sun831011 | 0:1aa8cad8b581 | 88 | |
sun831011 | 0:1aa8cad8b581 | 89 | void Timer0_init(void) |
sun831011 | 0:1aa8cad8b581 | 90 | { |
sun831011 | 0:1aa8cad8b581 | 91 | LPC_SC->PCLKSEL0 = 0x0066; |
sun831011 | 0:1aa8cad8b581 | 92 | LPC_TIM0->MR0 = 2880000000; // Match count for 100uS |
sun831011 | 0:1aa8cad8b581 | 93 | LPC_TIM0->MCR = 3; // Interrupt and Reset on Match |
sun831011 | 0:1aa8cad8b581 | 94 | LPC_TIM0->TCR = 1; // Enable Timer0 |
sun831011 | 0:1aa8cad8b581 | 95 | NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler); |
sun831011 | 0:1aa8cad8b581 | 96 | NVIC_EnableIRQ(TIMER0_IRQn); |
sun831011 | 0:1aa8cad8b581 | 97 | } |
sun831011 | 0:1aa8cad8b581 | 98 | |
sun831011 | 0:1aa8cad8b581 | 99 | |
sun831011 | 0:1aa8cad8b581 | 100 | |
sun831011 | 0:1aa8cad8b581 | 101 | void Timer0_IRQHandler(void) |
sun831011 | 0:1aa8cad8b581 | 102 | { |
sun831011 | 0:1aa8cad8b581 | 103 | LPC_TIM0->IR = 1; |
sun831011 | 0:1aa8cad8b581 | 104 | LPC_TIM0->MR0 = 2880000000; // Match count for 100uS |
sun831011 | 0:1aa8cad8b581 | 105 | LPC_TIM0->TCR = 1; // Enable Timer0 |
sun831011 | 0:1aa8cad8b581 | 106 | counter = counter + 1; |
sun831011 | 0:1aa8cad8b581 | 107 | } |
sun831011 | 0:1aa8cad8b581 | 108 | |
sun831011 | 0:1aa8cad8b581 | 109 | void slavesyn(void) |
sun831011 | 0:1aa8cad8b581 | 110 | { |
sun831011 | 0:1aa8cad8b581 | 111 | pc.printf("in to fuc\n"); |
sun831011 | 0:1aa8cad8b581 | 112 | t2 = counter * 4294967296 + LPC_TIM0->TC; |
sun831011 | 0:1aa8cad8b581 | 113 | pc.printf("t2 = %llu\n",t2); |
sun831011 | 0:1aa8cad8b581 | 114 | |
sun831011 | 0:1aa8cad8b581 | 115 | t1 = receive(); |
sun831011 | 0:1aa8cad8b581 | 116 | pc.printf("t1 = %llu\n",t1); |
sun831011 | 0:1aa8cad8b581 | 117 | |
sun831011 | 0:1aa8cad8b581 | 118 | wait(0.1); |
sun831011 | 0:1aa8cad8b581 | 119 | t3 = counter * 4294967296 + LPC_TIM0->TC; |
sun831011 | 0:1aa8cad8b581 | 120 | ss.putc('S'); |
sun831011 | 0:1aa8cad8b581 | 121 | pc.printf("t3 = %llu\n", t3); |
sun831011 | 0:1aa8cad8b581 | 122 | |
sun831011 | 0:1aa8cad8b581 | 123 | t4 = receive(); |
sun831011 | 0:1aa8cad8b581 | 124 | pc.printf("t4 = %llu\n",t4); |
sun831011 | 0:1aa8cad8b581 | 125 | |
sun831011 | 0:1aa8cad8b581 | 126 | |
sun831011 | 0:1aa8cad8b581 | 127 | t1 = assembletime(t1); |
sun831011 | 0:1aa8cad8b581 | 128 | t2 = assembletime(t2); |
sun831011 | 0:1aa8cad8b581 | 129 | t3 = assembletime(t3); |
sun831011 | 0:1aa8cad8b581 | 130 | t4 = assembletime(t4); |
sun831011 | 0:1aa8cad8b581 | 131 | |
sun831011 | 0:1aa8cad8b581 | 132 | // pc.printf("t2-t1 = %ll\n t1-t2 = %ll \n", (lld)t2-t1,(lld)t1-t2); |
sun831011 | 0:1aa8cad8b581 | 133 | |
sun831011 | 0:1aa8cad8b581 | 134 | /* |
sun831011 | 0:1aa8cad8b581 | 135 | offdri = (t2/4294967296 - t1/4294967296) *30*1000000000 + (t2%4294967296 - t1%4294967296); |
sun831011 | 0:1aa8cad8b581 | 136 | pc.printf("offderi = %llu\n",offdri); |
sun831011 | 0:1aa8cad8b581 | 137 | |
sun831011 | 0:1aa8cad8b581 | 138 | delay = (t4/4294967296 - t3/4294967296) *4294967296 + (t4%4294967296 - t3%4294967296); |
sun831011 | 0:1aa8cad8b581 | 139 | pc.printf("delay = %llu\n",delay); |
sun831011 | 0:1aa8cad8b581 | 140 | */ |
sun831011 | 0:1aa8cad8b581 | 141 | |
sun831011 | 0:1aa8cad8b581 | 142 | |
sun831011 | 0:1aa8cad8b581 | 143 | //assume t2>t1 since slave reset first |
sun831011 | 0:1aa8cad8b581 | 144 | if(t3>t4){ |
sun831011 | 0:1aa8cad8b581 | 145 | if ((t2-t1)>(t3-t4)){ |
sun831011 | 0:1aa8cad8b581 | 146 | delay = ((t2-t1)-(t3-t4))/2; |
sun831011 | 0:1aa8cad8b581 | 147 | offdri = ((t2-t1)+(t3-t4))/2; |
sun831011 | 0:1aa8cad8b581 | 148 | } |
sun831011 | 0:1aa8cad8b581 | 149 | else{ |
sun831011 | 0:1aa8cad8b581 | 150 | delay = ((t3-t4)-(t2-t1))/2; |
sun831011 | 0:1aa8cad8b581 | 151 | offdri = ((t2-t1)+(t3-t4))/2; |
sun831011 | 0:1aa8cad8b581 | 152 | } |
sun831011 | 0:1aa8cad8b581 | 153 | } |
sun831011 | 0:1aa8cad8b581 | 154 | else{ //t4>t3 |
sun831011 | 0:1aa8cad8b581 | 155 | if ((t2-t1)<(t4-t3)){ |
sun831011 | 0:1aa8cad8b581 | 156 | delay = ((t2-t1)+(t4-t3))/2; |
sun831011 | 0:1aa8cad8b581 | 157 | offdri = ((t4-t3)-(t2-t1))/2; |
sun831011 | 0:1aa8cad8b581 | 158 | } |
sun831011 | 0:1aa8cad8b581 | 159 | else{ |
sun831011 | 0:1aa8cad8b581 | 160 | delay = ((t2-t1)+(t4-t3))/2; |
sun831011 | 0:1aa8cad8b581 | 161 | offdri = ((t2-t1)-(t4-t3))/2; |
sun831011 | 0:1aa8cad8b581 | 162 | } |
sun831011 | 0:1aa8cad8b581 | 163 | } |
sun831011 | 0:1aa8cad8b581 | 164 | t_run2.tv_sec = t_run.tv_sec + offdri/1000000000; |
sun831011 | 0:1aa8cad8b581 | 165 | t_run2.tv_usec = t_run.tv_usec + (offdri%1000000000)/1000; |
sun831011 | 0:1aa8cad8b581 | 166 | pc.printf("\nt2v_sec = %u, t2v_usec = %u\n",t_run2.tv_sec,t_run2.tv_usec); |
sun831011 | 0:1aa8cad8b581 | 167 | pc.printf("Delay = %llu\n",delay); |
sun831011 | 0:1aa8cad8b581 | 168 | pc.printf("offderi = %llu\n",offdri); |
sun831011 | 0:1aa8cad8b581 | 169 | |
sun831011 | 0:1aa8cad8b581 | 170 | |
sun831011 | 0:1aa8cad8b581 | 171 | } |
sun831011 | 0:1aa8cad8b581 | 172 | |
sun831011 | 0:1aa8cad8b581 | 173 | long long receive(void){ |
sun831011 | 0:1aa8cad8b581 | 174 | char x; |
sun831011 | 0:1aa8cad8b581 | 175 | long long y; |
sun831011 | 0:1aa8cad8b581 | 176 | |
sun831011 | 0:1aa8cad8b581 | 177 | y=0; |
sun831011 | 0:1aa8cad8b581 | 178 | while(1){ |
sun831011 | 0:1aa8cad8b581 | 179 | if(ss.readable()){ |
sun831011 | 0:1aa8cad8b581 | 180 | x = ss.getc(); |
sun831011 | 0:1aa8cad8b581 | 181 | if(x == 'E'){ |
sun831011 | 0:1aa8cad8b581 | 182 | return y; |
sun831011 | 0:1aa8cad8b581 | 183 | } |
sun831011 | 0:1aa8cad8b581 | 184 | else{ |
sun831011 | 0:1aa8cad8b581 | 185 | y = y*10 + (int)(x-48); |
sun831011 | 0:1aa8cad8b581 | 186 | } |
sun831011 | 0:1aa8cad8b581 | 187 | } |
sun831011 | 0:1aa8cad8b581 | 188 | } |
sun831011 | 0:1aa8cad8b581 | 189 | } |
sun831011 | 0:1aa8cad8b581 | 190 | |
sun831011 | 0:1aa8cad8b581 | 191 | long long assembletime(long long t){ |
sun831011 | 0:1aa8cad8b581 | 192 | long long temp; |
sun831011 | 0:1aa8cad8b581 | 193 | |
sun831011 | 0:1aa8cad8b581 | 194 | temp = (t/4294967296)*30*1000000000 + (t%4294967296)*10; |
sun831011 | 0:1aa8cad8b581 | 195 | return temp; |
sun831011 | 0:1aa8cad8b581 | 196 | } |
sun831011 | 0:1aa8cad8b581 | 197 | |
sun831011 | 0:1aa8cad8b581 | 198 | void backtime(){ |
sun831011 | 0:1aa8cad8b581 | 199 | long long t; |
sun831011 | 0:1aa8cad8b581 | 200 | long long temp; |
sun831011 | 0:1aa8cad8b581 | 201 | t = counter * 4294967296 + LPC_TIM0->TC; |
sun831011 | 0:1aa8cad8b581 | 202 | t = assembletime(t); |
sun831011 | 0:1aa8cad8b581 | 203 | t = t - offdri + delay; |
sun831011 | 0:1aa8cad8b581 | 204 | /* |
sun831011 | 0:1aa8cad8b581 | 205 | temp = t/30/1000000000; |
sun831011 | 0:1aa8cad8b581 | 206 | t = (t%30000000000)/10 + temp*4294967296; |
sun831011 | 0:1aa8cad8b581 | 207 | */ |
sun831011 | 0:1aa8cad8b581 | 208 | ss.printf("%llu",t); |
sun831011 | 0:1aa8cad8b581 | 209 | ss.putc('E'); |
sun831011 | 0:1aa8cad8b581 | 210 | } |
sun831011 | 0:1aa8cad8b581 | 211 | |
sun831011 | 0:1aa8cad8b581 | 212 | void toggle(){ |
sun831011 | 0:1aa8cad8b581 | 213 | slaveOut = !slaveOut; |
sun831011 | 0:1aa8cad8b581 | 214 | } |
sun831011 | 0:1aa8cad8b581 | 215 | |
sun831011 | 0:1aa8cad8b581 | 216 | long long receive_sne(void){ |
sun831011 | 0:1aa8cad8b581 | 217 | char x; |
sun831011 | 0:1aa8cad8b581 | 218 | long long y; |
sun831011 | 0:1aa8cad8b581 | 219 | |
sun831011 | 0:1aa8cad8b581 | 220 | y=0; |
sun831011 | 0:1aa8cad8b581 | 221 | while(1){ |
sun831011 | 0:1aa8cad8b581 | 222 | if(ss_sne.readable()){ |
sun831011 | 0:1aa8cad8b581 | 223 | x = ss_sne.getc(); |
sun831011 | 0:1aa8cad8b581 | 224 | if(x == 'E'){ |
sun831011 | 0:1aa8cad8b581 | 225 | return y; |
sun831011 | 0:1aa8cad8b581 | 226 | } |
sun831011 | 0:1aa8cad8b581 | 227 | else{ |
sun831011 | 0:1aa8cad8b581 | 228 | y = y*10 + (int)(x-48); |
sun831011 | 0:1aa8cad8b581 | 229 | } |
sun831011 | 0:1aa8cad8b581 | 230 | } |
sun831011 | 0:1aa8cad8b581 | 231 | } |
sun831011 | 0:1aa8cad8b581 | 232 | } |
sun831011 | 0:1aa8cad8b581 | 233 | |
sun831011 | 0:1aa8cad8b581 | 234 | void getTime(struct timeval *tv){ |
sun831011 | 0:1aa8cad8b581 | 235 | unsigned long ns, hs; |
sun831011 | 0:1aa8cad8b581 | 236 | ns = LPC_TIM0->TC; |
sun831011 | 0:1aa8cad8b581 | 237 | hs = counter; |
sun831011 | 0:1aa8cad8b581 | 238 | tv->tv_sec = hs*30 + ns/100000000; |
sun831011 | 0:1aa8cad8b581 | 239 | tv->tv_usec = (ns%100000000)/100; |
sun831011 | 0:1aa8cad8b581 | 240 | } |
sun831011 | 0:1aa8cad8b581 | 241 | |
sun831011 | 0:1aa8cad8b581 | 242 | int runAtTime (void (*fptr)(void), struct timeval tv){ |
sun831011 | 0:1aa8cad8b581 | 243 | struct timeval current_t; |
sun831011 | 0:1aa8cad8b581 | 244 | getTime(¤t_t); |
sun831011 | 0:1aa8cad8b581 | 245 | if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){ |
sun831011 | 0:1aa8cad8b581 | 246 | (*fptr) ( ); |
sun831011 | 0:1aa8cad8b581 | 247 | return 1; |
sun831011 | 0:1aa8cad8b581 | 248 | } |
sun831011 | 0:1aa8cad8b581 | 249 | else |
sun831011 | 0:1aa8cad8b581 | 250 | return 0; |
sun831011 | 0:1aa8cad8b581 | 251 | } |
sun831011 | 0:1aa8cad8b581 | 252 |