changeset 209:2de856965c55

Merged in Ideenmodellierer/ostc4/ImprovmentSPI (pull request #9) ImprovmentSPI
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Sun, 24 Mar 2019 22:09:37 +0000
parents 7116c0ffb862 (current diff) 9fc06e1e0f66 (diff)
children b2a9e9b02df0 c878a08dcf86
files Discovery/Inc/global_constants.h
diffstat 10 files changed, 170 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/Common/Inc/global_constants.h	Fri Mar 22 08:15:30 2019 +0000
+++ b/Common/Inc/global_constants.h	Sun Mar 24 22:09:37 2019 +0000
@@ -40,10 +40,14 @@
 #define SPI_SHOW_SYNC_STATS 0
 #define SPI_MIN_ERROR_SHOW 10
 
-// SPI header by index used for synchronization check (package sequence counter)
-#define SPI_HEADER_INDEX_MASTER 1
-#define SPI_HEADER_INDEX_SLAVE 2
+/* Define INDEX for information exchanged within the header */
+#define SPI_HEADER_INDEX_RX_STATE  (1)
+#define SPI_HEADER_INDEX_FRAME_CNT (2)
 
+#define SPI_RX_STATE_OK 		(0)
+#define SPI_RX_STATE_SHIFTED	(1)
+#define SPI_RX_STATE_OFFLINE	(2)
+#define SPI_RX_STATE_INVALID	(3)
 
 //Text data
 #define TEXT_PRESSURE_UNIT "hPa"
--- a/Discovery/Inc/global_constants.h	Fri Mar 22 08:15:30 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-/// -*- coding: UTF-8 -*-
-///
-/// \file   Common/Inc/global_constants.h
-/// \brief
-/// \author Dmitry Romanov<kitt@bk.ru>
-/// \date   11.2018
-///
-/// $Id$
-///////////////////////////////////////////////////////////////////////////////
-/// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
-///
-///     This program is free software: you can redistribute it and/or modify
-///     it under the terms of the GNU General Public License as published by
-///     the Free Software Foundation, either version 3 of the License, or
-///     (at your option) any later version.
-///
-///     This program is distributed in the hope that it will be useful,
-///     but WITHOUT ANY WARRANTY; without even the implied warranty of
-///     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-///     GNU General Public License for more details.
-///
-///     You should have received a copy of the GNU General Public License
-///     along with this program.  If not, see <http://www.gnu.org/licenses/>.
-//////////////////////////////////////////////////////////////////////////////
-
-#ifndef GLOBAL_CONSTANTS_H
-#define GLOBAL_CONSTANTS_H
-//Rename it ahead.
-//Buttons section
-#define DEFAULT_BUTTONRESPONSIVENESS_GUI 90
-#define MIN_BUTTONRESPONSIVENESS_GUI 50 //50 (-10correction)
-#define MAX_BUTTONRESPONSIVENESS_GUI 110//100 (+10correction)
-#define MIN_BUTTONRESPONSIVENESS MIN_BUTTONRESPONSIVENESS_GUI-20 //MIN_BUTTONRESPONSIVENESS_GUI-20 correction
-#define MAX_BUTTONRESPONSIVENESS MAX_BUTTONRESPONSIVENESS_GUI+20//MIN_BUTTONRESPONSIVENESS_GUI+20correction
-#define BUTTON_DEBOUNCE_DELAY 7 //Delay for debounce filter for piezo buttons
-
-
-//Spi sync data debug
-#define SPI_SHOW_SYNC_STATS 1
-#define SPI_MIN_ERROR_SHOW 2
-#endif
--- a/Discovery/Src/base.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Discovery/Src/base.c	Sun Mar 24 22:09:37 2019 +0000
@@ -495,7 +495,6 @@
             ext_flash_write_settings();
         }
         deco_loop();
-        EvaluateButton();
 
 #ifdef DEBUG_RUNTIME
         translateTime(stateUsed->lifeData.timeBinaryFormat, &Stime);
@@ -558,6 +557,8 @@
     else
         InDiveMode = 0;
 
+    EvaluateButton();
+
     if(returnFromCommCleanUpRequest)
     {
         tComm_exit();
--- a/Discovery/Src/data_exchange_main.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Discovery/Src/data_exchange_main.c	Sun Mar 24 22:09:37 2019 +0000
@@ -145,8 +145,8 @@
 	memset((void *)&dataOut, 0, sizeof(SDataReceiveFromMaster));
 
 	dataOut.header.checkCode[0] = 0xBB;
-	dataOut.header.checkCode[1] = 0x01;
-	dataOut.header.checkCode[2] = 0x01;
+	dataOut.header.checkCode[1] = SPI_RX_STATE_OK;
+	dataOut.header.checkCode[2] = SPI_RX_STATE_OK;
 	dataOut.header.checkCode[3] = 0xBB;
 
 	dataOut.footer.checkCode[0] = 0xF4;
@@ -258,42 +258,76 @@
 
 uint8_t DataEX_call(void)
 {
+	static uint32_t RTEOfflineCnt = 0;
+	static uint8_t SusppressCom = 0;
+
 	uint8_t SPI_DMA_answer = 0;
 
-	HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
-
-	if(data_old__lost_connection_to_slave_counter_temp >= 3)
+	if(SusppressCom)
 	{
-		data_old__lost_connection_to_slave_counter_temp = 0;
-		if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry == 0))
-		{
-			HAL_SPI_Abort_IT(&cpu2DmaSpi);
-		}
-		/* reset of own DMA does not work ==> request reset of slave dma */
-		if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry == 2))
-		{
-			dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0xA5;
-		}
-		data_old__lost_connection_to_slave_counter_retry++;
+		SusppressCom--;
 	}
-#if USE_OLD_SYNC_METHOD
-	/* one cycle with NotChipSelect true to clear slave spi buffer */
 	else
 	{
-		HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
-	}
-#endif
+		if(data_old__lost_connection_to_slave_counter_temp >= 2) /* error reaction is triggered whenever communication could not be reestablishen within two cycles */
+		{
+			data_old__lost_connection_to_slave_counter_temp = 0;
+			if(DataEX_check_header_and_footer_shifted())
+			{
+				if(RTEOfflineCnt > 1)		/* RTE restarted communication after a longer silent time => restart error handling to recover */
+				{
+					data_old__lost_connection_to_slave_counter_retry = 0;
+					RTEOfflineCnt = 0;
+				}
 
-	DataEx_call_helper_requests();
+				/* We received shifted data. Step one. Reset DMA to see if the problem is located at main */
+				if (data_old__lost_connection_to_slave_counter_retry == 0)
+				{
+					HAL_SPI_Abort_IT(&cpu2DmaSpi);
+				}
+				/* reset of own DMA does not work ==> request reset of slave dma by indicating shifted receiption */
+				if (data_old__lost_connection_to_slave_counter_retry == 1)
+				{
+					dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_SHIFTED;
+				}
+
+				/* stop communication with RTE to trigger RTE timeout reaction */
+				if (data_old__lost_connection_to_slave_counter_retry == 2)
+				{
+					SusppressCom = 3;
+				}
 
-//HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */
-
+				data_old__lost_connection_to_slave_counter_retry++;
+			}
+			else
+			{
+				RTEOfflineCnt++;	/* based on footer status the RTE does not seem to provide data in time */
+				dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OFFLINE;
+			}
+		}
+	#if USE_OLD_SYNC_METHOD
+		/* one cycle with NotChipSelect true to clear slave spi buffer */
+		else
+		{
+			HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
+		}
+	#endif
 
-	SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE);
-	if(SPI_DMA_answer != HAL_OK)
-	{
-		DataEX_Error_Handler(SPI_DMA_answer);
-	}		
+		DataEx_call_helper_requests();
+
+	//HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */
+
+		if(SusppressCom == 0)
+		{
+			HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
+
+			SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE);
+			if(SPI_DMA_answer != HAL_OK)
+			{
+				DataEX_Error_Handler(SPI_DMA_answer);
+			}
+		}
+	}
 //	HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET);
 //HAL_Delay(3);
 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_SET); /* only for testing with Oscilloscope */
@@ -311,7 +345,6 @@
 	return &dataIn;
 }
 
-
 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
 {
 	if(hspi == &cpu2DmaSpi)
@@ -671,6 +704,7 @@
 			data_old__lost_connection_to_slave_counter_retry = 0;
 			data_old__lost_connection_to_slave_counter_temp = 0;
 			stateRealGetPointerWrite()->data_old__lost_connection_to_slave = 0;
+			dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
 		}
 		else
 		{
@@ -757,6 +791,7 @@
 			data_old__lost_connection_to_slave_counter_temp = 0;
 			data_old__lost_connection_to_slave_counter_retry = 0;
 			pStateReal->data_old__lost_connection_to_slave = 0;
+			dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
 		}
 		else
 		{
@@ -771,12 +806,9 @@
 		data_old__lost_connection_to_slave_counter_temp = 0;
 		data_old__lost_connection_to_slave_counter_retry = 0;
 		pStateReal->data_old__lost_connection_to_slave = 0;
+		dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
 	}
 	
-	/* update SPI communication tokens */
-	dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = dataIn.header.checkCode[SPI_HEADER_INDEX_SLAVE];
-	dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] = (dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] + 1) & 0x7F;
-
 	if(getDeviceDataAfterStartOfMainCPU)
 	{
 		getDeviceDataAfterStartOfMainCPU--;
--- a/Discovery/Src/tHome.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Discovery/Src/tHome.c	Sun Mar 24 22:09:37 2019 +0000
@@ -425,12 +425,15 @@
 
 uint8_t tHome_show_lost_connection_count(GFX_DrawCfgScreen *ScreenToWriteOn)
 {
+	static uint8_t LastKnowRTEState = SPI_RX_STATE_INVALID;
+
 	if(!SPI_MIN_ERROR_SHOW) return 0;
 	if(DataEX_lost_connection_count()>=SPI_MIN_ERROR_SHOW && SPI_SHOW_SYNC_STATS){
 
     char text[64];
 
     SDataExchangeSlaveToMaster* dataIn=get_dataInPointer();
+    SDataReceiveFromMaster* pDataOut = dataOutGetPointer();
 
     snprintf(text,32,"spi err:\002 %i/%i",DataEX_lost_connection_count(),get_num_SPI_CALLBACKS());
     Gfx_write_label_var(ScreenToWriteOn,  100,300, 0,&FontT24,CLUT_ButtonSymbols,text);
@@ -438,7 +441,18 @@
 //    snprintf(text,32,"header:\002%X%X%X%X",dataIn->header.checkCode[0],dataIn->header.checkCode[1],dataIn->header.checkCode[2],dataIn->header.checkCode[3]);
 //    Gfx_write_label_var(ScreenToWriteOn,  350,550, 0,&FontT24,CLUT_ButtonSymbols,text);
 
-    snprintf(text,32,"footer:\002%X%X%X%X",dataIn->footer.checkCode[0],dataIn->footer.checkCode[1],dataIn->footer.checkCode[2],dataIn->footer.checkCode[3]);
+    //snprintf(text,32,"footer:\002%X%X%X%X",dataIn->footer.checkCode[0],dataIn->footer.checkCode[1],dataIn->footer.checkCode[2],dataIn->footer.checkCode[3]);
+
+    /* data shifted => ignore received data */
+    if((pDataOut->header.checkCode[SPI_HEADER_INDEX_RX_STATE] == SPI_RX_STATE_SHIFTED) || (pDataOut->header.checkCode[SPI_HEADER_INDEX_RX_STATE] == SPI_RX_STATE_OFFLINE))
+    {
+    	dataIn->header.checkCode[SPI_HEADER_INDEX_RX_STATE] = LastKnowRTEState;
+    }
+    else
+    {
+    	LastKnowRTEState =dataIn->header.checkCode[SPI_HEADER_INDEX_RX_STATE];
+    }
+    snprintf(text,32,"RX State M|R:\002%X|%X",pDataOut->header.checkCode[SPI_HEADER_INDEX_RX_STATE], dataIn->header.checkCode[SPI_HEADER_INDEX_RX_STATE] );
     Gfx_write_label_var(ScreenToWriteOn,  600,800, 0,&FontT24,CLUT_ButtonSymbols,text);
     }
 
--- a/Small_CPU/Inc/scheduler.h	Fri Mar 22 08:15:30 2019 +0000
+++ b/Small_CPU/Inc/scheduler.h	Sun Mar 24 22:09:37 2019 +0000
@@ -32,6 +32,11 @@
 #define SENSOR_PRESSURE_ID 0
 #define MAX_SENSORS 1
  
+#define SPI_SYNC_METHOD_NONE	(0u)
+#define SPI_SYNC_METHOD_HARD	(1u)	/* Scheduler shall reset all counters to 0 */
+#define SPI_SYNC_METHOD_SOFT	(2u)	/* Scheduler shall reset adjust counters to 100ms SPI data exchange cycle */
+#define SPI_SYNC_METHOD_INVALID	(4u)
+
 typedef struct
 {
 	uint8_t mode;
@@ -83,7 +88,6 @@
 
 /* Variables ---------------------------------------------------------*/
 extern SGlobal global;
-extern uint8_t dohardspisync;
 
 
 /* Function prototypes -----------------------------------------------*/
@@ -101,6 +105,9 @@
 void scheduleUpdateDeviceDataChargerFull(void);
 void scheduleUpdateDeviceDataChargerCharging(void);
 
+void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod);
+void Scheduler_SyncToSPI(void);
+
 uint8_t scheduleSetButtonResponsiveness(void);
 
 void copyBatteryData(void);
--- a/Small_CPU/Src/baseCPU2.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Small_CPU/Src/baseCPU2.c	Sun Mar 24 22:09:37 2019 +0000
@@ -379,7 +379,7 @@
 			MX_DMA_Init();
 			MX_SPI1_Init();
 			SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */
-			dohardspisync = 1;
+			Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 			EXTI_Test_Button_Init();
 			global.mode = MODE_SURFACE;
 			break;
--- a/Small_CPU/Src/compass.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Small_CPU/Src/compass.c	Sun Mar 24 22:09:37 2019 +0000
@@ -34,7 +34,6 @@
 #include "compass_LSM303D.h"
 #include "compass_LSM303DLHC.h"
 
-#include "spi.h"
 #include "i2c.h"
 #include "RTE_FlashAccess.h" // to store compass_calib_data
 
--- a/Small_CPU/Src/scheduler.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Small_CPU/Src/scheduler.c	Sun Mar 24 22:09:37 2019 +0000
@@ -57,7 +57,6 @@
 SDevice DeviceDataFlash;
 uint8_t deviceDataFlashValid = 0;
 uint8_t deviceDataSubSeconds = 0;
-uint8_t dohardspisync = 1;
 
 /* Private variables ---------------------------------------------------------*/
 /* can be lost while in sleep */
@@ -67,6 +66,8 @@
 /* has to be in SRAM2 */
 uint8_t secondsCount = 0;
 
+static uint8_t dospisync = SPI_SYNC_METHOD_NONE;
+
 SScheduleCtrl Scheduler;
  
 /* Private function prototypes -----------------------------------------------*/
@@ -180,7 +181,7 @@
 	global.deviceData.temperatureMinimum.value_int32 = INT32_MAX;
 	global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
 
-	dohardspisync = 1;
+	Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 }
 
 
@@ -440,10 +441,7 @@
 		 * function error handler
 		 */
 		SPI_Start_single_TxRx_with_Master();
-	}
-	if((global.check_sync_not_running == 10)) /* connection lost for about a second. Could be debugging or Firmware update */
-	{
-		dohardspisync = 1;
+		Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
 	}
 }
 
@@ -754,8 +752,6 @@
 
 	while(global.mode == MODE_SURFACE)
 	{
-	/*    printf("surface...\n"); */
-//	    SPI_Start_single_TxRx_with_Master();
 		schedule_check_resync();
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
@@ -766,7 +762,6 @@
 				setButtonsNow = 0;
 		}
 		
-
 		//Evaluate received data at 10 ms, 110 ms, 210 ms,...
 		if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
 		{
@@ -788,8 +783,7 @@
 					global.mode = MODE_DIVE;
 		}
 		
-		//evaluate compass data at 50 ms, 150 ms, 250 ms,...
-
+		//Evaluate compass data at 50 ms, 150 ms, 250 ms,...
 		if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50)
 		{
 			compass_read();
@@ -881,17 +875,44 @@
 	}
 }
 
-void HardSyncToSPI()
+inline void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod)
 {
-	if(dohardspisync)
+	if( SyncMethod < SPI_SYNC_METHOD_INVALID)
+	{
+		dospisync = SyncMethod;
+	}
+}
+
+void Scheduler_SyncToSPI()
+{
+	uint32_t deltatick = 0;
+
+	switch(dospisync)
 	{
-		//Set back tick counter
-		Scheduler.tickstart = HAL_GetTick();
-		Scheduler.counterSPIdata100msec = 0;
-		Scheduler.counterCompass100msec = 0;
-		Scheduler.counterPressure100msec = 0;
-		Scheduler.counterAmbientLight100msec = 0;
-		dohardspisync = 0;
+		case SPI_SYNC_METHOD_HARD:
+				//Set back tick counter
+				Scheduler.tickstart = HAL_GetTick();
+				Scheduler.counterSPIdata100msec = 0;
+				Scheduler.counterCompass100msec = 0;
+				Scheduler.counterPressure100msec = 0;
+				Scheduler.counterAmbientLight100msec = 0;
+				dospisync = SPI_SYNC_METHOD_NONE;
+			break;
+		case SPI_SYNC_METHOD_SOFT:
+				deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
+				deltatick %= 100;  						/* clip to 100ms window */
+				if(Scheduler.tickstart - deltatick >= 0) /* adjust start time to the next 100ms window */
+				{
+					Scheduler.tickstart -= deltatick;
+				}
+				else
+				{
+					Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart);
+				}
+				dospisync = SPI_SYNC_METHOD_NONE;
+			break;
+		default:
+			break;
 	}
 }
 
--- a/Small_CPU/Src/spi.c	Fri Mar 22 08:15:30 2019 +0000
+++ b/Small_CPU/Src/spi.c	Sun Mar 24 22:09:37 2019 +0000
@@ -35,15 +35,10 @@
 extern void GPIO_new_DEBUG_HIGH(void);
 #endif
 
-// SPI header by index used for synchronization check (package sequence counter)
-#define SPI_HEADER_INDEX_MASTER 1
-#define SPI_HEADER_INDEX_SLAVE 2
-
 uint8_t data_error = 0;
 uint32_t data_error_time = 0;
 uint8_t SPIDataRX = 0; /* Flag to signal that SPI RX callback has been triggered */
 
-extern void HardSyncToSPI(void);
 static void SPI_Error_Handler(void);
 
 /* USER CODE END 0 */
@@ -322,10 +317,9 @@
 	/* restart SPI */
 	if (hspi == &hspi1)
 	{
-		HardSyncToSPI();
+		Scheduler_SyncToSPI();
 		SPIDataRX = 1;
 
-		global.check_sync_not_running = 0;
 		/* stop data exchange? */
 		if (global.mode == MODE_SHUTDOWN) {
 			global.mode = MODE_SLEEP;
@@ -338,37 +332,52 @@
 
 void SPI_Evaluate_RX_Data()
 {
+	uint8_t resettimeout = 1;
+
 	if ((global.mode != MODE_SHUTDOWN) && ( global.mode != MODE_SLEEP) && (SPIDataRX))
 	{
 		SPIDataRX = 0;
 		/* data consistent? */
 		if (SPI_check_header_and_footer_ok()) {
+			global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
 	//		GPIO_new_DEBUG_HIGH(); //For debug.
 			global.dataSendToSlaveIsValid = 1;
 			global.dataSendToSlaveIsNotValidCount = 0;
-			/* use sequence index from master to indicate correct reception */
-			if(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] > 0x7F)
+			/* Master signal a data shift outside of his control => reset own DMA and resync */
+			if(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_RX_STATE] == SPI_RX_STATE_SHIFTED)
 			{
 				HAL_SPI_Abort_IT(&hspi1);
-				global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER];
-				global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0;
+				Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 			}
 			 else
 			 {
-				 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER];
 			 }
-		} else {
+		}
+		else
+		{
 	//		GPIO_new_DEBUG_LOW(); //For debug.
 				global.dataSendToSlaveIsValid = 0;
 				global.dataSendToSlaveIsNotValidCount++;
 				if(DataEX_check_header_and_footer_shifted())
 				{
-					if (global.dataSendToSlaveIsNotValidCount == 1) 
+
+					/* Reset own DMA */
+					if ((global.dataSendToSlaveIsNotValidCount % 10) == 1)  //% 10
 					{	
 						HAL_SPI_Abort_IT(&hspi1); /* reset DMA only once */
 					}
+					/* Signal problem to master */
+					if ((global.dataSendToSlaveIsNotValidCount ) >= 2)
+					{
+						global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_SHIFTED;
+					}
 				}
-			}
+				else /* handle received data as if no data would have been received */
+				{
+					global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OFFLINE;
+					resettimeout = 0;
+				}
+		}
 
 		global.dataSendToMaster.power_on_reset = 0;
 		global.deviceDataSendToMaster.power_on_reset = 0;
@@ -379,7 +388,12 @@
 	//		}
 		scheduleSpecial_Evaluate_DataSendToSlave();
 
-		SPI_Start_single_TxRx_with_Master(); //Send data always.
+		SPI_Start_single_TxRx_with_Master();
+	}
+
+	if(resettimeout)
+	{
+			global.check_sync_not_running = 0;
 	}
 }