changeset 409:2e2d34c1cc99 Improment_NVM

Restore last known date: After a power off RTC looses time data and restarts in the year 2000. Code will fix this date to the year 2016. This change will set the date to the last known date of the operation hour counter
author ideenmodellierer
date Sun, 12 Jan 2020 19:55:22 +0100
parents 2fc08a0d1ec3
children f9458e979154
files Small_CPU/Src/scheduler.c
diffstat 1 files changed, 41 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Small_CPU/Src/scheduler.c	Sun Jan 12 19:50:14 2020 +0100
+++ b/Small_CPU/Src/scheduler.c	Sun Jan 12 19:55:22 2020 +0100
@@ -1200,6 +1200,41 @@
 }
 
 
+void scheduletranslateDate(uint32_t datetmpreg, RTC_DateTypeDef *sDate)
+{
+  datetmpreg = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
+
+  /* Fill the structure fields with the read parameters */
+  sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
+  sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
+  sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
+  sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
+
+	/* Convert the date structure parameters to Binary format */
+	sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
+	sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
+	sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
+}
+
+void scheduleCheckDate(void)
+{
+	uint32_t localdate;
+	RTC_DateTypeDef sDate;
+	localdate =	(uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK);
+	scheduletranslateDate(localdate, &sDate);
+
+	/* RTC start in year 2000 in case of a power loss. Use the operation counter time stamp to bring at last date to a more realistic value */
+	if(sDate.Year < 15)
+	{
+		scheduletranslateDate(DeviceDataFlash.hoursOfOperation.date_rtc_dr, &sDate);
+		if(sDate.Year > 16)
+		{
+			RTC_SetDate(sDate);
+		}
+	}
+
+}
+
 void scheduleUpdateDeviceData(void)
 {
 	/* first step, main CPU */
@@ -1207,6 +1242,11 @@
 	if(deviceDataFlashValid)
 	{
 		/* max values */
+		if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32)
+		{
+			scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation);
+			scheduleCheckDate();
+		}
 		if(global.deviceData.batteryChargeCompleteCycles.value_int32 < DeviceDataFlash.batteryChargeCompleteCycles.value_int32)
 		{
 			scheduleCopyDeviceData(&global.deviceData.batteryChargeCompleteCycles, &DeviceDataFlash.batteryChargeCompleteCycles);
@@ -1227,10 +1267,6 @@
 		{
 			scheduleCopyDeviceData(&global.deviceData.diveCycles, &DeviceDataFlash.diveCycles);
 		}
-		if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32)
-		{
-			scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation);
-		}
 		
 		/* min values */
 		if(global.deviceData.temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32)
@@ -1286,6 +1322,7 @@
 			{
 				deviceDataSubSeconds = 0;
 				global.deviceData.hoursOfOperation.value_int32++;
+				scheduleSetDate(&global.deviceData.hoursOfOperation);
 			}
 			break;