comparison Small_CPU/Src/scheduler.c @ 264:b3685fbada3b IPC_Sync_Improvment_2

Sync to Main 100ms time stamp & added Reinitialization of globals after startup Added function to synchronize RTE to the timestamp provided by Main 100ms callback. This was introduced to compensated small shifts in execution and program flow shifts (for example during long operation times)
author ideenmodellierer
date Sun, 14 Apr 2019 11:43:29 +0200
parents 625d20070261
children a91d99265884
comparison
equal deleted inserted replaced
263:a6c0375bc950 264:b3685fbada3b
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;
174 global.deviceData.voltageMinimum.value_int32 = INT32_MAX; 173 global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
175 174
176 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); 175 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
177 } 176 }
178 177
178 void reinitGlobals(void)
179 {
180 global.dataSendToSlavePending = 0;
181 global.dataSendToSlaveIsValid = 0;
182 global.dataSendToSlaveIsNotValidCount = 0;
183 global.sync_error_count = 0;
184 global.check_sync_not_running = 0;
185 }
179 186
180 void scheduleSpecial_Evaluate_DataSendToSlave(void) 187 void scheduleSpecial_Evaluate_DataSendToSlave(void)
181 { 188 {
182 //TEMPORARY fix for compass calibration. 189 //TEMPORARY fix for compass calibration.
183 //TODO: Fix I2C timeout for complete solving problem. 190 //TODO: Fix I2C timeout for complete solving problem.
274 /* for device data updates */ 281 /* for device data updates */
275 deviceDataFlashValid = 0; 282 deviceDataFlashValid = 0;
276 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); 283 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice));
277 deviceDataFlashValid = 1; 284 deviceDataFlashValid = 1;
278 285
279 286 #if 0
280 //TODO: Temporary placed here. Duration ~210 ms. 287 //TODO: Temporary placed here. Duration ~210 ms.
281 if (global.I2C_SystemStatus != HAL_OK) { 288 if (global.I2C_SystemStatus != HAL_OK) {
282 MX_I2C1_TestAndClear(); 289 MX_I2C1_TestAndClear();
283 MX_I2C1_Init(); 290 MX_I2C1_Init();
284 // init_pressure(); 291 // init_pressure();
285 // compass_init(0, 7); 292 // compass_init(0, 7);
286 // accelerator_init(); 293 // accelerator_init();
287 } 294 }
295 #endif /* already called once a second */
288 } 296 }
289 297
290 298
291 /** 299 /**
292 ****************************************************************************** 300 ******************************************************************************
400 */ 408 */
401 409
402 void schedule_check_resync(void) 410 void schedule_check_resync(void)
403 { 411 {
404 /* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */ 412 /* 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)) 413 if((global.check_sync_not_running >= 5))
406 { 414 {
407 // global.dataSendToSlaveIsNotValidCount = 0; 415 // global.dataSendToSlaveIsNotValidCount = 0;
408 global.check_sync_not_running = 0; 416 global.check_sync_not_running = 0;
409 global.sync_error_count++; 417 global.sync_error_count++;
410 418
411 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the 419 /* 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 420 * function error handler
413 */ 421 */
414 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
415 SPI_Start_single_TxRx_with_Master(); 422 SPI_Start_single_TxRx_with_Master();
423 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
416 } 424 }
417 } 425 }
418 426
419 427
420 /** 428 /**
432 uint8_t counterAscentRate = 0; 440 uint8_t counterAscentRate = 0;
433 float lastPressure_bar = 0.0f; 441 float lastPressure_bar = 0.0f;
434 global.dataSendToMaster.mode = MODE_DIVE; 442 global.dataSendToMaster.mode = MODE_DIVE;
435 global.deviceDataSendToMaster.mode = MODE_DIVE; 443 global.deviceDataSendToMaster.mode = MODE_DIVE;
436 uint8_t counter_exit = 0; 444 uint8_t counter_exit = 0;
437 445
438 Scheduler.counterSPIdata100msec = 0; 446 Scheduler.counterSPIdata100msec = 0;
439 Scheduler.counterCompass100msec = 0; 447 Scheduler.counterCompass100msec = 0;
440 Scheduler.counterPressure100msec = 0; 448 Scheduler.counterPressure100msec = 0;
441 Scheduler.counterAmbientLight100msec = 0; 449 Scheduler.counterAmbientLight100msec = 0;
442 Scheduler.tick_execute1second = SCHEDULER_TICK_EXE1SEC; 450 Scheduler.tick_execute1second = SCHEDULER_TICK_EXE1SEC;
737 } 745 }
738 746
739 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */ 747 /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */
740 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) 748 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
741 { 749 {
742 SPI_Evaluate_RX_Data(); 750 if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */
743 Scheduler.counterSPIdata100msec++; 751 {
752 Scheduler.counterSPIdata100msec++;
753 }
744 } 754 }
745 755
746 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */ 756 /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */
747 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) 757 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20)
748 { 758 {
860 { 870 {
861 dospisync = SyncMethod; 871 dospisync = SyncMethod;
862 } 872 }
863 } 873 }
864 874
865 void Scheduler_SyncToSPI() 875 void Scheduler_SyncToSPI(uint8_t TXtick)
866 { 876 {
867 uint32_t deltatick = 0; 877 uint32_t deltatick = 0;
878 int8_t TXcompensation;
868 879
869 switch(dospisync) 880 switch(dospisync)
870 { 881 {
871 case SPI_SYNC_METHOD_HARD: 882 case SPI_SYNC_METHOD_HARD:
872 //Set back tick counter 883 //Set back tick counter
873 Scheduler.tickstart = HAL_GetTick(); 884 Scheduler.tickstart = HAL_GetTick() - 4; /* consider 4ms offset for transfer */
874 Scheduler.counterSPIdata100msec = 0; 885 Scheduler.counterSPIdata100msec = 0;
875 Scheduler.counterCompass100msec = 0; 886 Scheduler.counterCompass100msec = 0;
876 Scheduler.counterPressure100msec = 0; 887 Scheduler.counterPressure100msec = 0;
877 Scheduler.counterAmbientLight100msec = 0; 888 Scheduler.counterAmbientLight100msec = 0;
878 dospisync = SPI_SYNC_METHOD_NONE; 889 dospisync = SPI_SYNC_METHOD_NONE;
888 { 899 {
889 Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart); 900 Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart);
890 } 901 }
891 dospisync = SPI_SYNC_METHOD_NONE; 902 dospisync = SPI_SYNC_METHOD_NONE;
892 break; 903 break;
893 default: 904 default: /* continous sync activity */
905 if(TXtick < 100) /* do not handle unexpected jump length > 100ms */
906 {
907 TXtick += 4; /* add 4ms TX time to offset of 100ms time stamp */
908 deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
909 deltatick %= 100;
910 if(deltatick > 50)
911 {
912 TXcompensation = deltatick - 100; /* neg drift */
913 }
914 else
915 {
916 TXcompensation = deltatick; /* pos drift */
917 }
918 TXcompensation = TXtick - TXcompensation;
919 Scheduler.tickstart -= TXcompensation;
920 }
921 else
922 {
923 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT); /* A large shift in 100ms cycle occured => clip to 100ms in next sync call */
924 }
894 break; 925 break;
895 } 926 }
896 } 927 }
897 928
898 /** 929 /**
998 while(global.mode == MODE_SLEEP); 1029 while(global.mode == MODE_SLEEP);
999 /* new section for system after Standby */ 1030 /* new section for system after Standby */
1000 scheduleUpdateLifeData(-1); 1031 scheduleUpdateLifeData(-1);
1001 clearDecoNow = 0; 1032 clearDecoNow = 0;
1002 setButtonsNow = 0; 1033 setButtonsNow = 0;
1034 reinitGlobals();
1003 } 1035 }
1004 1036
1005 1037
1006 1038
1007 /* Private functions ---------------------------------------------------------*/ 1039 /* Private functions ---------------------------------------------------------*/