changeset 278:cc30d1aa03a7

Merged in Ideenmodellierer/ostc4/IPC_Sync_Improvment_3 (pull request #21) IPC Sync Improvment 3
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Sun, 28 Apr 2019 15:54:46 +0000
parents 2f0eb65df557 (current diff) 580822b5d3d1 (diff)
children b29fb616c458
files Discovery/Src/data_exchange_main.c
diffstat 5 files changed, 93 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/Discovery/Src/data_exchange_main.c	Sun Apr 28 09:41:43 2019 +0200
+++ b/Discovery/Src/data_exchange_main.c	Sun Apr 28 15:54:46 2019 +0000
@@ -779,7 +779,8 @@
 			DataEX_copy_to_VpmRepetitiveData();
 			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;
+			/* Do not yet reset state. Wait till common data has been received in next cycle. Otherwise invalid data may be forwarded for processing */
+			/* pStateReal->data_old__lost_connection_to_slave = 0; */
 			dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
 		}
 		else
@@ -966,6 +967,17 @@
 		pStateReal->lifeData.ambient_light_level = dataIn.data[dataIn.boolAmbientLightData].ambient_light_level;
 		pStateReal->lifeData.battery_charge = dataIn.data[dataIn.boolBatteryData].battery_charge;
 		pStateReal->lifeData.battery_voltage = dataIn.data[dataIn.boolBatteryData].battery_voltage;
+
+		/* PIC data
+	 	 */
+		for(int i=0;i<4;i++)
+		{
+			pStateReal->lifeData.buttonPICdata[i] = dataIn.data[dataIn.boolPICdata].button_setting[i];
+		}
+
+		/* sensorErrors
+		 */
+		pStateReal->sensorErrorsRTE = dataIn.sensorErrors;
 	}
 
 	/* apnea specials
@@ -1040,69 +1052,6 @@
 		pStateReal->lifeData.boolResetStopwatch = 0;
 	}
 	pStateReal->lifeData.stopwatch_seconds = pStateReal->lifeData.dive_time_seconds - pStateReal->lifeData.internal.stopwatch_start_at_this_dive_time_seconds;
-
-	/* wireless data
-	 */
-	uint16_t wirelessData[4][3];
-	for(int i=0;i<4;i++)
-	{
-		pStateReal->lifeData.wireless_data[i].ageInMilliSeconds = dataIn.data[dataIn.boolWirelessData].wireless_data[i].ageInMilliSeconds;	
-		pStateReal->lifeData.wireless_data[i].status = dataIn.data[dataIn.boolWirelessData].wireless_data[i].status;	
-		pStateReal->lifeData.wireless_data[i].numberOfBytes = dataIn.data[dataIn.boolWirelessData].wireless_data[i].numberOfBytes;	
-		for(int j=0;j<12;j++)
-			pStateReal->lifeData.wireless_data[i].data[j] = dataIn.data[dataIn.boolWirelessData].wireless_data[i].data[j];
-	}
-
-	// neu 160412
-	for(int i=0;i<4;i++)
-	{
-		if(pStateReal->lifeData.wireless_data[i].numberOfBytes == 10)
-		{
-			wirelessData[i][0] = (pStateReal->lifeData.wireless_data[i].data[0] >> 4) & 0x7F;
-			wirelessData[i][1] = 0;
-			wirelessData[i][2] = pStateReal->lifeData.wireless_data[i].ageInMilliSeconds;
-		}
-		else
-		{
-			wirelessData[i][0] = 0;
-			wirelessData[i][1] = 0;
-			wirelessData[i][2] = 0;
-		}
-	}
-
-	// aussortieren doppelte ids, j�ngster datensatz ist relevant
-	for(int i=0;i<3;i++)
-	{
-		if(wirelessData[i][0])
-		{
-			for(int j=i+1; j<4; j++)
-			{
-				if(wirelessData[i][0] == wirelessData[j][0])
-				{
-					if(wirelessData[i][2] > wirelessData[j][2])
-					{
-						wirelessData[i][0] = wirelessData[j][0];
-						wirelessData[i][1] = wirelessData[j][1];
-						wirelessData[i][2] = wirelessData[j][2];
-					}
-					wirelessData[j][0] = 0;
-					wirelessData[j][1] = 0;
-					wirelessData[j][2] = 0;
-				}
-			}
-		}
-	}
-
-	/* PIC data
- 	 */
-	for(int i=0;i<4;i++)
-	{
-		pStateReal->lifeData.buttonPICdata[i] = dataIn.data[dataIn.boolPICdata].button_setting[i];
-	}
-
-	/* sensorErrors
-	 */
-	pStateReal->sensorErrorsRTE = dataIn.sensorErrors;
 }
 
 
--- a/Small_CPU/Inc/pressure.h	Sun Apr 28 09:41:43 2019 +0200
+++ b/Small_CPU/Inc/pressure.h	Sun Apr 28 15:54:46 2019 +0000
@@ -6,11 +6,12 @@
 
 uint8_t init_pressure(void);
 uint8_t pressure_update(void);
+void pressure_update_alternating(void);
 
 uint8_t is_init_pressure_done(void);
 
-void  pressure_sensor_get_pressure_raw(void);
-void  pressure_sensor_get_temperature_raw(void);
+HAL_StatusTypeDef  pressure_sensor_get_pressure_raw(void);
+HAL_StatusTypeDef  pressure_sensor_get_temperature_raw(void);
 void pressure_calculation(void);
 
 float get_temperature(void);
--- a/Small_CPU/Src/pressure.c	Sun Apr 28 09:41:43 2019 +0200
+++ b/Small_CPU/Src/pressure.c	Sun Apr 28 15:54:46 2019 +0000
@@ -306,6 +306,28 @@
 	return (uint8_t)statusReturn;
 }
 
+/* Switch between pressure and temperature measurement with every successful read operation */
+void pressure_update_alternating(void)
+{
+	static uint8_t getTemperature= 0;
+
+	if(getTemperature)
+	{
+		if(pressure_sensor_get_temperature_raw() == HAL_OK)
+		{
+			getTemperature = 0;
+		}
+	}
+	else
+	{
+		if(pressure_sensor_get_pressure_raw() == HAL_OK)
+		{
+			getTemperature = 1;
+		}
+	}
+	pressure_calculation();
+	return;
+}
 
 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
 {
@@ -333,12 +355,23 @@
 
 static HAL_StatusTypeDef pressure_sensor_get_data(void)
 {
+	uint32_t requestedValue = 0;
 	HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT;
 	HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT;
 	
-	D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, &statusReturn1);
-	D1 = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_4096, &statusReturn2);
+
 
+	requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn2);
+	if (statusReturn2 == HAL_OK)
+	{
+		D2 = requestedValue;
+	}
+
+	requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn1);
+	if (statusReturn1 == HAL_OK)
+	{
+		D1 = requestedValue;
+	}
 	if(statusReturn2 > statusReturn1) // if anything is not HAL_OK (0x00) or worse
 		return statusReturn2;
 	else
@@ -346,15 +379,32 @@
 }
 
 
-void  pressure_sensor_get_pressure_raw(void)
+HAL_StatusTypeDef  pressure_sensor_get_pressure_raw(void)
 {
-	D1 = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_4096, 0);
+	uint32_t requestedValue = 0;
+	HAL_StatusTypeDef statusReturn = HAL_TIMEOUT;
+
+	requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn);
+	if (statusReturn == HAL_OK)
+	{
+		D1 = requestedValue;
+	}
+
+	return statusReturn;
 }
 
 
-void  pressure_sensor_get_temperature_raw(void)
+HAL_StatusTypeDef  pressure_sensor_get_temperature_raw(void)
 {
-	D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, 0);
+	uint32_t requestedValue = 0;
+	HAL_StatusTypeDef statusReturn = HAL_TIMEOUT;
+
+	requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn);
+	if (statusReturn == HAL_OK)
+	{
+		D2 = requestedValue;
+	}
+	return statusReturn;
 }
 
 
--- a/Small_CPU/Src/scheduler.c	Sun Apr 28 09:41:43 2019 +0200
+++ b/Small_CPU/Src/scheduler.c	Sun Apr 28 15:54:46 2019 +0000
@@ -421,7 +421,7 @@
 		/* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the
 		 * function error handler
 		 */
-		SPI_Start_single_TxRx_with_Master();
+		HAL_SPI_TransmitReceive_DMA(&hspi1,(uint8_t*) &(global.dataSendToMaster),(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE);
 		Scheduler.communicationTimeout = SPI_COM_TIMEOUT_COMMON;	/* Reduce error detection time */
 		Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 	}
@@ -459,21 +459,23 @@
 	Scheduler.tickstart = HAL_GetTick();
 	while(global.mode == MODE_DIVE)
 	{
-		schedule_check_resync();
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
 
 		if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
 		{
-			SPI_Evaluate_RX_Data();
-			Scheduler.counterSPIdata100msec++;
+			if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */
+			{
+				Scheduler.counterSPIdata100msec++;
+			}
+			schedule_check_resync();
 		}
 
 		//Evaluate pressure at 20 ms, 120 ms, 220 ms,....
 		if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20)
 		{
 				global.check_sync_not_running++;
-				pressure_update();
+				pressure_update_alternating();
 				scheduleUpdateDeviceData();
 #ifdef DEMOMODE
 				if(global.demo_mode)
@@ -663,15 +665,17 @@
 	
 	while(global.mode == MODE_TEST)
 	{
-		schedule_check_resync();
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
 
 		//Evaluate received data at 10 ms, 110 ms, 210 ms,...
 		if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
 		{
-			SPI_Evaluate_RX_Data();
-			Scheduler.counterSPIdata100msec++;
+			if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */
+			{
+				Scheduler.counterSPIdata100msec++;
+			}
+			schedule_check_resync();
 		}
 		
 		//Evaluate pressure at 20 ms, 120 ms, 220 ms,...
@@ -679,7 +683,7 @@
 		{
 				global.check_sync_not_running++;
 
-				pressure_update();
+				pressure_update_alternating();
 				scheduleUpdateDeviceData();
 				global.lifeData.ascent_rate_meter_per_min = 0;
 				copyPressureData();
@@ -737,7 +741,7 @@
 
 	while(global.mode == MODE_SURFACE)
 	{
-		schedule_check_resync();
+
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
 
@@ -754,13 +758,14 @@
 			{			
 				Scheduler.counterSPIdata100msec++;
 			}
+			schedule_check_resync();
 		}
 
 		/* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */
 		if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20)
 		{
 				global.check_sync_not_running++;
-				pressure_update();
+				pressure_update_alternating();
 				scheduleUpdateDeviceData();
 				global.lifeData.ascent_rate_meter_per_min = 0;
 				copyPressureData();
--- a/Small_CPU/Src/spi.c	Sun Apr 28 09:41:43 2019 +0200
+++ b/Small_CPU/Src/spi.c	Sun Apr 28 15:54:46 2019 +0000
@@ -358,9 +358,10 @@
 				HAL_SPI_Abort_IT(&hspi1);
 				Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 			}
-			 else
-			 {
-			 }
+			else
+			{
+			}
+			SPI_Start_single_TxRx_with_Master();
 		}
 		else
 		{
@@ -386,6 +387,7 @@
 					global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OFFLINE;
 					resettimeout = 0;
 				}
+				HAL_SPI_TransmitReceive_DMA(&hspi1,(uint8_t*) &(global.dataSendToMaster),(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE);
 		}
 
 		global.dataSendToMaster.power_on_reset = 0;
@@ -393,8 +395,6 @@
 
 		scheduleSpecial_Evaluate_DataSendToSlave();
 
-		SPI_Start_single_TxRx_with_Master();
-
 		if(resettimeout)
 		{
 				global.check_sync_not_running = 0;