use GPS time to set intermal RTC

This commit is contained in:
King Kévin 2017-06-27 15:58:12 +02:00
parent 6c0c5bb7be
commit c1ff3c2d39
1 changed files with 48 additions and 0 deletions

View File

@ -41,6 +41,7 @@
#include "print.h" // printing utilities
#include "usart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "radio_gps.h" // GPS communication
#define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */
@ -212,6 +213,12 @@ void main(void)
nvic_enable_irq(NVIC_RTC_IRQ); // allow the RTC to interrupt
printf("OK\n");
// setup GPS communication
printf("setup GPS: ");
radio_gps_setup();
printf("OK\n");
// print time
time_rtc= rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
printf("date: %d-%02d-%02d %02d:%02d:%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
@ -274,6 +281,47 @@ void main(void)
printf("time: %02d:%02d:%02d\n", time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
}
}
while (radio_gps_received) { // a GPS message has been received
radio_gps_received = false; // clear flag
action = true; // action has been performed
//printf("%s", radio_gps_message); // print GPS message
if (0==strncmp((const char *)radio_gps_message,"$GPRMC,",7)) { // get time from GPS
uint8_t arg = 0;
uint8_t arg_start = 0;
for (uint8_t i = 0; i < LENGTH(radio_gps_message) && radio_gps_message[i]!='\0'; i++) {
if (','==radio_gps_message[i]) {
if (1==arg) { // got time
if (i-arg_start<7) { // time not provided
break;
} else {
time_tm->tm_hour = (radio_gps_message[arg_start+1]-'0')*10+(radio_gps_message[arg_start+2]-'0')*1; // set hours
time_tm->tm_min = (radio_gps_message[arg_start+3]-'0')*10+(radio_gps_message[arg_start+4]-'0')*1; // set minutes
time_tm->tm_sec = (radio_gps_message[arg_start+5]-'0')*10+(radio_gps_message[arg_start+6]-'0')*1; // set seconds
}
} else if (2==arg) { // got validity
if (i-arg_start<2) { // validity not provided
break;
} else if ('A'!=radio_gps_message[arg_start+1]) { // not valid
break;
}
} else if (9==arg) { // got date
if (i-arg_start<7) { // date not provided
break;
} else {
time_tm->tm_mday = (radio_gps_message[arg_start+1]-'0')*10+(radio_gps_message[arg_start+2]-'0')*1; // set day of month
time_tm->tm_mon = (radio_gps_message[arg_start+3]-'0')*10+(radio_gps_message[arg_start+4]-'0')*1; // set month
time_tm->tm_year = 2000+(radio_gps_message[arg_start+5]-'0')*10+(radio_gps_message[arg_start+6]-'0')*1-1900; // set year
time_rtc = mktime(time_tm); // get back seconds
rtc_set_counter_val(time_rtc); // save date/time to internal RTC
//printf("GPS date saved: %d-%02d-%02d %02d:%02d:%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
}
}
arg++; // next argument starts
arg_start = i; // save start of next argument
}
}
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
} else {