FRDM-KL46Z board sLCD demo code using RTC clock.

Dependencies:   SLCD mbed

Fork of FRDM-KL46Z LCD rtc Demo by Paul Staron

Committer:
arthshah1
Date:
Thu Dec 18 21:47:44 2014 +0000
Revision:
2:1c12897871e5
Parent:
1:34f0bfc62803
asdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:4f67859595b2 1 #include "mbed.h"
star297 0:4f67859595b2 2 #include "SLCD.h"
arthshah1 2:1c12897871e5 3 #include "InterruptManager.h"
arthshah1 2:1c12897871e5 4 #include "TPM_init.h"
star297 0:4f67859595b2 5
arthshah1 2:1c12897871e5 6 #define TOTAL_SYNC 3
arthshah1 2:1c12897871e5 7 #define PI 3.1415926f
arthshah1 2:1c12897871e5 8
arthshah1 2:1c12897871e5 9 DigitalOut d_out(D2);
arthshah1 2:1c12897871e5 10 DigitalIn d_in(D3);
arthshah1 2:1c12897871e5 11 InterruptIn d_IRQ(D3);
arthshah1 2:1c12897871e5 12
arthshah1 2:1c12897871e5 13 int temp_pulse_flag[TOTAL_SYNC];
arthshah1 2:1c12897871e5 14 int temp_pulse_tpm[TOTAL_SYNC];
arthshah1 2:1c12897871e5 15
arthshah1 2:1c12897871e5 16 int temp_sync_flag[TOTAL_SYNC];
arthshah1 2:1c12897871e5 17 int temp_sync_tpm[TOTAL_SYNC];
arthshah1 2:1c12897871e5 18
arthshah1 2:1c12897871e5 19 bool dbg = 0;
star297 1:34f0bfc62803 20 time_t seconds = time(NULL); // needed to start rtc on reset to maintain reasonable time if hard reset
star297 0:4f67859595b2 21
star297 0:4f67859595b2 22 SLCD slcd;
arthshah1 2:1c12897871e5 23 //Timer scroll;
arthshah1 2:1c12897871e5 24
arthshah1 2:1c12897871e5 25 Serial pc(USBTX, USBRX); // tx, rx
arthshah1 2:1c12897871e5 26 RawSerial rpi(USBTX, USBRX); // tx, rx
star297 1:34f0bfc62803 27 DigitalOut led1(LED1);
star297 1:34f0bfc62803 28 DigitalOut led2(LED2);
arthshah1 2:1c12897871e5 29 DigitalOut led_r(LED_RED);
arthshah1 2:1c12897871e5 30 DigitalOut pps(D7);
arthshah1 2:1c12897871e5 31 AnalogIn lightSensor(PTE22);
arthshah1 2:1c12897871e5 32 //InterruptIn setmin (SW1);
arthshah1 2:1c12897871e5 33 //InterruptIn sethour (SW3);
star297 0:4f67859595b2 34
arthshah1 2:1c12897871e5 35 //struct tm t;
star297 0:4f67859595b2 36
arthshah1 2:1c12897871e5 37 //int i,j,k,lastscroll,display_timer,minute,hour,colon,dp;
arthshah1 2:1c12897871e5 38 int i;
star297 1:34f0bfc62803 39 char message[60];
arthshah1 2:1c12897871e5 40 //void scroll_message();
star297 0:4f67859595b2 41 char buffer[32];
star297 0:4f67859595b2 42
arthshah1 2:1c12897871e5 43 //void setminIRQ();
arthshah1 2:1c12897871e5 44 //void sethourIRQ();
arthshah1 2:1c12897871e5 45 void Tx_interrupt();
arthshah1 2:1c12897871e5 46 void Rx_interrupt();
arthshah1 2:1c12897871e5 47 void send_line();
arthshah1 2:1c12897871e5 48 void read_line();
arthshah1 2:1c12897871e5 49
arthshah1 2:1c12897871e5 50 // Circular buffers for serial TX and RX data - used by interrupt routines
arthshah1 2:1c12897871e5 51 const int buffer_size = 255;
arthshah1 2:1c12897871e5 52 // might need to increase buffer size for high baud rates
arthshah1 2:1c12897871e5 53 char tx_buffer[buffer_size+1];
arthshah1 2:1c12897871e5 54 char rx_buffer[buffer_size+1];
arthshah1 2:1c12897871e5 55 char temp_rx_buffer[2];
arthshah1 2:1c12897871e5 56 char str[6];
arthshah1 2:1c12897871e5 57 // Circular buffer pointers
arthshah1 2:1c12897871e5 58 // volatile makes read-modify-write atomic
arthshah1 2:1c12897871e5 59 volatile int tx_in=0;
arthshah1 2:1c12897871e5 60 volatile int tx_out=0;
arthshah1 2:1c12897871e5 61 volatile int rx_in=0;
arthshah1 2:1c12897871e5 62 volatile int rx_out=0;
arthshah1 2:1c12897871e5 63 // Line buffers for sprintf and sscanf
arthshah1 2:1c12897871e5 64 char tx_line[80];
arthshah1 2:1c12897871e5 65 char rx_line[80];
arthshah1 2:1c12897871e5 66 unsigned int flag = 0;
arthshah1 2:1c12897871e5 67 unsigned int modulo_val = 48000; //period = 1 ms
arthshah1 2:1c12897871e5 68
arthshah1 2:1c12897871e5 69 unsigned int new_sync_flag = 0;
arthshah1 2:1c12897871e5 70 unsigned int new_sync_tpm = 0;
arthshah1 2:1c12897871e5 71 unsigned int old_sync_flag = 0;
arthshah1 2:1c12897871e5 72 unsigned int old_sync_tpm = 0;
arthshah1 2:1c12897871e5 73 unsigned int sync_flag = 0;
arthshah1 2:1c12897871e5 74 unsigned int sync_tpm = 0;
arthshah1 2:1c12897871e5 75
arthshah1 2:1c12897871e5 76 unsigned int new_pulse_flag = 100;
arthshah1 2:1c12897871e5 77 unsigned int new_pulse_tpm = 0;
arthshah1 2:1c12897871e5 78 unsigned int old_pulse_flag = 100;
arthshah1 2:1c12897871e5 79 unsigned int old_pulse_tpm = 0;
arthshah1 2:1c12897871e5 80 unsigned int pulse_flag = 0;
arthshah1 2:1c12897871e5 81 unsigned int pulse_tpm = 0;
arthshah1 2:1c12897871e5 82
arthshah1 2:1c12897871e5 83 unsigned int new_arrival_flag = 0;
arthshah1 2:1c12897871e5 84 unsigned int new_arrival_tpm = 0;
arthshah1 2:1c12897871e5 85 unsigned int old_arrival_flag = 0;
arthshah1 2:1c12897871e5 86 unsigned int old_arrival_tpm = 0;
arthshah1 2:1c12897871e5 87 unsigned int arrival_flag = 0;
arthshah1 2:1c12897871e5 88 unsigned int arrival_tpm = 0;
arthshah1 2:1c12897871e5 89
arthshah1 2:1c12897871e5 90 unsigned int state = 0;
arthshah1 2:1c12897871e5 91 unsigned int pps_state = 0;
arthshah1 2:1c12897871e5 92
arthshah1 2:1c12897871e5 93 Ticker tick_DAC;
arthshah1 2:1c12897871e5 94 Ticker tick_test;
arthshah1 2:1c12897871e5 95 AnalogOut DAC(PTE30);
arthshah1 2:1c12897871e5 96
arthshah1 2:1c12897871e5 97 const long num_samp_per_sec = 10000;
arthshah1 2:1c12897871e5 98 const int freq_DAC[4] = {1,10,100,1000};
arthshah1 2:1c12897871e5 99 int count_ADC;
arthshah1 2:1c12897871e5 100 long count_DAC;
arthshah1 2:1c12897871e5 101 int sel_freq_DAC;
arthshah1 2:1c12897871e5 102 char ADC_string[6];
arthshah1 2:1c12897871e5 103 char final_string[7] = "V";
arthshah1 2:1c12897871e5 104 bool on_DAC;
arthshah1 2:1c12897871e5 105
arthshah1 2:1c12897871e5 106 AnalogIn in_0(A0);
arthshah1 2:1c12897871e5 107 //AnalogIn in_1(A1);
arthshah1 2:1c12897871e5 108 bool flag_for_d;
star297 0:4f67859595b2 109
arthshah1 2:1c12897871e5 110 void sine_gen() {
arthshah1 2:1c12897871e5 111 float tem = on_DAC? 0.5*sin(2*PI*freq_DAC[sel_freq_DAC]*count_DAC/num_samp_per_sec) + 0.5 : 0.0;
arthshah1 2:1c12897871e5 112 //float temf = 0.1;
arthshah1 2:1c12897871e5 113 //float tem = 0.5*sin(2*PI*temf*count_DAC/num_samp_per_sec) + 0.5;
arthshah1 2:1c12897871e5 114 //if (count_DAC % 10 == 0) pc.printf("write%f\n",tem);
arthshah1 2:1c12897871e5 115 DAC.write(tem);
arthshah1 2:1c12897871e5 116 count_DAC += 1;
arthshah1 2:1c12897871e5 117 if (count_DAC == num_samp_per_sec - 1) count_DAC = 0;
arthshah1 2:1c12897871e5 118 //if (count_DAC == 10*num_samp_per_sec - 1) count_DAC = 0;
arthshah1 2:1c12897871e5 119 count_ADC += 1;
arthshah1 2:1c12897871e5 120 if (count_ADC == 10000)
arthshah1 2:1c12897871e5 121 {
arthshah1 2:1c12897871e5 122 //sprintf(str, "v%0.3f",in_0.read());
arthshah1 2:1c12897871e5 123 //for(i=0; i<6; i++) rpi.putc(str[i]);
arthshah1 2:1c12897871e5 124 //rpi.printf("V%0.3f",in_0.read());
arthshah1 2:1c12897871e5 125 count_ADC = 0;
arthshah1 2:1c12897871e5 126 }
arthshah1 2:1c12897871e5 127 }
arthshah1 2:1c12897871e5 128
arthshah1 2:1c12897871e5 129 void init() {
arthshah1 2:1c12897871e5 130 d_out = false;
arthshah1 2:1c12897871e5 131 flag_for_d = false;
arthshah1 2:1c12897871e5 132
arthshah1 2:1c12897871e5 133 count_DAC = 0;
arthshah1 2:1c12897871e5 134 count_ADC = 0;
arthshah1 2:1c12897871e5 135 sel_freq_DAC = 0;
arthshah1 2:1c12897871e5 136 on_DAC = true;
arthshah1 2:1c12897871e5 137 led_r = true;
arthshah1 2:1c12897871e5 138 //led_g = false;
arthshah1 2:1c12897871e5 139 }
arthshah1 2:1c12897871e5 140
arthshah1 2:1c12897871e5 141 void logichigh() {
arthshah1 2:1c12897871e5 142 led_r=false;
arthshah1 2:1c12897871e5 143 rpi.putc('H');
arthshah1 2:1c12897871e5 144 }
arthshah1 2:1c12897871e5 145
arthshah1 2:1c12897871e5 146 void logiclow() {
arthshah1 2:1c12897871e5 147 led_r=true;
arthshah1 2:1c12897871e5 148 rpi.putc('L');
arthshah1 2:1c12897871e5 149 }
arthshah1 2:1c12897871e5 150
arthshah1 2:1c12897871e5 151 int num_expire = 0;
arthshah1 2:1c12897871e5 152 void TPM0_handler() {
arthshah1 2:1c12897871e5 153 unsigned int overflow = TPM0_SC_read() & 0x000000C0;
arthshah1 2:1c12897871e5 154 if(overflow == 0xC0) {
arthshah1 2:1c12897871e5 155 TPM0_clear_overflow();
arthshah1 2:1c12897871e5 156 flag++;
arthshah1 2:1c12897871e5 157 if(state==0){
arthshah1 2:1c12897871e5 158 if(pps_state == 0){
arthshah1 2:1c12897871e5 159 if( sync_flag==0 && sync_tpm==0 ){
arthshah1 2:1c12897871e5 160 pps = 1;
arthshah1 2:1c12897871e5 161 pps_state = 1;
arthshah1 2:1c12897871e5 162 if(dbg) pc.printf("pulse high!\n");
arthshah1 2:1c12897871e5 163 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 164 TPM0_start();
arthshah1 2:1c12897871e5 165 //num_expire = 0;
arthshah1 2:1c12897871e5 166 } else if ( sync_flag==0 ){
arthshah1 2:1c12897871e5 167 //if(dbg) pc.printf("I shouldn't be here\n");
arthshah1 2:1c12897871e5 168 TPM0_init(sync_tpm);
arthshah1 2:1c12897871e5 169 TPM0_start();
arthshah1 2:1c12897871e5 170 sync_tpm = 0;
arthshah1 2:1c12897871e5 171 } else {
arthshah1 2:1c12897871e5 172 sync_flag--;
arthshah1 2:1c12897871e5 173 }
arthshah1 2:1c12897871e5 174 }
arthshah1 2:1c12897871e5 175 else {
arthshah1 2:1c12897871e5 176 if( pulse_flag==0 && pulse_tpm==0 ){
arthshah1 2:1c12897871e5 177 pps = 0;
arthshah1 2:1c12897871e5 178 pps_state = 0;
arthshah1 2:1c12897871e5 179 if(dbg) pc.printf("pulse low!\n");
arthshah1 2:1c12897871e5 180 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 181 //TPM0_start();
arthshah1 2:1c12897871e5 182 state = 1;
arthshah1 2:1c12897871e5 183 } else if ( pulse_flag==0 ){
arthshah1 2:1c12897871e5 184 //if(dbg) pc.printf("I shouldn't be here\n");
arthshah1 2:1c12897871e5 185 TPM0_init(pulse_tpm);
arthshah1 2:1c12897871e5 186 TPM0_start();
arthshah1 2:1c12897871e5 187 pulse_tpm = 0;
arthshah1 2:1c12897871e5 188 } else {
arthshah1 2:1c12897871e5 189 pulse_flag--;
arthshah1 2:1c12897871e5 190 }
arthshah1 2:1c12897871e5 191 }
arthshah1 2:1c12897871e5 192 } else if (state == 1){
arthshah1 2:1c12897871e5 193 }
arthshah1 2:1c12897871e5 194 //if(dbg) pc.printf("flag = %d\n", flag);
arthshah1 2:1c12897871e5 195
arthshah1 2:1c12897871e5 196 NVIC_ClearPendingIRQ(TPM0_IRQn);
arthshah1 2:1c12897871e5 197 }
arthshah1 2:1c12897871e5 198 }
star297 0:4f67859595b2 199
star297 0:4f67859595b2 200 main()
star297 1:34f0bfc62803 201 {
arthshah1 2:1c12897871e5 202 init();
arthshah1 2:1c12897871e5 203 //pc.printf("Welcome\r\na:on/off (default: on) s:1Hz d:10Hz f:100Hz g:1kHz\r\nq:digital on w:digital off\r\np:print values\r\n");
arthshah1 2:1c12897871e5 204 tick_DAC.attach_us(&sine_gen, (double)1000000/num_samp_per_sec);
arthshah1 2:1c12897871e5 205 //tick_test.attach(&print_in, (double)1.0/sampling_freq);
arthshah1 2:1c12897871e5 206
arthshah1 2:1c12897871e5 207 d_IRQ.rise(&logichigh);
arthshah1 2:1c12897871e5 208 d_IRQ.fall(&logiclow);
arthshah1 2:1c12897871e5 209
star297 1:34f0bfc62803 210 slcd.All_Segments(1);
arthshah1 2:1c12897871e5 211 wait(1);
star297 1:34f0bfc62803 212 slcd.All_Segments(0);
star297 1:34f0bfc62803 213 wait(1);
star297 1:34f0bfc62803 214
arthshah1 2:1c12897871e5 215 led1 = 1; led2 = 1; pps = 1;
arthshah1 2:1c12897871e5 216
arthshah1 2:1c12897871e5 217 sprintf(message, "1234");
arthshah1 2:1c12897871e5 218 slcd.printf(message);
arthshah1 2:1c12897871e5 219 if(dbg) pc.printf("Hello World!\n");
arthshah1 2:1c12897871e5 220 //pc.putc('1');
arthshah1 2:1c12897871e5 221 NVIC_SetVector(TPM0_IRQn, (uint32_t) TPM0_handler);
arthshah1 2:1c12897871e5 222 NVIC_SetPriority(TPM0_IRQn, 0);
arthshah1 2:1c12897871e5 223 NVIC_EnableIRQ(TPM0_IRQn);
arthshah1 2:1c12897871e5 224 //TPM0_init(modulo_val);
star297 1:34f0bfc62803 225
arthshah1 2:1c12897871e5 226 if(dbg) pc.printf("Setting up UART interrupts!\n");
arthshah1 2:1c12897871e5 227 // Setup a serial interrupt function to receive data
arthshah1 2:1c12897871e5 228 rpi.attach(&Rx_interrupt, Serial::RxIrq);
arthshah1 2:1c12897871e5 229 // Setup a serial interrupt function to transmit data
arthshah1 2:1c12897871e5 230 //rpi.attach(&Tx_interrupt, Serial::TxIrq);
arthshah1 2:1c12897871e5 231
arthshah1 2:1c12897871e5 232 //char temp[4];
arthshah1 2:1c12897871e5 233 while(1){
star297 1:34f0bfc62803 234 }
arthshah1 2:1c12897871e5 235 //wait(3);
arthshah1 2:1c12897871e5 236 //pulse_flag = 100;
arthshah1 2:1c12897871e5 237 //pulse_tpm = 5;
arthshah1 2:1c12897871e5 238 //TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 239 //TPM0_start();
arthshah1 2:1c12897871e5 240 //if(dbg) pc.printf("Timer started!\n");
arthshah1 2:1c12897871e5 241 /*
star297 1:34f0bfc62803 242 while(1) {
arthshah1 2:1c12897871e5 243 flag = 0;
arthshah1 2:1c12897871e5 244 TPM0_start();
arthshah1 2:1c12897871e5 245 //slcd.putc(pc.getc());
arthshah1 2:1c12897871e5 246 //led1 = 0;
arthshah1 2:1c12897871e5 247 wait(0.5);
arthshah1 2:1c12897871e5 248 if(dbg) pc.printf("flag = %d, TPM = %x\n", flag, TPM0_read());
arthshah1 2:1c12897871e5 249 //pc.putc('1');
arthshah1 2:1c12897871e5 250 //led1 = 1;
star297 0:4f67859595b2 251 }
arthshah1 2:1c12897871e5 252 */
star297 1:34f0bfc62803 253 }
star297 0:4f67859595b2 254
arthshah1 2:1c12897871e5 255 unsigned int rx_state = 0;
arthshah1 2:1c12897871e5 256 unsigned int num_sync = 0;
arthshah1 2:1c12897871e5 257 unsigned int num_sec = 0;
arthshah1 2:1c12897871e5 258 // Interupt Routine to read in data from serial port
arthshah1 2:1c12897871e5 259 void Rx_interrupt() {
arthshah1 2:1c12897871e5 260 char c = rpi.getc();
arthshah1 2:1c12897871e5 261 //led1=0;
arthshah1 2:1c12897871e5 262 // Loop just in case more than one character is in UART's receive FIFO buffer
arthshah1 2:1c12897871e5 263 // Stop if buffer full
arthshah1 2:1c12897871e5 264 if( c == 't'){
arthshah1 2:1c12897871e5 265 state = 0;
arthshah1 2:1c12897871e5 266 rx_state = 0;
arthshah1 2:1c12897871e5 267 num_sec++;
arthshah1 2:1c12897871e5 268 if(num_sec==4) num_sec =0;
arthshah1 2:1c12897871e5 269 if( pulse_flag==0 && pulse_tpm==0){
arthshah1 2:1c12897871e5 270 pulse_flag = old_pulse_flag;
arthshah1 2:1c12897871e5 271 pulse_tpm = old_pulse_tpm;
arthshah1 2:1c12897871e5 272 }
arthshah1 2:1c12897871e5 273 if( arrival_flag==0 && arrival_tpm==0){
arthshah1 2:1c12897871e5 274 sync_flag = old_sync_flag;
arthshah1 2:1c12897871e5 275 sync_tpm = old_sync_tpm;
star297 0:4f67859595b2 276 }
arthshah1 2:1c12897871e5 277 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 278 TPM0_start();
arthshah1 2:1c12897871e5 279 arrival_flag= 0;
arthshah1 2:1c12897871e5 280 arrival_tpm = 0;
arthshah1 2:1c12897871e5 281 } else if ( c == 'n'){
arthshah1 2:1c12897871e5 282 on_DAC = true;
arthshah1 2:1c12897871e5 283 } else if ( c == 'o'){
arthshah1 2:1c12897871e5 284 on_DAC = false;
arthshah1 2:1c12897871e5 285 } else if ( c == 'x'){
arthshah1 2:1c12897871e5 286 sel_freq_DAC = 0;
arthshah1 2:1c12897871e5 287 } else if ( c == 'y'){
arthshah1 2:1c12897871e5 288 sel_freq_DAC = 1;
arthshah1 2:1c12897871e5 289 } else if ( c == 'z'){
arthshah1 2:1c12897871e5 290 sel_freq_DAC = 2;
arthshah1 2:1c12897871e5 291 } else if ( c == 'u'){
arthshah1 2:1c12897871e5 292 sel_freq_DAC = 3;
arthshah1 2:1c12897871e5 293 } else if ( c == 'q'){
arthshah1 2:1c12897871e5 294 d_out = true;
arthshah1 2:1c12897871e5 295 } else if ( c == 'w'){
arthshah1 2:1c12897871e5 296 d_out = false;
arthshah1 2:1c12897871e5 297 } else if ( c == 'p'){
arthshah1 2:1c12897871e5 298 if(num_sync > 0){
arthshah1 2:1c12897871e5 299 temp_pulse_flag[num_sync-1] = flag;
arthshah1 2:1c12897871e5 300 temp_pulse_tpm[num_sync-1] = TPM0_read();
arthshah1 2:1c12897871e5 301 if(dbg) pc.printf("temp_pulse_flag[%d] = %d, temp_pulse_tpm[%d] = %d\n", num_sync-1, temp_pulse_flag[num_sync-1], num_sync-1, temp_pulse_tpm[num_sync-1]);
arthshah1 2:1c12897871e5 302 }
arthshah1 2:1c12897871e5 303 flag = 0;
arthshah1 2:1c12897871e5 304 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 305 if(num_sync < TOTAL_SYNC){
arthshah1 2:1c12897871e5 306 TPM0_start();
arthshah1 2:1c12897871e5 307 num_sync++;
star297 0:4f67859595b2 308 } else {
arthshah1 2:1c12897871e5 309 pulse_flag = 0;
arthshah1 2:1c12897871e5 310 pulse_tpm = 0;
arthshah1 2:1c12897871e5 311 for(i=0; i<TOTAL_SYNC; i++){
arthshah1 2:1c12897871e5 312 pulse_flag += temp_pulse_flag[i];
arthshah1 2:1c12897871e5 313 pulse_tpm += temp_pulse_tpm[i];
arthshah1 2:1c12897871e5 314 }
arthshah1 2:1c12897871e5 315 pulse_flag /= TOTAL_SYNC;
arthshah1 2:1c12897871e5 316 pulse_tpm /= TOTAL_SYNC;
arthshah1 2:1c12897871e5 317 if(pulse_flag<50 || pulse_flag>150){
arthshah1 2:1c12897871e5 318 pulse_flag = 0;
arthshah1 2:1c12897871e5 319 pulse_tpm = 0;
arthshah1 2:1c12897871e5 320 } else {
arthshah1 2:1c12897871e5 321 old_pulse_flag = pulse_flag;
arthshah1 2:1c12897871e5 322 old_pulse_tpm = pulse_tpm;
arthshah1 2:1c12897871e5 323 }
arthshah1 2:1c12897871e5 324 flag = 0;
arthshah1 2:1c12897871e5 325 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 326 if(dbg) pc.printf("pulse_flag = %d, pulse_tpm = %x\n", pulse_flag, pulse_tpm);
arthshah1 2:1c12897871e5 327 if(num_sec==0){
arthshah1 2:1c12897871e5 328 rpi.putc('S');
arthshah1 2:1c12897871e5 329 TPM0_start();
arthshah1 2:1c12897871e5 330 num_sync=1;
arthshah1 2:1c12897871e5 331 } else {
arthshah1 2:1c12897871e5 332 num_sync=0;
arthshah1 2:1c12897871e5 333 }
star297 0:4f67859595b2 334 }
arthshah1 2:1c12897871e5 335 } else if ( c == 's'){
arthshah1 2:1c12897871e5 336 temp_sync_flag[num_sync-1] = flag;
arthshah1 2:1c12897871e5 337 temp_sync_tpm[num_sync-1] = TPM0_read();
arthshah1 2:1c12897871e5 338 if(dbg) pc.printf("temp_sync_flag[%d] = %d, temp_sync_tpm[%d] = %d\n", num_sync-1, temp_sync_flag[num_sync-1], num_sync-1, temp_sync_tpm[num_sync-1]);
arthshah1 2:1c12897871e5 339 flag = 0;
arthshah1 2:1c12897871e5 340 TPM0_init(modulo_val);
arthshah1 2:1c12897871e5 341 if(num_sync < TOTAL_SYNC){
arthshah1 2:1c12897871e5 342 rpi.putc('S');
arthshah1 2:1c12897871e5 343 TPM0_start();
arthshah1 2:1c12897871e5 344 num_sync++;
arthshah1 2:1c12897871e5 345 } else {
arthshah1 2:1c12897871e5 346 arrival_flag = 0;
arthshah1 2:1c12897871e5 347 arrival_tpm = 0;
arthshah1 2:1c12897871e5 348 for(i=0; i<TOTAL_SYNC; i++){
arthshah1 2:1c12897871e5 349 arrival_flag += temp_sync_flag[i];
arthshah1 2:1c12897871e5 350 arrival_tpm += temp_sync_tpm[i];
arthshah1 2:1c12897871e5 351 }
arthshah1 2:1c12897871e5 352 arrival_flag /= (2*TOTAL_SYNC);
arthshah1 2:1c12897871e5 353 arrival_tpm /= (2*TOTAL_SYNC);
arthshah1 2:1c12897871e5 354 old_arrival_flag = arrival_flag;
arthshah1 2:1c12897871e5 355 old_arrival_tpm = arrival_tpm;
arthshah1 2:1c12897871e5 356 num_sync=0;
arthshah1 2:1c12897871e5 357 if(dbg) pc.printf("arrival_flag = %d, arrival_tpm = %x\n", arrival_flag, arrival_tpm);
arthshah1 2:1c12897871e5 358 rpi.putc('M');
arthshah1 2:1c12897871e5 359 sprintf(str, "%02d", arrival_flag);
arthshah1 2:1c12897871e5 360 rpi.putc(str[0]);
arthshah1 2:1c12897871e5 361 rpi.putc(str[1]);
arthshah1 2:1c12897871e5 362 sprintf(str, "%04x", arrival_tpm);
arthshah1 2:1c12897871e5 363 rpi.putc(str[0]);
arthshah1 2:1c12897871e5 364 rpi.putc(str[1]);
arthshah1 2:1c12897871e5 365 rpi.putc(str[2]);
arthshah1 2:1c12897871e5 366 rpi.putc(str[3]);
arthshah1 2:1c12897871e5 367 }
arthshah1 2:1c12897871e5 368 } else if( c == 'm' ){
arthshah1 2:1c12897871e5 369 rx_state = 1;
arthshah1 2:1c12897871e5 370 } else {
arthshah1 2:1c12897871e5 371 //while ((rpi.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
arthshah1 2:1c12897871e5 372 if(rx_state == 0){
arthshah1 2:1c12897871e5 373 //if(dbg) pc.printf("t_rx_in = %d\n", rx_in);
arthshah1 2:1c12897871e5 374 rx_buffer[rx_in] = c;
arthshah1 2:1c12897871e5 375 rx_in = (rx_in + 1) % buffer_size;
arthshah1 2:1c12897871e5 376 if(rx_in == 4){
arthshah1 2:1c12897871e5 377 slcd.Home();
arthshah1 2:1c12897871e5 378 slcd.printf(rx_buffer);
arthshah1 2:1c12897871e5 379 slcd.Colon(1);
arthshah1 2:1c12897871e5 380 rx_in = 0;
arthshah1 2:1c12897871e5 381 //rpi.printf("l%0.3f",lightSensor.read());
arthshah1 2:1c12897871e5 382 //rpi.printf("V%0.3f",in_0.read());
arthshah1 2:1c12897871e5 383 sprintf(str, "l%0.3f",lightSensor.read());
arthshah1 2:1c12897871e5 384 for(i=0; i<6; i++) rpi.putc(str[i]);
arthshah1 2:1c12897871e5 385 sprintf(str, "v%0.3f",in_0.read());
arthshah1 2:1c12897871e5 386 for(i=0; i<6; i++) rpi.putc(str[i]);
arthshah1 2:1c12897871e5 387 }
arthshah1 2:1c12897871e5 388 } else if(rx_state == 1){
arthshah1 2:1c12897871e5 389 //if(dbg) pc.printf("m_rx_in = %d\n", rx_in);
arthshah1 2:1c12897871e5 390 rx_buffer[rx_in] = c;
arthshah1 2:1c12897871e5 391 rx_in = (rx_in + 1) % buffer_size;
arthshah1 2:1c12897871e5 392 if(rx_in >= 6){
arthshah1 2:1c12897871e5 393 sync_flag = (10*(rx_buffer[0]-'0') + (rx_buffer[1]-'0'));
arthshah1 2:1c12897871e5 394 if(sync_flag >= arrival_flag){
arthshah1 2:1c12897871e5 395 sync_flag -= arrival_flag;
arthshah1 2:1c12897871e5 396 for(i=0; i<6; i++){
arthshah1 2:1c12897871e5 397 if( i<4 ) {rx_buffer[i] = rx_buffer[i+2];} else { rx_buffer[i]=0; }
arthshah1 2:1c12897871e5 398 }
arthshah1 2:1c12897871e5 399 sync_tpm = (int)strtol(rx_buffer, NULL, 16);
arthshah1 2:1c12897871e5 400 if(sync_tpm >= arrival_tpm){
arthshah1 2:1c12897871e5 401 sync_tpm -= arrival_tpm;
arthshah1 2:1c12897871e5 402 } else {
arthshah1 2:1c12897871e5 403 sync_tpm = 0x10000 + sync_tpm - arrival_tpm;
arthshah1 2:1c12897871e5 404 sync_flag--;
arthshah1 2:1c12897871e5 405 }
arthshah1 2:1c12897871e5 406 } else { sync_flag =0; sync_tpm = 0; }
arthshah1 2:1c12897871e5 407 if(dbg) pc.printf("sync_flag = %d, sync_tpm = %x\n", sync_flag, sync_tpm);
arthshah1 2:1c12897871e5 408 rx_in = 0;
arthshah1 2:1c12897871e5 409 }
arthshah1 2:1c12897871e5 410 }
star297 0:4f67859595b2 411 }
arthshah1 2:1c12897871e5 412 //led1=1;
arthshah1 2:1c12897871e5 413 return;
arthshah1 2:1c12897871e5 414 }