comparison Small_CPU/Src/scheduler.c @ 267:cf6ad20380fb

Merged in Ideenmodellierer/ostc4/IPC_Sync_Improvment_2 (pull request #18) IPC Sync Improvment 2
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Fri, 26 Apr 2019 10:36:11 +0000
parents a91d99265884
children 580822b5d3d1
comparison
equal deleted inserted replaced
262:f4c16ea0354a 267:cf6ad20380fb
86 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow); 86 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow);
87 87
88 _Bool scheduleCheck_pressure_reached_dive_mode_level(void); 88 _Bool scheduleCheck_pressure_reached_dive_mode_level(void);
89 void scheduleSetDate(SDeviceLine *line); 89 void scheduleSetDate(SDeviceLine *line);
90 90
91 extern void SPI_Evaluate_RX_Data();
92 /* Exported functions --------------------------------------------------------*/ 91 /* Exported functions --------------------------------------------------------*/
93 92
94 void initGlobals(void) 93 void initGlobals(void)
95 { 94 {
96 bzero(&global, sizeof(SGlobal)); 95 bzero(&global, sizeof(SGlobal));
134 global.dataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW;; 133 global.dataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW;;
135 global.dataSendToMaster.chargeStatus = 0; 134 global.dataSendToMaster.chargeStatus = 0;
136 135
137 global.dataSendToMaster.power_on_reset = 1; 136 global.dataSendToMaster.power_on_reset = 1;
138 global.dataSendToMaster.header.checkCode[0] = 0xA1; 137 global.dataSendToMaster.header.checkCode[0] = 0xA1;
139 global.dataSendToMaster.header.checkCode[1] = 0xA2; 138 global.dataSendToMaster.header.checkCode[1] = SPI_RX_STATE_OFFLINE;
140 global.dataSendToMaster.header.checkCode[2] = 0xA3; 139 global.dataSendToMaster.header.checkCode[2] = 0xA3;
141 global.dataSendToMaster.header.checkCode[3] = 0xA4; 140 global.dataSendToMaster.header.checkCode[3] = 0xA4;
142 global.dataSendToMaster.footer.checkCode[3] = 0xE4; 141 global.dataSendToMaster.footer.checkCode[3] = 0xE4;
143 global.dataSendToMaster.footer.checkCode[2] = 0xE3; 142 global.dataSendToMaster.footer.checkCode[2] = 0xE3;
144 global.dataSendToMaster.footer.checkCode[1] = 0xE2; 143 global.dataSendToMaster.footer.checkCode[1] = 0xE2;
171 global.deviceData.hoursOfOperation.value_int32 = 0; 170 global.deviceData.hoursOfOperation.value_int32 = 0;
172 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN; 171 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN;
173 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX; 172 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX;
174 global.deviceData.voltageMinimum.value_int32 = INT32_MAX; 173 global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
175 174
175 Scheduler.communicationTimeout = SPI_COM_TIMEOUT_START;
176 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); 176 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
177 } 177 }
178 178
179 void reinitGlobals(void)
180 {
181 global.dataSendToSlavePending = 0;
182 global.dataSendToSlaveIsValid = 0;
183 global.dataSendToSlaveIsNotValidCount = 0;
184 global.sync_error_count = 0;
185 global.check_sync_not_running = 0;
186 Scheduler.communicationTimeout = SPI_COM_TIMEOUT_START;
187 }
179 188
180 void scheduleSpecial_Evaluate_DataSendToSlave(void) 189 void scheduleSpecial_Evaluate_DataSendToSlave(void)
181 { 190 {
182 //TEMPORARY fix for compass calibration. 191 //TEMPORARY fix for compass calibration.
183 //TODO: Fix I2C timeout for complete solving problem. 192 //TODO: Fix I2C timeout for complete solving problem.
274 /* for device data updates */ 283 /* for device data updates */
275 deviceDataFlashValid = 0; 284 deviceDataFlashValid = 0;
276 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); 285 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice));
277 deviceDataFlashValid = 1; 286 deviceDataFlashValid = 1;
278 287
279 288 #if 0
280 //TODO: Temporary placed here. Duration ~210 ms. 289 //TODO: Temporary placed here. Duration ~210 ms.
281 if (global.I2C_SystemStatus != HAL_OK) { 290 if (global.I2C_SystemStatus != HAL_OK) {
282 MX_I2C1_TestAndClear(); 291 MX_I2C1_TestAndClear();
283 MX_I2C1_Init(); 292 MX_I2C1_Init();
284 // init_pressure(); 293 // init_pressure();
285 // compass_init(0, 7); 294 // compass_init(0, 7);
286 // accelerator_init(); 295 // accelerator_init();
287 } 296 }
297 #endif /* already called once a second */
288 } 298 }
289 299
290 300
291 /** 301 /**
292 ****************************************************************************** 302 ******************************************************************************
400 */ 410 */
401 411
402 void schedule_check_resync(void) 412 void schedule_check_resync(void)
403 { 413 {
404 /* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */ 414 /* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */
405 if((global.check_sync_not_running >= 3)) 415 if((global.check_sync_not_running >= Scheduler.communicationTimeout))
406 { 416 {
407 // global.dataSendToSlaveIsNotValidCount = 0; 417 // global.dataSendToSlaveIsNotValidCount = 0;
408 global.check_sync_not_running = 0; 418 global.check_sync_not_running = 0;
409 global.sync_error_count++; 419 global.sync_error_count++;
410 420
411 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the 421 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the
412 * function error handler 422 * function error handler
413 */ 423 */
414 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
415 SPI_Start_single_TxRx_with_Master(); 424 SPI_Start_single_TxRx_with_Master();
425 Scheduler.communicationTimeout = SPI_COM_TIMEOUT_COMMON; /* Reduce error detection time */
426 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
416 } 427 }
417 } 428 }
418 429
419 430
420 /** 431 /**
432 uint8_t counterAscentRate = 0; 443 uint8_t counterAscentRate = 0;
433 float lastPressure_bar = 0.0f; 444 float lastPressure_bar = 0.0f;
434 global.dataSendToMaster.mode = MODE_DIVE; 445 global.dataSendToMaster.mode = MODE_DIVE;
435 global.deviceDataSendToMaster.mode = MODE_DIVE; 446 global.deviceDataSendToMaster.mode = MODE_DIVE;
436 uint8_t counter_exit = 0; 447 uint8_t counter_exit = 0;
437 448
438 Scheduler.counterSPIdata100msec = 0; 449 Scheduler.counterSPIdata100msec = 0;
439 Scheduler.counterCompass100msec = 0; 450 Scheduler.counterCompass100msec = 0;
440 Scheduler.counterPressure100msec = 0; 451 Scheduler.counterPressure100msec = 0;
441 Scheduler.counterAmbientLight100msec = 0; 452 Scheduler.counterAmbientLight100msec = 0;
442 Scheduler.tick_execute1second = SCHEDULER_TICK_EXE1SEC; 453 Scheduler.tick_execute1second = SCHEDULER_TICK_EXE1SEC;
737 } 748 }
738 749
739 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */ 750 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */
740 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) 751 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
741 { 752 {
742 SPI_Evaluate_RX_Data(); 753 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */
743 Scheduler.counterSPIdata100msec++; 754 {
755 Scheduler.counterSPIdata100msec++;
756 }
744 } 757 }
745 758
746 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */ 759 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */
747 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) 760 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20)
748 { 761 {
860 { 873 {
861 dospisync = SyncMethod; 874 dospisync = SyncMethod;
862 } 875 }
863 } 876 }
864 877
865 void Scheduler_SyncToSPI() 878 void Scheduler_SyncToSPI(uint8_t TXtick)
866 { 879 {
867 uint32_t deltatick = 0; 880 uint32_t deltatick = 0;
881 int8_t TXcompensation;
868 882
869 switch(dospisync) 883 switch(dospisync)
870 { 884 {
871 case SPI_SYNC_METHOD_HARD: 885 case SPI_SYNC_METHOD_HARD:
872 //Set back tick counter 886 //Set back tick counter
873 Scheduler.tickstart = HAL_GetTick(); 887 Scheduler.tickstart = HAL_GetTick() - 4; /* consider 4ms offset for transfer */
874 Scheduler.counterSPIdata100msec = 0; 888 Scheduler.counterSPIdata100msec = 0;
875 Scheduler.counterCompass100msec = 0; 889 Scheduler.counterCompass100msec = 0;
876 Scheduler.counterPressure100msec = 0; 890 Scheduler.counterPressure100msec = 0;
877 Scheduler.counterAmbientLight100msec = 0; 891 Scheduler.counterAmbientLight100msec = 0;
878 dospisync = SPI_SYNC_METHOD_NONE; 892 dospisync = SPI_SYNC_METHOD_NONE;
888 { 902 {
889 Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart); 903 Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart);
890 } 904 }
891 dospisync = SPI_SYNC_METHOD_NONE; 905 dospisync = SPI_SYNC_METHOD_NONE;
892 break; 906 break;
893 default: 907 default: /* continous sync activity */
908 if(TXtick < 100) /* do not handle unexpected jump length > 100ms */
909 {
910 TXtick += 4; /* add 4ms TX time to offset of 100ms time stamp */
911 deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
912 deltatick %= 100;
913 if(deltatick > 50)
914 {
915 TXcompensation = deltatick - 100; /* neg drift */
916 }
917 else
918 {
919 TXcompensation = deltatick; /* pos drift */
920 }
921 TXcompensation = TXtick - TXcompensation;
922 Scheduler.tickstart -= TXcompensation;
923 }
924 else
925 {
926 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT); /* A large shift in 100ms cycle occured => clip to 100ms in next sync call */
927 }
894 break; 928 break;
895 } 929 }
896 } 930 }
897 931
898 /** 932 /**
998 while(global.mode == MODE_SLEEP); 1032 while(global.mode == MODE_SLEEP);
999 /* new section for system after Standby */ 1033 /* new section for system after Standby */
1000 scheduleUpdateLifeData(-1); 1034 scheduleUpdateLifeData(-1);
1001 clearDecoNow = 0; 1035 clearDecoNow = 0;
1002 setButtonsNow = 0; 1036 setButtonsNow = 0;
1037 reinitGlobals();
1003 } 1038 }
1004 1039
1005 1040
1006 1041
1007 /* Private functions ---------------------------------------------------------*/ 1042 /* Private functions ---------------------------------------------------------*/