diff 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
line wrap: on
line diff
--- a/Small_CPU/Src/scheduler.c	Tue Apr 23 14:21:32 2019 +0000
+++ b/Small_CPU/Src/scheduler.c	Fri Apr 26 10:36:11 2019 +0000
@@ -88,7 +88,6 @@
 _Bool scheduleCheck_pressure_reached_dive_mode_level(void);
 void scheduleSetDate(SDeviceLine *line);
 
-extern void SPI_Evaluate_RX_Data();
 /* Exported functions --------------------------------------------------------*/
 
 void initGlobals(void)
@@ -136,7 +135,7 @@
 	
 	global.dataSendToMaster.power_on_reset = 1;
 	global.dataSendToMaster.header.checkCode[0] = 0xA1;
-	global.dataSendToMaster.header.checkCode[1] = 0xA2;
+	global.dataSendToMaster.header.checkCode[1] = SPI_RX_STATE_OFFLINE;
 	global.dataSendToMaster.header.checkCode[2] = 0xA3;
 	global.dataSendToMaster.header.checkCode[3] = 0xA4;
 	global.dataSendToMaster.footer.checkCode[3] = 0xE4;
@@ -173,9 +172,19 @@
 	global.deviceData.temperatureMinimum.value_int32 = INT32_MAX;
 	global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
 
+	Scheduler.communicationTimeout = SPI_COM_TIMEOUT_START;
 	Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 }
 
+void reinitGlobals(void)
+{
+	global.dataSendToSlavePending = 0;
+	global.dataSendToSlaveIsValid = 0;
+	global.dataSendToSlaveIsNotValidCount = 0;
+	global.sync_error_count = 0;
+	global.check_sync_not_running = 0;
+	Scheduler.communicationTimeout = SPI_COM_TIMEOUT_START;
+}
 
 void scheduleSpecial_Evaluate_DataSendToSlave(void)
 {
@@ -276,7 +285,7 @@
 	memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice));
 	deviceDataFlashValid = 1;
 
-
+#if 0
 	//TODO: Temporary placed here. Duration ~210 ms.
 	if (global.I2C_SystemStatus != HAL_OK) {
 		MX_I2C1_TestAndClear();
@@ -285,6 +294,7 @@
 //		compass_init(0, 7);
 //		accelerator_init();
 	}
+#endif /* already called once a second */
 }
 
 
@@ -402,7 +412,7 @@
 void schedule_check_resync(void)
 {
 	/* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */
-	if((global.check_sync_not_running >= 3))
+	if((global.check_sync_not_running >= Scheduler.communicationTimeout))
 	{
 //		global.dataSendToSlaveIsNotValidCount = 0;
 		global.check_sync_not_running = 0;
@@ -411,8 +421,9 @@
 		/* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the
 		 * function error handler
 		 */
-		Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
 		SPI_Start_single_TxRx_with_Master();
+		Scheduler.communicationTimeout = SPI_COM_TIMEOUT_COMMON;	/* Reduce error detection time */
+		Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 	}
 }
 
@@ -434,7 +445,7 @@
 	global.dataSendToMaster.mode = MODE_DIVE;
 	global.deviceDataSendToMaster.mode = MODE_DIVE;
 	uint8_t counter_exit = 0;
-	
+
 	Scheduler.counterSPIdata100msec = 0;
 	Scheduler.counterCompass100msec = 0;
 	Scheduler.counterPressure100msec = 0;
@@ -739,8 +750,10 @@
 		/* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */
 		if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
 		{
-			SPI_Evaluate_RX_Data();
-			Scheduler.counterSPIdata100msec++;
+			if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */
+			{			
+				Scheduler.counterSPIdata100msec++;
+			}
 		}
 
 		/* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */
@@ -862,15 +875,16 @@
 	}
 }
 
-void Scheduler_SyncToSPI()
+void Scheduler_SyncToSPI(uint8_t TXtick)
 {
 	uint32_t deltatick = 0;
+	int8_t TXcompensation;
 
 	switch(dospisync)
 	{
 		case SPI_SYNC_METHOD_HARD:
 				//Set back tick counter
-				Scheduler.tickstart = HAL_GetTick();
+				Scheduler.tickstart = HAL_GetTick() - 4; /* consider 4ms offset for transfer */
 				Scheduler.counterSPIdata100msec = 0;
 				Scheduler.counterCompass100msec = 0;
 				Scheduler.counterPressure100msec = 0;
@@ -890,7 +904,27 @@
 				}
 				dospisync = SPI_SYNC_METHOD_NONE;
 			break;
-		default:
+		default:										/* continous sync activity */
+			if(TXtick < 100)							/* do not handle unexpected jump length > 100ms */
+			{
+				TXtick += 4;	/* add 4ms TX time to offset of 100ms time stamp */
+				deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
+				deltatick %= 100;
+				if(deltatick > 50)
+				{
+					TXcompensation = deltatick - 100; /* neg drift */
+				}
+				else
+				{
+					TXcompensation = deltatick;		/* pos drift */
+				}
+				TXcompensation = TXtick - TXcompensation;
+				Scheduler.tickstart -= TXcompensation;
+			}
+			else
+			{
+				Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT); /* A large shift in 100ms cycle occured => clip to 100ms in next sync call */
+			}
 			break;
 	}
 }
@@ -1000,6 +1034,7 @@
 	scheduleUpdateLifeData(-1);
 	clearDecoNow = 0;
 	setButtonsNow = 0;
+	reinitGlobals();
 }