/* Set Date/Time to Epoch 0 (Thu, 01 Jan 1970 00:00:00 GMT) */ void HAL_RTC_Initialize_UnixTime() { RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; /* Get calendar_time time struct values */ RTC_TimeStructure.RTC_Hours = 0; RTC_TimeStructure.RTC_Minutes = 0; RTC_TimeStructure.RTC_Seconds = 0; /* Get calendar_time date struct values */ RTC_DateStructure.RTC_WeekDay = 4; RTC_DateStructure.RTC_Date = 1; RTC_DateStructure.RTC_Month = 0; RTC_DateStructure.RTC_Year = 70; setRTCTime(&RTC_TimeStructure, &RTC_DateStructure); }
void HAL_RTC_Set_UnixTime(time_t value) { RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; struct tm *calendar_time; calendar_time = localtime(&value); /* Get calendar_time time struct values */ RTC_TimeStructure.RTC_Hours = calendar_time->tm_hour; RTC_TimeStructure.RTC_Minutes = calendar_time->tm_min; RTC_TimeStructure.RTC_Seconds = calendar_time->tm_sec; /* Get calendar_time date struct values */ RTC_DateStructure.RTC_WeekDay = calendar_time->tm_wday; RTC_DateStructure.RTC_Date = calendar_time->tm_mday; RTC_DateStructure.RTC_Month = calendar_time->tm_mon+1; RTC_DateStructure.RTC_Year = calendar_time->tm_year; setRTCTime(&RTC_TimeStructure, &RTC_DateStructure); }
//$GPRMC,225446.000,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n // 0 1 2 3 4 5 6 7 // 0123456789012345678901234567890123456789012345678901234567890123456789012 // 0 1 2 3 4 5 6 7 8 9 10 11 12 void parseGPSdata(char *gpsBuffer) { time_t tNow; tmElements_t tm; uint8_t gpsCheck1, gpsCheck2; // checksums // char gpsTime[10]; // time including fraction hhmmss.fff char gpsFixStat; // fix status // char gpsLat[7]; // ddmm.ff (with decimal point) // char gpsLatH; // hemisphere // char gpsLong[8]; // dddmm.ff (with decimal point) // char gpsLongH; // hemisphere // char gpsSpeed[5]; // speed over ground // char gpsCourse[5]; // Course // char gpsDate[6]; // Date // char gpsMagV[5]; // Magnetic variation // char gpsMagD; // Mag var E/W // char gpsCKS[2]; // Checksum without asterisk char *ptr; uint32_t tmp; if ( strncmp( gpsBuffer, "$GPRMC,", 7 ) == 0 ) { //Serial.println("parseGPSData"); //Serial.println(gpsBuffer); //beep(1000, 1); //Calculate checksum from the received data ptr = &gpsBuffer[1]; // start at the "G" gpsCheck1 = 0; // init collector /* Loop through entire string, XORing each character to the next */ while (*ptr != '*') // count all the bytes up to the asterisk { gpsCheck1 ^= *ptr; ptr++; if (ptr>(gpsBuffer+GPSBUFFERSIZE)) goto GPSerror1; // extra sanity check, can't hurt... } // now get the checksum from the string itself, which is in hex gpsCheck2 = atoh(*(ptr+1)) * 16 + atoh(*(ptr+2)); if (gpsCheck1 == gpsCheck2) { // if checksums match, process the data //beep(1000, 1); ptr = strtok(gpsBuffer, ",*\r"); // parse $GPRMC if (ptr == NULL) goto GPSerror1; ptr = strtok(NULL, ",*\r"); // Time including fraction hhmmss.fff if (ptr == NULL) goto GPSerror1; if ((strlen(ptr) < 6) || (strlen(ptr) > 10)) goto GPSerror1; // check time length // strncpy(gpsTime, ptr, 10); // copy time string hhmmss tmp = parsedecimal(ptr); // parse integer portion tm.Hour = tmp / 10000; tm.Minute = (tmp / 100) % 100; tm.Second = tmp % 100; ptr = strtok(NULL, ",*\r"); // Status if (ptr == NULL) goto GPSerror1; gpsFixStat = ptr[0]; if (gpsFixStat == 'A') { // if data valid, parse time & date gpsTimeout = 0; // reset gps timeout counter ptr = strtok(NULL, ",*\r"); // Latitude including fraction if (ptr == NULL) goto GPSerror1; // strncpy(gpsLat, ptr, 7); // copy Latitude ddmm.ff ptr = strtok(NULL, ",*\r"); // Latitude N/S if (ptr == NULL) goto GPSerror1; // gpsLatH = ptr[0]; ptr = strtok(NULL, ",*\r"); // Longitude including fraction hhmm.ff if (ptr == NULL) goto GPSerror1; // strncpy(gpsLong, ptr, 7); ptr = strtok(NULL, ",*\r"); // Longitude Hemisphere if (ptr == NULL) goto GPSerror1; // gpsLongH = ptr[0]; ptr = strtok(NULL, ",*\r"); // Ground speed 000.5 if (ptr == NULL) goto GPSerror1; // strncpy(gpsSpeed, ptr, 5); ptr = strtok(NULL, ",*\r"); // Track angle (course) 054.7 if (ptr == NULL) goto GPSerror1; // strncpy(gpsCourse, ptr, 5); ptr = strtok(NULL, ",*\r"); // Date ddmmyy if (ptr == NULL) goto GPSerror1; // strncpy(gpsDate, ptr, 6); if (strlen(ptr) != 6) goto GPSerror1; // check date length tmp = parsedecimal(ptr); tm.Day = tmp / 10000; tm.Month = (tmp / 100) % 100; tm.Year = tmp % 100; ptr = strtok(NULL, "*\r"); // magnetic variation & dir if (ptr == NULL) goto GPSerror1; if (ptr == NULL) goto GPSerror1; ptr = strtok(NULL, ",*\r"); // Checksum if (ptr == NULL) goto GPSerror1; // strncpy(gpsCKS, ptr, 2); // save checksum chars tm.Year = y2kYearToTm(tm.Year); // convert yy year to (yyyy-1970) (add 30) tNow = makeTime(&tm); // convert to time_t if ((tGPSupdate>0) && (abs(tNow-tGPSupdate)>SECS_PER_DAY)) goto GPSerror2; // GPS time jumped more than 1 day if ((tm.Second == 0) || ((tNow - tGPSupdate)>=60)) { // update RTC once/minute or if it's been 60 seconds //beep(1000, 1); // debugging g_gps_updating = true; tGPSupdate = tNow; // remember time of this update tNow = tNow + (long)(g_TZ_hour + g_DST_offset) * SECS_PER_HOUR; // add time zone hour offset & DST offset if (g_TZ_hour < 0) // add or subtract time zone minute offset tNow = tNow - (long)g_TZ_minute * SECS_PER_HOUR; else tNow = tNow + (long)g_TZ_minute * SECS_PER_HOUR; setRTCTime(tNow); // set RTC from adjusted GPS time & date } else g_gps_updating = false; } // if fix status is A } // if checksums match else // checksums do not match g_gps_cks_errors++; // increment error count return; GPSerror1: g_gps_parse_errors++; // increment error count goto GPSerror2a; GPSerror2: g_gps_time_errors++; // increment error count GPSerror2a: //beep(2093,1); // error signal - I'm leaving this in for now /wm //flash_display(200); // flash display to show GPS error strcpy(gpsBuffer, ""); // wipe GPS buffer } // if "$GPRMC" }