Mercurial > public > ostc4
comparison Small_CPU/Src/scheduler.c @ 696:cc542448fb28
Merge
author | heinrichsweikamp |
---|---|
date | Fri, 19 Aug 2022 11:30:24 +0200 |
parents | 52d68cf9994c |
children | f1b40364b0af |
comparison
equal
deleted
inserted
replaced
661:87bee7cc77b3 | 696:cc542448fb28 |
---|---|
40 #include "calc_crush.h" | 40 #include "calc_crush.h" |
41 #include "stm32f4xx_hal_rtc_ex.h" | 41 #include "stm32f4xx_hal_rtc_ex.h" |
42 #include "decom.h" | 42 #include "decom.h" |
43 #include "tm_stm32f4_otp.h" | 43 #include "tm_stm32f4_otp.h" |
44 #include "externalInterface.h" | 44 #include "externalInterface.h" |
45 #include "uart.h" | |
45 | 46 |
46 /* uncomment to enable restoting of last known date in case of a power loss (RTC looses timing data) */ | 47 /* uncomment to enable restoting of last known date in case of a power loss (RTC looses timing data) */ |
47 /* #define RESTORE_LAST_KNOWN_DATE */ | 48 /* #define RESTORE_LAST_KNOWN_DATE */ |
48 | 49 |
49 #define INVALID_PREASURE_VALUE (0.0f) | 50 #define INVALID_PREASURE_VALUE (0.0f) |
88 void copyTissueData(void); | 89 void copyTissueData(void); |
89 void copyVpmCrushingData(void); | 90 void copyVpmCrushingData(void); |
90 void copyDeviceData(void); | 91 void copyDeviceData(void); |
91 void copyPICdata(void); | 92 void copyPICdata(void); |
92 void copyExtADCdata(); | 93 void copyExtADCdata(); |
94 void copyExtCO2data(); | |
93 static void schedule_update_timer_helper(int8_t thisSeconds); | 95 static void schedule_update_timer_helper(int8_t thisSeconds); |
94 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow); | 96 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow); |
95 | 97 |
96 void scheduleSetDate(SDeviceLine *line); | 98 void scheduleSetDate(SDeviceLine *line); |
97 | 99 |
123 global.ButtonPICdata[2] = 0xFF; | 125 global.ButtonPICdata[2] = 0xFF; |
124 global.ButtonPICdata[3] = 0xFF; | 126 global.ButtonPICdata[3] = 0xFF; |
125 | 127 |
126 global.I2C_SystemStatus = HAL_ERROR; // 0x00 would be everything working | 128 global.I2C_SystemStatus = HAL_ERROR; // 0x00 would be everything working |
127 | 129 |
130 global.lifeData.battery_voltage = BATTERY_DEFAULT_VOLTAGE; | |
131 | |
128 global.lifeData.pressure_ambient_bar = INVALID_PREASURE_VALUE; | 132 global.lifeData.pressure_ambient_bar = INVALID_PREASURE_VALUE; |
129 global.lifeData.pressure_surface_bar = INVALID_PREASURE_VALUE; | 133 global.lifeData.pressure_surface_bar = INVALID_PREASURE_VALUE; |
130 decom_reset_with_1000mbar(&global.lifeData); | 134 decom_reset_with_1000mbar(&global.lifeData); |
131 | 135 |
132 global.demo_mode = 0; | 136 global.demo_mode = 0; |
299 /* for device data updates */ | 303 /* for device data updates */ |
300 deviceDataFlashValid = 0; | 304 deviceDataFlashValid = 0; |
301 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); | 305 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); |
302 deviceDataFlashValid = 1; | 306 deviceDataFlashValid = 1; |
303 | 307 |
308 | |
309 /* handle external interface requests */ | |
310 | |
311 if((global.dataSendToSlave.data.externalInterface_Cmd && EXT_INTERFACE_33V_ON) != externalInterface_isEnabledPower33()) | |
312 { | |
313 externalInterface_SwitchPower33(global.dataSendToSlave.data.externalInterface_Cmd && EXT_INTERFACE_33V_ON); | |
314 } | |
315 | |
316 if(((global.dataSendToSlave.data.externalInterface_Cmd & EXT_INTERFACE_ADC_ON) != 0) != externalInterface_isEnabledADC()) | |
317 { | |
318 externalInterface_SwitchADC(global.dataSendToSlave.data.externalInterface_Cmd && EXT_INTERFACE_ADC_ON); | |
319 } | |
320 | |
321 | |
322 if(global.dataSendToSlave.data.externalInterface_Cmd & 0x00FF) /* lowest nibble for commands */ | |
323 { | |
324 externalInterface_ExecuteCmd(global.dataSendToSlave.data.externalInterface_Cmd); | |
325 } | |
326 | |
327 | |
304 #if 0 | 328 #if 0 |
305 //TODO: Temporary placed here. Duration ~210 ms. | 329 //TODO: Temporary placed here. Duration ~210 ms. |
306 if (global.I2C_SystemStatus != HAL_OK) { | 330 if (global.I2C_SystemStatus != HAL_OK) { |
307 MX_I2C1_TestAndClear(); | 331 MX_I2C1_TestAndClear(); |
308 MX_I2C1_Init(); | 332 MX_I2C1_Init(); |
485 while(global.mode == MODE_DIVE) | 509 while(global.mode == MODE_DIVE) |
486 { | 510 { |
487 lasttick = HAL_GetTick(); | 511 lasttick = HAL_GetTick(); |
488 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); | 512 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
489 | 513 |
514 #ifdef ENABLE_CO2_SUPPORT | |
515 if(global.dataSendToSlave.data.externalInterface_Cmd & EXT_INTERFACE_UART_SENTINEL) | |
516 { | |
517 HandleUARTCO2Data(); | |
518 } | |
519 #endif | |
520 #ifdef ENABLE_SENTINEL_MODE | |
521 if(global.dataSendToSlave.data.externalInterface_Cmd & EXT_INTERFACE_UART_SENTINEL) | |
522 { | |
523 HandleUARTSentinelData(); | |
524 } | |
525 #endif | |
526 | |
527 | |
490 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) | 528 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
491 { | 529 { |
492 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */ | 530 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */ |
493 { | 531 { |
494 Scheduler.counterSPIdata100msec++; | 532 Scheduler.counterSPIdata100msec++; |
495 } | 533 } |
496 schedule_check_resync(); | 534 schedule_check_resync(); |
497 | 535 |
498 extAdcChannel = externalInterface_ReadAndSwitch(); | 536 if(externalInterface_isEnabledADC()) |
499 if(extAdcChannel != EXTERNAL_ADC_NO_DATA) | 537 { |
500 { | 538 extAdcChannel = externalInterface_ReadAndSwitch(); |
501 externalInterface_CalculateADCValue(extAdcChannel); | 539 if(extAdcChannel != EXTERNAL_ADC_NO_DATA) |
502 copyExtADCdata(); | 540 { |
503 } | 541 externalInterface_CalculateADCValue(extAdcChannel); |
542 } | |
543 } | |
544 copyExtADCdata(); | |
545 copyExtCO2data(); | |
504 } | 546 } |
505 | 547 |
506 //Evaluate pressure at 20 ms, 120 ms, 220 ms,.... | 548 //Evaluate pressure at 20 ms, 120 ms, 220 ms,.... |
507 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) | 549 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
508 { | 550 { |
759 | 801 |
760 | 802 |
761 | 803 |
762 void scheduleSurfaceMode(void) | 804 void scheduleSurfaceMode(void) |
763 { | 805 { |
764 | |
765 uint32_t ticksdiff = 0; | 806 uint32_t ticksdiff = 0; |
766 uint32_t lasttick = 0; | 807 uint32_t lasttick = 0; |
767 uint8_t extAdcChannel = 0; | 808 uint8_t extAdcChannel = 0; |
809 uint8_t batteryToggle = 0; /* ADC is operating in automatic 2 second cycles => consider for battery charge function call */ | |
810 | |
768 Scheduler.tickstart = HAL_GetTick(); | 811 Scheduler.tickstart = HAL_GetTick(); |
769 Scheduler.counterSPIdata100msec = 0; | 812 Scheduler.counterSPIdata100msec = 0; |
770 Scheduler.counterCompass100msec = 0; | 813 Scheduler.counterCompass100msec = 0; |
771 Scheduler.counterPressure100msec = 0; | 814 Scheduler.counterPressure100msec = 0; |
772 Scheduler.counterAmbientLight100msec = 0; | 815 Scheduler.counterAmbientLight100msec = 0; |
784 if(setButtonsNow == 1) | 827 if(setButtonsNow == 1) |
785 { | 828 { |
786 if(scheduleSetButtonResponsiveness()) | 829 if(scheduleSetButtonResponsiveness()) |
787 setButtonsNow = 0; | 830 setButtonsNow = 0; |
788 } | 831 } |
789 | 832 |
833 #ifdef ENABLE_CO2_SUPPORT | |
834 if(global.dataSendToSlave.data.externalInterface_Cmd & EXT_INTERFACE_UART_SENTINEL) | |
835 { | |
836 HandleUARTCO2Data(); | |
837 } | |
838 #endif | |
839 #ifdef ENABLE_SENTINEL_MODE | |
840 if(global.dataSendToSlave.data.externalInterface_Cmd & EXT_INTERFACE_UART_SENTINEL) | |
841 { | |
842 HandleUARTSentinelData(); | |
843 } | |
844 #endif | |
845 | |
790 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */ | 846 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */ |
791 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) | 847 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
792 { | 848 { |
793 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */ | 849 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */ |
794 { | 850 { |
795 Scheduler.counterSPIdata100msec++; | 851 Scheduler.counterSPIdata100msec++; |
796 } | 852 } |
797 schedule_check_resync(); | 853 schedule_check_resync(); |
798 extAdcChannel = externalInterface_ReadAndSwitch(); | 854 if(externalInterface_isEnabledADC()) |
799 if(extAdcChannel != EXTERNAL_ADC_NO_DATA) | 855 { |
800 { | 856 extAdcChannel = externalInterface_ReadAndSwitch(); |
801 externalInterface_CalculateADCValue(extAdcChannel); | 857 if(extAdcChannel != EXTERNAL_ADC_NO_DATA) |
802 copyExtADCdata(); | 858 { |
803 } | 859 externalInterface_CalculateADCValue(extAdcChannel); |
860 | |
861 } | |
862 } | |
863 copyExtADCdata(); | |
864 copyExtCO2data(); | |
804 } | 865 } |
805 | 866 |
806 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */ | 867 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */ |
807 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) | 868 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
808 { | 869 { |
884 } | 945 } |
885 else | 946 else |
886 { | 947 { |
887 global.lifeData.desaturation_time_minutes = 0; | 948 global.lifeData.desaturation_time_minutes = 0; |
888 } | 949 } |
889 battery_gas_gauge_get_data(); | 950 |
890 battery_charger_get_status_and_contral_battery_gas_gauge(1); | 951 if(!batteryToggle) |
952 { | |
953 battery_gas_gauge_get_data(); | |
954 battery_charger_get_status_and_contral_battery_gas_gauge(2); | |
955 batteryToggle = 1; | |
956 } | |
957 else | |
958 { | |
959 batteryToggle = 0; | |
960 } | |
891 | 961 |
892 copyCnsAndOtuData(); | 962 copyCnsAndOtuData(); |
893 copyTimeData(); | 963 copyTimeData(); |
894 copyBatteryData(); | 964 copyBatteryData(); |
895 copyDeviceData(); | 965 copyDeviceData(); |
966 | |
896 | 967 |
897 /* check if I2C is not up an running and try to reactivate if necessary. Also do initialization if problem occured during startup */ | 968 /* check if I2C is not up an running and try to reactivate if necessary. Also do initialization if problem occured during startup */ |
898 if(global.I2C_SystemStatus != HAL_OK) | 969 if(global.I2C_SystemStatus != HAL_OK) |
899 { | 970 { |
900 MX_I2C1_TestAndClear(); | 971 MX_I2C1_TestAndClear(); |
1032 | 1103 |
1033 void scheduleSleepMode(void) | 1104 void scheduleSleepMode(void) |
1034 { | 1105 { |
1035 global.dataSendToMaster.mode = 0; | 1106 global.dataSendToMaster.mode = 0; |
1036 global.deviceDataSendToMaster.mode = 0; | 1107 global.deviceDataSendToMaster.mode = 0; |
1108 secondsCount = 0; | |
1037 | 1109 |
1038 /* prevent button wake up problem while in sleep_prepare | 1110 /* prevent button wake up problem while in sleep_prepare |
1039 * sleep prepare does I2C_DeInit() | 1111 * sleep prepare does I2C_DeInit() |
1040 */ | 1112 */ |
1041 if(global.mode != MODE_SLEEP) | 1113 if(global.mode != MODE_SLEEP) |
1054 | 1126 |
1055 | 1127 |
1056 if(global.mode == MODE_SLEEP) | 1128 if(global.mode == MODE_SLEEP) |
1057 secondsCount += 2; | 1129 secondsCount += 2; |
1058 | 1130 |
1131 externalInterface_InitPower33(); | |
1059 MX_I2C1_Init(); | 1132 MX_I2C1_Init(); |
1060 pressure_sensor_get_pressure_raw(); | 1133 pressure_sensor_get_pressure_raw(); |
1061 | 1134 |
1062 /* check if I2C is not up and running and try to reactivate if necessary. Also do initialization if problem occurred during startup */ | 1135 /* check if I2C is not up and running and try to reactivate if necessary. Also do initialization if problem occurred during startup */ |
1063 if(global.I2C_SystemStatus != HAL_OK) | 1136 if(global.I2C_SystemStatus != HAL_OK) |
1067 I2C_DeInit(); | 1140 I2C_DeInit(); |
1068 HAL_Delay(100); | 1141 HAL_Delay(100); |
1069 MX_I2C1_Init(); | 1142 MX_I2C1_Init(); |
1070 HAL_Delay(100); | 1143 HAL_Delay(100); |
1071 | 1144 |
1072 | |
1073 if((global.I2C_SystemStatus == HAL_OK) && (!is_init_pressure_done())) | 1145 if((global.I2C_SystemStatus == HAL_OK) && (!is_init_pressure_done())) |
1074 { | 1146 { |
1075 init_pressure(); | 1147 init_pressure(); |
1076 } | 1148 } |
1077 } | 1149 } |
1078 | 1150 |
1079 if(secondsCount >= 30) | 1151 if((secondsCount >= 30) || (global.mode != MODE_SLEEP)) /* Service battery charge state in case sleep is left */ |
1080 { | 1152 { |
1081 pressure_sensor_get_temperature_raw(); | 1153 pressure_sensor_get_temperature_raw(); |
1082 battery_gas_gauge_get_data(); | 1154 battery_gas_gauge_get_data(); |
1083 // ReInit_battery_charger_status_pins(); | 1155 ReInit_battery_charger_status_pins(); |
1084 battery_charger_get_status_and_contral_battery_gas_gauge(30); | 1156 battery_charger_get_status_and_contral_battery_gas_gauge(secondsCount); |
1085 // DeInit_battery_charger_status_pins(); | 1157 // DeInit_battery_charger_status_pins(); |
1086 secondsCount = 0; | 1158 secondsCount = 0; |
1087 } | 1159 } |
1088 | 1160 |
1089 pressure_calculation(); | 1161 pressure_calculation(); |
1118 /* new section for system after Standby */ | 1190 /* new section for system after Standby */ |
1119 scheduleUpdateLifeData(-1); | 1191 scheduleUpdateLifeData(-1); |
1120 clearDecoNow = 0; | 1192 clearDecoNow = 0; |
1121 setButtonsNow = 0; | 1193 setButtonsNow = 0; |
1122 reinitGlobals(); | 1194 reinitGlobals(); |
1195 ReInit_battery_charger_status_pins(); | |
1123 } | 1196 } |
1124 | 1197 |
1125 | 1198 |
1126 | 1199 |
1127 /* Private functions ---------------------------------------------------------*/ | 1200 /* Private functions ---------------------------------------------------------*/ |
1361 global.deviceData.hoursOfOperation.value_int32++; | 1434 global.deviceData.hoursOfOperation.value_int32++; |
1362 scheduleSetDate(&global.deviceData.hoursOfOperation); | 1435 scheduleSetDate(&global.deviceData.hoursOfOperation); |
1363 } | 1436 } |
1364 break; | 1437 break; |
1365 | 1438 |
1366 case MODE_SLEEP: | 1439 case MODE_SLEEP: |
1367 case MODE_SHUTDOWN: | 1440 case MODE_SHUTDOWN: |
1368 break; | 1441 break; |
1369 } | 1442 } |
1370 } | 1443 } |
1371 | 1444 |
1547 | 1620 |
1548 //Supports threadsave copying!!! | 1621 //Supports threadsave copying!!! |
1549 void copyBatteryData(void) | 1622 void copyBatteryData(void) |
1550 { | 1623 { |
1551 uint8_t boolBatteryData = !global.dataSendToMaster.boolBatteryData; | 1624 uint8_t boolBatteryData = !global.dataSendToMaster.boolBatteryData; |
1625 global.lifeData.battery_charge = get_charge(); | |
1552 global.dataSendToMaster.data[boolBatteryData].battery_voltage = get_voltage(); | 1626 global.dataSendToMaster.data[boolBatteryData].battery_voltage = get_voltage(); |
1553 global.dataSendToMaster.data[boolBatteryData].battery_charge= get_charge(); | 1627 |
1628 if(battery_gas_gauge_isChargeValueValid()) | |
1629 { | |
1630 global.dataSendToMaster.data[boolBatteryData].battery_charge= global.lifeData.battery_charge; | |
1631 } | |
1632 else | |
1633 { | |
1634 global.dataSendToMaster.data[boolBatteryData].battery_charge = global.lifeData.battery_charge * -1.0; /* negate value to show that this is just an assumption */ | |
1635 } | |
1554 global.dataSendToMaster.boolBatteryData = boolBatteryData; | 1636 global.dataSendToMaster.boolBatteryData = boolBatteryData; |
1555 } | 1637 } |
1556 | 1638 |
1557 | 1639 |
1558 //Supports threadsave copying!!! | 1640 //Supports threadsave copying!!! |
1627 { | 1709 { |
1628 float value; | 1710 float value; |
1629 | 1711 |
1630 uint8_t channel = 0; | 1712 uint8_t channel = 0; |
1631 | 1713 |
1714 uint8_t boolADCBuffer = ~(global.dataSendToMaster.boolADCO2Data & DATA_BUFFER_ADC); | |
1715 | |
1716 boolADCBuffer &= DATA_BUFFER_ADC; | |
1717 global.dataSendToMaster.boolADCO2Data &= ~DATA_BUFFER_ADC; | |
1718 | |
1632 for(channel = 0; channel < MAX_ADC_CHANNEL; channel++) | 1719 for(channel = 0; channel < MAX_ADC_CHANNEL; channel++) |
1633 { | 1720 { |
1634 value = getExternalInterfaceChannel(channel); | 1721 value = getExternalInterfaceChannel(channel); |
1635 global.dataSendToMaster.data[0].extADC_voltage[channel] = value; | 1722 global.dataSendToMaster.data[boolADCBuffer && DATA_BUFFER_ADC].extADC_voltage[channel] = value; |
1636 } | 1723 } |
1637 } | 1724 global.dataSendToMaster.boolADCO2Data |= boolADCBuffer; |
1638 | 1725 } |
1726 | |
1727 void copyExtCO2data() | |
1728 { | |
1729 uint16_t value; | |
1730 uint8_t boolCO2Buffer = ~(global.dataSendToMaster.boolADCO2Data & DATA_BUFFER_CO2); | |
1731 | |
1732 global.dataSendToMaster.boolADCO2Data &= ~DATA_BUFFER_CO2; | |
1733 boolCO2Buffer &= DATA_BUFFER_CO2; | |
1734 | |
1735 if(externalInterface_GetCO2State()) | |
1736 { | |
1737 value = externalInterface_GetCO2Value(); | |
1738 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].CO2_ppm = value; | |
1739 value = externalInterface_GetCO2SignalStrength(); | |
1740 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].CO2_signalStrength = value; | |
1741 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].externalInterface_CmdAnswer = externalInterface_GetCO2State(); | |
1742 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON); /* clear command responses */ | |
1743 } | |
1744 else | |
1745 { | |
1746 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].CO2_ppm = 0; | |
1747 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].CO2_signalStrength = 0; | |
1748 global.dataSendToMaster.data[(boolCO2Buffer && DATA_BUFFER_CO2)].externalInterface_CmdAnswer = 0; | |
1749 } | |
1750 global.dataSendToMaster.boolADCO2Data |= boolCO2Buffer; | |
1751 } | |
1639 | 1752 |
1640 typedef enum | 1753 typedef enum |
1641 { | 1754 { |
1642 SPI3_OK = 0x00, | 1755 SPI3_OK = 0x00, |
1643 SPI3_DEINIT = 0x01, | 1756 SPI3_DEINIT = 0x01, |