comparison Small_CPU/Src/scheduler.c @ 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 cb3870f79e9d
children 6886aeeca454
comparison
equal deleted inserted replaced
408:2fc08a0d1ec3 409:2e2d34c1cc99
1198 lineWrite->time_rtc_tr = lineRead->time_rtc_tr; 1198 lineWrite->time_rtc_tr = lineRead->time_rtc_tr;
1199 lineWrite->value_int32 = lineRead->value_int32; 1199 lineWrite->value_int32 = lineRead->value_int32;
1200 } 1200 }
1201 1201
1202 1202
1203 void scheduletranslateDate(uint32_t datetmpreg, RTC_DateTypeDef *sDate)
1204 {
1205 datetmpreg = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1206
1207 /* Fill the structure fields with the read parameters */
1208 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
1209 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1210 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
1211 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
1212
1213 /* Convert the date structure parameters to Binary format */
1214 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1215 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1216 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1217 }
1218
1219 void scheduleCheckDate(void)
1220 {
1221 uint32_t localdate;
1222 RTC_DateTypeDef sDate;
1223 localdate = (uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK);
1224 scheduletranslateDate(localdate, &sDate);
1225
1226 /* 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 */
1227 if(sDate.Year < 15)
1228 {
1229 scheduletranslateDate(DeviceDataFlash.hoursOfOperation.date_rtc_dr, &sDate);
1230 if(sDate.Year > 16)
1231 {
1232 RTC_SetDate(sDate);
1233 }
1234 }
1235
1236 }
1237
1203 void scheduleUpdateDeviceData(void) 1238 void scheduleUpdateDeviceData(void)
1204 { 1239 {
1205 /* first step, main CPU */ 1240 /* first step, main CPU */
1206 1241
1207 if(deviceDataFlashValid) 1242 if(deviceDataFlashValid)
1208 { 1243 {
1209 /* max values */ 1244 /* max values */
1245 if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32)
1246 {
1247 scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation);
1248 scheduleCheckDate();
1249 }
1210 if(global.deviceData.batteryChargeCompleteCycles.value_int32 < DeviceDataFlash.batteryChargeCompleteCycles.value_int32) 1250 if(global.deviceData.batteryChargeCompleteCycles.value_int32 < DeviceDataFlash.batteryChargeCompleteCycles.value_int32)
1211 { 1251 {
1212 scheduleCopyDeviceData(&global.deviceData.batteryChargeCompleteCycles, &DeviceDataFlash.batteryChargeCompleteCycles); 1252 scheduleCopyDeviceData(&global.deviceData.batteryChargeCompleteCycles, &DeviceDataFlash.batteryChargeCompleteCycles);
1213 } 1253 }
1214 if(global.deviceData.batteryChargeCycles.value_int32 < DeviceDataFlash.batteryChargeCycles.value_int32) 1254 if(global.deviceData.batteryChargeCycles.value_int32 < DeviceDataFlash.batteryChargeCycles.value_int32)
1224 scheduleCopyDeviceData(&global.deviceData.depthMaximum, &DeviceDataFlash.depthMaximum); 1264 scheduleCopyDeviceData(&global.deviceData.depthMaximum, &DeviceDataFlash.depthMaximum);
1225 } 1265 }
1226 if(global.deviceData.diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32) 1266 if(global.deviceData.diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32)
1227 { 1267 {
1228 scheduleCopyDeviceData(&global.deviceData.diveCycles, &DeviceDataFlash.diveCycles); 1268 scheduleCopyDeviceData(&global.deviceData.diveCycles, &DeviceDataFlash.diveCycles);
1229 }
1230 if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32)
1231 {
1232 scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation);
1233 } 1269 }
1234 1270
1235 /* min values */ 1271 /* min values */
1236 if(global.deviceData.temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32) 1272 if(global.deviceData.temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32)
1237 { 1273 {
1284 deviceDataSubSeconds++; 1320 deviceDataSubSeconds++;
1285 if(deviceDataSubSeconds > 10) 1321 if(deviceDataSubSeconds > 10)
1286 { 1322 {
1287 deviceDataSubSeconds = 0; 1323 deviceDataSubSeconds = 0;
1288 global.deviceData.hoursOfOperation.value_int32++; 1324 global.deviceData.hoursOfOperation.value_int32++;
1325 scheduleSetDate(&global.deviceData.hoursOfOperation);
1289 } 1326 }
1290 break; 1327 break;
1291 1328
1292 case MODE_SLEEP: 1329 case MODE_SLEEP:
1293 case MODE_SHUTDOWN: 1330 case MODE_SHUTDOWN: