diff Small_CPU/Src/pressure.c @ 474:4be72d55b09a Improve_Button_Sleep

Added error detection for reading of ADC values in sleep mode: ADC value were used for pressure calculation without making sure that the I2C operations used for reading the values were called successfully. As result a invalid pressure could be calculated causing the OSTC to wakeup or (worstcase) to enter dive mode. To avoid this the return values are now evaluated and pressure calculation is only done if I2C signaled no error during transmission
author ideenmodellierer
date Tue, 12 May 2020 22:42:52 +0200
parents 6f30f2011667
children 4b9427ae0a65
line wrap: on
line diff
--- a/Small_CPU/Src/pressure.c	Tue May 12 22:39:18 2020 +0200
+++ b/Small_CPU/Src/pressure.c	Tue May 12 22:42:52 2020 +0200
@@ -391,7 +391,6 @@
 	{
 		pressureSensorInitSuccess = 1;
 		retValue = pressure_update();
-
 	}
 	return retValue;
 }
@@ -401,14 +400,17 @@
 {
 	uint8_t buffer[1];
 	uint8_t resivebuf[4];
-	uint32_t answer = 0;
+	uint32_t answer = 0xFFFFFFFF;
 
 	buffer[0] = 0x00; // Get ADC
-	I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1);
-	I2C_Master_Receive(  PRESSURE_ADDRESS, resivebuf, 4);
-	resivebuf[3] = 0;
-	answer = 256*256 *(uint32_t)resivebuf[0]  + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
-
+	if(I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1) == HAL_OK)
+	{
+		if(I2C_Master_Receive(  PRESSURE_ADDRESS, resivebuf, 4) == HAL_OK)
+		{
+			resivebuf[3] = 0;
+			answer = 256*256 *(uint32_t)resivebuf[0]  + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
+		}
+	}
 	return answer;
 }
 
@@ -460,6 +462,7 @@
 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
 {
 	uint8_t command = CMD_ADC_CONV + cmd;
+	uint32_t adcValue = 0;
 	HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
 	
 	statusReturnTemp = I2C_Master_Transmit( PRESSURE_ADDRESS, &command, 1);
@@ -468,16 +471,25 @@
 	{
 		*statusReturn = statusReturnTemp;
 	}
-	
-	switch (cmd & 0x0f) // wait necessary conversion time
+	else
 	{
-	case CMD_ADC_256 : HAL_Delay(1); break;
-	case CMD_ADC_512 : HAL_Delay(3); break;
-	case CMD_ADC_1024: HAL_Delay(4); break;
-	case CMD_ADC_2048: HAL_Delay(6); break;
-	case CMD_ADC_4096: HAL_Delay(10); break;
-	}	
-	return get_adc();
+		switch (cmd & 0x0f) // wait necessary conversion time
+		{
+			case CMD_ADC_256 : HAL_Delay(1); break;
+			case CMD_ADC_512 : HAL_Delay(3); break;
+			case CMD_ADC_1024: HAL_Delay(4); break;
+			case CMD_ADC_2048: HAL_Delay(6); break;
+			case CMD_ADC_4096: HAL_Delay(10); break;
+			default:
+				break;
+		}
+		adcValue = get_adc();
+		if(adcValue == 0xFFFFFFFF)
+		{
+			*statusReturn = HAL_ERROR;
+		}
+	}
+	return adcValue;
 }