Mercurial > public > ostc4
comparison Discovery/Src/data_exchange_main.c @ 141:30d855ea60d8 FlipDisplay
Only use data if data frame is valid
Intraduced token handling between main and RTE
author | Ideenmodellierer |
---|---|
date | Thu, 28 Feb 2019 19:07:10 +0100 |
parents | cc9c18075e00 |
children | e1d66bc78f9a |
comparison
equal
deleted
inserted
replaced
140:f6c52eb0e25d | 141:30d855ea60d8 |
---|---|
85 uint8_t told_reset_logik_alles_ok = 0; | 85 uint8_t told_reset_logik_alles_ok = 0; |
86 | 86 |
87 SDataReceiveFromMaster dataOut; | 87 SDataReceiveFromMaster dataOut; |
88 SDataExchangeSlaveToMaster dataIn; | 88 SDataExchangeSlaveToMaster dataIn; |
89 | 89 |
90 uint32_t systick_last; | |
91 uint32_t systick_last_spi; | |
92 uint8_t data_old__lost_connection_to_slave_counter_temp = 0; | 90 uint8_t data_old__lost_connection_to_slave_counter_temp = 0; |
93 uint8_t data_old__lost_connection_to_slave_counter_retry = 0; | 91 uint8_t data_old__lost_connection_to_slave_counter_retry = 0; |
94 uint32_t data_old__lost_connection_to_slave_counter_total = 0; | 92 uint32_t data_old__lost_connection_to_slave_counter_total = 0; |
95 | 93 |
96 /* Private types -------------------------------------------------------------*/ | 94 /* Private types -------------------------------------------------------------*/ |
192 | 190 |
193 dataOut.footer.checkCode[0] = 0xF4; | 191 dataOut.footer.checkCode[0] = 0xF4; |
194 dataOut.footer.checkCode[1] = 0xF3; | 192 dataOut.footer.checkCode[1] = 0xF3; |
195 dataOut.footer.checkCode[2] = 0xF2; | 193 dataOut.footer.checkCode[2] = 0xF2; |
196 dataOut.footer.checkCode[3] = 0xF1; | 194 dataOut.footer.checkCode[3] = 0xF1; |
197 | |
198 systick_last = HAL_GetTick() - 100; | |
199 } | 195 } |
200 | 196 |
201 | 197 |
202 void DataEx_call_helper_requests(void) | 198 void DataEx_call_helper_requests(void) |
203 { | 199 { |
300 | 296 |
301 | 297 |
302 uint8_t DataEX_call(void) | 298 uint8_t DataEX_call(void) |
303 { | 299 { |
304 uint8_t SPI_DMA_answer = 0; | 300 uint8_t SPI_DMA_answer = 0; |
305 | 301 |
306 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET); | 302 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET); |
307 delayMicros(20); //~exchange time(+20% reserve) | 303 delayMicros(10); //~exchange time(+20% reserve) |
308 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET); | 304 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET); |
309 /* one cycle with NotChipSelect true to clear slave spi buffer */ | 305 /* one cycle with NotChipSelect true to clear slave spi buffer */ |
310 | 306 |
307 | |
311 if(data_old__lost_connection_to_slave_counter_temp >= 3) | 308 if(data_old__lost_connection_to_slave_counter_temp >= 3) |
312 { | 309 { |
313 data_old__lost_connection_to_slave_counter_temp = 0; | 310 data_old__lost_connection_to_slave_counter_temp = 0; |
314 if(DataEX_check_header_and_footer_shifted()) | 311 if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry == 0)) |
315 { | 312 { |
316 HAL_SPI_Abort_IT(&cpu2DmaSpi); | 313 HAL_SPI_Abort_IT(&cpu2DmaSpi); |
317 } | 314 } |
315 | |
316 /* reset of own DMA does not work ==> request reset of slave dma */ | |
317 if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry >= 2)) | |
318 { | |
319 dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0xA5; | |
320 } | |
318 data_old__lost_connection_to_slave_counter_retry++; | 321 data_old__lost_connection_to_slave_counter_retry++; |
319 } | 322 } |
320 | 323 |
321 DataEx_call_helper_requests(); | 324 DataEx_call_helper_requests(); |
322 | |
323 systick_last = HAL_GetTick(); | |
324 | 325 |
325 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */ | 326 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */ |
326 | 327 |
327 | 328 |
328 SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE); | 329 SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE); |
352 { | 353 { |
353 | 354 |
354 | 355 |
355 if(hspi == &cpu2DmaSpi) | 356 if(hspi == &cpu2DmaSpi) |
356 { | 357 { |
357 systick_last_spi = HAL_GetTick(); | |
358 SPI_CALLBACKS+=1; | 358 SPI_CALLBACKS+=1; |
359 } | 359 } |
360 } | 360 } |
361 | 361 |
362 | 362 |
803 data_old__lost_connection_to_slave_counter_temp += 1; | 803 data_old__lost_connection_to_slave_counter_temp += 1; |
804 data_old__lost_connection_to_slave_counter_total += 1; | 804 data_old__lost_connection_to_slave_counter_total += 1; |
805 } | 805 } |
806 return; | 806 return; |
807 } | 807 } |
808 | 808 else /* RX data OK */ |
809 { | |
810 data_old__lost_connection_to_slave_counter_temp = 0; | |
811 data_old__lost_connection_to_slave_counter_retry = 0; | |
812 pStateReal->data_old__lost_connection_to_slave = 0; | |
813 } | |
814 | |
815 /* update SPI communication tokens */ | |
816 dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = dataIn.header.checkCode[SPI_HEADER_INDEX_SLAVE]; | |
817 dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] = (dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] + 1) & 0x7F; | |
818 | |
809 if(getDeviceDataAfterStartOfMainCPU) | 819 if(getDeviceDataAfterStartOfMainCPU) |
810 { | 820 { |
811 getDeviceDataAfterStartOfMainCPU--; | 821 getDeviceDataAfterStartOfMainCPU--; |
812 if(getDeviceDataAfterStartOfMainCPU == 0) | 822 if(getDeviceDataAfterStartOfMainCPU == 0) |
813 { | 823 { |
814 dataOut.getDeviceDataNow = 1; | 824 dataOut.getDeviceDataNow = 1; |
815 getDeviceDataAfterStartOfMainCPU = 10*60*10;// * 100ms | 825 getDeviceDataAfterStartOfMainCPU = 10*60*10; /* * 100ms = 60 second => update device data every 10 minutes */ |
816 } | 826 } |
817 } | 827 } |
818 | 828 |
819 /* new 151207 hw */ | 829 /* new 151207 hw */ |
820 if(requestNecessary.uw != 0) | 830 if(requestNecessary.uw != 0) |
865 | 875 |
866 pStateReal->pressure_uTick_old = pStateReal->pressure_uTick_new; | 876 pStateReal->pressure_uTick_old = pStateReal->pressure_uTick_new; |
867 pStateReal->pressure_uTick_new = dataIn.data[dataIn.boolPressureData].pressure_uTick; | 877 pStateReal->pressure_uTick_new = dataIn.data[dataIn.boolPressureData].pressure_uTick; |
868 pStateReal->pressure_uTick_local_new = HAL_GetTick(); | 878 pStateReal->pressure_uTick_local_new = HAL_GetTick(); |
869 | 879 |
870 if(ambient < (surface + 0.04f)) | 880 /* what was the code behind this if statement ? */ |
881 /* if(ambient < (surface + 0.04f)) */ | |
871 | 882 |
872 pStateReal->lifeData.dateBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_dr; | 883 pStateReal->lifeData.dateBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_dr; |
873 pStateReal->lifeData.timeBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_tr; | 884 pStateReal->lifeData.timeBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_tr; |
874 } | 885 } |
875 dataOut.setAccidentFlag = 0; | 886 dataOut.setAccidentFlag = 0; |
876 | 887 |
877 //Start of diveMode? | 888 if(pStateReal->data_old__lost_connection_to_slave == 0) |
878 if(pStateReal->mode != MODE_DIVE && dataIn.mode == MODE_DIVE) | 889 { |
879 { | 890 //Start of diveMode? |
880 if(modeChangeFlag) | 891 if(pStateReal->mode != MODE_DIVE && dataIn.mode == MODE_DIVE) |
881 *modeChangeFlag = 1; | 892 { |
882 if(stateUsed == stateSimGetPointer()) | 893 if(modeChangeFlag) |
894 { | |
895 *modeChangeFlag = 1; | |
896 } | |
897 if(stateUsed == stateSimGetPointer()) | |
883 { | 898 { |
884 simulation_exit(); | 899 simulation_exit(); |
885 } | 900 } |
886 // new 170508 | 901 // new 170508 |
887 settingsGetPointer()->bluetoothActive = 0; | 902 settingsGetPointer()->bluetoothActive = 0; |
888 MX_Bluetooth_PowerOff(); | 903 MX_Bluetooth_PowerOff(); |
889 //Init dive Mode | 904 //Init dive Mode |
890 decoLock = DECO_CALC_init_as_is_start_of_dive; | 905 decoLock = DECO_CALC_init_as_is_start_of_dive; |
891 pStateReal->lifeData.boolResetAverageDepth = 1; | 906 pStateReal->lifeData.boolResetAverageDepth = 1; |
892 pStateReal->lifeData.boolResetStopwatch = 1; | 907 pStateReal->lifeData.boolResetStopwatch = 1; |
893 } | 908 } |
894 | 909 |
895 //End of diveMode? | 910 //End of diveMode? |
896 if(pStateReal->mode == MODE_DIVE && dataIn.mode != MODE_DIVE) | 911 if(pStateReal->mode == MODE_DIVE && dataIn.mode != MODE_DIVE) |
897 { | 912 { |
898 if(modeChangeFlag) | 913 if(modeChangeFlag) |
899 *modeChangeFlag = 1; | 914 { |
900 createDiveSettings(); | 915 *modeChangeFlag = 1; |
901 | 916 } |
902 if(pStateReal->warnings.cnsHigh) | 917 createDiveSettings(); |
903 { | 918 |
904 if(pStateReal->lifeData.cns >= 130) | 919 if(pStateReal->warnings.cnsHigh) |
905 dataOut.setAccidentFlag += ACCIDENT_CNSLVL2; | 920 { |
906 else if(pStateReal->lifeData.cns >= 100) | 921 if(pStateReal->lifeData.cns >= 130) |
907 dataOut.setAccidentFlag += ACCIDENT_CNS; | 922 dataOut.setAccidentFlag += ACCIDENT_CNSLVL2; |
908 } | 923 else if(pStateReal->lifeData.cns >= 100) |
909 if(pStateReal->warnings.decoMissed) | 924 dataOut.setAccidentFlag += ACCIDENT_CNS; |
910 dataOut.setAccidentFlag += ACCIDENT_DECOSTOP; | 925 } |
911 } | 926 if(pStateReal->warnings.decoMissed) |
912 pStateReal->mode = dataIn.mode; | 927 dataOut.setAccidentFlag += ACCIDENT_DECOSTOP; |
913 pStateReal->chargeStatus = dataIn.chargeStatus; | 928 } |
914 | 929 pStateReal->mode = dataIn.mode; |
915 pStateReal->lifeData.pressure_ambient_bar = ambient; | 930 pStateReal->chargeStatus = dataIn.chargeStatus; |
916 pStateReal->lifeData.pressure_surface_bar = surface; | 931 |
917 if(is_ambient_pressure_close_to_surface(&pStateReal->lifeData)) | 932 pStateReal->lifeData.pressure_ambient_bar = ambient; |
918 { | 933 pStateReal->lifeData.pressure_surface_bar = surface; |
919 pStateReal->lifeData.depth_meter = 0; | 934 if(is_ambient_pressure_close_to_surface(&pStateReal->lifeData)) |
920 } | 935 { |
921 else | 936 pStateReal->lifeData.depth_meter = 0; |
922 { | 937 } |
923 pStateReal->lifeData.depth_meter = meter; | 938 else |
924 } | 939 { |
925 | 940 pStateReal->lifeData.depth_meter = meter; |
926 if(pStateReal->data_old__lost_connection_to_slave == 0) | 941 } |
927 { | 942 |
928 pStateReal->lifeData.temperature_celsius = dataIn.data[dataIn.boolPressureData].temperature; | 943 pStateReal->lifeData.temperature_celsius = dataIn.data[dataIn.boolPressureData].temperature; |
929 pStateReal->lifeData.ascent_rate_meter_per_min = dataIn.data[dataIn.boolPressureData].ascent_rate_meter_per_min; | 944 pStateReal->lifeData.ascent_rate_meter_per_min = dataIn.data[dataIn.boolPressureData].ascent_rate_meter_per_min; |
930 if(pStateReal->mode != MODE_DIVE) | 945 if(pStateReal->mode != MODE_DIVE) |
931 pStateReal->lifeData.max_depth_meter = 0; | 946 pStateReal->lifeData.max_depth_meter = 0; |
932 else | 947 else |
963 | 978 |
964 pStateReal->compass_uTick_old = pStateReal->compass_uTick_new; | 979 pStateReal->compass_uTick_old = pStateReal->compass_uTick_new; |
965 pStateReal->compass_uTick_new = dataIn.data[dataIn.boolCompassData].compass_uTick; | 980 pStateReal->compass_uTick_new = dataIn.data[dataIn.boolCompassData].compass_uTick; |
966 pStateReal->compass_uTick_local_new = HAL_GetTick(); | 981 pStateReal->compass_uTick_local_new = HAL_GetTick(); |
967 | 982 |
968 pStateReal->lifeData.cns = dataIn.data[dataIn.boolToxicData].cns; | 983 pStateReal->lifeData.cns = dataIn.data[dataIn.boolToxicData].cns; |
969 pStateReal->lifeData.otu = dataIn.data[dataIn.boolToxicData].otu; | 984 pStateReal->lifeData.otu = dataIn.data[dataIn.boolToxicData].otu; |
970 pStateReal->lifeData.no_fly_time_minutes = dataIn.data[dataIn.boolToxicData].no_fly_time_minutes; | 985 pStateReal->lifeData.no_fly_time_minutes = dataIn.data[dataIn.boolToxicData].no_fly_time_minutes; |
971 pStateReal->lifeData.desaturation_time_minutes = dataIn.data[dataIn.boolToxicData].desaturation_time_minutes; | 986 pStateReal->lifeData.desaturation_time_minutes = dataIn.data[dataIn.boolToxicData].desaturation_time_minutes; |
972 | 987 |
973 memcpy(pStateReal->lifeData.tissue_nitrogen_bar, dataIn.data[dataIn.boolTisssueData].tissue_nitrogen_bar,sizeof(pStateReal->lifeData.tissue_nitrogen_bar)); | 988 memcpy(pStateReal->lifeData.tissue_nitrogen_bar, dataIn.data[dataIn.boolTisssueData].tissue_nitrogen_bar,sizeof(pStateReal->lifeData.tissue_nitrogen_bar)); |
974 memcpy(pStateReal->lifeData.tissue_helium_bar, dataIn.data[dataIn.boolTisssueData].tissue_helium_bar,sizeof(pStateReal->lifeData.tissue_helium_bar)); | 989 memcpy(pStateReal->lifeData.tissue_helium_bar, dataIn.data[dataIn.boolTisssueData].tissue_helium_bar,sizeof(pStateReal->lifeData.tissue_helium_bar)); |
975 | 990 |
1241 } | 1256 } |
1242 | 1257 |
1243 /* sensorErrors | 1258 /* sensorErrors |
1244 */ | 1259 */ |
1245 pStateReal->sensorErrorsRTE = dataIn.sensorErrors; | 1260 pStateReal->sensorErrorsRTE = dataIn.sensorErrors; |
1246 | |
1247 /* end | |
1248 */ | |
1249 data_old__lost_connection_to_slave_counter_temp = 0; | |
1250 data_old__lost_connection_to_slave_counter_retry = 0; | |
1251 pStateReal->data_old__lost_connection_to_slave = 0; | |
1252 } | 1261 } |
1253 | 1262 |
1254 | 1263 |
1255 uint8_t DataEX_check_RTE_version__needs_update(void) | 1264 uint8_t DataEX_check_RTE_version__needs_update(void) |
1256 { | 1265 { |
1282 * If that is not the case the DMA is somehow not in sync | 1291 * If that is not the case the DMA is somehow not in sync |
1283 */ | 1292 */ |
1284 uint8_t DataEX_check_header_and_footer_shifted() | 1293 uint8_t DataEX_check_header_and_footer_shifted() |
1285 { | 1294 { |
1286 uint8_t ret = 1; | 1295 uint8_t ret = 1; |
1287 if((dataIn.footer.checkCode[0] != 0x00) | 1296 if((dataIn.footer.checkCode[0] == 0x00) |
1288 && (dataIn.footer.checkCode[1] != 0x00) | 1297 && (dataIn.footer.checkCode[1] == 0x00) |
1289 && (dataIn.footer.checkCode[2] != 0x00) | 1298 && (dataIn.footer.checkCode[2] == 0x00) |
1290 && (dataIn.footer.checkCode[3] != 0x00)) { ret = 0; } | 1299 && (dataIn.footer.checkCode[3] == 0x00)) { ret = 0; } |
1291 | 1300 |
1292 if((dataIn.footer.checkCode[0] != 0xff) | 1301 if((dataIn.footer.checkCode[0] == 0xff) |
1293 && (dataIn.footer.checkCode[1] != 0xff) | 1302 && (dataIn.footer.checkCode[1] == 0xff) |
1294 && (dataIn.footer.checkCode[2] != 0xff) | 1303 && (dataIn.footer.checkCode[2] == 0xff) |
1295 && (dataIn.footer.checkCode[3] != 0xff)) { ret = 0; } | 1304 && (dataIn.footer.checkCode[3] == 0xff)) { ret = 0; } |
1296 | 1305 |
1297 return ret; | 1306 return ret; |
1298 } | 1307 } |
1299 | 1308 |
1300 uint8_t DataEX_check_header_and_footer_ok(void) | 1309 uint8_t DataEX_check_header_and_footer_ok(void) |
1301 { | 1310 { |
1302 if(dataIn.header.checkCode[0] != 0xA1) | 1311 if(dataIn.header.checkCode[0] != 0xA1) |
1303 return 0; | 1312 return 0; |
1313 #if USE_OLD_HEADER_FORMAT | |
1304 if(dataIn.header.checkCode[1] != 0xA2) | 1314 if(dataIn.header.checkCode[1] != 0xA2) |
1305 return 0; | 1315 return 0; |
1306 if(dataIn.header.checkCode[2] != 0xA3) | 1316 if(dataIn.header.checkCode[2] != 0xA3) |
1307 return 0; | 1317 return 0; |
1318 #endif | |
1308 if(dataIn.header.checkCode[3] != 0xA4) | 1319 if(dataIn.header.checkCode[3] != 0xA4) |
1309 return 0; | 1320 return 0; |
1310 if(dataIn.footer.checkCode[0] != 0xE1) | 1321 if(dataIn.footer.checkCode[0] != 0xE1) |
1311 return 0; | 1322 return 0; |
1312 if(dataIn.footer.checkCode[1] != 0xE2) | 1323 if(dataIn.footer.checkCode[1] != 0xE2) |