mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-02 21:35:13 +03:00
add trigger to check GPS sync
This commit is contained in:
parent
589d00fd4e
commit
c556bc8ab7
BIN
GPS/GPS.bin
BIN
GPS/GPS.bin
Binary file not shown.
@ -180,12 +180,11 @@ void GPS_parse_answer(uint8_t *buf){
|
|||||||
ptr = ustrchr(buf, ',');
|
ptr = ustrchr(buf, ',');
|
||||||
*ptr++ = 0;
|
*ptr++ = 0;
|
||||||
//P(buf);
|
//P(buf);
|
||||||
if(*ptr == 'A')
|
if(*ptr == 'A'){
|
||||||
GPS_status = GPS_VALID;
|
GPS_status = GPS_VALID;
|
||||||
else
|
set_time(buf);
|
||||||
|
}else
|
||||||
GPS_status = GPS_NOT_VALID;
|
GPS_status = GPS_NOT_VALID;
|
||||||
print_curtime();
|
|
||||||
set_time(buf);
|
|
||||||
// buf = ustrchr(ptr, ',');
|
// buf = ustrchr(ptr, ',');
|
||||||
// P(" ");
|
// P(" ");
|
||||||
P("\n");
|
P("\n");
|
||||||
|
|||||||
@ -35,13 +35,17 @@ void GPIO_init(){
|
|||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
|
||||||
RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN |
|
RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN |
|
||||||
RCC_APB2ENR_IOPEEN);
|
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;
|
//AFIO_EXTICR2 = 0;
|
||||||
exti_enable_request(EXTI4);
|
exti_enable_request(EXTI4 | EXTI5);
|
||||||
// trigger on rising edge
|
// 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_EXTI4_IRQ);
|
||||||
|
nvic_enable_irq(NVIC_EXTI9_5_IRQ);
|
||||||
/*
|
/*
|
||||||
// Buttons: pull-up input
|
// Buttons: pull-up input
|
||||||
gpio_set_mode(BTNS_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
|
gpio_set_mode(BTNS_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
|
||||||
|
|||||||
80
GPS/main.c
80
GPS/main.c
@ -26,9 +26,10 @@
|
|||||||
#include "GPS.h"
|
#include "GPS.h"
|
||||||
|
|
||||||
volatile uint32_t Timer = 0; // milliseconds
|
volatile uint32_t Timer = 0; // milliseconds
|
||||||
|
volatile uint32_t msctr = 0; // global milliseconds for different purposes
|
||||||
usbd_device *usbd_dev;
|
usbd_device *usbd_dev;
|
||||||
volatile uint32_t systick_val = 0;
|
volatile int32_t systick_val = 0;
|
||||||
volatile uint32_t timer_val = 0;
|
volatile int32_t timer_val = 0;
|
||||||
volatile int clear_ST_on_connect = 1;
|
volatile int clear_ST_on_connect = 1;
|
||||||
|
|
||||||
volatile int need_sync = 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};
|
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(){
|
void time_increment(){
|
||||||
Timer = 0;
|
Timer = 0;
|
||||||
if(current_time.H == 25) return; // Time not initialized
|
if(current_time.H == 25) return; // Time not initialized
|
||||||
@ -77,6 +82,7 @@ int main(){
|
|||||||
|
|
||||||
GPS_send_start_seq();
|
GPS_send_start_seq();
|
||||||
|
|
||||||
|
uint32_t trigrtm = 0;
|
||||||
while(1){
|
while(1){
|
||||||
usbd_poll(usbd_dev);
|
usbd_poll(usbd_dev);
|
||||||
if(usbdatalen){ // there's something in USB buffer
|
if(usbdatalen){ // there's something in USB buffer
|
||||||
@ -97,6 +103,13 @@ int main(){
|
|||||||
P(", RVR1 = ");
|
P(", RVR1 = ");
|
||||||
print_int(RVR1);
|
print_int(RVR1);
|
||||||
P("\n");
|
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(){
|
void sys_tick_handler(){
|
||||||
++Timer;
|
++Timer;
|
||||||
|
++msctr;
|
||||||
if(Timer == 999){
|
if(Timer == 999){
|
||||||
STK_RVR = RVR1;
|
STK_RVR = RVR1;
|
||||||
}else if(Timer == 1000){
|
}else if(Timer == 1000){
|
||||||
@ -126,9 +140,9 @@ void exti4_isr(){
|
|||||||
// correct
|
// correct
|
||||||
systick_val = STK_CVR;
|
systick_val = STK_CVR;
|
||||||
STK_CVR = RVR0;
|
STK_CVR = RVR0;
|
||||||
systick_val = RVR0 + 1 - systick_val; // Systick counts down!
|
|
||||||
timer_val = Timer;
|
timer_val = Timer;
|
||||||
Timer = 0;
|
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
|
if(timer_val < 10) timer_val += 1000; // our closks go faster than real
|
||||||
else if(timer_val < 990){ // something wrong
|
else if(timer_val < 990){ // something wrong
|
||||||
RVR0 = RVR1 = STK_RVR_DEFAULT_VAL;
|
RVR0 = RVR1 = STK_RVR_DEFAULT_VAL;
|
||||||
@ -143,14 +157,14 @@ void exti4_isr(){
|
|||||||
}else{
|
}else{
|
||||||
// || (last_corr_time == 86399 && t == 0)
|
// || (last_corr_time == 86399 && t == 0)
|
||||||
if(t - last_corr_time == 1){ // PPS interval == 1s
|
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;
|
++N;
|
||||||
ticksavr += ticks;
|
ticksavr += ticks;
|
||||||
if(N > 20){
|
if(N > 20){
|
||||||
ticks = ticksavr / N;
|
ticks = ticksavr / N;
|
||||||
RVR0 = ticks / 1000 - 1; // main RVR value
|
RVR0 = ticks / 1000 - 1; // main RVR value
|
||||||
STK_RVR = RVR0;
|
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;
|
N = 0;
|
||||||
ticksavr = 0;
|
ticksavr = 0;
|
||||||
need_sync = 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
|
// pause function, delay in ms
|
||||||
void Delay(uint16_t _U_ time){
|
void Delay(uint16_t time){
|
||||||
uint32_t waitto = Timer + time;
|
uint32_t waitto = msctr + time;
|
||||||
while(Timer < waitto);
|
while(msctr != waitto);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,26 +213,30 @@ void set_time(uint8_t *buf){
|
|||||||
current_time.S = atou(&buf[4]);
|
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(){
|
void print_curtime(){
|
||||||
int T = Timer;
|
uint32_t T = Timer;
|
||||||
newline();
|
if(current_time.H < 24 && GPS_status != GPS_WAIT){
|
||||||
if(current_time.H < 25 && GPS_status != GPS_WAIT){
|
|
||||||
P("Current time: ");
|
P("Current time: ");
|
||||||
if(current_time.H < 10) usb_send('0');
|
print_time(¤t_time, T);
|
||||||
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();
|
|
||||||
}else
|
}else
|
||||||
P("Waiting for satellites\n");
|
P("Waiting for satellites\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,6 +67,7 @@ extern volatile int need_sync;
|
|||||||
void Delay(uint16_t time);
|
void Delay(uint16_t time);
|
||||||
void set_time(uint8_t *buf);
|
void set_time(uint8_t *buf);
|
||||||
|
|
||||||
|
void print_time(curtime *T, uint32_t m);
|
||||||
void print_curtime();
|
void print_curtime();
|
||||||
|
|
||||||
#endif // __MAIN_H__
|
#endif // __MAIN_H__
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user