diff Small_CPU/Src/uart.c @ 798:e9eba334b942

Migrated CO2 protocol implementation to new format: The previous implementation was a monolithic protocol implementation which was not usable together with the multiplexer. The new implementation moves the CO2 implementation into a separate C file and decoubles the upper layer external interface, which is not able to handle DiveO2 and CO2 sensors in parallel without restriction to port assignments.
author Ideenmodellierer
date Mon, 07 Aug 2023 20:29:44 +0200
parents bb37d4f3e50e
children 96ffad0a4e57
line wrap: on
line diff
--- a/Small_CPU/Src/uart.c	Mon Jul 31 20:10:27 2023 +0200
+++ b/Small_CPU/Src/uart.c	Mon Aug 07 20:29:44 2023 +0200
@@ -21,6 +21,7 @@
 /* Includes ------------------------------------------------------------------*/
 #include "uart.h"
 #include "uartProtocol_O2.h"
+#include "uartProtocol_Co2.h"
 #include "externalInterface.h"
 #include "data_exchange.h"
 #include <string.h>	/* memset */
@@ -42,22 +43,13 @@
 static uint8_t lastCmdIndex;							/* Index of last command which has not been completly received */
 static uint8_t dmaActive;								/* Indicator if DMA reception needs to be started */
 
-static uint8_t CO2Connected = 0;						/* Binary indicator if a sensor is connected or not */
+
 static uint8_t SentinelConnected = 0;					/* Binary indicator if a sensor is connected or not */
 
 
-
-static uartCO2Status_t ComStatus_CO2 = UART_CO2_INIT;
-
-float LED_Level = 0.0;							/* Normalized LED value which may be used as indication for the health status of the sensor */
-float LED_ZeroOffset = 0.0;
-float pCO2 = 0.0;
 /* Exported functions --------------------------------------------------------*/
 
 
-
-//huart.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), new_baudrate);
-
 void MX_USART1_UART_Init(void)
 {
 /* regular init */	
@@ -71,7 +63,6 @@
   else
   {
 	  huart1.Init.BaudRate = 9600;
-	  ComStatus_CO2 = UART_CO2_INIT;
   }
   huart1.Init.WordLength = UART_WORDLENGTH_8B;
   huart1.Init.StopBits = UART_STOPBITS_1;
@@ -90,7 +81,6 @@
   rxWriteIndex = 0;
   dmaActive = 0;
 
-  CO2Connected = 0;
   SentinelConnected = 0;
 
 }
@@ -157,28 +147,6 @@
 	}
 }
 
-void DigitalCO2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint16_t *cmdLength)
-{
-	switch (CO2Cmd)
-	{
-		case CO2CMD_MODE_POLL:		*cmdLength = snprintf((char*)cmdString, 10, "K 2\r\n");
-				break;
-		case CO2CMD_MODE_STREAM:	*cmdLength = snprintf((char*)cmdString, 10, "K 1\r\n");
-				break;
-		case CO2CMD_CALIBRATE:		*cmdLength = snprintf((char*)cmdString, 10, "G\r\n");
-				break;
-		case CO2CMD_GETDATA:		*cmdLength = snprintf((char*)cmdString, 10, "Q\r\n");
-				break;
-		case CO2CMD_GETSCALE:		*cmdLength = snprintf((char*)cmdString, 10, ".\r\n");
-				break;
-		default: *cmdLength = 0;
-			break;
-	}
-	if(cmdLength != 0)
-	{
-		HAL_UART_Transmit(&huart1,cmdString,*cmdLength,10);
-	}
-}
 
 void StringToInt(char *pstr, uint32_t *puInt32)
 {
@@ -230,154 +198,36 @@
 
 void UART_StartDMA_Receiption()
 {
-	if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
+	if(dmaActive == 0)
 	{
-		dmaActive = 1;
+		if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
+		{
+			dmaActive = 1;
+		}
 	}
 }
 
-#ifdef ENABLE_CO2_SUPPORT
-void UART_HandleCO2Data(void)
+void UART_ChangeBaudrate(uint32_t newBaudrate)
 {
-	uint8_t localRX = rxReadIndex;
-	static uint8_t dataType = 0;
-	static uint32_t dataValue = 0;
-	static receiveState_t rxState = RX_Ready;
-	static uint32_t lastReceiveTick = 0;
-	static uint32_t lastTransmitTick = 0;
-	static uint8_t cmdString[10];
-	static uint16_t cmdLength = 0;
-
-	uint32_t Tick = HAL_GetTick();
-
-	uint8_t *pmap = externalInterface_GetSensorMapPointer(0);
-
-	if(ComStatus_CO2 == UART_CO2_INIT)
-	{
-		UART_StartDMA_Receiption();
-		ComStatus_CO2 = UART_CO2_SETUP;
-	}
 
-	if(ComStatus_CO2 == UART_CO2_SETUP)
-	{
-		if(time_elapsed_ms(lastTransmitTick,Tick) > 200)
-		{
-			if(externalInterface_GetCO2Scale() == 0.0)
-			{
-				DigitalCO2_SendCmd(CO2CMD_GETDATA, cmdString, &cmdLength);
-				lastTransmitTick = Tick;
-			}
-			else
-			{
-				ComStatus_CO2 = UART_CO2_OPERATING;
-			}
-		}
-	}
-	else
-	{
-		if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX)		/* sensor is working in polling mode if mux is connected to avoid interference with other sensors */
-		{
-			if(time_elapsed_ms(lastTransmitTick,Tick) > 2000)	/* poll every two seconds */
-			{
-				lastTransmitTick = Tick;
-				if(cmdLength == 0)							/* poll data */
-				{
-					DigitalCO2_SendCmd(CO2CMD_GETDATA, cmdString, &cmdLength);
-				}
-				else											/* resend last command */
-				{
-					HAL_UART_Transmit(&huart1,cmdString,strlen((char*)cmdString),10);
-					cmdLength = 0;
-				}
-			}
-		}
-	}
-	while((rxBuffer[localRX]!=BUFFER_NODATA))
+//	HAL_DMA_Abort(&hdma_usart1_rx);
+		MX_USART1_UART_DeInit();
+		//HAL_UART_Abort(&huart1);
+		//HAL_DMA_DeInit(&hdma_usart1_rx);
+
+
+//	huart1.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq()/2, newBaudrate);
+	huart1.Init.BaudRate = newBaudrate;
+	HAL_UART_Init(&huart1);
+	MX_USART1_DMA_Init();
+	if(dmaActive)
 	{
-		lastReceiveTick = Tick;
-		if(rxState == RX_Ready)		/* identify data content */
-		{
-			switch(rxBuffer[localRX])
-			{
-				case 'l':
-				case 'D':
-				case 'Z':
-				case '.':
-									dataType = rxBuffer[localRX];
-									rxState = RX_Data0;
-									dataValue = 0;
-					break;
-
-				default:			/* unknown or corrupted => ignore */
-					break;
-			}
-		}
-		else if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
-		{
-			if((rxState >= RX_Data0) && (rxState <= RX_Data4))
-			{
-				dataValue = dataValue * 10 + (rxBuffer[localRX] - '0');
-				rxState++;
-				if(rxState == RX_Data5)
-				{
-					rxState = RX_DataComplete;
-					CO2Connected = 1;
-				}
-			}
-			else	/* protocol error data has max 5 digits */
-			{
-				rxState = RX_Ready;
-			}
-		}
-		if((rxBuffer[localRX] == ' ') || (rxBuffer[localRX] == '\n'))	/* Abort data detection */
-		{
-			if(rxState == RX_DataComplete)
-			{
-				if(externalInterface_GetCO2State() == 0)
-				{
-					externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
-				}
-				switch(dataType)
-				{
-					case 'D':			externalInterface_SetCO2SignalStrength(dataValue);
-						break;
-					case 'l':			LED_ZeroOffset = dataValue;
-						break;
-					case 'Z':			externalInterface_SetCO2Value(dataValue);
-						break;
-					case '.':			externalInterface_SetCO2Scale(dataValue);
-						break;
-					default:			rxState = RX_Ready;
-						break;
-				}
-			}
-			if(rxState != RX_Data0)	/* reset state machine because message in wrong format */
-			{
-				rxState = RX_Ready;
-			}
-		}
-		rxBuffer[localRX] = BUFFER_NODATA;
-		localRX++;
-		rxReadIndex++;
-		if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
-		{
-			localRX = 0;
-			rxReadIndex = 0;
-		}
-	}
-
-	if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 2000)	/* check for communication timeout */
-	{
-		externalInterface_SetCO2State(0);
-		CO2Connected = 0;
-	}
-
-	if((dmaActive == 0)	&& (externalInterface_isEnabledPower33()))	/* Should never happen in normal operation => restart in case of communication error */
-	{
+		rxReadIndex = 0;
+		rxWriteIndex = 0;
+		dmaActive = 0;
 		UART_StartDMA_Receiption();
 	}
 }
-#endif
 
 #ifdef ENABLE_SENTINEL_MODE
 void UART_HandleSentinelData(void)
@@ -531,10 +381,7 @@
 #endif
 
 
-uint8_t UART_isCO2Connected()
-{
-	return CO2Connected;
-}
+
 uint8_t UART_isSentinelConnected()
 {
 	return SentinelConnected;
@@ -571,8 +418,10 @@
 			case SENSOR_MUX:
 			case SENSOR_DIGO2:	uartO2_ProcessData(rxBuffer[localRX]);
 				break;
-	//	case SENSOR_CO2:	uartCO2_Control();
+#ifdef ENABLE_CO2_SUPPORT
+			case SENSOR_CO2:	uartCo2_ProcessData(rxBuffer[localRX]);
 				break;
+#endif
 			default:
 				break;
 		}