changeset 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 f4c16ea0354a (current diff) fd47e5631b9d (diff)
children 1b9847d40e81 5fe136480a47 189387bf23a8
files
diffstat 7 files changed, 87 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/Common/Inc/global_constants.h	Tue Apr 23 14:21:32 2019 +0000
+++ b/Common/Inc/global_constants.h	Fri Apr 26 10:36:11 2019 +0000
@@ -42,7 +42,7 @@
 
 /* Define INDEX for information exchanged within the header */
 #define SPI_HEADER_INDEX_RX_STATE  (1)
-#define SPI_HEADER_INDEX_FRAME_CNT (2)
+#define SPI_HEADER_INDEX_TX_TICK   (2)
 
 #define SPI_RX_STATE_OK 		(0)
 #define SPI_RX_STATE_SHIFTED	(1)
--- a/Discovery/Src/base.c	Tue Apr 23 14:21:32 2019 +0000
+++ b/Discovery/Src/base.c	Fri Apr 26 10:36:11 2019 +0000
@@ -266,6 +266,7 @@
 
 uint8_t LastButtonPressed;
 uint32_t LastButtonPressedTick;
+uint32_t BaseTick100ms;			/* Tick at last 100ms cycle */
 
 /*
 uint32_t time_before;
@@ -384,8 +385,6 @@
     set_new_settings_missing_in_ext_flash(); // inlcudes update of firmware version  161121
 
     GFX_init( &pLayerInvisible );
-
-    TIM_init();
     TIM_BACKLIGHT_init();
 
     /*
@@ -476,6 +475,8 @@
         openInfo( StIDEBUG );
     }
 
+    TIM_init();		/* start cylic 100ms task */
+
     /* @brief main LOOP
      *
      * this is executed while no IRQ interrupts it
@@ -506,6 +507,9 @@
         {
 	        DoDisplayRefresh = 0;
         	RefreshDisplay();
+
+        	if(stateUsed == stateRealGetPointer())	/* Handle log entries while in dive mode*/
+                logbook_InitAndWrite();
         }
 
 #ifdef DEBUG_RUNTIME
@@ -561,6 +565,9 @@
     uint32_t timeout_in_seconds;
     uint32_t timeout_limit_Surface_in_seconds;
 
+
+    BaseTick100ms = HAL_GetTick();	/* store start of 100ms cycle */
+
     _Bool InDiveMode = 0;
     _Bool modeChange = 0; // to exit from menu and logbook
 
@@ -594,10 +601,11 @@
         DataEX_call();
 
         if(stateUsed == stateSimGetPointer())
+        {
             simulation_UpdateLifeData(1);
+        }
+
         check_warning();
-        if(stateUsed == stateRealGetPointer())
-            logbook_InitAndWrite();
         updateMiniLiveLogbook(1);
         timer_UpdateSecond(1);
         base_tempLightLevel = TIM_BACKLIGHT_adjust();
@@ -1338,7 +1346,7 @@
     sConfig.OCMode     = TIM_OCMODE_PWM1;
     sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
     sConfig.OCFastMode = TIM_OCFAST_DISABLE;
-    sConfig.Pulse = 50 * 6;
+    sConfig.Pulse = 100; /* Initial brigthness of display */
 
     HAL_TIM_PWM_ConfigChannel(&TimBacklightHandle, &sConfig, TIM_BACKLIGHT_CHANNEL);
     HAL_TIM_PWM_Start(&TimBacklightHandle, TIM_BACKLIGHT_CHANNEL);
--- a/Small_CPU/Inc/scheduler.h	Tue Apr 23 14:21:32 2019 +0000
+++ b/Small_CPU/Inc/scheduler.h	Fri Apr 26 10:36:11 2019 +0000
@@ -40,6 +40,9 @@
 #define SCHEDULER_TICK_EXE1SEC	(980u) 	/* tick count based on cycle start which is used to trigger functions which */
 										/* shall be executed once in a second (20ms before cycle restarts) */
 
+#define SPI_COM_TIMEOUT_START		(5)		/* *100 ms timeout to avoid tiemout e.g. after Main wakeup */
+#define SPI_COM_TIMEOUT_COMMON		(3)		/* *100ms shorter timeout during normal operation to have a faster error reaction */
+
 typedef struct
 {
 	uint8_t mode;
@@ -84,6 +87,7 @@
 	uint8_t counterPressure100msec;
 	uint8_t counterCompass100msec;
 	uint8_t counterAmbientLight100msec;
+	uint8_t	communicationTimeout;
 	uint32_t tick_execute1second;
 	uint32_t tickstart;
 } SScheduleCtrl;
@@ -96,6 +100,7 @@
 /* Function prototypes -----------------------------------------------*/
 
 void initGlobals(void);
+void reinitGlobals(void);
 
 void scheduleSurfaceMode(void);
 void scheduleDiveMode(void);
@@ -109,7 +114,7 @@
 void scheduleUpdateDeviceDataChargerCharging(void);
 
 void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod);
-void Scheduler_SyncToSPI(void);
+void Scheduler_SyncToSPI(uint8_t TXtick);
 
 uint8_t scheduleSetButtonResponsiveness(void);
 
--- a/Small_CPU/Inc/spi.h	Tue Apr 23 14:21:32 2019 +0000
+++ b/Small_CPU/Inc/spi.h	Fri Apr 26 10:36:11 2019 +0000
@@ -30,7 +30,7 @@
 //void SPI_Start_single_TxRx_with_Master_and_Stop_ChipSelectControl(void);
 void SPI_Start_single_TxRx_with_Master(void);
 void SPI_synchronize_with_Master(void);
-void SPI_Evaluate_RX_Data(void); /*process the data received during last 100ms cycle */
+uint8_t SPI_Evaluate_RX_Data(void); /*process the data received during last 100ms cycle */
 
 void MX_SPI_DeInit(void);
 
--- 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();
 }
 
 
--- a/Small_CPU/Src/spi.c	Tue Apr 23 14:21:32 2019 +0000
+++ b/Small_CPU/Src/spi.c	Fri Apr 26 10:36:11 2019 +0000
@@ -317,7 +317,15 @@
 	/* restart SPI */
 	if (hspi == &hspi1)
 	{
-		Scheduler_SyncToSPI();
+		if(SPI_check_header_and_footer_ok())	/* process timestamp provided by main */
+		{
+			Scheduler_SyncToSPI(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_TX_TICK]);
+		}
+		else
+		{
+			Scheduler_SyncToSPI(0); /* => no async will be calculated */
+		}
+
 		SPIDataRX = 1;
 
 		/* stop data exchange? */
@@ -330,9 +338,10 @@
 	}
 }
 
-void SPI_Evaluate_RX_Data()
+uint8_t SPI_Evaluate_RX_Data()
 {
 	uint8_t resettimeout = 1;
+	uint8_t ret = SPIDataRX;
 
 	if ((global.mode != MODE_SHUTDOWN) && ( global.mode != MODE_SLEEP) && (SPIDataRX))
 	{
@@ -385,12 +394,13 @@
 		scheduleSpecial_Evaluate_DataSendToSlave();
 
 		SPI_Start_single_TxRx_with_Master();
+
+		if(resettimeout)
+		{
+				global.check_sync_not_running = 0;
+		}
 	}
-
-	if(resettimeout)
-	{
-			global.check_sync_not_running = 0;
-	}
+	return ret;
 }
 
 static uint8_t SPI_check_header_and_footer_ok(void) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ostc4pack/step0_copy_release.bat	Fri Apr 26 10:36:11 2019 +0000
@@ -0,0 +1,3 @@
+copy ..\..\OSTC4_Firmware\Release\OSTC4_Firmware.bin OSTC4_Firmware.bin /V /Y
+copy ..\..\OSTC4_RTE\Release\OSTC4_RTE.bin OSTC4_RTE.bin /V /Y
+