comparison Discovery/Src/externLogbookFlash.c @ 1027:158100a84ebd GasConsumption

New profile feature: In the past the OSTC provide just one instance for settings. If for example a diver switched from OC to CCR configuration several settings had to be modified. To improve this and to be more flexible in adapting the OSTC to differend dive scenarions the usage of up to 4 profiles has beem introduced. The profiles are copies of the common settings but stored in a separate, previously not used, flash section => no impact to existings settings handling. For access to the profiles the existing setting flash functions are reused. To enable this a parameter war introduced which defines the target of the operation (common settings or profiles).
author Ideenmodellierer
date Sun, 07 Sep 2025 19:03:44 +0200
parents bc6c90e20d9e
children 74be24428049
comparison
equal deleted inserted replaced
1026:5fedf7ba2392 1027:158100a84ebd
82 #define RELEASE 1 82 #define RELEASE 1
83 #define HOLDCS 0 83 #define HOLDCS 0
84 84
85 #define HEADER2OFFSET 0x400 85 #define HEADER2OFFSET 0x400
86 86
87 typedef enum{
88 EF_HEADER,
89 EF_SAMPLE,
90 EF_DEVICEDATA,
91 EF_VPMDATA,
92 EF_SETTINGS,
93 EF_FIRMWARE,
94 EF_FIRMWARE2,
95 }which_ring_enum;
96
97
98 typedef struct{ 87 typedef struct{
99 uint8_t IsBusy:1; 88 uint8_t IsBusy:1;
100 uint8_t IsWriteEnabled:1; 89 uint8_t IsWriteEnabled:1;
101 uint8_t BlockProtect0:1; 90 uint8_t BlockProtect0:1;
102 uint8_t BlockProtect1:1; 91 uint8_t BlockProtect1:1;
128 static uint32_t actualPointerHeader = 0; 117 static uint32_t actualPointerHeader = 0;
129 static uint32_t actualPointerSample = 0; 118 static uint32_t actualPointerSample = 0;
130 static uint32_t actualPointerDevicedata = DDSTART; 119 static uint32_t actualPointerDevicedata = DDSTART;
131 static uint32_t actualPointerVPM = 0; 120 static uint32_t actualPointerVPM = 0;
132 static uint32_t actualPointerSettings = SETTINGSSTART; 121 static uint32_t actualPointerSettings = SETTINGSSTART;
122 static uint32_t actualPointerProfile = PROFILE0_START;
133 static uint32_t actualPointerFirmware = 0; 123 static uint32_t actualPointerFirmware = 0;
134 static uint32_t actualPointerFirmware2 = 0; 124 static uint32_t actualPointerFirmware2 = 0;
135 125
136 /* Private function prototypes -----------------------------------------------*/ 126 /* Private function prototypes -----------------------------------------------*/
137 static void chip_unselect(void); 127 static void chip_unselect(void);
525 void ext_flash_write_settings(void) 515 void ext_flash_write_settings(void)
526 { 516 {
527 return; 517 return;
528 } 518 }
529 #else 519 #else
530 void ext_flash_write_settings(uint8_t resetRing) 520 void ext_flash_write_settings(uint8_t whichSettings, uint8_t resetRing)
531 { 521 {
532 uint8_t *pData; 522 SSettings *pSettings;
533 const uint16_t length = sizeof(SSettings); 523 const uint16_t length = sizeof(SSettings);
534 uint8_t length_lo, length_hi; 524 uint8_t length_lo, length_hi;
535 uint8_t dataLength[2] = { 0 }; 525 uint8_t dataLength[2] = { 0 };
536 526
527 pSettings = settingsGetPointer();
528
537 ext_flash_disable_protection(); 529 ext_flash_disable_protection();
538 530
539 if(stateRealGetPointer()->lastKnownBatteryPercentage) 531 switch (whichSettings)
540 { 532 {
541 settingsGetPointer()->lastKnownBatteryPercentage = stateRealGetPointer()->lastKnownBatteryPercentage; 533 default:
542 } 534 case EF_SETTINGS: if(stateRealGetPointer()->lastKnownBatteryPercentage)
543 settingsGetPointer()->backup_localtime_rtc_tr = stateRealGetPointer()->lifeData.timeBinaryFormat; 535 {
544 settingsGetPointer()->backup_localtime_rtc_dr = stateRealGetPointer()->lifeData.dateBinaryFormat; 536 pSettings->lastKnownBatteryPercentage = stateRealGetPointer()->lastKnownBatteryPercentage;
545 537 }
546 pData = (uint8_t *)settingsGetPointer(); 538 pSettings->backup_localtime_rtc_tr = stateRealGetPointer()->lifeData.timeBinaryFormat;
547 539 pSettings->backup_localtime_rtc_dr = stateRealGetPointer()->lifeData.dateBinaryFormat;
548 /* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */ 540 /* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */
549 if((resetRing) || ((actualPointerSettings + length) >= SETTINGSSTOP)) 541 if((resetRing) || ((actualPointerSettings + length) >= SETTINGSSTOP))
550 { 542 {
551 actualPointerSettings = SETTINGSSTART; 543 actualPointerSettings = SETTINGSSTART;
544 }
545 break;
546 case EF_PROFILE0: actualPointerProfile = PROFILE0_START;
547 pSettings = profileGetPointer(0);
548 break;
549 case EF_PROFILE1: actualPointerProfile = PROFILE1_START;
550 pSettings = profileGetPointer(1);
551 break;
552 case EF_PROFILE2: actualPointerProfile = PROFILE2_START;
553 pSettings = profileGetPointer(2);
554 break;
555 case EF_PROFILE3: actualPointerProfile = PROFILE3_START;
556 pSettings = profileGetPointer(3);
557 break;
552 } 558 }
553 559
554 length_lo = (uint8_t)(length & 0xFF); 560 length_lo = (uint8_t)(length & 0xFF);
555 length_hi = (uint8_t)(length >> 8); 561 length_hi = (uint8_t)(length >> 8);
556 dataLength[0] = length_lo; 562 dataLength[0] = length_lo;
557 dataLength[1] = length_hi; 563 dataLength[1] = length_hi;
558 564
559 ef_write_block(dataLength,2, EF_SETTINGS, 0); 565 ef_write_block(dataLength,2, whichSettings, 0);
560 ef_write_block(pData,length, EF_SETTINGS, 0); 566 ef_write_block((uint8_t*)pSettings,length, whichSettings, 0);
561 // ext_flash_enable_protection(); 567 // ext_flash_enable_protection();
562 } 568 }
563 #endif 569 #endif
564 570
565 571
568 * but make sure that everything is fixed in 574 * but make sure that everything is fixed in
569 * set_new_settings_missing_in_ext_flash 575 * set_new_settings_missing_in_ext_flash
570 * new settings should be fine as they are added 576 * new settings should be fine as they are added
571 * and loaded before calling this function 577 * and loaded before calling this function
572 */ 578 */
573 uint8_t ext_flash_read_settings(void) 579 uint8_t ext_flash_read_settings(uint8_t whichSettings)
574 { 580 {
575 uint8_t returnValue = HAL_BUSY; 581 uint8_t returnValue = HAL_BUSY;
576 uint8_t exit = 0; 582 uint8_t exit = 0;
577 uint8_t *pData; 583 uint8_t *pData;
578 const uint16_t lengthStandardNow = sizeof(SSettings); 584 const uint16_t lengthStandardNow = sizeof(SSettings);
579 uint8_t length_lo, length_hi; 585 uint8_t length_lo, length_hi;
580 uint16_t lengthOnEEPROM; 586 uint16_t lengthOnEEPROM;
581 uint32_t header; 587 uint32_t header;
582 SSettings *pSettings = settingsGetPointer(); 588 SSettings* pSettings = settingsGetPointer();
583 589
584 actualAddress = SETTINGSSTART; 590 switch(whichSettings)
585 591 {
586 ext_flash_read_block_start(); 592 case EF_SETTINGS: pSettings = settingsGetPointer();
587 ext_flash_read_block(&length_lo, EF_SETTINGS); 593 actualAddress = SETTINGSSTART;
588 ext_flash_read_block(&length_hi, EF_SETTINGS); 594 break;
589 595 case EF_PROFILE0: pSettings = profileGetPointer(0);
590 while ((length_lo != 0xFF) && (length_hi != 0xFF) && (exit == 0)) /* get the latest stored setting block */ 596 actualAddress = PROFILE0_START;
591 { 597 break;
592 lengthOnEEPROM = length_hi * 256; 598 case EF_PROFILE1: pSettings = profileGetPointer(1);
593 lengthOnEEPROM += length_lo; 599 actualAddress = PROFILE1_START;
594 if(lengthOnEEPROM <= lengthStandardNow) /* EEPROM Header size equal or smaller => settings constant or upgraded */ 600 break;
595 { 601 case EF_PROFILE2: pSettings = profileGetPointer(2);
596 ext_flash_read_block_multi(&header, 4, EF_SETTINGS); 602 actualAddress = PROFILE2_START;
597 if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader)) /* check to allow update of header */ 603 break;
604 case EF_PROFILE3: pSettings = profileGetPointer(3);
605 actualAddress = PROFILE3_START;
606 break;
607 default: returnValue = HAL_ERROR;
608 }
609
610 if(returnValue != HAL_ERROR)
611 {
612 ext_flash_read_block_start();
613 ext_flash_read_block(&length_lo, whichSettings);
614 ext_flash_read_block(&length_hi, whichSettings);
615
616 while ((length_lo != 0xFF) && (length_hi != 0xFF) && (exit == 0)) /* get the latest stored setting block */
617 {
618 lengthOnEEPROM = length_hi * 256;
619 lengthOnEEPROM += length_lo;
620 if(lengthOnEEPROM <= lengthStandardNow) /* EEPROM Header size equal or smaller => settings constant or upgraded */
598 { 621 {
599 returnValue = HAL_OK; 622 ext_flash_read_block_multi(&header, 4, whichSettings);
600 pSettings->header = header; 623 if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader)) /* check to allow update of header */
601 pData = (uint8_t *)pSettings + 4; /* header */
602 for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++)
603 ext_flash_read_block(&pData[i], EF_SETTINGS);
604 if(header != pSettings->header) /* setting layout changed => no additional setting sets expected */
605 { 624 {
625 returnValue = HAL_OK;
626 pSettings->header = header;
627 pData = (uint8_t *)pSettings + 4; /* header */
628 for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++)
629 ext_flash_read_block(&pData[i], whichSettings);
630 if(header != pSettings->header) /* setting layout changed => no additional setting sets expected */
631 {
632 exit = 1;
633 }
634 }
635 else
636 {
637 returnValue = HAL_ERROR;
606 exit = 1; 638 exit = 1;
607 } 639 }
608 } 640 }
609 else 641 else /* size of settings decreased => possible downgrade of firmware */
610 { 642 {
611 returnValue = HAL_ERROR; 643 ext_flash_read_block_multi(&header, 4, whichSettings);
644
645 if(header > 0xFFFF0014) /* verify that new (old) header should be compatible (only less bytes, no change in layout) */
646 {
647 returnValue = HAL_OK;
648 pSettings->header = header;
649 pData = (uint8_t *)pSettings + 4; /* header */
650 for(uint16_t i = 0; i < (lengthStandardNow-4); i++) /* only read the data fitting into the structure */
651 ext_flash_read_block(&pData[i], whichSettings);
652 }
653 else
654 {
655 returnValue = HAL_ERROR;
656 }
612 exit = 1; 657 exit = 1;
613 } 658 }
614 } 659 ext_flash_read_block(&length_lo, whichSettings);
615 else /* size of settings decreased => possible downgrade of firmware */ 660 ext_flash_read_block(&length_hi, whichSettings);
616 { 661 }
617 ext_flash_read_block_multi(&header, 4, EF_SETTINGS); 662 ext_flash_decf_address_ring(whichSettings); /* set pointer back to empty address */
618 663 ext_flash_decf_address_ring(whichSettings);
619 if(header > 0xFFFF0014) /* verify that new (old) header should be compatible (only less bytes, no change in layout) */ 664 ext_flash_read_block_stop();
620 { 665
621 returnValue = HAL_OK; 666 if(actualAddress > actualPointerSettings) /* the write pointer has not yet been set up probably */
622 pSettings->header = header; 667 {
623 pData = (uint8_t *)pSettings + 4; /* header */ 668 actualPointerSettings = actualAddress;
624 for(uint16_t i = 0; i < (lengthStandardNow-4); i++) /* only read the data fitting into the structure */ 669 }
625 ext_flash_read_block(&pData[i], EF_SETTINGS); 670 ext_flash_read_block_stop();
626 } 671 }
627 else
628 {
629 returnValue = HAL_ERROR;
630 }
631 exit = 1;
632 }
633 ext_flash_read_block(&length_lo, EF_SETTINGS);
634 ext_flash_read_block(&length_hi, EF_SETTINGS);
635 }
636 ext_flash_decf_address_ring(EF_SETTINGS); /* set pointer back to empty address */
637 ext_flash_decf_address_ring(EF_SETTINGS);
638 ext_flash_read_block_stop();
639
640 if(actualAddress > actualPointerSettings) /* the write pointer has not yet been set up probably */
641 {
642 actualPointerSettings = actualAddress;
643 }
644 ext_flash_read_block_stop();
645 return returnValue; 672 return returnValue;
646 } 673 }
647 674
648 675
649 676
1849 case EF_FIRMWARE2: 1876 case EF_FIRMWARE2:
1850 actualAddress = actualPointerFirmware2; 1877 actualAddress = actualPointerFirmware2;
1851 ringStart = FWSTART2; 1878 ringStart = FWSTART2;
1852 ringStop = FWSTOP2; 1879 ringStop = FWSTOP2;
1853 break; 1880 break;
1881 case EF_PROFILE0:
1882 case EF_PROFILE1:
1883 case EF_PROFILE2:
1884 case EF_PROFILE3: actualAddress = actualPointerProfile;
1885 ringStart = PROFILE0_START;
1886 ringStop = PROFILE3_STOP;
1887 break;
1854 default: 1888 default:
1855 ringStart = FLASHSTART; 1889 ringStart = FLASHSTART;
1856 ringStop = FLASHSTOP; 1890 ringStop = FLASHSTOP;
1857 break; 1891 break;
1858 } 1892 }
1930 actualPointerFirmware = actualAddress; 1964 actualPointerFirmware = actualAddress;
1931 break; 1965 break;
1932 case EF_FIRMWARE2: 1966 case EF_FIRMWARE2:
1933 actualPointerFirmware2 = actualAddress; 1967 actualPointerFirmware2 = actualAddress;
1934 break; 1968 break;
1969 case EF_PROFILE0:
1970 case EF_PROFILE1:
1971 case EF_PROFILE2:
1972 case EF_PROFILE3: actualPointerProfile = actualAddress;
1973 break;
1935 default: 1974 default:
1936 break; 1975 break;
1937 } 1976 }
1938 } 1977 }
1939 1978
1994 break; 2033 break;
1995 case EF_SETTINGS: 2034 case EF_SETTINGS:
1996 ringStart = SETTINGSSTART; 2035 ringStart = SETTINGSSTART;
1997 ringStop = SETTINGSSTOP; 2036 ringStop = SETTINGSSTOP;
1998 break; 2037 break;
2038 case EF_PROFILE0:
2039 case EF_PROFILE1:
2040 case EF_PROFILE2:
2041 case EF_PROFILE3: ringStart = PROFILE0_START;
2042 ringStop = PROFILE3_STOP;
2043 break;
2044
1999 default: 2045 default:
2000 ringStart = FLASHSTART; 2046 ringStart = FLASHSTART;
2001 ringStop = FLASHSTOP; 2047 ringStop = FLASHSTOP;
2002 break; 2048 break;
2003 } 2049 }
2143 break; 2189 break;
2144 case EF_FIRMWARE2: 2190 case EF_FIRMWARE2:
2145 ringStart = FWSTART2; 2191 ringStart = FWSTART2;
2146 ringStop = FWSTOP2; 2192 ringStop = FWSTOP2;
2147 break; 2193 break;
2194 case EF_PROFILE0:
2195 case EF_PROFILE1:
2196 case EF_PROFILE2:
2197 case EF_PROFILE3: ringStart = PROFILE0_START;
2198 ringStop = PROFILE3_STOP;
2199 break;
2148 default: 2200 default:
2149 ringStart = FLASHSTART; 2201 ringStart = FLASHSTART;
2150 ringStop = FLASHSTOP; 2202 ringStop = FLASHSTOP;
2151 break; 2203 break;
2152 } 2204 }
2188 break; 2240 break;
2189 case EF_FIRMWARE2: 2241 case EF_FIRMWARE2:
2190 ringStart = FWSTART2; 2242 ringStart = FWSTART2;
2191 ringStop = FWSTOP2; 2243 ringStop = FWSTOP2;
2192 break; 2244 break;
2245 case EF_PROFILE0:
2246 case EF_PROFILE1:
2247 case EF_PROFILE2:
2248 case EF_PROFILE3: ringStart = PROFILE0_START;
2249 ringStop = PROFILE3_STOP;
2250 break;
2251
2193 default: 2252 default:
2194 ringStart = FLASHSTART; 2253 ringStart = FLASHSTART;
2195 ringStop = FLASHSTOP; 2254 ringStop = FLASHSTOP;
2196 break; 2255 break;
2197 } 2256 }