From c556bc8ab76764ec4c27f1a705b6323178e490ed Mon Sep 17 00:00:00 2001 From: eddyem Date: Tue, 4 Aug 2015 00:46:18 +0300 Subject: [PATCH] add trigger to check GPS sync --- GPS/GPS.bin | Bin 9212 -> 9412 bytes GPS/GPS.c | 7 ++-- GPS/hardware_ini.c | 12 ++++--- GPS/main.c | 80 +++++++++++++++++++++++++++++++-------------- GPS/main.h | 1 + 5 files changed, 67 insertions(+), 33 deletions(-) diff --git a/GPS/GPS.bin b/GPS/GPS.bin index 6f0e549cc597153805276b3a65ad50dad6a90750..1a1b635ea48644ba84ba6931c95e4659d64096dc 100755 GIT binary patch delta 2774 zcmbVOe{2)i9e?k9w(o2wIKPNvljP2ql(caYha|JvBIqR67~OO~p4v#N`0R zZp3-q`?TpF_#@bwU8C1LOizT}jt_00N*k&epJ>Hwj36 zl;})HOiJUmH%{xGxhZ4PE3l^$({?{n9q+NH>X*;XvwfT~a=9tZ%?q1=*KCk_5khg(6H93K5Bl9@5N4 z@F01X-H1!bBwO2heclogWMP!=!>W)G6#W*h1eyqf$v_?A`k?fC0Wi73HjIRmq{P@- zq|N`b>+Td6b|^^wnDiSfO3uOh#l<}(u&zs?H* z4k|hilvdU-zd((PvG99mb1PHQyF6K<#YMq z&qg-R6MS|V2|{yVo;{ichZvcd{L=94aE zZ0sbglTPPH*U~h6@HGpEd%z!^rUsxJ!%bhRy)NYolYi#M1GY34W47$Ld1$n`6LyAco8e&^JFzp^=r5Gdn}A9Wf|V3Q9&xN6R%0JS|VeGb6FK1}ltr~f42!{o?Qy%Iq90K-BZLR~}9^Vi(MD3jx zHn?|SG{l#JTS<*b~C#M_y_WQ^MY2)dqMkoLF>x%O$*xP z`3BJbJ=+X>?fIP^^JVJ6n&&-CfiuDV(Y1*zA9h7?BQwPXb?x67GbB8t1@;dCU<@}P_2mrQy{xY7$+c`kbq%tvO=e@w;7ZLQHI#lQ;Mvp?@NB-E zLI|mQvaL$h=!3AjEbE1>zsj2e!l`m8gZd0sd609H0c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bMEgg(#7?|w-TfC0*e=kZU9-aBak diff --git a/GPS/GPS.c b/GPS/GPS.c index 1633345..830a91a 100644 --- a/GPS/GPS.c +++ b/GPS/GPS.c @@ -180,12 +180,11 @@ void GPS_parse_answer(uint8_t *buf){ ptr = ustrchr(buf, ','); *ptr++ = 0; //P(buf); - if(*ptr == 'A') + if(*ptr == 'A'){ GPS_status = GPS_VALID; - else + set_time(buf); + }else GPS_status = GPS_NOT_VALID; - print_curtime(); - set_time(buf); // buf = ustrchr(ptr, ','); // P(" "); P("\n"); diff --git a/GPS/hardware_ini.c b/GPS/hardware_ini.c index 17d4e6b..1fe023e 100644 --- a/GPS/hardware_ini.c +++ b/GPS/hardware_ini.c @@ -35,13 +35,17 @@ void GPIO_init(){ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN | RCC_APB2ENR_IOPEEN); - // Setup EXTI on PA4 (PPS input from GPS) - pull down - gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO4); + /* + * Setup EXTI on PA4 (PPS input from GPS) - pull down + * EXTI on PA5 - also pull down (trigger for time measurement) + */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO4 | GPIO5); //AFIO_EXTICR2 = 0; - exti_enable_request(EXTI4); + exti_enable_request(EXTI4 | EXTI5); // trigger on rising edge - exti_set_trigger(EXTI4, EXTI_TRIGGER_RISING); + exti_set_trigger(EXTI4 | EXTI5, EXTI_TRIGGER_RISING); nvic_enable_irq(NVIC_EXTI4_IRQ); + nvic_enable_irq(NVIC_EXTI9_5_IRQ); /* // Buttons: pull-up input gpio_set_mode(BTNS_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, diff --git a/GPS/main.c b/GPS/main.c index 8a25db6..028dc69 100644 --- a/GPS/main.c +++ b/GPS/main.c @@ -26,9 +26,10 @@ #include "GPS.h" volatile uint32_t Timer = 0; // milliseconds +volatile uint32_t msctr = 0; // global milliseconds for different purposes usbd_device *usbd_dev; -volatile uint32_t systick_val = 0; -volatile uint32_t timer_val = 0; +volatile int32_t systick_val = 0; +volatile int32_t timer_val = 0; volatile int clear_ST_on_connect = 1; volatile int need_sync = 1; @@ -40,6 +41,10 @@ volatile uint32_t RVR0 = STK_RVR_DEFAULT_VAL, RVR1 = STK_RVR_DEFAULT_VAL; curtime current_time = {25,61,61}; +#define DIDNT_TRIGGERED (2000) +curtime trigger_time = {25, 61, 61}; +uint32_t trigger_ms = DIDNT_TRIGGERED; + void time_increment(){ Timer = 0; if(current_time.H == 25) return; // Time not initialized @@ -77,6 +82,7 @@ int main(){ GPS_send_start_seq(); + uint32_t trigrtm = 0; while(1){ usbd_poll(usbd_dev); if(usbdatalen){ // there's something in USB buffer @@ -97,6 +103,13 @@ int main(){ P(", RVR1 = "); print_int(RVR1); P("\n"); + print_curtime(); + } + if(trigger_ms != DIDNT_TRIGGERED && (msctr < trigrtm || (msctr - trigrtm) > 100)){ + trigrtm = msctr; + P("Trigger time: "); + print_time(&trigger_time, trigger_ms); + trigger_ms = DIDNT_TRIGGERED; } } } @@ -107,6 +120,7 @@ int main(){ */ void sys_tick_handler(){ ++Timer; + ++msctr; if(Timer == 999){ STK_RVR = RVR1; }else if(Timer == 1000){ @@ -126,9 +140,9 @@ void exti4_isr(){ // correct systick_val = STK_CVR; STK_CVR = RVR0; - systick_val = RVR0 + 1 - systick_val; // Systick counts down! timer_val = Timer; Timer = 0; + systick_val = STK_RVR + 1 - systick_val; // Systick counts down! if(timer_val < 10) timer_val += 1000; // our closks go faster than real else if(timer_val < 990){ // something wrong RVR0 = RVR1 = STK_RVR_DEFAULT_VAL; @@ -143,14 +157,14 @@ void exti4_isr(){ }else{ // || (last_corr_time == 86399 && t == 0) if(t - last_corr_time == 1){ // PPS interval == 1s - ticks = systick_val + timer_val*(RVR0 + 1); + ticks = systick_val + (timer_val-1)*(RVR0 + 1) + RVR1 + 1; ++N; ticksavr += ticks; if(N > 20){ ticks = ticksavr / N; RVR0 = ticks / 1000 - 1; // main RVR value STK_RVR = RVR0; - RVR1 = RVR0 + ticks % 1000 - 1; // last millisecond RVR value (with fine correction) + RVR1 = RVR0 + ticks % 1000; // last millisecond RVR value (with fine correction) N = 0; ticksavr = 0; need_sync = 0; @@ -166,11 +180,23 @@ void exti4_isr(){ } } +/** + * PA5 interrupt - print time + */ +void exti9_5_isr(){ + if(EXTI_PR & EXTI5){ + if(trigger_ms == DIDNT_TRIGGERED){ // prevent bounce + trigger_ms = Timer; + memcpy(&trigger_time, ¤t_time, sizeof(curtime)); + } + EXTI_PR = EXTI5; + } +} // pause function, delay in ms -void Delay(uint16_t _U_ time){ - uint32_t waitto = Timer + time; - while(Timer < waitto); +void Delay(uint16_t time){ + uint32_t waitto = msctr + time; + while(msctr != waitto); } /** @@ -187,26 +213,30 @@ void set_time(uint8_t *buf){ current_time.S = atou(&buf[4]); } +/** + * print time: Tm - time structure, T - milliseconds + */ +void print_time(curtime *Tm, uint32_t T){ + int S = Tm->S, M = Tm->M, H = Tm->H; + if(H < 10) usb_send('0'); + print_int(H); usb_send(':'); + if(M < 10) usb_send('0'); + print_int(M); usb_send(':'); + if(S < 10) usb_send('0'); + print_int(S); usb_send('.'); + if(T < 100) usb_send('0'); + if(T < 10) usb_send('0'); + print_int(T); + if(GPS_status == GPS_NOT_VALID) P(" (not valid)"); + if(need_sync) P(" need synchronisation"); + newline(); +} void print_curtime(){ - int T = Timer; - newline(); - if(current_time.H < 25 && GPS_status != GPS_WAIT){ + uint32_t T = Timer; + if(current_time.H < 24 && GPS_status != GPS_WAIT){ P("Current time: "); - if(current_time.H < 10) usb_send('0'); - print_int(current_time.H); usb_send(':'); - if(current_time.M < 10) usb_send('0'); - print_int(current_time.M); usb_send(':'); - if(current_time.S < 10) usb_send('0'); - print_int(current_time.S); usb_send('.'); - /* uint32_t millis = STK_CVR * 1000; - millis /= STK_RVR;*/ - if(T < 100) usb_send('0'); - if(T < 10) usb_send('0'); - print_int(T); - if(GPS_status == GPS_NOT_VALID) P(" (not valid)"); - if(need_sync) P(" need synchronisation"); - newline(); + print_time(¤t_time, T); }else P("Waiting for satellites\n"); } diff --git a/GPS/main.h b/GPS/main.h index 7767d51..fb24c30 100644 --- a/GPS/main.h +++ b/GPS/main.h @@ -67,6 +67,7 @@ extern volatile int need_sync; void Delay(uint16_t time); void set_time(uint8_t *buf); +void print_time(curtime *T, uint32_t m); void print_curtime(); #endif // __MAIN_H__