comparison Discovery/Src/data_exchange_main.c @ 416:bcf447646e07

Merged in Ideenmodellierer/ostc4/Improment_NVM (pull request #37) Improment NVM
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Wed, 15 Jan 2020 10:53:15 +0000
parents b11e50415982
children 3f7d80f37bfc
comparison
equal deleted inserted replaced
403:6f30f2011667 416:bcf447646e07
88 88
89 static uint8_t data_old__lost_connection_to_slave_counter_temp = 0; 89 static uint8_t data_old__lost_connection_to_slave_counter_temp = 0;
90 static uint8_t data_old__lost_connection_to_slave_counter_retry = 0; 90 static uint8_t data_old__lost_connection_to_slave_counter_retry = 0;
91 static uint32_t data_old__lost_connection_to_slave_counter_total = 0; 91 static uint32_t data_old__lost_connection_to_slave_counter_total = 0;
92 92
93 static uint8_t DeviceDataUpdated = 0;
94
93 /* Private types -------------------------------------------------------------*/ 95 /* Private types -------------------------------------------------------------*/
96 #define UNKNOWN_TIME_HOURS 1
97 #define UNKNOWN_TIME_MINUTES 0
98 #define UNKNOWN_TIME_SECOND 0
99 #define UNKNOWN_DATE_DAY 1
100 #define UNKNOWN_DATE_MONTH 1
101 #define UNKNOWN_DATE_YEAR 16
94 102
95 /* Private function prototypes -----------------------------------------------*/ 103 /* Private function prototypes -----------------------------------------------*/
96 static uint8_t DataEX_check_header_and_footer_ok(void); 104 static uint8_t DataEX_check_header_and_footer_ok(void);
97 static uint8_t DataEX_check_header_and_footer_shifted(void); 105 static uint8_t DataEX_check_header_and_footer_shifted(void);
98 static uint8_t DataEX_check_header_and_footer_devicedata(void); 106 static uint8_t DataEX_check_header_and_footer_devicedata(void);
99 static void DataEX_check_DeviceData(void); 107 static void DataEX_check_DeviceData(void);
100 108
101 /* Exported functions --------------------------------------------------------*/ 109 /* Exported functions --------------------------------------------------------*/
102
103 uint8_t DataEX_was_power_on(void) 110 uint8_t DataEX_was_power_on(void)
104 { 111 {
105 return wasPowerOn; 112 return wasPowerOn;
106 } 113 }
107 114
139 { 146 {
140 SDiveState * pStateReal = stateRealGetPointerWrite(); 147 SDiveState * pStateReal = stateRealGetPointerWrite();
141 pStateReal->data_old__lost_connection_to_slave = 0; //initial value 148 pStateReal->data_old__lost_connection_to_slave = 0; //initial value
142 data_old__lost_connection_to_slave_counter_temp = 0; 149 data_old__lost_connection_to_slave_counter_temp = 0;
143 data_old__lost_connection_to_slave_counter_total = 0; 150 data_old__lost_connection_to_slave_counter_total = 0;
151 DeviceDataUpdated = 0;
144 152
145 memset((void *)&dataOut, 0, sizeof(SDataReceiveFromMaster)); 153 memset((void *)&dataOut, 0, sizeof(SDataReceiveFromMaster));
146 154
147 dataOut.header.checkCode[0] = 0xBB; 155 dataOut.header.checkCode[0] = 0xBB;
148 dataOut.header.checkCode[1] = SPI_RX_STATE_OK; 156 dataOut.header.checkCode[1] = SPI_RX_STATE_OK;
515 ((uint32_t)RTC_ByteToBcd2(inSdatestructure.Date)) | \ 523 ((uint32_t)RTC_ByteToBcd2(inSdatestructure.Date)) | \
516 ((uint32_t)inSdatestructure.WeekDay << 13U)); 524 ((uint32_t)inSdatestructure.WeekDay << 13U));
517 } 525 }
518 526
519 527
520
521 static void DataEX_helper_set_Unknown_Date_deviceData(SDeviceLine *lineWrite) 528 static void DataEX_helper_set_Unknown_Date_deviceData(SDeviceLine *lineWrite)
522 { 529 {
523 RTC_DateTypeDef sdatestructure; 530 RTC_DateTypeDef sdatestructure;
524 RTC_TimeTypeDef stimestructure; 531 RTC_TimeTypeDef stimestructure;
525 532
526 stimestructure.Hours = 1; 533 stimestructure.Hours = UNKNOWN_TIME_HOURS;
527 stimestructure.Minutes = 0; 534 stimestructure.Minutes = UNKNOWN_TIME_MINUTES;
528 stimestructure.Seconds = 0; 535 stimestructure.Seconds = UNKNOWN_TIME_SECOND;
529 536
530 sdatestructure.Date = 1; 537 sdatestructure.Date = UNKNOWN_DATE_DAY;
531 sdatestructure.Month = 1; 538 sdatestructure.Month = UNKNOWN_DATE_MONTH;
532 sdatestructure.Year = 16; 539 sdatestructure.Year = UNKNOWN_DATE_YEAR;
533 setWeekday(&sdatestructure); 540 setWeekday(&sdatestructure);
534 541
535 DataEX_helper_SetTime(stimestructure, &lineWrite->time_rtc_tr); 542 DataEX_helper_SetTime(stimestructure, &lineWrite->time_rtc_tr);
536 DataEX_helper_SetDate(sdatestructure, &lineWrite->date_rtc_dr); 543 DataEX_helper_SetDate(sdatestructure, &lineWrite->date_rtc_dr);
537 } 544 }
538 545
539 546
540 static uint8_t DataEX_helper_Check_And_Correct_Date_deviceData(SDeviceLine *lineWrite) 547 static uint8_t DataEX_helper_Check_And_Correct_Date_deviceData(SDeviceLine *lineWrite)
541 { 548 {
549 uint8_t retval = 0;
542 RTC_DateTypeDef sdatestructure; 550 RTC_DateTypeDef sdatestructure;
543 RTC_TimeTypeDef stimestructure; 551 RTC_TimeTypeDef stimestructure;
544 552
545 // from lineWrite to structure 553 // from lineWrite to structure
546 translateDate(lineWrite->date_rtc_dr, &sdatestructure); 554 translateDate(lineWrite->date_rtc_dr, &sdatestructure);
547 translateTime(lineWrite->time_rtc_tr, &stimestructure); 555 translateTime(lineWrite->time_rtc_tr, &stimestructure);
548 556
549 if( (sdatestructure.Year >= 15) 557 /* Check if date is out of range */
558 if(!( (sdatestructure.Year >= 15)
550 && (sdatestructure.Year <= 30) 559 && (sdatestructure.Year <= 30)
551 && (sdatestructure.Month <= 12)) 560 && (sdatestructure.Month <= 12)))
552 return 0; 561 {
553 562 DataEX_helper_set_Unknown_Date_deviceData(lineWrite);
554 563 retval = 1;
555 DataEX_helper_set_Unknown_Date_deviceData(lineWrite); 564 }
556 return 1; 565 return retval;
557 } 566 }
558 567
559 568
560 static uint8_t DataEX_helper_Check_And_Correct_Value_deviceData(SDeviceLine *lineWrite, int32_t from, int32_t to) 569 static uint8_t DataEX_helper_Check_And_Correct_Value_deviceData(SDeviceLine *lineWrite, int32_t from, int32_t to, uint8_t defaulttofrom)
561 { 570 {
562 if(lineWrite->value_int32 >= from && lineWrite->value_int32 <= to) 571 uint8_t retval = 0;
563 return 0; 572 RTC_DateTypeDef sdatestructure;
564 573
565 if(lineWrite->value_int32 < from) 574 /* Is value out of valid range? */
566 lineWrite->value_int32 = from; 575 if(!(lineWrite->value_int32 >= from && lineWrite->value_int32 <= to))
567 else 576 {
568 lineWrite->value_int32 = to; 577 if(defaulttofrom)
569 578 {
570 DataEX_helper_set_Unknown_Date_deviceData(lineWrite); 579 lineWrite->value_int32 = from;
571 return 0; 580 }
581 else
582 {
583 lineWrite->value_int32 = to;
584 }
585 DataEX_helper_set_Unknown_Date_deviceData(lineWrite);
586 }
587
588 /* This is just a repair function to restore metric if a corruption occurred in an older fw version */
589 if(((lineWrite->value_int32 == to) && defaulttofrom )
590 || ((lineWrite->value_int32 == from) && !defaulttofrom ))
591 {
592 translateDate(lineWrite->date_rtc_dr, &sdatestructure);
593 if(sdatestructure.Year == UNKNOWN_DATE_YEAR)
594 {
595 if(defaulttofrom)
596 {
597 lineWrite->value_int32 = from;
598 }
599 else
600 {
601 lineWrite->value_int32 = to;
602 }
603 }
604 }
605 return retval;
572 } 606 }
573 607
574 608
575 static void DataEX_check_DeviceData(void) 609 static void DataEX_check_DeviceData(void)
576 { 610 {
583 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->hoursOfOperation); 617 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->hoursOfOperation);
584 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->temperatureMaximum); 618 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->temperatureMaximum);
585 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->temperatureMinimum); 619 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->temperatureMinimum);
586 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->voltageMinimum); 620 DataEX_helper_Check_And_Correct_Date_deviceData(&DeviceData->voltageMinimum);
587 621
588 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->batteryChargeCompleteCycles, 0, 10000); 622 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->batteryChargeCompleteCycles, 0, 10000,1);
589 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->batteryChargeCycles, 0, 20000); 623 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->batteryChargeCycles, 0, 20000,1);
590 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->depthMaximum, 0, (500*100)+1000); 624 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->depthMaximum, 0, (500*100)+1000,1);
591 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->diveCycles, 0, 20000); 625 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->diveCycles, 0, 20000,1);
592 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->hoursOfOperation, 0, 1000000); 626 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->hoursOfOperation, 0, 1000000,1);
593 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->temperatureMaximum, -30*100, 150*100); 627 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->temperatureMaximum, -30*100, 150*100,1);
594 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->temperatureMinimum, -30*100, 150*100); 628 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->temperatureMinimum, -30*100, 150*100,0);
595 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->voltageMinimum, -1*1000, 6*1000); 629 DataEX_helper_Check_And_Correct_Value_deviceData(&DeviceData->voltageMinimum, 2*1000, 6*1000,0);
596 } 630 }
597 631
598 632
599 static void DataEX_merge_DeviceData_and_store(void) 633 static void DataEX_merge_DeviceData_and_store(void)
600 { 634 {
629 } 663 }
630 if(DeviceData->diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32) 664 if(DeviceData->diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32)
631 { 665 {
632 DataEX_helper_copy_deviceData(&DeviceData->diveCycles, &DeviceDataFlash.diveCycles); 666 DataEX_helper_copy_deviceData(&DeviceData->diveCycles, &DeviceDataFlash.diveCycles);
633 } 667 }
634 668 if(DeviceData->hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32)
669 {
670 DataEX_helper_copy_deviceData(&DeviceData->hoursOfOperation, &DeviceDataFlash.hoursOfOperation);
671 }
672
673
635 /* min values */ 674 /* min values */
636 if(DeviceData->temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32) 675 if(DeviceData->temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32)
637 { 676 {
638 DataEX_helper_copy_deviceData(&DeviceData->temperatureMinimum, &DeviceDataFlash.temperatureMinimum); 677 DataEX_helper_copy_deviceData(&DeviceData->temperatureMinimum, &DeviceDataFlash.temperatureMinimum);
639 } 678 }
640 // Voltage minimum, keep limit to 2.0 Volt; hw 09.09.2015
641 if(DeviceData->voltageMinimum.value_int32 > DeviceDataFlash.voltageMinimum.value_int32) 679 if(DeviceData->voltageMinimum.value_int32 > DeviceDataFlash.voltageMinimum.value_int32)
642 { 680 {
643 if(DeviceDataFlash.voltageMinimum.value_int32 > 2000) // do not copy back 2000 and below
644 DataEX_helper_copy_deviceData(&DeviceData->voltageMinimum, &DeviceDataFlash.voltageMinimum); 681 DataEX_helper_copy_deviceData(&DeviceData->voltageMinimum, &DeviceDataFlash.voltageMinimum);
645 } 682 }
646 if(DeviceData->voltageMinimum.value_int32 < 2000)
647 DeviceData->voltageMinimum.value_int32 = 2000;
648 683
649 DataEX_check_DeviceData (); 684 DataEX_check_DeviceData ();
650 ext_flash_write_devicedata(); 685 ext_flash_write_devicedata();
651 } 686 }
652 687
655 { 690 {
656 SDataExchangeSlaveToMasterDeviceData * dataInDevice = (SDataExchangeSlaveToMasterDeviceData *)&dataIn; 691 SDataExchangeSlaveToMasterDeviceData * dataInDevice = (SDataExchangeSlaveToMasterDeviceData *)&dataIn;
657 SDevice * pDeviceState = stateDeviceGetPointerWrite(); 692 SDevice * pDeviceState = stateDeviceGetPointerWrite();
658 693
659 memcpy(pDeviceState, &dataInDevice->DeviceData[dataInDevice->boolDeviceData], sizeof(SDevice)); 694 memcpy(pDeviceState, &dataInDevice->DeviceData[dataInDevice->boolDeviceData], sizeof(SDevice));
695 DeviceDataUpdated = 1; /* indicate new data to be written to flash by background task (at last op hour count will be updated) */
660 } 696 }
661 697
662 698
663 static void DataEX_copy_to_VpmRepetitiveData(void) 699 static void DataEX_copy_to_VpmRepetitiveData(void)
664 { 700 {
753 if(!DataEX_check_header_and_footer_ok()) 789 if(!DataEX_check_header_and_footer_ok())
754 { 790 {
755 if(DataEX_check_header_and_footer_devicedata()) 791 if(DataEX_check_header_and_footer_devicedata())
756 { 792 {
757 DataEX_copy_to_DeviceData(); 793 DataEX_copy_to_DeviceData();
758 DataEX_merge_DeviceData_and_store();
759 DataEX_copy_to_VpmRepetitiveData(); 794 DataEX_copy_to_VpmRepetitiveData();
760 data_old__lost_connection_to_slave_counter_temp = 0; 795 data_old__lost_connection_to_slave_counter_temp = 0;
761 data_old__lost_connection_to_slave_counter_retry = 0; 796 data_old__lost_connection_to_slave_counter_retry = 0;
762 /* Do not yet reset state. Wait till common data has been received in next cycle. Otherwise invalid data may be forwarded for processing */ 797 /* Do not yet reset state. Wait till common data has been received in next cycle. Otherwise invalid data may be forwarded for processing */
763 /* pStateReal->data_old__lost_connection_to_slave = 0; */ 798 /* pStateReal->data_old__lost_connection_to_slave = 0; */
1107 if(dataIn.footer.checkCode[3] != 0xE4) 1142 if(dataIn.footer.checkCode[3] != 0xE4)
1108 return 0; 1143 return 0;
1109 1144
1110 return 1; 1145 return 1;
1111 } 1146 }
1147
1148 void DataEX_merge_devicedata(void)
1149 {
1150 if(DeviceDataUpdated)
1151 {
1152 DeviceDataUpdated = 0;
1153 DataEX_merge_DeviceData_and_store();
1154 }
1155 }
1156